Handle HAVE_ALL and HAVE_NONE. Cleanup the BITFIELD message.
[etorrent.git] / lib / etorrent-1.0 / src / etorrent_http_uri.erl
blob57fe065e8b574b51f7ef66b19390ce7e1cf1680e
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/.
6 %%
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
10 %% under the License.
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.''
16 %% $Id$
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).
24 -export([parse/1]).
26 %%%=========================================================================
27 %%% API
28 %%%=========================================================================
29 parse(AbsURI) ->
30 case parse_scheme(AbsURI) of
31 {error, Reason} ->
32 {error, Reason};
33 {Scheme, Rest} ->
34 case (catch parse_uri_rest(Scheme, Rest)) of
35 {UserInfo, Host, Port, Path, Query} ->
36 {Scheme, UserInfo, Host, Port, Path, Query};
37 _ ->
38 {error, {malformed_url, AbsURI}}
39 end
40 end.
42 %%%========================================================================
43 %%% Internal functions
44 %%%========================================================================
45 parse_scheme(AbsURI) ->
46 case split_uri(AbsURI, ":", {error, no_scheme}, 1, 1) of
47 {error, no_scheme} ->
48 {error, no_scheme};
49 {StrScheme, Rest} ->
50 case list_to_atom(http_util:to_lower(StrScheme)) of
51 Scheme when Scheme == http; Scheme == https ->
52 {Scheme, Rest};
53 Scheme ->
54 {error, {not_supported_scheme, Scheme}}
55 end
56 end.
58 parse_uri_rest(Scheme, "//" ++ URIPart) ->
60 {Authority, PathQuery} =
61 case split_uri(URIPart, "/", URIPart, 1, 0) of
62 Split = {_, _} ->
63 Split;
64 URIPart ->
65 case split_uri(URIPart, "\\?", URIPart, 1, 0) of
66 Split = {_, _} ->
67 Split;
68 URIPart ->
69 {URIPart,""}
70 end
71 end,
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),
81 {path(Path), Query}.
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
97 {match, Match, _} ->
98 {string:substr(UriPart, 1, Match - SkipLeft),
99 string:substr(UriPart, Match + SkipRight, length(UriPart))};
100 nomatch ->
101 NoMatchResult
102 end.
104 default_port(http) ->
106 default_port(https) ->
107 443.
109 int_port(Port) when is_integer(Port) ->
110 Port;
111 int_port(Port) when is_list(Port) ->
112 list_to_integer(Port).
114 path("") ->
115 "/";
116 path(Path) ->
117 Path.