@@ -75,7 +75,9 @@ init(Req0, State) ->
75
75
% We got a response from the remote server!
76
76
{ok , Resp = {{_RespVersion , RespStatus , RespReason }, _RespHeaders , RespBody }} ->
77
77
? 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 ),
79
81
{ok , OkReq1 , State };
80
82
% Proxy error (not error on remote server, actual e.g. network error)
81
83
Error ->
@@ -91,18 +93,31 @@ init(Req0, State) ->
91
93
92
94
% % Builds the response headers from the remote servers response.
93
95
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
95
98
true ->
96
- tuple_list_to_binary ( RespHeaders ) ;
99
+ Headers ;
97
100
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
+ }
104
106
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 ).
106
121
107
122
% %%-------------------------------------------------------------------
108
123
% %% REQUEST
@@ -214,10 +229,6 @@ to_string(List) -> binary_to_list(iolist_to_binary(List)).
214
229
to_binary (Binary ) when is_binary (Binary ) -> Binary ;
215
230
to_binary (List ) -> iolist_to_binary (List ).
216
231
217
- % % Converts all keys in to binary
218
- tuple_list_to_binary (List ) ->
219
- [{to_binary (Key ), to_binary (Value )} || {Key , Value } <- List ].
220
-
221
232
% % Dumps any term into a string representation.
222
233
dump (Term ) ->
223
234
to_string (io_lib :format (" ~p " , [Term ])).
0 commit comments