From 3047f0a5ef1872a1d8533c90bccb434d575d98f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 19 Oct 2020 18:01:40 +0200 Subject: Fix cookies for tunnels There are still small issues left to fix. In particular the set_cookie command should be replaced with doing the same in the protocol itself so that the scheme is correct. So CookieStore must be propagated to all callbacks. --- src/gun_cookies.erl | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/gun_cookies.erl') diff --git a/src/gun_cookies.erl b/src/gun_cookies.erl index ce33b49..965c3a5 100644 --- a/src/gun_cookies.erl +++ b/src/gun_cookies.erl @@ -14,6 +14,7 @@ -module(gun_cookies). +-export([add_cookie_header/5]). -export([domain_match/2]). -export([gc/1]). -export([path_match/2]). @@ -79,6 +80,31 @@ -> {ok, State} when State::store_state(). +-spec add_cookie_header(binary(), iodata(), iodata(), Headers, Store) + -> {Headers, Store} when Headers :: [{binary(), iodata()}], Store :: undefined | store(). +add_cookie_header(_, _, _, Headers, Store=undefined) -> + {Headers, Store}; +add_cookie_header(Scheme, Authority, PathWithQs, Headers0, Store0) -> + #{ + host := Host, + path := Path + } = uri_string:parse([Scheme, <<"://">>, Authority, PathWithQs]), + URIMap = uri_string:normalize(#{ + scheme => Scheme, + host => iolist_to_binary(Host), + path => iolist_to_binary(Path) + }, [return_map]), + {ok, Cookies0, Store} = query(Store0, URIMap), + Headers = case Cookies0 of + [] -> + Headers0; + _ -> + Cookies = [{Name, Value} || #{name := Name, value := Value} <- Cookies0], + %% We put cookies at the end of the headers list as it's the least important header. + Headers0 ++ [{<<"cookie">>, cow_cookie:cookie(Cookies)}] + end, + {Headers, Store}. + -spec domain_match(binary(), binary()) -> boolean(). domain_match(String, String) -> true; -- cgit v1.2.3