This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
forked from lpgauth/lhttpc
/
lhttpc_lib.erl
255 lines (232 loc) · 8.59 KB
/
lhttpc_lib.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
%%% coding: latin-1
%%% ----------------------------------------------------------------------------
%%% Copyright (c) 2009, Erlang Training and Consulting Ltd.
%%% 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 Erlang Training and Consulting Ltd. 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 Erlang Training and Consulting Ltd. ''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 Erlang Training and Consulting Ltd. BE
%%% LIABLE 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.
%%% ----------------------------------------------------------------------------
%%% @private
%%% @author Oscar Hellström <oscar@hellstrom.st>
%%% @doc
%%% This module implements various library functions used in lhttpc.
-module(lhttpc_lib).
-export([
parse_url/1,
format_request/7,
header_value/2,
header_value/3,
normalize_method/1
]).
-export([maybe_atom_to_list/1]).
-export([format_hdrs/1, dec/1]).
-include("lhttpc_types.hrl").
%% @spec header_value(Header, Headers) -> undefined | term()
%% Header = string()
%% Headers = [{string(), term()}]
%% Value = term()
%% @doc
%% Returns the value associated with the `Header' in `Headers'.
%% `Header' must be a lowercase string, since every header is mangled to
%% check the match.
%% @end
-spec header_value(string(), [{string(), Value}]) -> undefined | Value.
header_value(Hdr, Hdrs) ->
header_value(Hdr, Hdrs, undefined).
%% @spec header_value(Header, Headers, Default) -> Default | term()
%% Header = string()
%% Headers = [{string(), term()}]
%% Value = term()
%% Default = term()
%% @doc
%% Returns the value associated with the `Header' in `Headers'.
%% `Header' must be a lowercase string, since every header is mangled to
%% check the match. If no match is found, `Default' is returned.
%% @end
-spec header_value(string(), [{string(), Value}], Default) ->
Default | Value.
header_value(Hdr, [{Hdr, Value} | _], _) ->
Value;
header_value(Hdr, [{ThisHdr, Value}| Hdrs], Default) ->
case string:equal(string:to_lower(ThisHdr), Hdr) of
true -> Value;
false -> header_value(Hdr, Hdrs, Default)
end;
header_value(_, [], Default) ->
Default.
%% @spec (Item) -> OtherItem
%% Item = atom() | list()
%% OtherItem = list()
%% @doc
%% Will make any item, being an atom or a list, in to a list. If it is a
%% list, it is simple returned.
%% @end
-spec maybe_atom_to_list(atom() | list()) -> list().
maybe_atom_to_list(Atom) when is_atom(Atom) ->
atom_to_list(Atom);
maybe_atom_to_list(List) when is_list(List) ->
List.
%% @spec (URL) -> {Host, Port, Path, Ssl}
%% URL = string()
%% Host = string()
%% Port = integer()
%% Path = string()
%% Ssl = boolean()
%% @doc
-spec parse_url(string()) -> {string(), integer(), string(), boolean()}.
parse_url(URL) ->
% XXX This should be possible to do with the re module?
{Scheme, HostPortPath} = split_scheme(URL),
{Host, PortPath} = split_host(HostPortPath, []),
{Port, Path} = split_port(Scheme, PortPath, []),
{string:to_lower(Host), Port, Path, Scheme =:= https}.
split_scheme("http://" ++ HostPortPath) ->
{http, HostPortPath};
split_scheme("https://" ++ HostPortPath) ->
{https, HostPortPath}.
split_host([$: | PortPath], Host) ->
{lists:reverse(Host), PortPath};
split_host([$/ | _] = PortPath, Host) ->
{lists:reverse(Host), PortPath};
split_host([H | T], Host) ->
split_host(T, [H | Host]);
split_host([], Host) ->
{lists:reverse(Host), []}.
split_port(http, [$/ | _] = Path, []) ->
{80, Path};
split_port(https, [$/ | _] = Path, []) ->
{443, Path};
split_port(http, [], []) ->
{80, "/"};
split_port(https, [], []) ->
{443, "/"};
split_port(_, [], Port) ->
{list_to_integer(lists:reverse(Port)), "/"};
split_port(_,[$/ | _] = Path, Port) ->
{list_to_integer(lists:reverse(Port)), Path};
split_port(Scheme, [P | T], Port) ->
split_port(Scheme, T, [P | Port]).
%% @spec (Path, Method, Headers, Host, Port, Body, PartialUpload) -> Request
%% Path = iolist()
%% Method = atom() | string()
%% Headers = [{atom() | string(), string()}]
%% Host = string()
%% Port = integer()
%% Body = iolist()
%% PartialUpload = true | false
-spec format_request(iolist(), atom() | string(), headers(), string(),
integer(), iolist(), true | false ) -> {true | false, iolist()}.
format_request(Path, Method, Hdrs, Host, Port, Body, PartialUpload) ->
AllHdrs = add_mandatory_hdrs(Method, Hdrs, Host, Port, Body, PartialUpload),
IsChunked = is_chunked(AllHdrs),
{
IsChunked,
[
Method, " ", Path, " HTTP/1.1\r\n",
format_hdrs(AllHdrs),
format_body(Body, IsChunked)
]
}.
%% @spec normalize_method(AtomOrString) -> Method
%% AtomOrString = atom() | string()
%% Method = string()
%% @doc
%% Turns the method in to a string suitable for inclusion in a HTTP request
%% line.
%% @end
-spec normalize_method(atom() | string()) -> string().
normalize_method(Method) when is_atom(Method) ->
string:to_upper(atom_to_list(Method));
normalize_method(Method) ->
Method.
-spec format_hdrs(headers()) -> iolist().
format_hdrs(Headers) ->
format_hdrs(Headers, []).
format_hdrs([{Hdr, Value} | T], Acc) ->
NewAcc = [
maybe_atom_to_list(Hdr), ": ", maybe_atom_to_list(Value), "\r\n" | Acc
],
format_hdrs(T, NewAcc);
format_hdrs([], Acc) ->
[Acc, "\r\n"].
format_body(Body, false) ->
Body;
format_body(Body, true) ->
case iolist_size(Body) of
0 ->
<<>>;
Size ->
[
erlang:integer_to_list(Size, 16), <<"\r\n">>,
Body, <<"\r\n">>
]
end.
add_mandatory_hdrs(Method, Hdrs, Host, Port, Body, PartialUpload) ->
ContentHdrs = add_content_headers(Method, Hdrs, Body, PartialUpload),
add_host(ContentHdrs, Host, Port).
add_content_headers("POST", Hdrs, Body, PartialUpload) ->
add_content_headers(Hdrs, Body, PartialUpload);
add_content_headers("PUT", Hdrs, Body, PartialUpload) ->
add_content_headers(Hdrs, Body, PartialUpload);
add_content_headers(_, Hdrs, _, _PartialUpload) ->
Hdrs.
add_content_headers(Hdrs, Body, false) ->
case header_value("content-length", Hdrs) of
undefined ->
ContentLength = integer_to_list(iolist_size(Body)),
[{"Content-Length", ContentLength} | Hdrs];
_ -> % We have a content length
Hdrs
end;
add_content_headers(Hdrs, _Body, true) ->
case {header_value("content-length", Hdrs),
header_value("transfer-encoding", Hdrs)} of
{undefined, undefined} ->
[{"Transfer-Encoding", "chunked"} | Hdrs];
{undefined, TransferEncoding} ->
case string:to_lower(TransferEncoding) of
"chunked" -> Hdrs;
_ -> erlang:error({error, unsupported_transfer_encoding})
end;
{_Length, undefined} ->
Hdrs;
{_Length, _TransferEncoding} -> %% have both cont.length and chunked
erlang:error({error, bad_header})
end.
add_host(Hdrs, Host, Port) ->
case header_value("host", Hdrs) of
undefined ->
[{"Host", host(Host, Port) } | Hdrs];
_ -> % We have a host
Hdrs
end.
is_chunked(Hdrs) ->
TransferEncoding = string:to_lower(
header_value("transfer-encoding", Hdrs, "undefined")),
case TransferEncoding of
"chunked" -> true;
_ -> false
end.
-spec dec(timeout()) -> timeout().
dec(Num) when is_integer(Num) -> Num - 1;
dec(Else) -> Else.
host(Host, 80) -> Host;
host(Host, Port) -> [Host, $:, integer_to_list(Port)].