4
%% Copyright Ericsson AB 2006-2010. All Rights Reserved.
6
%% The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
23
-export([parse/1, encode/1, decode/1]).
25
%%%=========================================================================
27
%%%=========================================================================
29
case parse_scheme(AbsURI) of
33
case (catch parse_uri_rest(Scheme, Rest)) of
34
{UserInfo, Host, Port, Path, Query} ->
35
{Scheme, UserInfo, Host, Port, Path, Query};
37
{error, {malformed_url, AbsURI}}
42
Reserved = sets:from_list([$;, $:, $@, $&, $=, $+, $,, $/, $?,
43
$#, $[, $], $<, $>, $\", ${, $}, $|,
44
$\\, $', $^, $%, $ ]),
45
lists:append(lists:map(fun(Char) ->
46
uri_encode(Char, Reserved)
49
decode([$%,Hex1,Hex2|Rest]) ->
50
[hex2dec(Hex1)*16+hex2dec(Hex2)|decode(Rest)];
51
decode([First|Rest]) ->
56
%%%========================================================================
57
%%% Internal functions
58
%%%========================================================================
59
parse_scheme(AbsURI) ->
60
case split_uri(AbsURI, ":", {error, no_scheme}, 1, 1) of
64
case list_to_atom(http_util:to_lower(StrScheme)) of
65
Scheme when Scheme == http; Scheme == https ->
68
{error, {not_supported_scheme, Scheme}}
72
parse_uri_rest(Scheme, "//" ++ URIPart) ->
74
{Authority, PathQuery} =
75
case split_uri(URIPart, "/", URIPart, 1, 0) of
79
case split_uri(URIPart, "\\?", URIPart, 1, 0) of
87
{UserInfo, HostPort} = split_uri(Authority, "@", {"", Authority}, 1, 1),
88
{Host, Port} = parse_host_port(Scheme, HostPort),
89
{Path, Query} = parse_path_query(PathQuery),
90
{UserInfo, Host, Port, Path, Query}.
93
parse_path_query(PathQuery) ->
94
{Path, Query} = split_uri(PathQuery, "\\?", {PathQuery, ""}, 1, 0),
97
parse_host_port(Scheme,"[" ++ HostPort) -> %ipv6
98
DefaultPort = default_port(Scheme),
99
{Host, ColonPort} = split_uri(HostPort, "\\]", {HostPort, ""}, 1, 1),
100
{_, Port} = split_uri(ColonPort, ":", {"", DefaultPort}, 0, 1),
101
{Host, int_port(Port)};
103
parse_host_port(Scheme, HostPort) ->
104
DefaultPort = default_port(Scheme),
105
{Host, Port} = split_uri(HostPort, ":", {HostPort, DefaultPort}, 1, 1),
106
{Host, int_port(Port)}.
108
split_uri(UriPart, SplitChar, NoMatchResult, SkipLeft, SkipRight) ->
109
case inets_regexp:first_match(UriPart, SplitChar) of
111
{string:substr(UriPart, 1, Match - SkipLeft),
112
string:substr(UriPart, Match + SkipRight, length(UriPart))};
117
default_port(http) ->
119
default_port(https) ->
122
int_port(Port) when is_integer(Port) ->
124
int_port(Port) when is_list(Port) ->
125
list_to_integer(Port).
132
uri_encode(Char, Reserved) ->
133
case sets:is_element(Char, Reserved) of
135
[ $% | http_util:integer_to_hexlist(Char)];
140
hex2dec(X) when (X>=$0) andalso (X=<$9) -> X-$0;
141
hex2dec(X) when (X>=$A) andalso (X=<$F) -> X-$A+10;
142
hex2dec(X) when (X>=$a) andalso (X=<$f) -> X-$a+10.