1%% @author Bob Ippolito <bob@mochimedia.com>
2%% @copyright 2007 Mochi Media, Inc.
3
4%% @doc Utilities for parsing and quoting.
5
6-module(mochiweb_util).
7-author('bob@mochimedia.com').
8-export([join/2, quote_plus/1, urlencode/1, parse_qs/1, unquote/1]).
9-export([path_split/1]).
10-export([urlsplit/1, urlsplit_path/1, urlunsplit/1, urlunsplit_path/1]).
11-export([guess_mime/1, parse_header/1]).
12-export([shell_quote/1, cmd/1, cmd_string/1, cmd_port/2, cmd_status/1, cmd_status/2]).
13-export([record_to_proplist/2, record_to_proplist/3]).
14-export([safe_relative_path/1, partition/2]).
15-export([parse_qvalues/1, pick_accepted_encodings/3]).
16-export([make_io/1]).
17
18-define(PERCENT, 37).  % $\%
19-define(FULLSTOP, 46). % $\.
20-define(IS_HEX(C), ((C >= $0 andalso C =< $9) orelse
21                    (C >= $a andalso C =< $f) orelse
22                    (C >= $A andalso C =< $F))).
23-define(QS_SAFE(C), ((C >= $a andalso C =< $z) orelse
24                     (C >= $A andalso C =< $Z) orelse
25                     (C >= $0 andalso C =< $9) orelse
26                     (C =:= ?FULLSTOP orelse C =:= $- orelse C =:= $~ orelse
27                      C =:= $_))).
28
29hexdigit(C) when C < 10 -> $0 + C;
30hexdigit(C) when C < 16 -> $A + (C - 10).
31
32unhexdigit(C) when C >= $0, C =< $9 -> C - $0;
33unhexdigit(C) when C >= $a, C =< $f -> C - $a + 10;
34unhexdigit(C) when C >= $A, C =< $F -> C - $A + 10.
35
36%% @spec partition(String, Sep) -> {String, [], []} | {Prefix, Sep, Postfix}
37%% @doc Inspired by Python 2.5's str.partition:
38%%      partition("foo/bar", "/") = {"foo", "/", "bar"},
39%%      partition("foo", "/") = {"foo", "", ""}.
40partition(String, Sep) ->
41    case partition(String, Sep, []) of
42        undefined ->
43            {String, "", ""};
44        Result ->
45            Result
46    end.
47
48partition("", _Sep, _Acc) ->
49    undefined;
50partition(S, Sep, Acc) ->
51    case partition2(S, Sep) of
52        undefined ->
53            [C | Rest] = S,
54            partition(Rest, Sep, [C | Acc]);
55        Rest ->
56            {lists:reverse(Acc), Sep, Rest}
57    end.
58
59partition2(Rest, "") ->
60    Rest;
61partition2([C | R1], [C | R2]) ->
62    partition2(R1, R2);
63partition2(_S, _Sep) ->
64    undefined.
65
66
67
68%% @spec safe_relative_path(string()) -> string() | undefined
69%% @doc Return the reduced version of a relative path or undefined if it
70%%      is not safe. safe relative paths can be joined with an absolute path
71%%      and will result in a subdirectory of the absolute path. Safe paths
72%%      never contain a backslash character.
73safe_relative_path("/" ++ _) ->
74    undefined;
75safe_relative_path(P) ->
76    case string:chr(P, $\\) of
77        0 ->
78           safe_relative_path(P, []);
79        _ ->
80           undefined
81    end.
82
83safe_relative_path("", Acc) ->
84    case Acc of
85        [] ->
86            "";
87        _ ->
88            string:join(lists:reverse(Acc), "/")
89    end;
90safe_relative_path(P, Acc) ->
91    case partition(P, "/") of
92        {"", "/", _} ->
93            %% /foo or foo//bar
94            undefined;
95        {"..", _, _} when Acc =:= [] ->
96            undefined;
97        {"..", _, Rest} ->
98            safe_relative_path(Rest, tl(Acc));
99        {Part, "/", ""} ->
100            safe_relative_path("", ["", Part | Acc]);
101        {Part, _, Rest} ->
102            safe_relative_path(Rest, [Part | Acc])
103    end.
104
105%% @spec shell_quote(string()) -> string()
106%% @doc Quote a string according to UNIX shell quoting rules, returns a string
107%%      surrounded by double quotes.
108shell_quote(L) ->
109    shell_quote(L, [$\"]).
110
111%% @spec cmd_port([string()], Options) -> port()
112%% @doc open_port({spawn, mochiweb_util:cmd_string(Argv)}, Options).
113cmd_port(Argv, Options) ->
114    open_port({spawn, cmd_string(Argv)}, Options).
115
116%% @spec cmd([string()]) -> string()
117%% @doc os:cmd(cmd_string(Argv)).
118cmd(Argv) ->
119    os:cmd(cmd_string(Argv)).
120
121%% @spec cmd_string([string()]) -> string()
122%% @doc Create a shell quoted command string from a list of arguments.
123cmd_string(Argv) ->
124    string:join([shell_quote(X) || X <- Argv], " ").
125
126%% @spec cmd_status([string()]) -> {ExitStatus::integer(), Stdout::binary()}
127%% @doc Accumulate the output and exit status from the given application,
128%%      will be spawned with cmd_port/2.
129cmd_status(Argv) ->
130    cmd_status(Argv, []).
131
132%% @spec cmd_status([string()], [atom()]) -> {ExitStatus::integer(), Stdout::binary()}
133%% @doc Accumulate the output and exit status from the given application,
134%%      will be spawned with cmd_port/2.
135cmd_status(Argv, Options) ->
136    Port = cmd_port(Argv, [exit_status, stderr_to_stdout,
137                           use_stdio, binary | Options]),
138    try cmd_loop(Port, [])
139    after catch port_close(Port)
140    end.
141
142%% @spec cmd_loop(port(), list()) -> {ExitStatus::integer(), Stdout::binary()}
143%% @doc Accumulate the output and exit status from a port.
144cmd_loop(Port, Acc) ->
145    receive
146        {Port, {exit_status, Status}} ->
147            {Status, iolist_to_binary(lists:reverse(Acc))};
148        {Port, {data, Data}} ->
149            cmd_loop(Port, [Data | Acc])
150    end.
151
152%% @spec join([iolist()], iolist()) -> iolist()
153%% @doc Join a list of strings or binaries together with the given separator
154%%      string or char or binary. The output is flattened, but may be an
155%%      iolist() instead of a string() if any of the inputs are binary().
156join([], _Separator) ->
157    [];
158join([S], _Separator) ->
159    lists:flatten(S);
160join(Strings, Separator) ->
161    lists:flatten(revjoin(lists:reverse(Strings), Separator, [])).
162
163revjoin([], _Separator, Acc) ->
164    Acc;
165revjoin([S | Rest], Separator, []) ->
166    revjoin(Rest, Separator, [S]);
167revjoin([S | Rest], Separator, Acc) ->
168    revjoin(Rest, Separator, [S, Separator | Acc]).
169
170%% @spec quote_plus(atom() | integer() | float() | string() | binary()) -> string()
171%% @doc URL safe encoding of the given term.
172quote_plus(Atom) when is_atom(Atom) ->
173    quote_plus(atom_to_list(Atom));
174quote_plus(Int) when is_integer(Int) ->
175    quote_plus(integer_to_list(Int));
176quote_plus(Binary) when is_binary(Binary) ->
177    quote_plus(binary_to_list(Binary));
178quote_plus(Float) when is_float(Float) ->
179    quote_plus(mochinum:digits(Float));
180quote_plus(String) ->
181    quote_plus(String, []).
182
183quote_plus([], Acc) ->
184    lists:reverse(Acc);
185quote_plus([C | Rest], Acc) when ?QS_SAFE(C) ->
186    quote_plus(Rest, [C | Acc]);
187quote_plus([$\s | Rest], Acc) ->
188    quote_plus(Rest, [$+ | Acc]);
189quote_plus([C | Rest], Acc) ->
190    <<Hi:4, Lo:4>> = <<C>>,
191    quote_plus(Rest, [hexdigit(Lo), hexdigit(Hi), ?PERCENT | Acc]).
192
193%% @spec urlencode([{Key, Value}]) -> string()
194%% @doc URL encode the property list.
195urlencode(Props) ->
196    Pairs = lists:foldr(
197              fun ({K, V}, Acc) ->
198                      [quote_plus(K) ++ "=" ++ quote_plus(V) | Acc]
199              end, [], Props),
200    string:join(Pairs, "&").
201
202%% @spec parse_qs(string() | binary()) -> [{Key, Value}]
203%% @doc Parse a query string or application/x-www-form-urlencoded.
204parse_qs(Binary) when is_binary(Binary) ->
205    parse_qs(binary_to_list(Binary));
206parse_qs(String) ->
207    parse_qs(String, []).
208
209parse_qs([], Acc) ->
210    lists:reverse(Acc);
211parse_qs(String, Acc) ->
212    {Key, Rest} = parse_qs_key(String),
213    {Value, Rest1} = parse_qs_value(Rest),
214    parse_qs(Rest1, [{Key, Value} | Acc]).
215
216parse_qs_key(String) ->
217    parse_qs_key(String, []).
218
219parse_qs_key([], Acc) ->
220    {qs_revdecode(Acc), ""};
221parse_qs_key([$= | Rest], Acc) ->
222    {qs_revdecode(Acc), Rest};
223parse_qs_key(Rest=[$; | _], Acc) ->
224    {qs_revdecode(Acc), Rest};
225parse_qs_key(Rest=[$& | _], Acc) ->
226    {qs_revdecode(Acc), Rest};
227parse_qs_key([C | Rest], Acc) ->
228    parse_qs_key(Rest, [C | Acc]).
229
230parse_qs_value(String) ->
231    parse_qs_value(String, []).
232
233parse_qs_value([], Acc) ->
234    {qs_revdecode(Acc), ""};
235parse_qs_value([$; | Rest], Acc) ->
236    {qs_revdecode(Acc), Rest};
237parse_qs_value([$& | Rest], Acc) ->
238    {qs_revdecode(Acc), Rest};
239parse_qs_value([C | Rest], Acc) ->
240    parse_qs_value(Rest, [C | Acc]).
241
242%% @spec unquote(string() | binary()) -> string()
243%% @doc Unquote a URL encoded string.
244unquote(Binary) when is_binary(Binary) ->
245    unquote(binary_to_list(Binary));
246unquote(String) ->
247    qs_revdecode(lists:reverse(String)).
248
249qs_revdecode(S) ->
250    qs_revdecode(S, []).
251
252qs_revdecode([], Acc) ->
253    Acc;
254qs_revdecode([$+ | Rest], Acc) ->
255    qs_revdecode(Rest, [$\s | Acc]);
256qs_revdecode([Lo, Hi, ?PERCENT | Rest], Acc) when ?IS_HEX(Lo), ?IS_HEX(Hi) ->
257    qs_revdecode(Rest, [(unhexdigit(Lo) bor (unhexdigit(Hi) bsl 4)) | Acc]);
258qs_revdecode([C | Rest], Acc) ->
259    qs_revdecode(Rest, [C | Acc]).
260
261%% @spec urlsplit(Url) -> {Scheme, Netloc, Path, Query, Fragment}
262%% @doc Return a 5-tuple, does not expand % escapes. Only supports HTTP style
263%%      URLs.
264urlsplit(Url) ->
265    {Scheme, Url1} = urlsplit_scheme(Url),
266    {Netloc, Url2} = urlsplit_netloc(Url1),
267    {Path, Query, Fragment} = urlsplit_path(Url2),
268    {Scheme, Netloc, Path, Query, Fragment}.
269
270urlsplit_scheme(Url) ->
271    case urlsplit_scheme(Url, []) of
272        no_scheme ->
273            {"", Url};
274        Res ->
275            Res
276    end.
277
278urlsplit_scheme([C | Rest], Acc) when ((C >= $a andalso C =< $z) orelse
279                                       (C >= $A andalso C =< $Z) orelse
280                                       (C >= $0 andalso C =< $9) orelse
281                                       C =:= $+ orelse C =:= $- orelse
282                                       C =:= $.) ->
283    urlsplit_scheme(Rest, [C | Acc]);
284urlsplit_scheme([$: | Rest], Acc=[_ | _]) ->
285    {string:to_lower(lists:reverse(Acc)), Rest};
286urlsplit_scheme(_Rest, _Acc) ->
287    no_scheme.
288
289urlsplit_netloc("//" ++ Rest) ->
290    urlsplit_netloc(Rest, []);
291urlsplit_netloc(Path) ->
292    {"", Path}.
293
294urlsplit_netloc("", Acc) ->
295    {lists:reverse(Acc), ""};
296urlsplit_netloc(Rest=[C | _], Acc) when C =:= $/; C =:= $?; C =:= $# ->
297    {lists:reverse(Acc), Rest};
298urlsplit_netloc([C | Rest], Acc) ->
299    urlsplit_netloc(Rest, [C | Acc]).
300
301
302%% @spec path_split(string()) -> {Part, Rest}
303%% @doc Split a path starting from the left, as in URL traversal.
304%%      path_split("foo/bar") = {"foo", "bar"},
305%%      path_split("/foo/bar") = {"", "foo/bar"}.
306path_split(S) ->
307    path_split(S, []).
308
309path_split("", Acc) ->
310    {lists:reverse(Acc), ""};
311path_split("/" ++ Rest, Acc) ->
312    {lists:reverse(Acc), Rest};
313path_split([C | Rest], Acc) ->
314    path_split(Rest, [C | Acc]).
315
316
317%% @spec urlunsplit({Scheme, Netloc, Path, Query, Fragment}) -> string()
318%% @doc Assemble a URL from the 5-tuple. Path must be absolute.
319urlunsplit({Scheme, Netloc, Path, Query, Fragment}) ->
320    lists:flatten([case Scheme of "" -> "";  _ -> [Scheme, "://"] end,
321                   Netloc,
322                   urlunsplit_path({Path, Query, Fragment})]).
323
324%% @spec urlunsplit_path({Path, Query, Fragment}) -> string()
325%% @doc Assemble a URL path from the 3-tuple.
326urlunsplit_path({Path, Query, Fragment}) ->
327    lists:flatten([Path,
328                   case Query of "" -> ""; _ -> [$? | Query] end,
329                   case Fragment of "" -> ""; _ -> [$# | Fragment] end]).
330
331%% @spec urlsplit_path(Url) -> {Path, Query, Fragment}
332%% @doc Return a 3-tuple, does not expand % escapes. Only supports HTTP style
333%%      paths.
334urlsplit_path(Path) ->
335    urlsplit_path(Path, []).
336
337urlsplit_path("", Acc) ->
338    {lists:reverse(Acc), "", ""};
339urlsplit_path("?" ++ Rest, Acc) ->
340    {Query, Fragment} = urlsplit_query(Rest),
341    {lists:reverse(Acc), Query, Fragment};
342urlsplit_path("#" ++ Rest, Acc) ->
343    {lists:reverse(Acc), "", Rest};
344urlsplit_path([C | Rest], Acc) ->
345    urlsplit_path(Rest, [C | Acc]).
346
347urlsplit_query(Query) ->
348    urlsplit_query(Query, []).
349
350urlsplit_query("", Acc) ->
351    {lists:reverse(Acc), ""};
352urlsplit_query("#" ++ Rest, Acc) ->
353    {lists:reverse(Acc), Rest};
354urlsplit_query([C | Rest], Acc) ->
355    urlsplit_query(Rest, [C | Acc]).
356
357%% @spec guess_mime(string()) -> string()
358%% @doc  Guess the mime type of a file by the extension of its filename.
359guess_mime(File) ->
360    case mochiweb_mime:from_extension(filename:extension(File)) of
361        undefined ->
362            "text/plain";
363        Mime ->
364            Mime
365    end.
366
367%% @spec parse_header(string()) -> {Type, [{K, V}]}
368%% @doc  Parse a Content-Type like header, return the main Content-Type
369%%       and a property list of options.
370parse_header(String) ->
371    %% TODO: This is exactly as broken as Python's cgi module.
372    %%       Should parse properly like mochiweb_cookies.
373    [Type | Parts] = [string:strip(S) || S <- string:tokens(String, ";")],
374    F = fun (S, Acc) ->
375                case lists:splitwith(fun (C) -> C =/= $= end, S) of
376                    {"", _} ->
377                        %% Skip anything with no name
378                        Acc;
379                    {_, ""} ->
380                        %% Skip anything with no value
381                        Acc;
382                    {Name, [$\= | Value]} ->
383                        [{string:to_lower(string:strip(Name)),
384                          unquote_header(string:strip(Value))} | Acc]
385                end
386        end,
387    {string:to_lower(Type),
388     lists:foldr(F, [], Parts)}.
389
390unquote_header("\"" ++ Rest) ->
391    unquote_header(Rest, []);
392unquote_header(S) ->
393    S.
394
395unquote_header("", Acc) ->
396    lists:reverse(Acc);
397unquote_header("\"", Acc) ->
398    lists:reverse(Acc);
399unquote_header([$\\, C | Rest], Acc) ->
400    unquote_header(Rest, [C | Acc]);
401unquote_header([C | Rest], Acc) ->
402    unquote_header(Rest, [C | Acc]).
403
404%% @spec record_to_proplist(Record, Fields) -> proplist()
405%% @doc calls record_to_proplist/3 with a default TypeKey of '__record'
406record_to_proplist(Record, Fields) ->
407    record_to_proplist(Record, Fields, '__record').
408
409%% @spec record_to_proplist(Record, Fields, TypeKey) -> proplist()
410%% @doc Return a proplist of the given Record with each field in the
411%%      Fields list set as a key with the corresponding value in the Record.
412%%      TypeKey is the key that is used to store the record type
413%%      Fields should be obtained by calling record_info(fields, record_type)
414%%      where record_type is the record type of Record
415record_to_proplist(Record, Fields, TypeKey)
416  when tuple_size(Record) - 1 =:= length(Fields) ->
417    lists:zip([TypeKey | Fields], tuple_to_list(Record)).
418
419
420shell_quote([], Acc) ->
421    lists:reverse([$\" | Acc]);
422shell_quote([C | Rest], Acc) when C =:= $\" orelse C =:= $\` orelse
423                                  C =:= $\\ orelse C =:= $\$ ->
424    shell_quote(Rest, [C, $\\ | Acc]);
425shell_quote([C | Rest], Acc) ->
426    shell_quote(Rest, [C | Acc]).
427
428%% @spec parse_qvalues(string()) -> [qvalue()] | invalid_qvalue_string
429%% @type qvalue() = {media_type() | encoding() , float()}.
430%% @type media_type() = string().
431%% @type encoding() = string().
432%%
433%% @doc Parses a list (given as a string) of elements with Q values associated
434%%      to them. Elements are separated by commas and each element is separated
435%%      from its Q value by a semicolon. Q values are optional but when missing
436%%      the value of an element is considered as 1.0. A Q value is always in the
437%%      range [0.0, 1.0]. A Q value list is used for example as the value of the
438%%      HTTP "Accept" and "Accept-Encoding" headers.
439%%
440%%      Q values are described in section 2.9 of the RFC 2616 (HTTP 1.1).
441%%
442%%      Example:
443%%
444%%      parse_qvalues("gzip; q=0.5, deflate, identity;q=0.0") ->
445%%          [{"gzip", 0.5}, {"deflate", 1.0}, {"identity", 0.0}]
446%%
447parse_qvalues(QValuesStr) ->
448    try
449        lists:map(
450            fun(Pair) ->
451                [Type | Params] = string:tokens(Pair, ";"),
452                NormParams = normalize_media_params(Params),
453                {Q, NonQParams} = extract_q(NormParams),
454                {string:join([string:strip(Type) | NonQParams], ";"), Q}
455            end,
456            string:tokens(string:to_lower(QValuesStr), ",")
457        )
458    catch
459        _Type:_Error ->
460            invalid_qvalue_string
461    end.
462
463normalize_media_params(Params) ->
464    {ok, Re} = re:compile("\\s"),
465    normalize_media_params(Re, Params, []).
466
467normalize_media_params(_Re, [], Acc) ->
468    lists:reverse(Acc);
469normalize_media_params(Re, [Param | Rest], Acc) ->
470    NormParam = re:replace(Param, Re, "", [global, {return, list}]),
471    normalize_media_params(Re, Rest, [NormParam | Acc]).
472
473extract_q(NormParams) ->
474    {ok, KVRe} = re:compile("^([^=]+)=([^=]+)$"),
475    {ok, QRe} = re:compile("^((?:0|1)(?:\\.\\d{1,3})?)$"),
476    extract_q(KVRe, QRe, NormParams, []).
477
478extract_q(_KVRe, _QRe, [], Acc) ->
479    {1.0, lists:reverse(Acc)};
480extract_q(KVRe, QRe, [Param | Rest], Acc) ->
481    case re:run(Param, KVRe, [{capture, [1, 2], list}]) of
482        {match, [Name, Value]} ->
483            case Name of
484            "q" ->
485                {match, [Q]} = re:run(Value, QRe, [{capture, [1], list}]),
486                QVal = case Q of
487                    "0" ->
488                        0.0;
489                    "1" ->
490                        1.0;
491                    Else ->
492                        list_to_float(Else)
493                end,
494                case QVal < 0.0 orelse QVal > 1.0 of
495                false ->
496                    {QVal, lists:reverse(Acc) ++ Rest}
497                end;
498            _ ->
499                extract_q(KVRe, QRe, Rest, [Param | Acc])
500            end
501    end.
502
503%% @spec pick_accepted_encodings([qvalue()], [encoding()], encoding()) ->
504%%    [encoding()]
505%%
506%% @doc Determines which encodings specified in the given Q values list are
507%%      valid according to a list of supported encodings and a default encoding.
508%%
509%%      The returned list of encodings is sorted, descendingly, according to the
510%%      Q values of the given list. The last element of this list is the given
511%%      default encoding unless this encoding is explicitily or implicitily
512%%      marked with a Q value of 0.0 in the given Q values list.
513%%      Note: encodings with the same Q value are kept in the same order as
514%%            found in the input Q values list.
515%%
516%%      This encoding picking process is described in section 14.3 of the
517%%      RFC 2616 (HTTP 1.1).
518%%
519%%      Example:
520%%
521%%      pick_accepted_encodings(
522%%          [{"gzip", 0.5}, {"deflate", 1.0}],
523%%          ["gzip", "identity"],
524%%          "identity"
525%%      ) ->
526%%          ["gzip", "identity"]
527%%
528pick_accepted_encodings(AcceptedEncs, SupportedEncs, DefaultEnc) ->
529    SortedQList = lists:reverse(
530        lists:sort(fun({_, Q1}, {_, Q2}) -> Q1 < Q2 end, AcceptedEncs)
531    ),
532    {Accepted, Refused} = lists:foldr(
533        fun({E, Q}, {A, R}) ->
534            case Q > 0.0 of
535                true ->
536                    {[E | A], R};
537                false ->
538                    {A, [E | R]}
539            end
540        end,
541        {[], []},
542        SortedQList
543    ),
544    Refused1 = lists:foldr(
545        fun(Enc, Acc) ->
546            case Enc of
547                "*" ->
548                    lists:subtract(SupportedEncs, Accepted) ++ Acc;
549                _ ->
550                    [Enc | Acc]
551            end
552        end,
553        [],
554        Refused
555    ),
556    Accepted1 = lists:foldr(
557        fun(Enc, Acc) ->
558            case Enc of
559                "*" ->
560                    lists:subtract(SupportedEncs, Accepted ++ Refused1) ++ Acc;
561                _ ->
562                    [Enc | Acc]
563            end
564        end,
565        [],
566        Accepted
567    ),
568    Accepted2 = case lists:member(DefaultEnc, Accepted1) of
569        true ->
570            Accepted1;
571        false ->
572            Accepted1 ++ [DefaultEnc]
573    end,
574    [E || E <- Accepted2, lists:member(E, SupportedEncs),
575        not lists:member(E, Refused1)].
576
577make_io(Atom) when is_atom(Atom) ->
578    atom_to_list(Atom);
579make_io(Integer) when is_integer(Integer) ->
580    integer_to_list(Integer);
581make_io(Io) when is_list(Io); is_binary(Io) ->
582    Io.
583
584%%
585%% Tests
586%%
587-ifdef(TEST).
588-include_lib("eunit/include/eunit.hrl").
589
590make_io_test() ->
591    ?assertEqual(
592       <<"atom">>,
593       iolist_to_binary(make_io(atom))),
594    ?assertEqual(
595       <<"20">>,
596       iolist_to_binary(make_io(20))),
597    ?assertEqual(
598       <<"list">>,
599       iolist_to_binary(make_io("list"))),
600    ?assertEqual(
601       <<"binary">>,
602       iolist_to_binary(make_io(<<"binary">>))),
603    ok.
604
605-record(test_record, {field1=f1, field2=f2}).
606record_to_proplist_test() ->
607    ?assertEqual(
608       [{'__record', test_record},
609        {field1, f1},
610        {field2, f2}],
611       record_to_proplist(#test_record{}, record_info(fields, test_record))),
612    ?assertEqual(
613       [{'typekey', test_record},
614        {field1, f1},
615        {field2, f2}],
616       record_to_proplist(#test_record{},
617                          record_info(fields, test_record),
618                          typekey)),
619    ok.
620
621shell_quote_test() ->
622    ?assertEqual(
623       "\"foo \\$bar\\\"\\`' baz\"",
624       shell_quote("foo $bar\"`' baz")),
625    ok.
626
627cmd_port_test_spool(Port, Acc) ->
628    receive
629        {Port, eof} ->
630            Acc;
631        {Port, {data, {eol, Data}}} ->
632            cmd_port_test_spool(Port, ["\n", Data | Acc]);
633        {Port, Unknown} ->
634            throw({unknown, Unknown})
635    after 1000 ->
636            throw(timeout)
637    end.
638
639cmd_port_test() ->
640    Port = cmd_port(["echo", "$bling$ `word`!"],
641                    [eof, stream, {line, 4096}]),
642    Res = try lists:append(lists:reverse(cmd_port_test_spool(Port, [])))
643          after catch port_close(Port)
644          end,
645    self() ! {Port, wtf},
646    try cmd_port_test_spool(Port, [])
647    catch throw:{unknown, wtf} -> ok
648    end,
649    try cmd_port_test_spool(Port, [])
650    catch throw:timeout -> ok
651    end,
652    ?assertEqual(
653       "$bling$ `word`!\n",
654       Res).
655
656cmd_test() ->
657    ?assertEqual(
658       "$bling$ `word`!\n",
659       cmd(["echo", "$bling$ `word`!"])),
660    ok.
661
662cmd_string_test() ->
663    ?assertEqual(
664       "\"echo\" \"\\$bling\\$ \\`word\\`!\"",
665       cmd_string(["echo", "$bling$ `word`!"])),
666    ok.
667
668cmd_status_test() ->
669    ?assertEqual(
670       {0, <<"$bling$ `word`!\n">>},
671       cmd_status(["echo", "$bling$ `word`!"])),
672    ok.
673
674
675parse_header_test() ->
676    ?assertEqual(
677       {"multipart/form-data", [{"boundary", "AaB03x"}]},
678       parse_header("multipart/form-data; boundary=AaB03x")),
679    %% This tests (currently) intentionally broken behavior
680    ?assertEqual(
681       {"multipart/form-data",
682        [{"b", ""},
683         {"cgi", "is"},
684         {"broken", "true\"e"}]},
685       parse_header("multipart/form-data;b=;cgi=\"i\\s;broken=true\"e;=z;z")),
686    ok.
687
688guess_mime_test() ->
689    "text/plain" = guess_mime(""),
690    "text/plain" = guess_mime(".text"),
691    "application/zip" = guess_mime(".zip"),
692    "application/zip" = guess_mime("x.zip"),
693    "text/html" = guess_mime("x.html"),
694    "application/xhtml+xml" = guess_mime("x.xhtml"),
695    ok.
696
697path_split_test() ->
698    {"", "foo/bar"} = path_split("/foo/bar"),
699    {"foo", "bar"} = path_split("foo/bar"),
700    {"bar", ""} = path_split("bar"),
701    ok.
702
703urlsplit_test() ->
704    {"", "", "/foo", "", "bar?baz"} = urlsplit("/foo#bar?baz"),
705    {"http", "host:port", "/foo", "", "bar?baz"} =
706        urlsplit("http://host:port/foo#bar?baz"),
707    {"http", "host", "", "", ""} = urlsplit("http://host"),
708    {"", "", "/wiki/Category:Fruit", "", ""} =
709        urlsplit("/wiki/Category:Fruit"),
710    ok.
711
712urlsplit_path_test() ->
713    {"/foo/bar", "", ""} = urlsplit_path("/foo/bar"),
714    {"/foo", "baz", ""} = urlsplit_path("/foo?baz"),
715    {"/foo", "", "bar?baz"} = urlsplit_path("/foo#bar?baz"),
716    {"/foo", "", "bar?baz#wibble"} = urlsplit_path("/foo#bar?baz#wibble"),
717    {"/foo", "bar", "baz"} = urlsplit_path("/foo?bar#baz"),
718    {"/foo", "bar?baz", "baz"} = urlsplit_path("/foo?bar?baz#baz"),
719    ok.
720
721urlunsplit_test() ->
722    "/foo#bar?baz" = urlunsplit({"", "", "/foo", "", "bar?baz"}),
723    "http://host:port/foo#bar?baz" =
724        urlunsplit({"http", "host:port", "/foo", "", "bar?baz"}),
725    ok.
726
727urlunsplit_path_test() ->
728    "/foo/bar" = urlunsplit_path({"/foo/bar", "", ""}),
729    "/foo?baz" = urlunsplit_path({"/foo", "baz", ""}),
730    "/foo#bar?baz" = urlunsplit_path({"/foo", "", "bar?baz"}),
731    "/foo#bar?baz#wibble" = urlunsplit_path({"/foo", "", "bar?baz#wibble"}),
732    "/foo?bar#baz" = urlunsplit_path({"/foo", "bar", "baz"}),
733    "/foo?bar?baz#baz" = urlunsplit_path({"/foo", "bar?baz", "baz"}),
734    ok.
735
736join_test() ->
737    ?assertEqual("foo,bar,baz",
738                  join(["foo", "bar", "baz"], $,)),
739    ?assertEqual("foo,bar,baz",
740                  join(["foo", "bar", "baz"], ",")),
741    ?assertEqual("foo bar",
742                  join([["foo", " bar"]], ",")),
743    ?assertEqual("foo bar,baz",
744                  join([["foo", " bar"], "baz"], ",")),
745    ?assertEqual("foo",
746                  join(["foo"], ",")),
747    ?assertEqual("foobarbaz",
748                  join(["foo", "bar", "baz"], "")),
749    ?assertEqual("foo" ++ [<<>>] ++ "bar" ++ [<<>>] ++ "baz",
750                 join(["foo", "bar", "baz"], <<>>)),
751    ?assertEqual("foobar" ++ [<<"baz">>],
752                 join(["foo", "bar", <<"baz">>], "")),
753    ?assertEqual("",
754                 join([], "any")),
755    ok.
756
757quote_plus_test() ->
758    "foo" = quote_plus(foo),
759    "1" = quote_plus(1),
760    "1.1" = quote_plus(1.1),
761    "foo" = quote_plus("foo"),
762    "foo+bar" = quote_plus("foo bar"),
763    "foo%0A" = quote_plus("foo\n"),
764    "foo%0A" = quote_plus("foo\n"),
765    "foo%3B%26%3D" = quote_plus("foo;&="),
766    "foo%3B%26%3D" = quote_plus(<<"foo;&=">>),
767    ok.
768
769unquote_test() ->
770    ?assertEqual("foo bar",
771                 unquote("foo+bar")),
772    ?assertEqual("foo bar",
773                 unquote("foo%20bar")),
774    ?assertEqual("foo\r\n",
775                 unquote("foo%0D%0A")),
776    ?assertEqual("foo\r\n",
777                 unquote(<<"foo%0D%0A">>)),
778    ok.
779
780urlencode_test() ->
781    "foo=bar&baz=wibble+%0D%0A&z=1" = urlencode([{foo, "bar"},
782                                                 {"baz", "wibble \r\n"},
783                                                 {z, 1}]),
784    ok.
785
786parse_qs_test() ->
787    ?assertEqual(
788       [{"foo", "bar"}, {"baz", "wibble \r\n"}, {"z", "1"}],
789       parse_qs("foo=bar&baz=wibble+%0D%0a&z=1")),
790    ?assertEqual(
791       [{"", "bar"}, {"baz", "wibble \r\n"}, {"z", ""}],
792       parse_qs("=bar&baz=wibble+%0D%0a&z=")),
793    ?assertEqual(
794       [{"foo", "bar"}, {"baz", "wibble \r\n"}, {"z", "1"}],
795       parse_qs(<<"foo=bar&baz=wibble+%0D%0a&z=1">>)),
796    ?assertEqual(
797       [],
798       parse_qs("")),
799    ?assertEqual(
800       [{"foo", ""}, {"bar", ""}, {"baz", ""}],
801       parse_qs("foo;bar&baz")),
802    ok.
803
804partition_test() ->
805    {"foo", "", ""} = partition("foo", "/"),
806    {"foo", "/", "bar"} = partition("foo/bar", "/"),
807    {"foo", "/", ""} = partition("foo/", "/"),
808    {"", "/", "bar"} = partition("/bar", "/"),
809    {"f", "oo/ba", "r"} = partition("foo/bar", "oo/ba"),
810    ok.
811
812safe_relative_path_test() ->
813    "foo" = safe_relative_path("foo"),
814    "foo/" = safe_relative_path("foo/"),
815    "foo" = safe_relative_path("foo/bar/.."),
816    "bar" = safe_relative_path("foo/../bar"),
817    "bar/" = safe_relative_path("foo/../bar/"),
818    "" = safe_relative_path("foo/.."),
819    "" = safe_relative_path("foo/../"),
820    undefined = safe_relative_path("/foo"),
821    undefined = safe_relative_path("../foo"),
822    undefined = safe_relative_path("foo/../.."),
823    undefined = safe_relative_path("foo//"),
824    undefined = safe_relative_path("foo\\bar"),
825    ok.
826
827parse_qvalues_test() ->
828    [] = parse_qvalues(""),
829    [{"identity", 0.0}] = parse_qvalues("identity;q=0"),
830    [{"identity", 0.0}] = parse_qvalues("identity ;q=0"),
831    [{"identity", 0.0}] = parse_qvalues(" identity; q =0 "),
832    [{"identity", 0.0}] = parse_qvalues("identity ; q = 0"),
833    [{"identity", 0.0}] = parse_qvalues("identity ; q= 0.0"),
834    [{"gzip", 1.0}, {"deflate", 1.0}, {"identity", 0.0}] = parse_qvalues(
835        "gzip,deflate,identity;q=0.0"
836    ),
837    [{"deflate", 1.0}, {"gzip", 1.0}, {"identity", 0.0}] = parse_qvalues(
838        "deflate,gzip,identity;q=0.0"
839    ),
840    [{"gzip", 1.0}, {"deflate", 1.0}, {"gzip", 1.0}, {"identity", 0.0}] =
841        parse_qvalues("gzip,deflate,gzip,identity;q=0"),
842    [{"gzip", 1.0}, {"deflate", 1.0}, {"identity", 0.0}] = parse_qvalues(
843        "gzip, deflate , identity; q=0.0"
844    ),
845    [{"gzip", 1.0}, {"deflate", 1.0}, {"identity", 0.0}] = parse_qvalues(
846        "gzip; q=1, deflate;q=1.0, identity;q=0.0"
847    ),
848    [{"gzip", 0.5}, {"deflate", 1.0}, {"identity", 0.0}] = parse_qvalues(
849        "gzip; q=0.5, deflate;q=1.0, identity;q=0"
850    ),
851    [{"gzip", 0.5}, {"deflate", 1.0}, {"identity", 0.0}] = parse_qvalues(
852        "gzip; q=0.5, deflate , identity;q=0.0"
853    ),
854    [{"gzip", 0.5}, {"deflate", 0.8}, {"identity", 0.0}] = parse_qvalues(
855        "gzip; q=0.5, deflate;q=0.8, identity;q=0.0"
856    ),
857    [{"gzip", 0.5}, {"deflate", 1.0}, {"identity", 1.0}] = parse_qvalues(
858        "gzip; q=0.5,deflate,identity"
859    ),
860    [{"gzip", 0.5}, {"deflate", 1.0}, {"identity", 1.0}, {"identity", 1.0}] =
861        parse_qvalues("gzip; q=0.5,deflate,identity, identity "),
862    [{"text/html;level=1", 1.0}, {"text/plain", 0.5}] =
863        parse_qvalues("text/html;level=1, text/plain;q=0.5"),
864    [{"text/html;level=1", 0.3}, {"text/plain", 1.0}] =
865        parse_qvalues("text/html;level=1;q=0.3, text/plain"),
866    [{"text/html;level=1", 0.3}, {"text/plain", 1.0}] =
867        parse_qvalues("text/html; level = 1; q = 0.3, text/plain"),
868    [{"text/html;level=1", 0.3}, {"text/plain", 1.0}] =
869        parse_qvalues("text/html;q=0.3;level=1, text/plain"),
870    invalid_qvalue_string = parse_qvalues("gzip; q=1.1, deflate"),
871    invalid_qvalue_string = parse_qvalues("gzip; q=0.5, deflate;q=2"),
872    invalid_qvalue_string = parse_qvalues("gzip, deflate;q=AB"),
873    invalid_qvalue_string = parse_qvalues("gzip; q=2.1, deflate"),
874    invalid_qvalue_string = parse_qvalues("gzip; q=0.1234, deflate"),
875    invalid_qvalue_string = parse_qvalues("text/html;level=1;q=0.3, text/html;level"),
876    ok.
877
878pick_accepted_encodings_test() ->
879    ["identity"] = pick_accepted_encodings(
880        [],
881        ["gzip", "identity"],
882        "identity"
883    ),
884    ["gzip", "identity"] = pick_accepted_encodings(
885        [{"gzip", 1.0}],
886        ["gzip", "identity"],
887        "identity"
888    ),
889    ["identity"] = pick_accepted_encodings(
890        [{"gzip", 0.0}],
891        ["gzip", "identity"],
892        "identity"
893    ),
894    ["gzip", "identity"] = pick_accepted_encodings(
895        [{"gzip", 1.0}, {"deflate", 1.0}],
896        ["gzip", "identity"],
897        "identity"
898    ),
899    ["gzip", "identity"] = pick_accepted_encodings(
900        [{"gzip", 0.5}, {"deflate", 1.0}],
901        ["gzip", "identity"],
902        "identity"
903    ),
904    ["identity"] = pick_accepted_encodings(
905        [{"gzip", 0.0}, {"deflate", 0.0}],
906        ["gzip", "identity"],
907        "identity"
908    ),
909    ["gzip"] = pick_accepted_encodings(
910        [{"gzip", 1.0}, {"deflate", 1.0}, {"identity", 0.0}],
911        ["gzip", "identity"],
912        "identity"
913    ),
914    ["gzip", "deflate", "identity"] = pick_accepted_encodings(
915        [{"gzip", 1.0}, {"deflate", 1.0}],
916        ["gzip", "deflate", "identity"],
917        "identity"
918    ),
919    ["gzip", "deflate"] = pick_accepted_encodings(
920        [{"gzip", 1.0}, {"deflate", 1.0}, {"identity", 0.0}],
921        ["gzip", "deflate", "identity"],
922        "identity"
923    ),
924    ["deflate", "gzip", "identity"] = pick_accepted_encodings(
925        [{"gzip", 0.2}, {"deflate", 1.0}],
926        ["gzip", "deflate", "identity"],
927        "identity"
928    ),
929    ["deflate", "deflate", "gzip", "identity"] = pick_accepted_encodings(
930        [{"gzip", 0.2}, {"deflate", 1.0}, {"deflate", 1.0}],
931        ["gzip", "deflate", "identity"],
932        "identity"
933    ),
934    ["deflate", "gzip", "gzip", "identity"] = pick_accepted_encodings(
935        [{"gzip", 0.2}, {"deflate", 1.0}, {"gzip", 1.0}],
936        ["gzip", "deflate", "identity"],
937        "identity"
938    ),
939    ["gzip", "deflate", "gzip", "identity"] = pick_accepted_encodings(
940        [{"gzip", 0.2}, {"deflate", 0.9}, {"gzip", 1.0}],
941        ["gzip", "deflate", "identity"],
942        "identity"
943    ),
944    [] = pick_accepted_encodings(
945        [{"*", 0.0}],
946        ["gzip", "deflate", "identity"],
947        "identity"
948    ),
949    ["gzip", "deflate", "identity"] = pick_accepted_encodings(
950        [{"*", 1.0}],
951        ["gzip", "deflate", "identity"],
952        "identity"
953    ),
954    ["gzip", "deflate", "identity"] = pick_accepted_encodings(
955        [{"*", 0.6}],
956        ["gzip", "deflate", "identity"],
957        "identity"
958    ),
959    ["gzip"] = pick_accepted_encodings(
960        [{"gzip", 1.0}, {"*", 0.0}],
961        ["gzip", "deflate", "identity"],
962        "identity"
963    ),
964    ["gzip", "deflate"] = pick_accepted_encodings(
965        [{"gzip", 1.0}, {"deflate", 0.6}, {"*", 0.0}],
966        ["gzip", "deflate", "identity"],
967        "identity"
968    ),
969    ["deflate", "gzip"] = pick_accepted_encodings(
970        [{"gzip", 0.5}, {"deflate", 1.0}, {"*", 0.0}],
971        ["gzip", "deflate", "identity"],
972        "identity"
973    ),
974    ["gzip", "identity"] = pick_accepted_encodings(
975        [{"deflate", 0.0}, {"*", 1.0}],
976        ["gzip", "deflate", "identity"],
977        "identity"
978    ),
979    ["gzip", "identity"] = pick_accepted_encodings(
980        [{"*", 1.0}, {"deflate", 0.0}],
981        ["gzip", "deflate", "identity"],
982        "identity"
983    ),
984    ok.
985
986-endif.
987