Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
ex_uri is an URI-handling library application.
  • Loading branch information
nox committed Dec 16, 2010
0 parents commit c7c0a17
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ebin/
doc/
26 changes: 26 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2010, Dev:Extend
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Dev:Extend nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
2 changes: 2 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
An URI-handling library application.
Copyright (c) 2010, Dev:Extend
14 changes: 14 additions & 0 deletions include/ex_uri.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-record(ex_authority, {userinfo :: string(),
host :: string() | inet:ip_address(),
port :: integer()}).

-record(ex_uri, {scheme :: string(),
authority :: #ex_authority{},
path :: [string()],
q :: string(),
fragment :: string()}).

-record(ex_uri_ref, {authority :: #ex_authority{},
path :: [string()],
q :: string(),
fragment :: string()}).
154 changes: 154 additions & 0 deletions priv/ex_uri_parser.abnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
URI-reference = URI / relative-ref

URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] :
{Authority, Path} = _YY3,
Query = case _YY4 of
[[$?, Query1]] -> Query1;
[] -> undefined end,
Fragment = case _YY5 of
[[$#, Fragment1]] -> Fragment1;
[] -> undefined end,
#ex_uri{scheme = _YY1,
authority = Authority,
path = Path,
q = Query,
fragment = Fragment}.

relative-ref = relative-part [ "?" query ] [ "#" fragment ] :
{Authority, Path} = _YY1,
Query = case _YY2 of
[[$?, Query1]] -> Query1;
[] -> undefined end,
Fragment = case _YY3 of
[[$#, Fragment1]] -> Fragment1;
[] -> undefined end,
#ex_uri_ref{authority = Authority,
path = Path,
q = Query,
fragment = Fragment}.

relative-part = "//" authority path-abempty
/ path-absolute
/ path-noscheme
/ path-empty :
case _YY of
["//", Authority, Path] -> {Authority, Path};
Path -> {undefined, Path} end.

absolute-URI = scheme ":" hier-part [ "?" query ] :
{Authority, Path} = _YY3,
Query = case _YY4 of
[[$?, Query1]] -> Query1;
[] -> undefined end,
#ex_uri{scheme = _YY1, authority = Authority, path = Path, q = Query}.

scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) :
[_YY1 | _YY2].

hier-part = "//" authority path-abempty
/ path-absolute
/ path-rootless
/ path-empty :
case _YY of
["//", Authority, Path] -> {Authority, Path};
Path -> {undefined, Path} end.

authority = [ userinfo "@" ] host [ ":" port ] :
UserInfo = case _YY1 of
[] -> undefined;
[[UserInfo1, $@]] -> UserInfo1 end,
Port = case _YY3 of
[] -> undefined;
[[$:, Port1]] -> Port1 end,
#ex_authority{userinfo = UserInfo, host = _YY2, port = Port}.

userinfo = *( unreserved / pct-encoded / sub-delims / ":" )

host = IP-literal / IPv4address / reg-name

IP-literal = "[" ( IPv6address / IPvFuture ) "]" :
_YY2.

IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) :
Version = erlang:list_to_integer(lists:flatten([_YY2]), 16),
{ip, Version, lists:flatten([_YY4])}.

IPv6address = 6( h16 ":" ) ls32
/ "::" 5( h16 ":" ) ls32
/ [ h16 ] "::" 4( h16 ":" ) ls32
/ [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
/ [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
/ [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
/ [ *4( h16 ":" ) h16 ] "::" ls32
/ [ *5( h16 ":" ) h16 ] "::" h16
/ [ *6( h16 ":" ) h16 ] "::" :
{ok, Address} = inet_parse:address(lists:flatten(_YY)),
Address.

h16 = 1*4HEXDIG

ls32 = ( h16 ":" h16 ) / IPv4-literal

IPv4address = IPv4-literal :
{ok, Address} = inet_parse:address(lists:flatten(_YY)),
Address.

IPv4-literal = dec-octet "." dec-octet "." dec-octet "." dec-octet

dec-octet = DIGIT
/ %x31-39 DIGIT
/ "1" 2DIGIT
/ "2" %x30-34 DIGIT
/ "25" %x30-35

port = *DIGIT :
list_to_integer(_YY).

reg-name = *( unreserved / pct-encoded / sub-delims )

path = path-abempty
/ path-absolute
/ path-noscheme
/ path-rootless
/ path-empty

path-abempty = *( "/" segment ) :
case _YY of
[] -> _YY;
[[$/, ""] | Segments] -> ["/" | [ Segment || [$/, Segment] <- Segments ]];
_YY -> ["/" | [ Segment || [$/, Segment] <- _YY ]] end.
path-absolute = "/" [ segment-nz *( "/" segment ) ] :
Segments = case _YY2 of
[] -> _YY2;
[[SegmentNz, Segments1]] -> [[$/, SegmentNz] | Segments1] end,
["/" | [ Segment || [$/, Segment] <- Segments ]].
path-noscheme = segment-nz-nc *( "/" segment ) :
[_YY1 | [ Segment || [$/, Segment] <- _YY2 ]].
path-rootless = segment-nz *( "/" segment ) :
[_YY1 | [ Segment || [$/, Segment] <- _YY2 ]].
path-empty = 0pchar
segment = *pchar
segment-nz = 1*pchar
segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
query = *( pchar / "/" / "?" )
fragment = *( pchar / "/" / "?" )
pct-encoded = "%" HEXDIG HEXDIG :
erlang:list_to_integer([_YY2, _YY3], 16).
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ex_uri_parser.erl
11 changes: 11 additions & 0 deletions src/ex_uri.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{application, ex_uri,
[
{description, "An URI-handling library application"},
{vsn, "1.0.0"},
{registered, []},
{applications, [
kernel,
stdlib
]},
{env, []}
]}.
48 changes: 48 additions & 0 deletions src/ex_uri.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
%% Copyright (c) 2010, Dev:Extend
%% All rights reserved.
%%
%% Redistribution and use in source and binary forms, with or without
%% modification, are permitted provided that the following conditions are met:
%%
%% * Redistributions of source code must retain the above copyright notice,
%% this list of conditions and the following disclaimer.
%% * Redistributions in binary form must reproduce the above copyright
%% notice, this list of conditions and the following disclaimer in the
%% documentation and/or other materials provided with the distribution.
%% * Neither the name of Dev:Extend nor the names of its contributors may be
%% used to endorse or promote products derived from this software without
%% specific prior written permission.
%%
%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
%% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
%% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
%% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
%% POSSIBILITY OF SUCH DAMAGE.

-module(ex_uri).
-include("ex_uri.hrl").

-export([decode/1,
decode_ref/1,
decode_abs/1]).

%% @spec decode(string()) -> #ex_uri{}
%% @doc Decode an URI.
decode(String) ->
ex_uri_parser:decode('URI', String).

%% @spec decode_ref(string()) -> #ex_uri{} | #ex_uri_ref{}
%% @doc Decode an URI reference.
decode_ref(String) ->
ex_uri_parser:decode('URI-reference', String).

%% @spec decode_abs(string()) -> #ex_uri{}
%% @doc Decode an absolute URI.
decode_abs(String) ->
ex_uri_parser:decode('absolute-URI', String).
3 changes: 3 additions & 0 deletions src/ex_uri_parser.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-include("ex_uri.hrl").

-import(rfc4234_core, ['ALPHA'/0, 'DIGIT'/0, 'HEXDIG'/0]).

0 comments on commit c7c0a17

Please sign in to comment.