aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_http_req.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/cowboy_http_req.erl')
-rw-r--r--src/cowboy_http_req.erl42
1 files changed, 20 insertions, 22 deletions
diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl
index 4cb735a..b8bb8d2 100644
--- a/src/cowboy_http_req.erl
+++ b/src/cowboy_http_req.erl
@@ -71,30 +71,28 @@ peer(Req=#http_req{socket=Socket, transport=Transport, peer=undefined}) ->
peer(Req) ->
{Req#http_req.peer, Req}.
-
-%% @doc Returns the calculated peer addr in binary form, calculated from headers
+%% @doc Returns the peer address calculated from headers.
-spec peer_addr(#http_req{}) -> {inet:ip_address(), #http_req{}}.
peer_addr(Req = #http_req{}) ->
- {RealIp, Req1} = header(<<"X-Real-Ip">>, Req),
- {ForwardedForRaw, Req2} = header(<<"X-Forwarded-For">>, Req1),
- {{PeerIp, _}, Req3} = peer(Req2),
- ForwardedFor = case ForwardedForRaw of
- undefined ->
- undefined;
- _ ->
- case re:run(ForwardedForRaw, "^(?<first_ip>[^\\,]+)", [{capture,[first_ip],binary}]) of
- {match, [FirstIp]} -> FirstIp;
- _ -> undefined
- end
- end,
-
- {ok, PeerAddr} = if
- is_binary(RealIp) -> inet_parse:address(RealIp);
- is_binary(ForwardedFor) -> inet_parse:address(ForwardedFor);
- true -> {ok, PeerIp}
- end,
- {PeerAddr, Req3}.
-
+ {RealIp, Req1} = header(<<"X-Real-Ip">>, Req),
+ {ForwardedForRaw, Req2} = header(<<"X-Forwarded-For">>, Req1),
+ {{PeerIp, _PeerPort}, Req3} = peer(Req2),
+ ForwardedFor = case ForwardedForRaw of
+ undefined ->
+ undefined;
+ ForwardedForRaw ->
+ case re:run(ForwardedForRaw, "^(?<first_ip>[^\\,]+)",
+ [{capture, [first_ip], binary}]) of
+ {match, [FirstIp]} -> FirstIp;
+ _Any -> undefined
+ end
+ end,
+ {ok, PeerAddr} = if
+ is_binary(RealIp) -> inet_parse:address(RealIp);
+ is_binary(ForwardedFor) -> inet_parse:address(ForwardedFor);
+ true -> {ok, PeerIp}
+ end,
+ {PeerAddr, Req3}.
%% @doc Return the tokens for the hostname requested.
-spec host(#http_req{}) -> {cowboy_dispatcher:tokens(), #http_req{}}.