Skip to content

Commit 10d66e2

Browse files
author
Patrick Sachs
committed
Fixed: Error when proxying from HTTPS->HTTPS and the target server sends a set-cookie header
For now the header is ignored in the response, making the library unable to forward cookies. This is a missing feature that will need to be fixed in the future.
1 parent 0a82022 commit 10d66e2

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/cowboy_reverse_proxy.app.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{application, cowboy_reverse_proxy,
22
[{description, "A reverse proxy for cowboy using the httpc client"},
3-
{vsn, "0.1.0"},
3+
{vsn, "0.3.1"},
44
{registered, []},
55
{applications,
66
[kernel,

src/cowboy_reverse_proxy.erl

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ init(Req0, State) ->
7575
% We got a response from the remote server!
7676
{ok, Resp = {{_RespVersion, RespStatus, RespReason}, _RespHeaders, RespBody}} ->
7777
?LOG_INFO("Proxy response: ~p ~s", [RespStatus, RespReason]),
78-
OkReq1 = cowboy_req:reply(RespStatus, response_headers(Resp, State), RespBody, Req1),
78+
{CowboyRespHeaders, _CowboyCookies} = response_headers(Resp, State),
79+
%% TODO: Set cookies once they have been parsed
80+
OkReq1 = cowboy_req:reply(RespStatus, CowboyRespHeaders, RespBody, Req1),
7981
{ok, OkReq1, State};
8082
% Proxy error (not error on remote server, actual e.g. network error)
8183
Error ->
@@ -91,18 +93,31 @@ init(Req0, State) ->
9193

9294
%% Builds the response headers from the remote servers response.
9395
response_headers({{RespVersion, RespStatus, RespReason}, RespHeaders, _RespBody}, Opts) ->
94-
List = case opts_disable_proxy_headers(Opts) of
96+
{Headers, Cookies} = process_response_headers(RespHeaders),
97+
HeaderMap = case opts_disable_proxy_headers(Opts) of
9598
true ->
96-
tuple_list_to_binary(RespHeaders);
99+
Headers;
97100
false ->
98-
[
99-
{<<"x-proxy-http-version">>, RespVersion},
100-
{<<"x-proxy-status">>, to_string(RespStatus)},
101-
{<<"x-proxy-reason">>, to_string(RespReason)}
102-
| tuple_list_to_binary(RespHeaders)
103-
]
101+
Headers#{
102+
<<"x-proxy-http-version">> => RespVersion,
103+
<<"x-proxy-status">> => to_string(RespStatus),
104+
<<"x-proxy-reason">> => to_string(RespReason)
105+
}
104106
end,
105-
maps:from_list(List).
107+
{HeaderMap, Cookies}.
108+
109+
%% Converts all keys to binary and extracts cookies
110+
process_response_headers(List) ->
111+
process_response_headers(List, #{}, []).
112+
process_response_headers([], HeadersAcc, CookiesAcc) ->
113+
{HeadersAcc, CookiesAcc};
114+
process_response_headers([{Key, Value} | Next], HeadersAcc, CookiesAcc) when is_list(Key) ->
115+
process_response_headers([{string:lowercase(to_binary(Key)), to_binary(Value)} | Next], HeadersAcc, CookiesAcc);
116+
process_response_headers([{Key, Value} | Next], HeadersAcc, CookiesAcc) when Key =:= <<"set-cookie">> ->
117+
%% TODO: Parse cookie
118+
process_response_headers(Next, HeadersAcc, [Value | CookiesAcc]);
119+
process_response_headers([{Key, Value} | Next], HeadersAcc, CookiesAcc) ->
120+
process_response_headers(Next, maps:put(Key, Value, HeadersAcc), CookiesAcc).
106121

107122
%%%-------------------------------------------------------------------
108123
%%% REQUEST
@@ -214,10 +229,6 @@ to_string(List) -> binary_to_list(iolist_to_binary(List)).
214229
to_binary(Binary) when is_binary(Binary) -> Binary;
215230
to_binary(List) -> iolist_to_binary(List).
216231

217-
%% Converts all keys in to binary
218-
tuple_list_to_binary(List) ->
219-
[{to_binary(Key), to_binary(Value)} || {Key, Value} <- List].
220-
221232
%% Dumps any term into a string representation.
222233
dump(Term) ->
223234
to_string(io_lib:format("~p", [Term])).

0 commit comments

Comments
 (0)