PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_oauth/deps/erlang-oauth/src/oauth_uri.erl

https://code.google.com/p/zotonic/
Erlang | 91 lines | 71 code | 20 blank | 0 comment | 6 complexity | f6a832c0b0c745848e4b695da7bd26a9 MD5 | raw file
 1-module(oauth_uri).
 2
 3-export([normalize/1, calate/2, encode/1, decode/1]).
 4-export([params_from_string/1, params_to_string/1,
 5  params_from_header_string/1, params_to_header_string/1]).
 6
 7-import(lists, [concat/1]).
 8
 9-define(is_uppercase_alpha(C), C >= $A, C =< $Z).
10-define(is_lowercase_alpha(C), C >= $a, C =< $z).
11-define(is_alpha(C), ?is_uppercase_alpha(C); ?is_lowercase_alpha(C)).
12-define(is_digit(C), C >= $0, C =< $9).
13-define(is_alphanumeric(C), ?is_alpha(C); ?is_digit(C)).
14-define(is_unreserved(C), ?is_alphanumeric(C); C =:= $-; C =:= $_; C =:= $.; C =:= $~).
15-define(is_hex(C), ?is_digit(C); C >= $A, C =< $F).
16
17
18normalize(URI) ->
19  case http_uri:parse(URI) of
20    {Scheme, UserInfo, Host, Port, Path, _Query} ->
21      normalize(Scheme, UserInfo, string:to_lower(Host), Port, [Path]);
22    Else ->
23      Else
24  end.
25
26normalize(http, UserInfo, Host, 80, Acc) ->
27  normalize(http, UserInfo, [Host|Acc]);
28normalize(https, UserInfo, Host, 443, Acc) ->
29  normalize(https, UserInfo, [Host|Acc]);
30normalize(Scheme, UserInfo, Host, Port, Acc) ->
31  normalize(Scheme, UserInfo, [Host, ":", Port|Acc]).
32
33normalize(Scheme, [], Acc) ->
34  concat([Scheme, "://"|Acc]);
35normalize(Scheme, UserInfo, Acc) ->
36  concat([Scheme, "://", UserInfo, "@"|Acc]).
37
38params_to_header_string(Params) ->
39  intercalate(", ", [concat([encode(K), "=\"", encode(V), "\""]) || {K, V} <- Params]).
40
41params_from_header_string(String) ->
42  [param_from_header_string(Param) || Param <- re:split(String, ",\\s*", [{return, list}])].
43
44param_from_header_string(Param) ->
45  [Key, QuotedValue] = string:tokens(Param, "="),
46  Value = string:substr(QuotedValue, 2, length(QuotedValue) - 2),
47  {decode(Key), decode(Value)}.
48
49params_from_string(Params) ->
50  [param_from_string(Param) || Param <- string:tokens(Params, "&")].
51
52param_from_string(Param) ->
53  list_to_tuple([decode(Value) || Value <- string:tokens(Param, "=")]).
54
55params_to_string(Params) ->
56  intercalate("&", [calate("=", [K, V]) || {K, V} <- Params]).
57
58calate(Sep, Xs) ->
59  intercalate(Sep, [encode(X) || X <- Xs]).
60
61intercalate(Sep, Xs) ->
62  concat(intersperse(Sep, Xs)).
63
64intersperse(_, []) -> [];
65intersperse(_, [X]) -> [X];
66intersperse(Sep, [X|Xs]) ->
67  [X, Sep|intersperse(Sep, Xs)].
68
69decode(Chars) ->
70  decode(Chars, []).
71
72decode([], Decoded) ->
73  lists:reverse(Decoded);
74decode([$%,A,B|Etc], Decoded) when ?is_hex(A), ?is_hex(B) ->
75  decode(Etc, [erlang:list_to_integer([A,B], 16)|Decoded]);
76decode([C|Etc], Decoded) ->
77  decode(Etc, [C|Decoded]).
78
79encode(Chars) ->
80  encode(Chars, []).
81
82encode([], Encoded) ->
83  lists:flatten(lists:reverse(Encoded));
84encode([C|Etc], Encoded) when ?is_unreserved(C) ->
85  encode(Etc, [C|Encoded]);
86encode([C|Etc], Encoded) when C >= 16 ->
87  Value = [ $% | erlang:integer_to_list(C, 16) ],
88  encode(Etc, [Value|Encoded]);
89encode([C|Etc], Encoded) when C < 16 ->
90  Value = [ $%, $0 | erlang:integer_to_list(C, 16) ],
91  encode(Etc, [Value|Encoded]).