aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-12-29 18:06:02 +0100
committerLoïc Hoguin <[email protected]>2014-12-29 18:06:02 +0100
commit163356129ebc08981e80e900de845afd964ef3bc (patch)
treef9e94b823e00aab788e9933fe44bd60c2dfdaa16
parent1c732125bfd12fb3a25997f93cdf9e418666bddb (diff)
downloadcowlib-163356129ebc08981e80e900de845afd964ef3bc.tar.gz
cowlib-163356129ebc08981e80e900de845afd964ef3bc.tar.bz2
cowlib-163356129ebc08981e80e900de845afd964ef3bc.zip
Add cow_http_hd:parse_x_forwarded_for/1
From thin air.
-rw-r--r--src/cow_http_hd.erl27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/cow_http_hd.erl b/src/cow_http_hd.erl
index bd0a258..66cb746 100644
--- a/src/cow_http_hd.erl
+++ b/src/cow_http_hd.erl
@@ -55,6 +55,7 @@
-export([parse_transfer_encoding/1]).
-export([parse_upgrade/1]).
-export([parse_vary/1]).
+-export([parse_x_forwarded_for/1]).
-type etag() :: {weak | strong, binary()}.
-export_type([etag/0]).
@@ -2936,6 +2937,32 @@ parse_vary_error_test_() ->
[{V, fun() -> {'EXIT', _} = (catch parse_vary(V)) end} || V <- Tests].
-endif.
+%% @doc Parse the X-Forwarded-For header.
+%%
+%% This header has no specification but *looks like* it is
+%% a list of tokens.
+%%
+%% This header is deprecated in favor of the Forwarded header.
+
+-spec parse_x_forwarded_for(binary()) -> [binary()].
+parse_x_forwarded_for(XForwardedFor) ->
+ nonempty(token_list(XForwardedFor, [])).
+
+-ifdef(TEST).
+parse_x_forwarded_for_test_() ->
+ Tests = [
+ {<<"client, proxy1, proxy2">>, [<<"client">>, <<"proxy1">>, <<"proxy2">>]},
+ {<<"128.138.243.150, unknown, 192.52.106.30">>, [<<"128.138.243.150">>, <<"unknown">>, <<"192.52.106.30">>]}
+ ],
+ [{V, fun() -> R = parse_x_forwarded_for(V) end} || {V, R} <- Tests].
+
+parse_x_forwarded_for_error_test_() ->
+ Tests = [
+ <<>>
+ ],
+ [{V, fun() -> {'EXIT', _} = (catch parse_x_forwarded_for(V)) end} || V <- Tests].
+-endif.
+
%% Internal.
%% Only return if the list is not empty.