diff options
-rw-r--r-- | doc/src/guide/http.asciidoc | 12 | ||||
-rw-r--r-- | doc/src/manual/gun.asciidoc | 2 | ||||
-rw-r--r-- | doc/src/manual/gun.headers.asciidoc | 86 | ||||
-rw-r--r-- | doc/src/manual/gun.patch.asciidoc | 12 | ||||
-rw-r--r-- | doc/src/manual/gun.post.asciidoc | 12 | ||||
-rw-r--r-- | doc/src/manual/gun.put.asciidoc | 12 | ||||
-rw-r--r-- | doc/src/manual/gun.request.asciidoc | 16 | ||||
-rw-r--r-- | src/gun.erl | 69 | ||||
-rw-r--r-- | src/gun_http.erl | 9 | ||||
-rw-r--r-- | src/gun_http2.erl | 11 | ||||
-rw-r--r-- | test/gun_SUITE.erl | 2 | ||||
-rw-r--r-- | test/rfc7231_SUITE.erl | 36 |
12 files changed, 190 insertions, 89 deletions
diff --git a/doc/src/guide/http.asciidoc b/doc/src/guide/http.asciidoc index 652030a..fb0c7dc 100644 --- a/doc/src/guide/http.asciidoc +++ b/doc/src/guide/http.asciidoc @@ -108,7 +108,7 @@ desirable. The request body of a PATCH method may be a partial representation or a list of instructions on how to update the resource. -The `gun:post/4,5`, `gun:put/4,5` and `gun:patch/4,5` functions +The functions `gun:post/4,5`, `gun:put/4,5` and `gun:patch/4,5` take a body as their fourth argument. These functions do not require any body-specific header to be set, although it is always recommended to set the content-type header. @@ -127,13 +127,9 @@ StreamRef = gun:post(ConnPid, "/organizations/ninenines", [ ], Body). ---- -The `gun:post/3`, `gun:put/3` and `gun:patch/3` functions -do not take a body in their arguments. If a body is to be -provided later on, using the `gun:data/4` function, then -the request headers must indicate this. This can be done -by setting the content-length or content-type request -headers. If these headers are not set then Gun will assume -the request has no body. +The functions `gun:post/3,4`, `gun:put/3,4` and `gun:patch/3,4` +do not take a body in their arguments: the body must be +provided later on using the `gun:data/4` function. It is recommended to send the content-length header if you know it in advance, although this is not required. If it diff --git a/doc/src/manual/gun.asciidoc b/doc/src/manual/gun.asciidoc index a210635..1b4e6d7 100644 --- a/doc/src/manual/gun.asciidoc +++ b/doc/src/manual/gun.asciidoc @@ -29,6 +29,7 @@ Requests: * link:man:gun:post(3)[gun:post(3)] - Process the enclosed representation according to a resource's own semantics * link:man:gun:put(3)[gun:put(3)] - Create or replace a resource * link:man:gun:delete(3)[gun:delete(3)] - Delete a resource +* link:man:gun:headers(3)[gun:headers(3)] - Initiate the given request * link:man:gun:request(3)[gun:request(3)] - Perform the given request * link:man:gun:data(3)[gun:data(3)] - Stream the body of a request @@ -313,6 +314,7 @@ undocumented and must be set to `gun_ws_h`. == Changelog +* *2.0*: Function `gun:headers/4,5` introduced. * *1.3*: Add the CONNECT destination's `protocols` option and deprecate the previously introduced `protocol` option. * *1.2*: Introduce the type `connect_destination()`. diff --git a/doc/src/manual/gun.headers.asciidoc b/doc/src/manual/gun.headers.asciidoc new file mode 100644 index 0000000..bab1b82 --- /dev/null +++ b/doc/src/manual/gun.headers.asciidoc @@ -0,0 +1,86 @@ += gun:headers(3) + +== Name + +gun:headers - Initiate the given request + +== Description + +[source,erlang] +---- +headers(ConnPid, Method, Path, Headers) + -> headers(ConnPid, Method, Path, Headers, #{}) + +headers(ConnPid, Method, Path, Headers, ReqOpts) + -> StreamRef + +ConnPid :: pid() +Method :: binary() +Path :: iodata() +Headers :: [{binary(), iodata()}] +ReqOpts :: gun:req_opts() +StreamRef :: reference() +---- + +Initiate the given request. + +This is a general purpose function that should only be +used when other method-specific functions do not apply. + +The function `headers/4,5` initiates a request but does +not send the request body. It must be sent separately +using link:man:gun:data(3)[gun:data(3)]. + +== Arguments + +ConnPid:: + +The pid of the Gun connection process. + +Method:: + +Method to be used for the request. + +Path:: + +Path to the resource. + +Headers:: + +Additional request headers. + +ReqOpts:: + +Request options. + +== Return value + +A reference that identifies the newly created stream is +returned. It is this reference that must be passed in +subsequent calls and will be received in messages related +to this new stream. + +== Changelog + +* *2.0*: Function introduced. + +== Examples + +.Initiate a request +[source,erlang] +---- +StreamRef = gun:headers(ConnPid, <<"PUT">>, + "/lang/fr_FR/hello", + [{<<"content-type">>, <<"text/plain">>}]). +---- + +== See also + +link:man:gun(3)[gun(3)], +link:man:gun:request(3)[gun:request(3)], +link:man:gun:await(3)[gun:await(3)], +link:man:gun:await_body(3)[gun:await_body(3)], +link:man:gun_push(3)[gun_push(3)], +link:man:gun_inform(3)[gun_inform(3)], +link:man:gun_response(3)[gun_response(3)], +link:man:gun_data(3)[gun_data(3)] diff --git a/doc/src/manual/gun.patch.asciidoc b/doc/src/manual/gun.patch.asciidoc index 3f7baa9..c240a4d 100644 --- a/doc/src/manual/gun.patch.asciidoc +++ b/doc/src/manual/gun.patch.asciidoc @@ -9,6 +9,9 @@ gun:patch - Apply a set of changes to a resource [source,erlang] ---- patch(ConnPid, Path, Headers) + -> patch(ConnPid, Path, Headers, #{}) + +patch(ConnPid, Path, Headers, ReqOpts) -> StreamRef patch(ConnPid, Path, Headers, Body) @@ -30,10 +33,8 @@ Apply a set of changes to a resource. The behavior of this function varies depending on whether a body is provided. -The function `patch/3` expects either a content-length -or content-type header to indicate that a body will be -sent afterwards. The body can then be sent using -link:man:gun:data(3)[gun:data(3)]. +The function `patch/3,4` does not send a body. It must be +sent separately using link:man:gun:data(3)[gun:data(3)]. The function `patch/4,5` sends the entire request, including the request body, immediately. It is therefore not possible @@ -75,6 +76,9 @@ to this new stream. == Changelog +* *2.0*: Implicit body detection has been removed. The body + must now be provided either directly (even if empty) + or using separate calls. * *1.0*: Function introduced. == Examples diff --git a/doc/src/manual/gun.post.asciidoc b/doc/src/manual/gun.post.asciidoc index e548824..aed8b3f 100644 --- a/doc/src/manual/gun.post.asciidoc +++ b/doc/src/manual/gun.post.asciidoc @@ -9,6 +9,9 @@ gun:post - Process the enclosed representation according to a resource's own sem [source,erlang] ---- post(ConnPid, Path, Headers) + -> post(ConnPid, Path, Headers, #{}) + +post(ConnPid, Path, Headers, ReqOpts) -> StreamRef post(ConnPid, Path, Headers, Body) @@ -31,10 +34,8 @@ own semantics. The behavior of this function varies depending on whether a body is provided. -The function `post/3` expects either a content-length -or content-type header to indicate that a body will be -sent afterwards. The body can then be sent using -link:man:gun:data(3)[gun:data(3)]. +The function `post/3,4` does not send a body. It must be +sent separately using link:man:gun:data(3)[gun:data(3)]. The function `post/4,5` sends the entire request, including the request body, immediately. It is therefore not possible @@ -73,6 +74,9 @@ to this new stream. == Changelog +* *2.0*: Implicit body detection has been removed. The body + must now be provided either directly (even if empty) + or using separate calls. * *1.0*: Function introduced. == Examples diff --git a/doc/src/manual/gun.put.asciidoc b/doc/src/manual/gun.put.asciidoc index 4f0cb39..a60849b 100644 --- a/doc/src/manual/gun.put.asciidoc +++ b/doc/src/manual/gun.put.asciidoc @@ -9,6 +9,9 @@ gun:put - Create or replace a resource [source,erlang] ---- put(ConnPid, Path, Headers) + -> put(ConnPid, Path, Headers, #{}) + +put(ConnPid, Path, Headers, ReqOpts) -> StreamRef put(ConnPid, Path, Headers, Body) @@ -30,10 +33,8 @@ Create or replace a resource. The behavior of this function varies depending on whether a body is provided. -The function `put/3` expects either a content-length -or content-type header to indicate that a body will be -sent afterwards. The body can then be sent using -link:man:gun:data(3)[gun:data(3)]. +The function `put/3,4` does not send a body. It must be +sent separately using link:man:gun:data(3)[gun:data(3)]. The function `put/4,5` sends the entire request, including the request body, immediately. It is therefore not possible @@ -72,6 +73,9 @@ to this new stream. == Changelog +* *2.0*: Implicit body detection has been removed. The body + must now be provided either directly (even if empty) + or using separate calls. * *1.0*: Function introduced. == Examples diff --git a/doc/src/manual/gun.request.asciidoc b/doc/src/manual/gun.request.asciidoc index ec169ac..f5c1cd8 100644 --- a/doc/src/manual/gun.request.asciidoc +++ b/doc/src/manual/gun.request.asciidoc @@ -8,9 +8,6 @@ gun:request - Perform the given request [source,erlang] ---- -request(ConnPid, Method, Path, Headers) - -> StreamRef - request(ConnPid, Method, Path, Headers, Body) -> request(ConnPid, Method, Path, Headers, Body, #{}) @@ -31,15 +28,6 @@ Perform the given request. This is a general purpose function that should only be used when other method-specific functions do not apply. -The behavior of this function varies depending on whether -a body is provided. - -The function `request/4` expects either a content-length -or content-type header to indicate that a body will be -sent afterwards. Gun will assume the request has no body -otherwise. The body can then be sent using -link:man:gun:data(3)[gun:data(3)]. - The function `request/5,6` sends the entire request, including the request body, immediately. It is therefore not possible to use link:man:gun:data(3)[gun:data(3)] after that. You @@ -81,6 +69,9 @@ to this new stream. == Changelog +* *2.0*: Implicit body detection has been removed. The body + must now be provided either directly (even if empty) + or using link:man:gun:headers(3)[gun:headers(3)]. * *1.0*: Function introduced. == Examples @@ -97,6 +88,7 @@ StreamRef = gun:request(ConnPid, <<"PUT">>, == See also link:man:gun(3)[gun(3)], +link:man:gun:headers(3)[gun:headers(3)], link:man:gun:await(3)[gun:await(3)], link:man:gun:await_body(3)[gun:await_body(3)], link:man:gun_push(3)[gun_push(3)], diff --git a/src/gun.erl b/src/gun.erl index 1e83979..10ae2a7 100644 --- a/src/gun.erl +++ b/src/gun.erl @@ -49,7 +49,10 @@ -export([put/3]). -export([put/4]). -export([put/5]). --export([request/4]). + +%% Generic requests interface. +-export([headers/4]). +-export([headers/5]). -export([request/5]). -export([request/6]). @@ -316,11 +319,11 @@ shutdown(ServerPid) -> -spec delete(pid(), iodata()) -> reference(). delete(ServerPid, Path) -> - request(ServerPid, <<"DELETE">>, Path, []). + request(ServerPid, <<"DELETE">>, Path, [], <<>>). -spec delete(pid(), iodata(), headers()) -> reference(). delete(ServerPid, Path, Headers) -> - request(ServerPid, <<"DELETE">>, Path, Headers). + request(ServerPid, <<"DELETE">>, Path, Headers, <<>>). -spec delete(pid(), iodata(), headers(), req_opts()) -> reference(). delete(ServerPid, Path, Headers, ReqOpts) -> @@ -328,11 +331,11 @@ delete(ServerPid, Path, Headers, ReqOpts) -> -spec get(pid(), iodata()) -> reference(). get(ServerPid, Path) -> - request(ServerPid, <<"GET">>, Path, []). + request(ServerPid, <<"GET">>, Path, [], <<>>). -spec get(pid(), iodata(), headers()) -> reference(). get(ServerPid, Path, Headers) -> - request(ServerPid, <<"GET">>, Path, Headers). + request(ServerPid, <<"GET">>, Path, Headers, <<>>). -spec get(pid(), iodata(), headers(), req_opts()) -> reference(). get(ServerPid, Path, Headers, ReqOpts) -> @@ -340,11 +343,11 @@ get(ServerPid, Path, Headers, ReqOpts) -> -spec head(pid(), iodata()) -> reference(). head(ServerPid, Path) -> - request(ServerPid, <<"HEAD">>, Path, []). + request(ServerPid, <<"HEAD">>, Path, [], <<>>). -spec head(pid(), iodata(), headers()) -> reference(). head(ServerPid, Path, Headers) -> - request(ServerPid, <<"HEAD">>, Path, Headers). + request(ServerPid, <<"HEAD">>, Path, Headers, <<>>). -spec head(pid(), iodata(), headers(), req_opts()) -> reference(). head(ServerPid, Path, Headers, ReqOpts) -> @@ -352,11 +355,11 @@ head(ServerPid, Path, Headers, ReqOpts) -> -spec options(pid(), iodata()) -> reference(). options(ServerPid, Path) -> - request(ServerPid, <<"OPTIONS">>, Path, []). + request(ServerPid, <<"OPTIONS">>, Path, [], <<>>). -spec options(pid(), iodata(), headers()) -> reference(). options(ServerPid, Path, Headers) -> - request(ServerPid, <<"OPTIONS">>, Path, Headers). + request(ServerPid, <<"OPTIONS">>, Path, Headers, <<>>). -spec options(pid(), iodata(), headers(), req_opts()) -> reference(). options(ServerPid, Path, Headers, ReqOpts) -> @@ -364,9 +367,11 @@ options(ServerPid, Path, Headers, ReqOpts) -> -spec patch(pid(), iodata(), headers()) -> reference(). patch(ServerPid, Path, Headers) -> - request(ServerPid, <<"PATCH">>, Path, Headers). + headers(ServerPid, <<"PATCH">>, Path, Headers). --spec patch(pid(), iodata(), headers(), iodata()) -> reference(). +-spec patch(pid(), iodata(), headers(), iodata() | req_opts()) -> reference(). +patch(ServerPid, Path, Headers, ReqOpts) when is_map(ReqOpts) -> + headers(ServerPid, <<"PATCH">>, Path, Headers, ReqOpts); patch(ServerPid, Path, Headers, Body) -> request(ServerPid, <<"PATCH">>, Path, Headers, Body). @@ -376,9 +381,11 @@ patch(ServerPid, Path, Headers, Body, ReqOpts) -> -spec post(pid(), iodata(), headers()) -> reference(). post(ServerPid, Path, Headers) -> - request(ServerPid, <<"POST">>, Path, Headers). + headers(ServerPid, <<"POST">>, Path, Headers). --spec post(pid(), iodata(), headers(), iodata()) -> reference(). +-spec post(pid(), iodata(), headers(), iodata() | req_opts()) -> reference(). +post(ServerPid, Path, Headers, ReqOpts) when is_map(ReqOpts) -> + headers(ServerPid, <<"POST">>, Path, Headers, ReqOpts); post(ServerPid, Path, Headers, Body) -> request(ServerPid, <<"POST">>, Path, Headers, Body). @@ -388,9 +395,11 @@ post(ServerPid, Path, Headers, Body, ReqOpts) -> -spec put(pid(), iodata(), headers()) -> reference(). put(ServerPid, Path, Headers) -> - request(ServerPid, <<"PUT">>, Path, Headers). + headers(ServerPid, <<"PUT">>, Path, Headers). --spec put(pid(), iodata(), headers(), iodata()) -> reference(). +-spec put(pid(), iodata(), headers(), iodata() | req_opts()) -> reference(). +put(ServerPid, Path, Headers, ReqOpts) when is_map(ReqOpts) -> + headers(ServerPid, <<"PUT">>, Path, Headers, ReqOpts); put(ServerPid, Path, Headers, Body) -> request(ServerPid, <<"PUT">>, Path, Headers, Body). @@ -398,9 +407,19 @@ put(ServerPid, Path, Headers, Body) -> put(ServerPid, Path, Headers, Body, ReqOpts) -> request(ServerPid, <<"PUT">>, Path, Headers, Body, ReqOpts). --spec request(pid(), iodata(), iodata(), headers()) -> reference(). -request(ServerPid, Method, Path, Headers) -> - request(ServerPid, Method, Path, Headers, <<>>, #{}). +%% Generic requests interface. + +-spec headers(pid(), iodata(), iodata(), headers()) -> reference(). +headers(ServerPid, Method, Path, Headers) -> + headers(ServerPid, Method, Path, Headers, #{}). + +%% @todo Accept header names as maps. +-spec headers(pid(), iodata(), iodata(), headers(), req_opts()) -> reference(). +headers(ServerPid, Method, Path, Headers, ReqOpts) -> + StreamRef = make_ref(), + ReplyTo = maps:get(reply_to, ReqOpts, self()), + gen_statem:cast(ServerPid, {headers, ReplyTo, StreamRef, Method, Path, Headers}), + StreamRef. -spec request(pid(), iodata(), iodata(), headers(), iodata()) -> reference(). request(ServerPid, Method, Path, Headers, Body) -> @@ -729,15 +748,17 @@ connected(info, keepalive, State=#state{protocol=Protocol, protocol_state=ProtoS ProtoState2 = Protocol:keepalive(ProtoState), {keep_state, keepalive_timeout(State#state{protocol_state=ProtoState2})}; %% Public HTTP interface. +connected(cast, {headers, ReplyTo, StreamRef, Method, Path, Headers}, + State=#state{origin_host=Host, origin_port=Port, + protocol=Protocol, protocol_state=ProtoState}) -> + ProtoState2 = Protocol:headers(ProtoState, + StreamRef, ReplyTo, Method, Host, Port, Path, Headers), + {keep_state, State#state{protocol_state=ProtoState2}}; connected(cast, {request, ReplyTo, StreamRef, Method, Path, Headers, Body}, State=#state{origin_host=Host, origin_port=Port, protocol=Protocol, protocol_state=ProtoState}) -> - ProtoState2 = case Body of - <<>> -> Protocol:request(ProtoState, - StreamRef, ReplyTo, Method, Host, Port, Path, Headers); - _ -> Protocol:request(ProtoState, - StreamRef, ReplyTo, Method, Host, Port, Path, Headers, Body) - end, + ProtoState2 = Protocol:request(ProtoState, + StreamRef, ReplyTo, Method, Host, Port, Path, Headers, Body), {keep_state, State#state{protocol_state=ProtoState2}}; %% @todo Do we want to reject ReplyTo if it's not the process %% who initiated the connection? For both data and cancel. diff --git a/src/gun_http.erl b/src/gun_http.erl index 57f72c7..5229a6d 100644 --- a/src/gun_http.erl +++ b/src/gun_http.erl @@ -20,7 +20,7 @@ -export([handle/2]). -export([close/2]). -export([keepalive/1]). --export([request/8]). +-export([headers/8]). -export([request/9]). -export([data/5]). -export([connect/5]). @@ -330,7 +330,7 @@ keepalive(State=#http_state{socket=Socket, transport=Transport, out=head}) -> keepalive(State) -> State. -request(State=#http_state{socket=Socket, transport=Transport, version=Version, +headers(State=#http_state{socket=Socket, transport=Transport, version=Version, out=head}, StreamRef, ReplyTo, Method, Host, Port, Path, Headers) -> Host2 = case Host of {local, _SocketPath} -> <<>>; @@ -509,10 +509,7 @@ request_io_from_headers(Headers) -> {_, Length} -> {body, cow_http_hd:parse_content_length(Length)}; _ -> - case lists:keymember(<<"content-type">>, 1, Headers) of - true -> body_chunked; - false -> head - end + body_chunked end. response_io_from_headers(<<"HEAD">>, _, _, _) -> diff --git a/src/gun_http2.erl b/src/gun_http2.erl index 6538083..f5cf834 100644 --- a/src/gun_http2.erl +++ b/src/gun_http2.erl @@ -20,7 +20,7 @@ -export([handle/2]). -export([close/2]). -export([keepalive/1]). --export([request/8]). +-export([headers/8]). -export([request/9]). -export([data/5]). -export([cancel/3]). @@ -240,19 +240,14 @@ keepalive(State=#http2_state{socket=Socket, transport=Transport}) -> Transport:send(Socket, cow_http2:ping(0)), State. -request(State=#http2_state{socket=Socket, transport=Transport, +headers(State=#http2_state{socket=Socket, transport=Transport, http2_machine=HTTP2Machine0, streams=Streams}, StreamRef, ReplyTo, Method, Host, Port, Path, Headers0) -> - IsFin0 = case (false =/= lists:keyfind(<<"content-type">>, 1, Headers0)) - orelse (false =/= lists:keyfind(<<"content-length">>, 1, Headers0)) of - true -> nofin; - false -> fin - end, {ok, StreamID, HTTP2Machine1} = cow_http2_machine:init_stream( iolist_to_binary(Method), HTTP2Machine0), {ok, PseudoHeaders, Headers} = prepare_headers(State, Method, Host, Port, Path, Headers0), {ok, IsFin, HeaderBlock, HTTP2Machine} = cow_http2_machine:prepare_headers( - StreamID, HTTP2Machine1, IsFin0, PseudoHeaders, Headers), + StreamID, HTTP2Machine1, nofin, PseudoHeaders, Headers), Transport:send(Socket, cow_http2:headers(StreamID, IsFin, HeaderBlock)), Stream = #stream{id=StreamID, ref=StreamRef, reply_to=ReplyTo}, State#http2_state{http2_machine=HTTP2Machine, streams=[Stream|Streams]}. diff --git a/test/gun_SUITE.erl b/test/gun_SUITE.erl index 9a49ff8..e7b660d 100644 --- a/test/gun_SUITE.erl +++ b/test/gun_SUITE.erl @@ -260,7 +260,7 @@ transform_header_name(_) -> {ok, Data} = gen_tcp:recv(ClientSocket, 0, 5000), %% We do some very crude parsing of the response headers %% to check that the header name was properly transformed. - Lines = binary:split(Data, <<"\r\n">>), + Lines = binary:split(Data, <<"\r\n">>, [global]), HostLines = [L || <<"HOST: ", _/bits>> = L <- Lines], 1 = length(HostLines), ok. diff --git a/test/rfc7231_SUITE.erl b/test/rfc7231_SUITE.erl index f985bd8..5f7bd6f 100644 --- a/test/rfc7231_SUITE.erl +++ b/test/rfc7231_SUITE.erl @@ -182,9 +182,9 @@ do_connect_http(Transport) -> {request, <<"CONNECT">>, Authority, 'HTTP/1.1', _} = do_receive(ProxyPid), {response, fin, 200, _} = gun:await(ConnPid, StreamRef), _ = gun:get(ConnPid, "/proxied"), - Len = byte_size(Authority), - <<"GET /proxied HTTP/1.1\r\nhost: ", Authority:Len/binary, "\r\n", _/bits>> - = do_receive(OriginPid), + Data = do_receive(OriginPid), + Lines = binary:split(Data, <<"\r\n">>, [global]), + [<<"host: ", Authority/bits>>] = [L || <<"host: ", _/bits>> = L <- Lines], #{ transport := Transport, protocol := http, @@ -271,9 +271,9 @@ connect_through_multiple_proxies(_) -> {request, <<"CONNECT">>, Authority2, 'HTTP/1.1', _} = do_receive(Proxy2Pid), {response, fin, 200, _} = gun:await(ConnPid, StreamRef2), _ = gun:get(ConnPid, "/proxied"), - Len = byte_size(Authority2), - <<"GET /proxied HTTP/1.1\r\nhost: ", Authority2:Len/binary, "\r\n", _/bits>> - = do_receive(OriginPid), + Data = do_receive(OriginPid), + Lines = binary:split(Data, <<"\r\n">>, [global]), + [<<"host: ", Authority2/bits>>] = [L || <<"host: ", _/bits>> = L <- Lines], #{ transport := tcp, protocol := http, @@ -309,9 +309,9 @@ connect_delay(_) -> {request, <<"CONNECT">>, Authority, 'HTTP/1.1', _} = do_receive(ProxyPid, 3000), {response, fin, 201, _} = gun:await(ConnPid, StreamRef), _ = gun:get(ConnPid, "/proxied"), - Len = byte_size(Authority), - <<"GET /proxied HTTP/1.1\r\nhost: ", Authority:Len/binary, "\r\n", _/bits>> - = do_receive(OriginPid), + Data = do_receive(OriginPid), + Lines = binary:split(Data, <<"\r\n">>, [global]), + [<<"host: ", Authority/bits>>] = [L || <<"host: ", _/bits>> = L <- Lines], #{ transport := tcp, protocol := http, @@ -341,9 +341,9 @@ connect_response_201(_) -> {request, <<"CONNECT">>, Authority, 'HTTP/1.1', _} = do_receive(ProxyPid), {response, fin, 201, _} = gun:await(ConnPid, StreamRef), _ = gun:get(ConnPid, "/proxied"), - Len = byte_size(Authority), - <<"GET /proxied HTTP/1.1\r\nhost: ", Authority:Len/binary, "\r\n", _/bits>> - = do_receive(OriginPid), + Data = do_receive(OriginPid), + Lines = binary:split(Data, <<"\r\n">>, [global]), + [<<"host: ", Authority/bits>>] = [L || <<"host: ", _/bits>> = L <- Lines], #{ transport := tcp, protocol := http, @@ -479,9 +479,9 @@ connect_response_ignore_transfer_encoding(_) -> {request, <<"CONNECT">>, Authority, 'HTTP/1.1', _} = do_receive(ProxyPid), {response, fin, 200, Headers} = gun:await(ConnPid, StreamRef), _ = gun:get(ConnPid, "/proxied"), - Len = byte_size(Authority), - <<"GET /proxied HTTP/1.1\r\nhost: ", Authority:Len/binary, "\r\n", _/bits>> - = do_receive(OriginPid), + Data = do_receive(OriginPid), + Lines = binary:split(Data, <<"\r\n">>, [global]), + [<<"host: ", Authority/bits>>] = [L || <<"host: ", _/bits>> = L <- Lines], gun:close(ConnPid). connect_response_ignore_content_length(_) -> @@ -500,7 +500,7 @@ connect_response_ignore_content_length(_) -> {request, <<"CONNECT">>, Authority, 'HTTP/1.1', _} = do_receive(ProxyPid), {response, fin, 200, Headers} = gun:await(ConnPid, StreamRef), _ = gun:get(ConnPid, "/proxied"), - Len = byte_size(Authority), - <<"GET /proxied HTTP/1.1\r\nhost: ", Authority:Len/binary, "\r\n", _/bits>> - = do_receive(OriginPid), + Data = do_receive(OriginPid), + Lines = binary:split(Data, <<"\r\n">>, [global]), + [<<"host: ", Authority/bits>>] = [L || <<"host: ", _/bits>> = L <- Lines], gun:close(ConnPid). |