aboutsummaryrefslogtreecommitdiffstats
path: root/src/gun_spdy.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2015-03-29 20:29:31 +0200
committerLoïc Hoguin <[email protected]>2015-03-29 20:29:31 +0200
commit6068736c62bae081c338349feca311cfd5443ded (patch)
tree96d55ccafb748f5010114bc7a4a7bd15d984ea2e /src/gun_spdy.erl
parentc216566ddc60670334bdcf54617e6daedfdd1928 (diff)
downloadgun-6068736c62bae081c338349feca311cfd5443ded.tar.gz
gun-6068736c62bae081c338349feca311cfd5443ded.tar.bz2
gun-6068736c62bae081c338349feca311cfd5443ded.zip
Improve request code
The content-type and content-length alone indicate whether a body is present for request/4. The host header can now be overriden. A number of headers like transfer-encoding are automatically deleted from the list of headers we receive. SPDY also deletes connection, keep-alive and proxy-connection. SPDY now sends the port in the :host header.
Diffstat (limited to 'src/gun_spdy.erl')
-rw-r--r--src/gun_spdy.erl30
1 files changed, 22 insertions, 8 deletions
diff --git a/src/gun_spdy.erl b/src/gun_spdy.erl
index fc2fb1b..7651584 100644
--- a/src/gun_spdy.erl
+++ b/src/gun_spdy.erl
@@ -189,30 +189,44 @@ keepalive(State=#spdy_state{socket=Socket, transport=Transport,
Transport:send(Socket, cow_spdy:ping(PingID)),
State#spdy_state{ping_id=PingID + 2}.
-%% @todo Allow overriding the host when doing requests.
+%% @todo Always https scheme?
request(State=#spdy_state{socket=Socket, transport=Transport, zdef=Zdef,
- stream_id=StreamID}, StreamRef, Method, Host, _Port, Path, Headers) ->
- Out = false =/= lists:keyfind(<<"content-type">>, 1, Headers),
+ stream_id=StreamID}, StreamRef, Method, Host, Port, Path, Headers) ->
+ {Host2, Headers2} = prepare_request(Headers, Host, Port),
+ Out = (false =/= lists:keyfind(<<"content-type">>, 1, Headers2))
+ orelse (false =/= lists:keyfind(<<"content-length">>, 1, Headers2)),
Transport:send(Socket, cow_spdy:syn_stream(Zdef,
StreamID, 0, not Out, false, 0,
- Method, <<"https">>, Host, Path, <<"HTTP/1.1">>, Headers)),
+ Method, <<"https">>, Host2, Path, <<"HTTP/1.1">>, Headers2)),
new_stream(StreamID, StreamRef, true, Out, <<"HTTP/1.1">>,
State#spdy_state{stream_id=StreamID + 2}).
%% @todo Handle Body > 16MB. (split it out into many frames)
+%% @todo Always https scheme?
request(State=#spdy_state{socket=Socket, transport=Transport, zdef=Zdef,
- stream_id=StreamID}, StreamRef, Method, Host, _Port, Path, Headers, Body) ->
- Headers2 = lists:keystore(<<"content-length">>, 1, Headers,
- {<<"content-length">>, integer_to_list(iolist_size(Body))}),
+ stream_id=StreamID}, StreamRef, Method, Host, Port, Path, Headers, Body) ->
+ {Host2, Headers2} = prepare_request(Headers, Host, Port),
+ Headers3 = lists:keystore(<<"content-length">>, 1, Headers2,
+ {<<"content-length">>, integer_to_binary(iolist_size(Body))}),
Transport:send(Socket, [
cow_spdy:syn_stream(Zdef,
StreamID, 0, false, false, 0,
- Method, <<"https">>, Host, Path, <<"HTTP/1.1">>, Headers2),
+ Method, <<"https">>, Host2, Path, <<"HTTP/1.1">>, Headers3),
cow_spdy:data(StreamID, true, Body)
]),
new_stream(StreamID, StreamRef, true, false, <<"HTTP/1.1">>,
State#spdy_state{stream_id=StreamID + 2}).
+prepare_request(Headers, Host, Port) ->
+ Headers2 = lists:keydelete(<<"keep-alive">>, 1,
+ lists:keydelete(<<"proxy-connection">>, 1,
+ lists:keydelete(<<"transfer-encoding">>, 1,
+ lists:keydelete(<<"connection">>, 1, Headers)))),
+ case lists:keytake(<<"host">>, 1, Headers2) of
+ false -> {[Host, $:, integer_to_binary(Port)], Headers2};
+ {value, {_, Host1}, Headers3} -> {Host1, Headers3}
+ end.
+
data(State=#spdy_state{socket=Socket, transport=Transport},
StreamRef, IsFin, Data) ->
case get_stream_by_ref(StreamRef, State) of