mruby-uriparser
Loading...
Searching...
No Matches
Data Structures | Macros | Functions
mrb_uriparser.c File Reference

mruby uriparser implementation More...

#include "mrb_uriparser.h"
#include <mruby/array.h>
#include <mruby/boxing_word.h>
#include <mruby/data.h>
#include <mruby/hash.h>
#include <mruby/string.h>
#include <mruby/value.h>
#include <mruby/variable.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <uriparser/Uri.h>

Data Structures

struct  mrb_uriparser_data
 Internal data structure for wrapping a UriUriA pointer in mruby. More...
 

Macros

#define MRB_URIPARSER_ERROR(mrb)    mrb_class_get_under(mrb, MRB_URIPARSER(mrb), "Error")
 Error class.
 
#define MRB_URIPARSER_NOMEM(mrb)   mrb_class_get(mrb, "NoMemoryError")
 No memory error class.
 
#define MRB_URIPARSER_DEFUN_GETTER(component)
 
#define MRB_URIPARSER_DEFUN_SETTER(component_name)
 Set the scheme component of the URI.
 

Functions

static mrb_value mrb_uriparser_parse (mrb_state *const mrb, const mrb_value self)
 Parse a string into a URI object.
 
static mrb_value mrb_uriparser_filename_to_uri_string (mrb_state *const mrb, const mrb_value self)
 Convert a filename to a URI string.
 
static mrb_value mrb_uriparser_uri_string_to_filename (mrb_state *const mrb, const mrb_value self)
 Convert a URI string to a filename.
 
static mrb_value mrb_uriparser_compose_query (mrb_state *const mrb, const mrb_value self)
 Encode an array of key-value pairs as a WWW form query string.
 
static mrb_value mrb_uriparser_initialize_copy (mrb_state *const mrb, const mrb_value self)
 Copy URI.
 
static mrb_value mrb_uriparser_equals (mrb_state *const mrb, const mrb_value self)
 Check two URIs for equivalence.
 
static mrb_value mrb_uriparser_has_host (mrb_state *const mrb, const mrb_value self)
 Check if the URI has host.
 
static mrb_value mrb_uriparser_path_segments (mrb_state *const mrb, const mrb_value self)
 Get the path segments as an array of strings.
 
static mrb_value mrb_uriparser_absolute_path (mrb_state *const mrb, const mrb_value self)
 Check if the URI has an absolute path.
 
static mrb_value mrb_uriparser_recompose (mrb_state *const mrb, const mrb_value self)
 Serialize the URI to a string.
 
static mrb_value mrb_uriparser_merge_mutably (mrb_state *const mrb, const mrb_value self)
 Mutably resolve a relative URI reference.
 
static mrb_value mrb_uriparser_merge (mrb_state *const mrb, const mrb_value self)
 Immutably resolve a relative URI reference.
 
static mrb_value mrb_uriparser_create_reference (mrb_state *const mrb, const mrb_value self)
 Create a relative reference from a base URI.
 
static mrb_value mrb_uriparser_normalize (mrb_state *const mrb, const mrb_value self)
 Normalize URI components in place.
 
static mrb_value mrb_uriparser_dissect_query (mrb_state *const mrb, const mrb_value self)
 Decode the query string into an array of key-value pairs.
 

Detailed Description

mruby uriparser implementation

mruby uriparser

Other Functions

URIParser.join(uri_str, *path)
URIParser::URI.join(uri_str, *path)

where uri is kind of URIParser::URI.

Planned Functions

URIParser.split(uri)
URIParser::URI.split(uri)
uri.hierarchical?

where uri is kind of URIParser::URI.

Comparison of Supported Features

Below is a comparison of supported features with CRuby's URI gem. A dash (-) indicates not supported or no plans to support.

mruby uriparser CRuby URI gem mruby-uri-parser ruby-uriparser
URIParser::URI#decode_www_form URI.decode_www_form - -
- URI.decode_www_form_component URI.decode -
URIParser.encode_www_form URI.encode_www_form - -
- URI.encode_www_form_component URI.encode -
- URI.extract - -
  • URIParser.join
  • URIParser::URI.join
URI.join - -
  • URIParser.parse
  • URIParser::URI.parse
URI.parse URI.parse UriParser::URI.parse
- URI.regexp - -
- URI.split - -
- URI::UNSAFE - -
- URI::Generic.build - -
- URI::Generic.build2 - -
- URI::Generic.component - -
- URI::Generic.default_port - -
- URI::Generic.new URI::Parsed.new UriParser::URI.new
- URI::Generic.use_registry - -
  • URIParser::URI#merge
  • URIParser::URI#@+
  • URI::Generic#merge
  • URI::Generic#@+
- -
  • URIParser::URI#route_from
  • URIParser::URI#@-
  • URI::Generic#route_from
  • URI::Generic#@-
- -
URIParser::URI#== URI::Generic#== - -
  • URIParser::URI#absolute
  • URIParser::URI#absolute?
  • URI::Generic#absolute
  • URI::Generic#absolute?
