Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Flesh out more IRI/URI work.

  • Loading branch information...
commit 03c6341bb3272756743e9ab4b7804d738f1273b5 1 parent d7b48a1
@jlouis authored
Showing with 72 additions and 10 deletions.
  1. +62 −4 src/iri.erl
  2. +10 −6 src/uri.erl
View
66 src/iri.erl
@@ -12,19 +12,33 @@
%% API
-export([mk_iri/2, mk_iri/3, mk_iri/4,
- to_iolist/1, to_string/1]).
+ to_iolist/1, to_string/1, to_uri/1,
+
+ should_escape/1]).
+
+-record(ihier,
+ {authority :: string() | none,
+ path :: string() | none}).
+
+-record(iri,
+ {scheme :: atom(),
+ ihier :: #ihier{},
+ q :: [{string(), string()}],
+ fragment :: string() | none}).
%%====================================================================
%% API
%%====================================================================
mk_iri(S, H) ->
- uri:mk_uri(S,H,[],none).
+ uri:mk(S,H,[],none).
mk_iri(S,H,Q) ->
- uri:mk_uri(S,H,Q,none).
+ uri:mk(S,H,Q,none).
+-spec mk_iri(atom(), #ihier{}, [{string(), string()}], string() | none) ->
+ #iri{}.
mk_iri(S,H,Q,F) ->
- uri:mk_uri(S,H,Q,F).
+ #iri{ scheme = S, ihier = H, q = Q, fragment = F}.
to_iolist(Iri) ->
uri:to_iolist(Iri).
@@ -32,3 +46,47 @@ to_iolist(Iri) ->
to_string(IRI) ->
uri:to_string(IRI).
+%% @doc Convert an IRI to a URI by RFC3987 Section 3.1.
+%% We assume that the IRI has already been converted into Unicode
+%% symbols (UCS) internally in Erlang. In particular, assume that the
+%% string has been <i>normalized</i>. Then we proceed to create a URI
+%% out of the IRI by converting it into UTF-8. Percent encoding then
+%% takes care of the utf_8 stream for us.
+%% @end
+to_uri(#iri{scheme = S, ihier = IH, q = Q, fragment = F}) ->
+ IH_E = utf_8_ihier(IH),
+ Q_E = utf_8_q(Q),
+ F_E = utf_8_f(F),
+ uri:mk(S, IH_E, Q_E, F_E).
+
+%% ----------------------------------------------------------------------
+utf_8_f(Fragment) ->
+ Bin = unicode:characters_to_binary(Fragment),
+ binary_to_list(Bin).
+
+utf_8_q(Q) ->
+ [{utf_8_f(K), utf_8_f(V)} || {K, V} <- Q].
+
+utf_8_ihier(#ihier{authority = A, path = P}) ->
+ A_E = case A of
+ none ->
+ none;
+ S -> utf_8_f(S)
+ end,
+ P_E = case P of
+ none ->
+ none;
+ Str -> utf_8_f(Str)
+ end,
+ uri:mk_hier(A_E, P_E).
+
+%% Determine if a character should be escaped in the "First run" of
+%% representing an IRI. Any character greater than the UCS value of
+%% U+007F is not to be mangled, as its representation is really
+%% unicode already.
+%%
+%% This string, including U+xxxx symbols then form the basis for
+%% conversion into URIs.
+should_escape(C) when C > 16#7f -> false;
+should_escape(_C) -> true.
+
View
16 src/uri.erl
@@ -7,7 +7,8 @@
%%%-------------------------------------------------------------------
-module(uri).
%% API
--export([mk_uri/2, mk_uri/3, mk_uri/4,
+-export([mk/2, mk/3, mk/4,
+ mk_hier/2,
to_iolist/1, to_string/1]).
-record(hier,
@@ -23,15 +24,18 @@
%%====================================================================
%% API
%%====================================================================
-mk_uri(S, H) ->
- mk_uri(S,H,[],none).
+mk(S, H) ->
+ mk(S,H,[],none).
-mk_uri(S,H,Q) ->
- mk_uri(S,H,Q,none).
+mk(S,H,Q) ->
+ mk(S,H,Q,none).
-mk_uri(S,H,Q,F) ->
+mk(S,H,Q,F) ->
#uri{scheme = S, hier = H, q = Q, fragment = F}.
+mk_hier(A, P) ->
+ #hier{authority = A, path = P}.
+
-spec to_iolist(#uri{}) -> iolist().
to_iolist(#uri{scheme = S, hier = H, q = Q, fragment = F}) ->
[atom_to_list(S),":",
Please sign in to comment.
Something went wrong with that request. Please try again.