/
simple_bridge_request_wrapper.erl
137 lines (109 loc) · 4.23 KB
/
simple_bridge_request_wrapper.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
% Simple Bridge
% Copyright (c) 2008-2010 Rusty Klophaus
% See MIT-LICENSE for licensing information.
-module (simple_bridge_request_wrapper, [Mod, Req, IsMultiPart, PostParams, PostFiles, Error]).
-compile(export_all).
-include_lib ("simple_bridge.hrl").
set_multipart(PostParams1, PostFiles1) ->
simple_bridge_request_wrapper:new(Mod, Req, true, PostParams1, PostFiles1, Error).
set_error(Error1) ->
simple_bridge_request_wrapper:new(Mod, Req, true, PostParams, PostFiles, Error1).
protocol() -> Mod:protocol(Req).
request_method() -> Mod:request_method(Req).
path() -> Mod:path(Req).
uri() -> Mod:uri(Req).
peer_ip() -> Mod:peer_ip(Req).
peer_port() -> Mod:peer_port(Req).
headers() -> Mod:headers(Req).
header(Header) ->
case erlang:function_exported(Mod, header, 2) of
true ->
Mod:header(Header, Req);
false ->
Headers = Mod:headers(Req),
proplists:get_value(Header, Headers)
end.
cookies() -> Mod:cookies(Req).
cookie(Cookie) ->
case erlang:function_exported(Mod, cookie, 2) of
true ->
Mod:cookie(Cookie, Req);
false ->
Cookies = Mod:cookies(Req),
proplists:get_value(Cookie, Cookies)
end.
query_params() -> Mod:query_params(Req).
query_param(Param) ->
query_param(Param, undefined).
query_param(Param, DefaultValue) ->
proplists:get_value(Param, query_params(), DefaultValue).
post_params() ->
case {request_method(), IsMultiPart} of
{'POST', true} -> PostParams;
{'POST', false} -> Mod:post_params(Req);
{'PUT', false} -> Mod:post_params(Req);
_ -> []
end.
post_param(Param) ->
post_param(Param, undefined).
post_param(Param, DefaultValue) ->
proplists:get_value(Param, post_params(), DefaultValue).
param(Param) ->
param(Param, undefined).
param(Param, DefaultValue) ->
post_param(Param, query_param(Param, DefaultValue)).
post_files() -> PostFiles.
request_body() -> Mod:request_body(Req).
socket() ->
case erlang:function_exported(Mod, socket, 1) of
true -> Mod:socket(Req);
false -> throw({not_supported, Mod, socket})
end.
get_peername() -> inet:peername(socket()).
recv_from_socket(Length, Timeout) ->
case erlang:function_exported(Mod, recv_from_socket, 3) of
true -> Mod:recv_from_socket(Length, Timeout, Req);
false -> throw({not_supported, Mod, recv_from_socket})
end.
error() -> Error.
deep_post_params() ->
Params = post_params(),
parse_deep_post_params(Params, []).
deep_post_param(Path) ->
find_deep_post_param(Path, deep_post_params()).
find_deep_post_param([], Params) ->
Params;
find_deep_post_param([Index|Rest], Params) when is_integer(Index) ->
find_deep_post_param(Rest, lists:nth(Index, Params));
find_deep_post_param([Index|Rest], Params) when is_list(Index) ->
find_deep_post_param(Rest, proplists:get_value(Index, Params)).
parse_deep_post_params([], Acc) ->
Acc;
parse_deep_post_params([{Key, Value}|Rest], Acc) ->
case re:run(Key, "^(\\w+)(?:\\[([\\w\\[\\]]+)\\])?$", [{capture, all_but_first, list}]) of
{match, [Key]} ->
parse_deep_post_params(Rest, [{Key, Value}|Acc]);
{match, [KeyName, Path]} ->
PathList = re:split(Path, "\\]\\[", [{return, list}]),
parse_deep_post_params(Rest, insert_into(Acc, [KeyName|PathList], Value))
end.
insert_into(_List, [], Value) ->
Value;
insert_into(undefined, PathList, Value) ->
insert_into([], PathList, Value);
insert_into(N, PathList, Value) when is_integer(N) ->
insert_into([], PathList, Value);
insert_into(List, [ThisKey|Rest], Value) ->
case catch list_to_integer(ThisKey) of
{'EXIT', _} ->
ExistingVal = proplists:get_value(ThisKey, List),
[{ThisKey, insert_into(ExistingVal, Rest, Value)}|
proplists:delete(ThisKey, List)];
N when N < erlang:length(List) ->
ExistingVal = lists:nth(N+1, List),
lists:sublist(List, N) ++ [insert_into(ExistingVal, Rest, Value)|
lists:nthtail(N+1, List)];
N when N >= erlang:length(List) ->
List ++ lists:reverse([insert_into(undefined, Rest, Value)|
lists:seq(0, N - erlang:length(List) - 1)])
end.