- -
- URI::Generic#coerce - -
- URI::Generic#component - -
- URI::Generic#find_proxy - -
URIParser::URI#fragment URI::Generic#fragment URI::Parsed#fragment UriParser::URI#fragment
URIParser::URI#fragment= URI::Generic#fragment= - UriParser::URI#fragment=
URIParser::URI#hierarchical? URI::Generic#hierarchical? - -
- URI::Generic#host URI::Parsed#host UriParser::URI#host
URIParser::URI#host= URI::Generic#host= - UriParser::URI#host=
URIParser::URI#hostname URI::Generic#hostname - -
- URI::Generic#hostname= - -
URIParser::URI#merge! URI::Generic#merge! - -
URIParser::URI#normalize!
  • URI::Generic#normalize
  • URI::Generic#normalize!
- UriParser::URI#normalize!
- URI::Generic#opaque - -
- URI::Generic#opaque= - -
- URI::Generic#parser - -
- URI::Generic#password - UriParser::URI#password
- URI::Generic#password= - -
URIParser::URI#path URI::Generic#path URI::Parsed#path UriParser::URI#path
URIParser::URI#path= URI::Generic#path= - UriParser::URI#path=
URIParser::URI#port URI::Generic#port URI::Parsed#port
  • UriParser::URI#port
  • UriParser::URI#str_port
URIParser::URI#port= URI::Generic#port= -
  • UriParser::URI#port=
  • UriParser::URI#str_port=
URIParser::URI#query URI::Generic#query URI::Parsed#query UriParser::URI#query
URIParser::URI#query= URI::Generic#query= - UriParser::URI#query=
- URI::Generic#registry - -
- URI::Generic#registry= - -
URIParser::URI#relative? URI::Generic#relative? - -
URIParser::URI#route_to URI::Generic#route_to - -
URIParser::URI#scheme URI::Generic#scheme
  • URI::Parsed#schema
  • URI::Parsed#scheme
UriParser::URI#scheme
URIParser::URI#scheme= URI::Generic#scheme= - UriParser::URI#scheme=
- URI::Generic#select - -
URIParser::URI#to_s URI::Generic#to_s URI::Parsed#to_s UriParser::URI#to_s
- URI::Generic#user - UriParser::URI#user
- URI::Generic#user= - -
URIParser::URI#userinfo URI::Generic#userinfo URI::Parsed#userinfo UriParser::URI#userinfo
URIParser::URI#userinfo= URI::Generic#userinfo= - UriParser::URI#userinfo=
- URI::Generic::COMPONENT - -
- URI::Generic::DEFAULT_PORT - -
URIParser.filename_to_uri_string - - -
URIParser.uri_string_to_filename - - -
URIParser::URI#path_segments - - -
URIParser::URI#absolute_path? - - -
URIParser::URI#dup ? ? ?
URIParser::URI#clone ? ? ?
URIParser::URI#host? ? ? ?

License

Copyright (C) 2025 gemmaro

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Macro Definition Documentation

◆ MRB_URIPARSER_DEFUN_GETTER

#define MRB_URIPARSER_DEFUN_GETTER (   component)
Value:
static mrb_value mrb_uriparser_##component(mrb_state *const mrb, \
const mrb_value self) { \
return MRB_URIPARSER_STR_IN_RANGE(mrb, MRB_URIPARSER_URI(self), \
component); \
}

Get the specific component of the URI.

uri.scheme
uri.userinfo
uri.hostname
uri.port
uri.query
uri.fragment

where uri is a URIParser::URI instance.

Note for hostname method: it returns ::1 for http://[::1]/bar so it corresponds to CRuby's URI gem's URI::Generic#hostname method.

Returns
String of the component or nil.
See also
mrb_uriparser_path_segments

◆ MRB_URIPARSER_DEFUN_SETTER

#define MRB_URIPARSER_DEFUN_SETTER (   component_name)
Value:
static mrb_value mrb_uriparser_set_##component_name(mrb_state *const mrb, \
const mrb_value self) { \
const char *component; \
mrb_get_args(mrb, "z", &component); \
if (uriSet##component_name##A(MRB_URIPARSER_URI(self), component, \
component + strlen(component))) \
MRB_URIPARSER_RAISE(mrb, "failed to set " #component_name); \
return mrb_nil_value(); \
}

Set the scheme component of the URI.

uri.scheme = ...
uri.userinfo = ...
uri.host = ...
uri.port = ...
uri.path = ...
uri.query = ...
uri.fragment = ...

where uri is a URIParser::URI instance.

Returns
nil.

◆ MRB_URIPARSER_ERROR

#define MRB_URIPARSER_ERROR (   mrb)     mrb_class_get_under(mrb, MRB_URIPARSER(mrb), "Error")

Error class.

URIParser::Error

◆ MRB_URIPARSER_NOMEM

#define MRB_URIPARSER_NOMEM (   mrb)    mrb_class_get(mrb, "NoMemoryError")

No memory error class.

URIParser::NoMemoryError

Function Documentation

◆ mrb_uriparser_absolute_path()

static mrb_value mrb_uriparser_absolute_path ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Check if the URI has an absolute path.

