From 28186a68d0c023987fe7334b0326fb6c87f9447c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 16 May 2013 12:56:01 +0200 Subject: Make the HTTP version type more practical Now instead of {1, 1} we have 'HTTP/1.1', and instead of {1, 0} we have 'HTTP/1.0'. This is more efficient, easier to read in crash logs, and clearer in the code. --- src/cowboy_client.erl | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/cowboy_client.erl') diff --git a/src/cowboy_client.erl b/src/cowboy_client.erl index 4d958b1..faee904 100644 --- a/src/cowboy_client.erl +++ b/src/cowboy_client.erl @@ -40,7 +40,7 @@ timeout = 5000 :: timeout(), %% @todo Configurable. buffer = <<>> :: binary(), connection = keepalive :: keepalive | close, - version = {1, 1} :: cowboy_http:version(), + version = 'HTTP/1.1' :: cowboy_http:version(), response_body = undefined :: undefined | non_neg_integer() }). @@ -91,7 +91,7 @@ request(Method, URL, Headers, Body, Client=#client{ wait -> connect(Transport, Host, Port, Client); request -> {ok, Client} end, - VersionBin = cowboy_http:version_to_binary(Version), + VersionBin = atom_to_binary(Version, latin1), %% @todo do keepalive too, allow override... Headers2 = [ {<<"host">>, FullHost}, @@ -173,7 +173,7 @@ stream_status(Client=#client{state=State, buffer=Buffer}) when State =:= request -> case binary:split(Buffer, <<"\r\n">>) of [Line, Rest] -> - parse_status(Client#client{state=response, buffer=Rest}, Line); + parse_version(Client#client{state=response, buffer=Rest}, Line); _ -> case recv(Client) of {ok, Data} -> @@ -184,11 +184,13 @@ stream_status(Client=#client{state=State, buffer=Buffer}) end end. -parse_status(Client, << "HTTP/", High, ".", Low, " ", - S3, S2, S1, " ", StatusStr/binary >>) - when High >= $0, High =< $9, Low >= $0, Low =< $9, - S3 >= $0, S3 =< $9, S2 >= $0, S2 =< $9, S1 >= $0, S1 =< $9 -> - Version = {High - $0, Low - $0}, +parse_version(Client, << "HTTP/1.1 ", Rest/binary >>) -> + parse_status(Client, Rest, 'HTTP/1.1'); +parse_version(Client, << "HTTP/1.0 ", Rest/binary >>) -> + parse_status(Client, Rest, 'HTTP/1.0'). + +parse_status(Client, << S3, S2, S1, " ", StatusStr/binary >>, Version) + when S3 >= $0, S3 =< $9, S2 >= $0, S2 =< $9, S1 >= $0, S1 =< $9 -> Status = (S3 - $0) * 100 + (S2 - $0) * 10 + S1 - $0, {ok, Status, StatusStr, Client#client{version=Version}}. -- cgit v1.2.3