mirror of
https://github.com/processone/ejabberd.git
synced 2024-10-31 15:21:38 +01:00
Unify GET and POST handling code
Code for both it almost identical, extract all differences to separate function extract_path_query.
This commit is contained in:
parent
2d05ddd466
commit
e58e6a09dd
@ -389,20 +389,13 @@ process(Handlers, Request) ->
|
|||||||
false -> process(HandlersLeft, Request)
|
false -> process(HandlersLeft, Request)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
process_request(#state{request_method = Method,
|
extract_path_query(#state{request_method = Method,
|
||||||
request_path = {abs_path, Path}, request_auth = Auth,
|
request_path = {abs_path, Path}})
|
||||||
request_lang = Lang, request_handlers = RequestHandlers,
|
|
||||||
request_host = Host, request_port = Port,
|
|
||||||
request_tp = TP, request_headers = RequestHeaders,
|
|
||||||
sockmod = SockMod,
|
|
||||||
websocket_handlers = WebSocketHandlers,
|
|
||||||
socket = Socket} =
|
|
||||||
State)
|
|
||||||
when Method =:= 'GET' orelse
|
when Method =:= 'GET' orelse
|
||||||
Method =:= 'HEAD' orelse
|
Method =:= 'HEAD' orelse
|
||||||
Method =:= 'DELETE' orelse Method =:= 'OPTIONS' ->
|
Method =:= 'DELETE' orelse Method =:= 'OPTIONS' ->
|
||||||
case catch url_decode_q_split(Path) of
|
case catch url_decode_q_split(Path) of
|
||||||
{'EXIT', _} -> make_bad_request(State);
|
{'EXIT', _} -> false;
|
||||||
{NPath, Query} ->
|
{NPath, Query} ->
|
||||||
LPath = [path_decode(NPE)
|
LPath = [path_decode(NPE)
|
||||||
|| NPE <- str:tokens(NPath, <<"/">>)],
|
|| NPE <- str:tokens(NPath, <<"/">>)],
|
||||||
@ -410,69 +403,15 @@ process_request(#state{request_method = Method,
|
|||||||
{'EXIT', _Reason} -> [];
|
{'EXIT', _Reason} -> [];
|
||||||
LQ -> LQ
|
LQ -> LQ
|
||||||
end,
|
end,
|
||||||
{ok, IPHere} = case SockMod of
|
{LPath, LQuery, <<"">>}
|
||||||
gen_tcp -> inet:peername(Socket);
|
|
||||||
_ -> SockMod:peername(Socket)
|
|
||||||
end,
|
|
||||||
XFF = proplists:get_value('X-Forwarded-For',
|
|
||||||
RequestHeaders, []),
|
|
||||||
IP = analyze_ip_xff(IPHere, XFF, Host),
|
|
||||||
case ejabberd_websocket:check(Path, RequestHeaders) of
|
|
||||||
{true, VSN} ->
|
|
||||||
{_, Origin} = case
|
|
||||||
lists:keyfind(<<"Sec-Websocket-Origin">>, 1,
|
|
||||||
RequestHeaders)
|
|
||||||
of
|
|
||||||
false ->
|
|
||||||
lists:keyfind(<<"Origin">>, 1,
|
|
||||||
RequestHeaders);
|
|
||||||
Value -> Value
|
|
||||||
end,
|
|
||||||
Ws = #ws{socket = Socket, sockmod = SockMod,
|
|
||||||
ws_autoexit = false, ip = IP, path = LPath, q = LQuery,
|
|
||||||
vsn = VSN, host = Host, port = Port, origin = Origin,
|
|
||||||
headers = RequestHeaders},
|
|
||||||
process(WebSocketHandlers, Ws),
|
|
||||||
none;
|
|
||||||
false ->
|
|
||||||
Request = #request{method = Method, path = LPath,
|
|
||||||
q = LQuery, auth = Auth, lang = Lang,
|
|
||||||
host = Host, port = Port, tp = TP,
|
|
||||||
headers = RequestHeaders, ip = IP},
|
|
||||||
case process(RequestHandlers, Request) of
|
|
||||||
El when is_record(El, xmlel) ->
|
|
||||||
make_xhtml_output(State, 200, [], El);
|
|
||||||
{Status, Headers, El}
|
|
||||||
when is_record(El, xmlel) ->
|
|
||||||
make_xhtml_output(State, Status, Headers, El);
|
|
||||||
Output when is_binary(Output) or is_list(Output) ->
|
|
||||||
make_text_output(State, 200, [], Output);
|
|
||||||
{Status, Headers, Output}
|
|
||||||
when is_binary(Output) or is_list(Output) ->
|
|
||||||
make_text_output(State, Status, Headers, Output);
|
|
||||||
{Status, Reason, Headers, Output}
|
|
||||||
when is_binary(Output) or is_list(Output) ->
|
|
||||||
make_text_output(State, Status, Reason, Headers, Output)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end;
|
end;
|
||||||
process_request(#state{request_method = Method,
|
extract_path_query(#state{request_method = Method,
|
||||||
request_path = {abs_path, Path}, request_auth = Auth,
|
request_path = {abs_path, Path},
|
||||||
request_content_length = Len, request_lang = Lang,
|
request_content_length = Len,
|
||||||
sockmod = SockMod, socket = Socket, request_host = Host,
|
sockmod = SockMod,
|
||||||
request_port = Port, request_tp = TP,
|
socket = Socket} = State)
|
||||||
request_headers = RequestHeaders,
|
|
||||||
request_handlers = RequestHandlers} =
|
|
||||||
State)
|
|
||||||
when (Method =:= 'POST' orelse Method =:= 'PUT') andalso
|
when (Method =:= 'POST' orelse Method =:= 'PUT') andalso
|
||||||
is_integer(Len) ->
|
is_integer(Len) ->
|
||||||
{ok, IPHere} = case SockMod of
|
|
||||||
gen_tcp -> inet:peername(Socket);
|
|
||||||
_ -> SockMod:peername(Socket)
|
|
||||||
end,
|
|
||||||
XFF = proplists:get_value('X-Forwarded-For',
|
|
||||||
RequestHeaders, []),
|
|
||||||
IP = analyze_ip_xff(IPHere, XFF, Host),
|
|
||||||
case SockMod of
|
case SockMod of
|
||||||
gen_tcp -> inet:setopts(Socket, [{packet, 0}]);
|
gen_tcp -> inet:setopts(Socket, [{packet, 0}]);
|
||||||
_ -> ok
|
_ -> ok
|
||||||
@ -480,7 +419,7 @@ process_request(#state{request_method = Method,
|
|||||||
Data = recv_data(State, Len),
|
Data = recv_data(State, Len),
|
||||||
?DEBUG("client data: ~p~n", [Data]),
|
?DEBUG("client data: ~p~n", [Data]),
|
||||||
case catch url_decode_q_split(Path) of
|
case catch url_decode_q_split(Path) of
|
||||||
{'EXIT', _} -> make_bad_request(State);
|
{'EXIT', _} -> false;
|
||||||
{NPath, _Query} ->
|
{NPath, _Query} ->
|
||||||
LPath = [path_decode(NPE)
|
LPath = [path_decode(NPE)
|
||||||
|| NPE <- str:tokens(NPath, <<"/">>)],
|
|| NPE <- str:tokens(NPath, <<"/">>)],
|
||||||
@ -488,25 +427,83 @@ process_request(#state{request_method = Method,
|
|||||||
{'EXIT', _Reason} -> [];
|
{'EXIT', _Reason} -> [];
|
||||||
LQ -> LQ
|
LQ -> LQ
|
||||||
end,
|
end,
|
||||||
Request = #request{method = Method, path = LPath,
|
{LPath, LQuery, Data}
|
||||||
q = LQuery, auth = Auth, data = Data, lang = Lang,
|
end;
|
||||||
host = Host, port = Port, tp = TP,
|
extract_path_query(_State) ->
|
||||||
headers = RequestHeaders, ip = IP},
|
false.
|
||||||
case process(RequestHandlers, Request) of
|
|
||||||
El when is_record(El, xmlel) ->
|
process_request(#state{request_method = Method,
|
||||||
make_xhtml_output(State, 200, [], El);
|
request_path = {abs_path, Path},
|
||||||
{Status, Headers, El}
|
request_auth = Auth,
|
||||||
when is_record(El, xmlel) ->
|
request_lang = Lang,
|
||||||
make_xhtml_output(State, Status, Headers, El);
|
sockmod = SockMod,
|
||||||
Output when is_binary(Output) or is_list(Output) ->
|
socket = Socket,
|
||||||
make_text_output(State, 200, [], Output);
|
request_host = Host,
|
||||||
{Status, Headers, Output}
|
request_port = Port,
|
||||||
when is_binary(Output) or is_list(Output) ->
|
request_tp = TP,
|
||||||
make_text_output(State, Status, Headers, Output);
|
websocket_handlers = WebSocketHandlers,
|
||||||
{Status, Reason, Headers, Output}
|
request_headers = RequestHeaders,
|
||||||
when is_binary(Output) or is_list(Output) ->
|
request_handlers = RequestHandlers} = State) ->
|
||||||
make_text_output(State, Status, Reason, Headers, Output)
|
case extract_path_query(State) of
|
||||||
end
|
false ->
|
||||||
|
make_bad_request(State);
|
||||||
|
{LPath, LQuery, Data} ->
|
||||||
|
{ok, IPHere} =
|
||||||
|
case SockMod of
|
||||||
|
gen_tcp ->
|
||||||
|
inet:peername(Socket);
|
||||||
|
_ ->
|
||||||
|
SockMod:peername(Socket)
|
||||||
|
end,
|
||||||
|
XFF = proplists:get_value('X-Forwarded-For', RequestHeaders, []),
|
||||||
|
IP = analyze_ip_xff(IPHere, XFF, Host),
|
||||||
|
case Method=:='GET' andalso ejabberd_websocket:check(Path, RequestHeaders) of
|
||||||
|
{true, VSN} ->
|
||||||
|
{_, Origin} = case lists:keyfind(<<"Sec-Websocket-Origin">>, 1, RequestHeaders) of
|
||||||
|
false -> lists:keyfind(<<"Origin">>, 1, RequestHeaders);
|
||||||
|
Value -> Value
|
||||||
|
end,
|
||||||
|
Ws = #ws{socket = Socket,
|
||||||
|
sockmod = SockMod,
|
||||||
|
ws_autoexit = false,
|
||||||
|
ip = IP,
|
||||||
|
path = LPath,
|
||||||
|
q = LQuery,
|
||||||
|
vsn = VSN,
|
||||||
|
host = Host,
|
||||||
|
port = Port,
|
||||||
|
origin = Origin,
|
||||||
|
headers = RequestHeaders
|
||||||
|
},
|
||||||
|
process(WebSocketHandlers, Ws);
|
||||||
|
false ->
|
||||||
|
Request = #request{method = Method,
|
||||||
|
path = LPath,
|
||||||
|
q = LQuery,
|
||||||
|
auth = Auth,
|
||||||
|
data = Data,
|
||||||
|
lang = Lang,
|
||||||
|
host = Host,
|
||||||
|
port = Port,
|
||||||
|
tp = TP,
|
||||||
|
headers = RequestHeaders,
|
||||||
|
ip = IP},
|
||||||
|
case process(RequestHandlers, Request) of
|
||||||
|
El when is_record(El, xmlel) ->
|
||||||
|
make_xhtml_output(State, 200, [], El);
|
||||||
|
{Status, Headers, El}
|
||||||
|
when is_record(El, xmlel) ->
|
||||||
|
make_xhtml_output(State, Status, Headers, El);
|
||||||
|
Output when is_binary(Output) or is_list(Output) ->
|
||||||
|
make_text_output(State, 200, [], Output);
|
||||||
|
{Status, Headers, Output}
|
||||||
|
when is_binary(Output) or is_list(Output) ->
|
||||||
|
make_text_output(State, Status, Headers, Output);
|
||||||
|
{Status, Reason, Headers, Output}
|
||||||
|
when is_binary(Output) or is_list(Output) ->
|
||||||
|
make_text_output(State, Status, Reason, Headers, Output)
|
||||||
|
end
|
||||||
|
end
|
||||||
end;
|
end;
|
||||||
process_request(State) -> make_bad_request(State).
|
process_request(State) -> make_bad_request(State).
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user