Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

DRY: moved get_peer functinoality

  • Loading branch information...
commit 7607709606024a681e4ce38ec16ef796e165f3a5 1 parent a09f63c
@ostinelli authored
View
8 include/misultin.hrl
@@ -162,8 +162,8 @@
-record(req, {
socket = undefined :: undefined | socket(), % the socket handling the request
socket_mode = http :: socketmode(),
- peer_addr = undefined :: undefined | inet:ip_address(), % peer IP | undefined
- peer_port = undefined :: undefined | non_neg_integer(), % peer port | undefined
+ peer_addr = undefined :: undefined | inet:ip_address(), % peer IP | undefined: this is the peer address of the connection, may differ from the client
+ peer_port = undefined :: undefined | non_neg_integer(), % peer port | undefined: this is the peer port of the connection, may differ from the client
peer_cert = undefined :: undefined | term(), % the DER encoded peer certificate that can be decoded with public_key:pkix_decode_cert/2
connection = close :: keep_alive | close,
content_length = undefined :: undefined | non_neg_integer(),
@@ -181,8 +181,8 @@
socket = undefined :: undefined | socket(), % the socket handling the request
socket_mode = http :: socketmode(),
ws_autoexit = true :: boolean(), % shoud the ws process be automatically killed?
- peer_addr = undefined :: undefined | inet:ip_address(), % peer IP | undefined
- peer_port = undefined :: undefined | non_neg_integer(), % peer port | undefined
+ peer_addr = undefined :: undefined | inet:ip_address(), % peer IP | undefined: this is the peer address of the connection, may differ from the client
+ peer_port = undefined :: undefined | non_neg_integer(), % peer port | undefined: this is the peer port of the connection, may differ from the client
peer_cert = undefined :: undefined | term(), % the DER encoded peer certificate that can be decoded with public_key:pkix_decode_cert/2
vsn = undefined :: undefined | websocket_version(),
origin = undefined :: undefined | list(), % the originator
View
21 src/misultin_req.erl
@@ -80,25 +80,8 @@ get(ReqInfo, {misultin_req, SocketPid, _TableDateRef}) when
get(peer_addr, {misultin_req, SocketPid, _TableDateRef}) ->
Headers = get(headers, {misultin_req, SocketPid, _TableDateRef}),
- Host = case misultin_utility:header_get_value('X-Real-Ip', Headers) of
- false ->
- case misultin_utility:header_get_value('X-Forwarded-For', Headers) of
- false -> undefined;
- Hosts0 -> string:strip(lists:nth(1, string:tokens(Hosts0, ",")))
- end;
- Host0 -> Host0
- end,
- case Host of
- undefined ->
- misultin_http:get_reqinfo(SocketPid, peer_addr);
- _ ->
- case inet_parse:address(Host) of
- {error, _Reason} ->
- misultin_http:get_reqinfo(SocketPid, peer_addr);
- {ok, IpTuple} ->
- IpTuple
- end
- end;
+ ConnectionPeerAddr = misultin_http:get_reqinfo(SocketPid, peer_addr),
+ misultin_utility:get_peer(Headers, ConnectionPeerAddr);
get(uri_unquoted, ReqT) ->
uri_unquote(get(uri, ReqT)).
View
26 src/misultin_utility.erl
@@ -34,7 +34,7 @@
% API
-export([get_http_status_code/2, get_http_status_message/1, get_content_type/1, get_key_value/2, header_get_value/2]).
-export([call/2, call/3, respond/2]).
--export([parse_qs/1, parse_qs/2, unquote/1, quote_plus/1]).
+-export([parse_qs/1, parse_qs/2, unquote/1, quote_plus/1, get_peer/2]).
-export([convert_ip_to_list/1]).
-export([hexstr/1, get_unix_timestamp/0, get_unix_timestamp/1]).
@@ -514,6 +514,30 @@ hex(N) when N >= 10, N < 16 -> $a + (N-10).
get_unix_timestamp() -> get_unix_timestamp(erlang:now()).
get_unix_timestamp({MegaSecs, Secs, _MicroSecs}) -> MegaSecs * 1000000 + Secs.
+
+% extract the peer address from the headers (in case of proxy specifying it in the address), or default to connection peer address
+-spec get_peer(Headers::http_headers(), ConnectionPeerAddr::undefined | inet:ip_address()) -> {error, term()} | {ok, inet:ip_address()}.
+get_peer(Headers, ConnectionPeerAddr) ->
+ Host = case header_get_value('X-Real-Ip', Headers) of
+ false ->
+ case header_get_value('X-Forwarded-For', Headers) of
+ false -> undefined;
+ Hosts0 -> string:strip(lists:nth(1, string:tokens(Hosts0, ",")))
+ end;
+ Host0 -> Host0
+ end,
+ case Host of
+ undefined ->
+ ConnectionPeerAddr;
+ _ ->
+ case inet_parse:address(Host) of
+ {error, _Reason} ->
+ ConnectionPeerAddr;
+ {ok, IpTuple} ->
+ IpTuple
+ end
+ end.
+
% ============================ /\ API ======================================================================
View
21 src/misultin_ws.erl
@@ -62,25 +62,8 @@ get(WsInfo, {misultin_ws, SocketPid}) when
misultin_websocket:get_wsinfo(SocketPid, WsInfo);
get(peer_addr, {misultin_ws, SocketPid}) ->
Headers = get(headers, {misultin_ws, SocketPid}),
- Host = case misultin_utility:header_get_value('X-Real-Ip', Headers) of
- false ->
- case misultin_utility:header_get_value('X-Forwarded-For', Headers) of
- false -> undefined;
- Hosts0 -> string:strip(lists:nth(1, string:tokens(Hosts0, ",")))
- end;
- Host0 -> Host0
- end,
- case Host of
- undefined ->
- misultin_websocket:get_wsinfo(SocketPid, peer_addr);
- _ ->
- case inet_parse:address(Host) of
- {error, _Reason} ->
- misultin_websocket:get_wsinfo(SocketPid, peer_addr);
- {ok, IpTuple} ->
- IpTuple
- end
- end.
+ ConnectionPeerAddr = misultin_websocket:get_wsinfo(SocketPid, peer_addr),
+ misultin_utility:get_peer(Headers, ConnectionPeerAddr).
% ---------------------------- \/ Cookies ------------------------------------------------------------------
Please sign in to comment.
Something went wrong with that request. Please try again.