uri.absolute_path?

There are also the following, which might be slightly different from absolute_path?.

uri.absolute?
uri.absolute
uri.relative?

where uri is a URIParser::URI instance.

Returns
Boolean.

◆ mrb_uriparser_compose_query()

static mrb_value mrb_uriparser_compose_query ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Encode an array of key-value pairs as a WWW form query string.

URIParser.encode_www_form(query_list)

where query_list is Array of [key, value] pairs. Key is String. Value may be nil or String.

Returns
Encoded query string.
See also
mrb_uriparser_dissect_query

No enc=nil parameter as seen in CRuby's URI gem.

◆ mrb_uriparser_create_reference()

static mrb_value mrb_uriparser_create_reference ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Create a relative reference from a base URI.

uri.route_from(base, domain_root: false)
uri - base
uri.route_to(dest, domain_root: false)

where base is a base URI (URIParser::URI). If domain_root is true, reference is from domain root.

Returns
New relative URIParser::URI instance.
See also
mrb_uriparser_merge

◆ mrb_uriparser_dissect_query()

static mrb_value mrb_uriparser_dissect_query ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Decode the query string into an array of key-value pairs.

uri.decode_www_form

where uri is a URIParser::URI instance.

Returns
Array of [key, value] pairs.
See also
mrb_uriparser_compose_query

No enc=Encoding::UTF_8 parameter as in CRuby's URI gem.

◆ mrb_uriparser_equals()

static mrb_value mrb_uriparser_equals ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Check two URIs for equivalence.

uri == another_uri

where uri and another_uri are URIParser::URI instances.

Returns
Boolean.

◆ mrb_uriparser_filename_to_uri_string()

static mrb_value mrb_uriparser_filename_to_uri_string ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Convert a filename to a URI string.

URIParser.filename_to_uri_string(filename, windows: false)
URIParser::URI.from_filename(filename, windows: false)

where filename is an absolute filename. If windows is true, use Windows path conversion.

Returns
URI string.
See also
mrb_uriparser_uri_string_to_filename

◆ mrb_uriparser_has_host()

static mrb_value mrb_uriparser_has_host ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Check if the URI has host.

uri.host?

where uri is a URIParser::URI instance.

Returns
Boolean.

◆ mrb_uriparser_initialize_copy()

static mrb_value mrb_uriparser_initialize_copy ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Copy URI.

uri.dup
uri.clone

where uri is a URIParser::URI instance.

Returns
New URI.

◆ mrb_uriparser_merge()

static mrb_value mrb_uriparser_merge ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Immutably resolve a relative URI reference.

uri.merge(rel)
uri + rel

where uri is a URIParser::URI instance, and rel is relative URI (URIParser::URI).

Returns
New resolved URIParser::URI instance.
See also
mrb_uriparser_merge_mutably
mrb_uriparser_create_reference

◆ mrb_uriparser_merge_mutably()

static mrb_value mrb_uriparser_merge_mutably ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Mutably resolve a relative URI reference.

uri.merge!(rel)

where uri is a URIParser::URI instance, and rel is relative URI (URIParser::URI).

Returns
Modified URIParser::URI instance.
See also
mrb_uriparser_merge

◆ mrb_uriparser_normalize()

static mrb_value mrb_uriparser_normalize ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Normalize URI components in place.

uri.normalize!(scheme: true,
userinfo: true,
host: true,
path: true,
query: true,
fragment: true)

where uri is kind of URIParser::URI. scheme, userinfo, host, path, query, fragment are for enabling normalization for each component.

Returns
Modified URIParser::URI instance.

By default all parts are normalized. If path is empty, this doen't set /, which is the difference against CRuby's URI gem's URI::Generic#normalize! method.

◆ mrb_uriparser_parse()

static mrb_value mrb_uriparser_parse ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Parse a string into a URI object.

URIParser.parse(str)
URIParser::URI.parse(str)

where str is a URI string to parse.

Returns
URIParser::URI instance.
See also
mrb_uriparser_recompose

◆ mrb_uriparser_path_segments()

static mrb_value mrb_uriparser_path_segments ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Get the path segments as an array of strings.

uri.path_segments
uri.path #=> string

where uri is a URIParser::URI instance.

Returns
Array of path segment strings.
See also
MRB_URIPARSER_DEFUN_GETTER

◆ mrb_uriparser_recompose()

static mrb_value mrb_uriparser_recompose ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Serialize the URI to a string.

uri.to_s

where uri is a URIParser::URI instance.

Returns
URI string.
See also
mrb_uriparser_parse

Recomposing means serializing.

◆ mrb_uriparser_uri_string_to_filename()

static mrb_value mrb_uriparser_uri_string_to_filename ( mrb_state *const  mrb,
const mrb_value  self 
)
static

Convert a URI string to a filename.

URIParser.uri_string_to_filename(uri, windows: false)

where uri is a URI string. If windows is true, use Windows path conversion.

uri.to_filename(windows: false)

where uri is kind of URIParser::URI.

Returns
Filename string.
See also
mrb_uriparser_filename_to_uri_string