Skip to content

Commit

Permalink
Add sockname to the info struct
Browse files Browse the repository at this point in the history
  • Loading branch information
majek committed Feb 27, 2012
1 parent 6762620 commit 7193657
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ simple. It has just a couple of methods:
to this connection. You should see:

* peername - ip address and port of the remote host
* sockname - ip address and port of the local endpoint
* path - the path used by the request that started the connection
* headers - a set of headers extracted from the request that
may be handy (don't expect to retrieve Cookie header).
Expand Down
4 changes: 3 additions & 1 deletion src/sockjs_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ default_logger(_Service, Req, _Type) ->

-spec extract_info(req()) -> {info(), req()}.
extract_info(Req) ->
{Peer, Req1} = sockjs_http:peer(Req),
{Peer, Req0} = sockjs_http:peername(Req),
{Sock, Req1} = sockjs_http:sockname(Req0),
{Path, Req2} = sockjs_http:path(Req1),
{Headers, Req3} = lists:foldl(fun (H, {Acc, R0}) ->
case sockjs_http:header(H, R0) of
Expand All @@ -222,5 +223,6 @@ extract_info(Req) ->
['Referer', 'X-Client-Ip', 'X-Forwarded-For',
'X-Cluster-Client-Ip', 'Via', 'X-Real-Ip']),
{[{peername, Peer},
{sockname, Sock},
{path, Path},
{headers, Headers}], Req3}.
18 changes: 15 additions & 3 deletions src/sockjs_http.erl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-module(sockjs_http).

-export([path/1, method/1, body/1, body_qs/1, header/2, jsessionid/1,
callback/1, peer/1]).
callback/1, peername/1, sockname/1]).
-export([reply/4, chunk_start/3, chunk/2, chunk_end/1]).
-export([hook_tcp_close/1, unhook_tcp_close/1, abruptly_kill/1]).
-include("sockjs_internal.hrl").
Expand Down Expand Up @@ -70,11 +70,23 @@ callback({cowboy, Req}) ->
_ -> {binary_to_list(CB), {cowboy, Req1}}
end.

-spec peer(req()) -> {{inet:ip_address(), non_neg_integer()}, req()}.
peer({cowboy, Req}) ->
-spec peername(req()) -> {{inet:ip_address(), non_neg_integer()}, req()}.
peername({cowboy, Req}) ->
{P, Req1} = cowboy_http_req:peer(Req),
{P, {cowboy, Req1}}.

-spec sockname(req()) -> {{inet:ip_address(), non_neg_integer()}, req()}.
sockname({cowboy, Req} = R) ->
{ok, _T, S} = cowboy_http_req:transport(Req),
%% Cowboy has peername(), but doesn't have sockname() equivalent.
{ok, Addr} = case S of
_ when is_port(S) ->
inet:sockname(S);
_ ->
{{0,0,0,0}, 0}
end,
{Addr, R}.

%% --------------------------------------------------------------------------

-spec reply(non_neg_integer(), headers(), iodata(), req()) -> req().
Expand Down

0 comments on commit 7193657

Please sign in to comment.