1 % ``The contents of this file are subject to the Erlang Public License,
2 %% Version 1.1, (the "License"); you may not use this file except in
3 %% compliance with the License. You should have received a copy of the
4 %% Erlang Public License along with this software. If not, it can be
5 %% retrieved via the world wide web at http://www.erlang.org/.
7 %% Software distributed under the License is distributed on an "AS IS"
8 %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9 %% the License for the specific language governing rights and limitations
12 %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13 %% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14 %% AB. All Rights Reserved.''
18 %%--------------------------------------------------------------------
19 %% Note: This URI-parsing library is stolen from R12B-3 from the inets
20 %% application. I have bastardized it here for our purposes.
21 %%--------------------------------------------------------------------
22 -module(etorrent_http_uri
).
26 %%%=========================================================================
28 %%%=========================================================================
30 case parse_scheme(AbsURI
) of
34 case (catch parse_uri_rest(Scheme
, Rest
)) of
35 {UserInfo
, Host
, Port
, Path
, Query
} ->
36 {Scheme
, UserInfo
, Host
, Port
, Path
, Query
};
38 {error
, {malformed_url
, AbsURI
}}
42 %%%========================================================================
43 %%% Internal functions
44 %%%========================================================================
45 parse_scheme(AbsURI
) ->
46 case split_uri(AbsURI
, ":", {error
, no_scheme
}, 1, 1) of
50 case list_to_atom(http_util:to_lower(StrScheme
)) of
51 Scheme
when Scheme
== http
; Scheme
== https
->
54 {error
, {not_supported_scheme
, Scheme
}}
58 parse_uri_rest(Scheme
, "//" ++ URIPart
) ->
60 {Authority
, PathQuery
} =
61 case split_uri(URIPart
, "/", URIPart
, 1, 0) of
65 case split_uri(URIPart
, "\\?", URIPart
, 1, 0) of
73 {UserInfo
, HostPort
} = split_uri(Authority
, "@", {"", Authority
}, 1, 1),
74 {Host
, Port
} = parse_host_port(Scheme
, HostPort
),
75 {Path
, Query
} = parse_path_query(PathQuery
),
76 {UserInfo
, Host
, Port
, Path
, Query
}.
79 parse_path_query(PathQuery
) ->
80 {Path
, Query
} = split_uri(PathQuery
, "\\?", {PathQuery
, ""}, 1, 0),
84 parse_host_port(Scheme
,"[" ++ HostPort
) -> %ipv6
85 DefaultPort
= default_port(Scheme
),
86 {Host
, ColonPort
} = split_uri(HostPort
, "\\]", {HostPort
, ""}, 1, 1),
87 {_
, Port
} = split_uri(ColonPort
, ":", {"", DefaultPort
}, 0, 1),
88 {Host
, int_port(Port
)};
90 parse_host_port(Scheme
, HostPort
) ->
91 DefaultPort
= default_port(Scheme
),
92 {Host
, Port
} = split_uri(HostPort
, ":", {HostPort
, DefaultPort
}, 1, 1),
93 {Host
, int_port(Port
)}.
95 split_uri(UriPart
, SplitChar
, NoMatchResult
, SkipLeft
, SkipRight
) ->
96 case regexp:first_match(UriPart
, SplitChar
) of
98 {string:substr(UriPart
, 1, Match
- SkipLeft
),
99 string:substr(UriPart
, Match
+ SkipRight
, length(UriPart
))};
104 default_port(http
) ->
106 default_port(https
) ->
109 int_port(Port
) when is_integer(Port
) ->
111 int_port(Port
) when is_list(Port
) ->
112 list_to_integer(Port
).