4
%% Copyright Ericsson AB 2006-2009. 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
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}}
41
%%%========================================================================
42
%%% Internal functions
43
%%%========================================================================
44
parse_scheme(AbsURI) ->
45
case split_uri(AbsURI, ":", {error, no_scheme}, 1, 1) of
49
case list_to_atom(http_util:to_lower(StrScheme)) of
50
Scheme when Scheme == http; Scheme == https ->
53
{error, {not_supported_scheme, Scheme}}
57
parse_uri_rest(Scheme, "//" ++ URIPart) ->
59
{Authority, PathQuery} =
60
case split_uri(URIPart, "/", URIPart, 1, 0) of
64
case split_uri(URIPart, "\\?", URIPart, 1, 0) of
72
{UserInfo, HostPort} = split_uri(Authority, "@", {"", Authority}, 1, 1),
73
{Host, Port} = parse_host_port(Scheme, HostPort),
74
{Path, Query} = parse_path_query(PathQuery),
75
{UserInfo, Host, Port, Path, Query}.
78
parse_path_query(PathQuery) ->
79
{Path, Query} = split_uri(PathQuery, "\\?", {PathQuery, ""}, 1, 0),
83
parse_host_port(Scheme,"[" ++ HostPort) -> %ipv6
84
DefaultPort = default_port(Scheme),
85
{Host, ColonPort} = split_uri(HostPort, "\\]", {HostPort, ""}, 1, 1),
86
{_, Port} = split_uri(ColonPort, ":", {"", DefaultPort}, 0, 1),
87
{Host, int_port(Port)};
89
parse_host_port(Scheme, HostPort) ->
90
DefaultPort = default_port(Scheme),
91
{Host, Port} = split_uri(HostPort, ":", {HostPort, DefaultPort}, 1, 1),
92
{Host, int_port(Port)}.
94
split_uri(UriPart, SplitChar, NoMatchResult, SkipLeft, SkipRight) ->
95
case inets_regexp:first_match(UriPart, SplitChar) of
97
{string:substr(UriPart, 1, Match - SkipLeft),
98
string:substr(UriPart, Match + SkipRight, length(UriPart))};
103
default_port(http) ->
105
default_port(https) ->
108
int_port(Port) when is_integer(Port) ->
110
int_port(Port) when is_list(Port) ->
111
list_to_integer(Port).