Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions mlxml/xml-c.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,12 @@ mllib_xml_doc_get_root_element (value docv)
}
}

value
mllib_xml_parse_uri (value strv)
/* Convert an xmlURIPtr to an OCaml uri record (9-field tuple). */
static value
uri_to_ocaml_tuple (xmlURIPtr uri)
{
CAMLparam1 (strv);
CAMLparam0 ();
CAMLlocal3 (rv, sv, ov);
xmlURIPtr uri;

uri = xmlParseURI (String_val (strv));
if (uri == NULL)
caml_invalid_argument ("parse_uri: unable to parse URI");

rv = caml_alloc_tuple (9);

Expand Down Expand Up @@ -514,6 +510,44 @@ mllib_xml_parse_uri (value strv)
else ov = Val_int (0);
Store_field (rv, 8, ov);

CAMLreturn (rv);
}

value
mllib_xml_parse_uri (value strv)
{
CAMLparam1 (strv);
xmlURIPtr uri;
value rv;

uri = xmlParseURI (String_val (strv));
if (uri == NULL)
caml_invalid_argument ("parse_uri: unable to parse URI");

rv = uri_to_ocaml_tuple (uri);
xmlFreeURI (uri);

CAMLreturn (rv);
}

/* Like parse_uri above, but uses xmlParseURIRaw to optionally
* preserve percent-encoding in URI components such as authority,
* server, path and query string. xmlParseURI decodes percent-encoded
* characters (e.g. %2f becomes '/'), which loses the distinction
* between encoded and literal characters.
*/
value
mllib_xml_parse_uri_raw (value strv, value rawv)
{
CAMLparam2 (strv, rawv);
xmlURIPtr uri;
value rv;

uri = xmlParseURIRaw (String_val (strv), Bool_val (rawv));
if (uri == NULL)
caml_invalid_argument ("parse_uri_raw: unable to parse URI");

rv = uri_to_ocaml_tuple (uri);
xmlFreeURI (uri);

CAMLreturn (rv);
Expand Down
1 change: 1 addition & 0 deletions mlxml/xml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,4 @@ type uri = {
}

external parse_uri : string -> uri = "mllib_xml_parse_uri"
external parse_uri_raw : string -> bool -> uri = "mllib_xml_parse_uri_raw"
8 changes: 8 additions & 0 deletions mlxml/xml.mli
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,11 @@ val parse_uri : string -> uri
Note this is different from the {!URI} module which is specialized
for parsing the [-a] parameter on the command line. This function
exposes the full [xmlParseURI] interface. *)

val parse_uri_raw : string -> bool -> uri
(** http://xmlsoft.org/html/libxml-uri.html#xmlParseURIRaw

Like {!parse_uri} but uses [xmlParseURIRaw]. When [raw] is [true],
percent-encoding in URI components such as authority, server, path
and query string is preserved. When [false], percent-encoded
characters are decoded (same as {!parse_uri}). *)