Skip to content
Browse files
Remove cowboy_req:peer_addr/1
This kind of function is highly dependent on the proxy used,
therefore parsing was added for x-forwarded-for instead and we
just let users write the function that works for them. The code
can be easily extracted if anyone was using the function.
  • Loading branch information
essen committed Apr 11, 2013
1 parent 67beb4d commit 6256429dc97d69cbb19076acfea8e1fc3efb1286
Showing with 0 additions and 25 deletions.
  1. +0 −1 guide/
  2. +0 −24 src/cowboy_req.erl
@@ -33,7 +33,6 @@ The following access functions are defined in `cowboy_req`:
* `method/1`: the request method (`<<"GET">>`, `<<"POST">>`...)
* `version/1`: the HTTP version (`{1,0}` or `{1,1}`)
* `peer/1`: the peer address and port number
* `peer_addr/1`: the peer address guessed using the request headers
* `host/1`: the hostname requested
* `host_info/1`: the result of the `[...]` match on the host
* `port/1`: the port number used for the connection
@@ -46,7 +46,6 @@
@@ -230,29 +229,6 @@ version(Req) ->
peer(Req) ->
{Req#http_req.peer, Req}.

%% @doc Returns the peer address calculated from headers.
-spec peer_addr(Req) -> {inet:ip_address(), Req} when Req::req().
peer_addr(Req = #http_req{}) ->
{RealIp, Req1} = header(<<"x-real-ip">>, Req),
{ForwardedForRaw, Req2} = header(<<"x-forwarded-for">>, Req1),
{{PeerIp, _PeerPort}, Req3} = peer(Req2),
ForwardedFor = case ForwardedForRaw of
undefined ->
ForwardedForRaw ->
case re:run(ForwardedForRaw, "^(?<first_ip>[^\\,]+)",
[{capture, [first_ip], binary}]) of
{match, [FirstIp]} -> FirstIp;
_Any -> undefined
{ok, PeerAddr} = if
is_binary(RealIp) -> inet_parse:address(binary_to_list(RealIp));
is_binary(ForwardedFor) -> inet_parse:address(binary_to_list(ForwardedFor));
true -> {ok, PeerIp}
{PeerAddr, Req3}.

%% @doc Return the host binary string.
-spec host(Req) -> {binary(), Req} when Req::req().
host(Req) ->

1 comment on commit 6256429


This comment has been minimized.

Copy link

@bfrog bfrog commented on 6256429 Apr 16, 2013

I was definitely using this function, and its a little disappointing to see it go. The reasoning seems mostly sound though.

Please sign in to comment.