aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2018-05-18 18:01:53 +0200
committerLoïc Hoguin <[email protected]>2018-05-18 18:01:53 +0200
commit070b931ce5f4d911e23b1535457c4414a36610c6 (patch)
tree5e74271cb6e516bb681586221e5718592d10f8ce
parent3b24804ecb442b6c89a9987781547550be3a7f93 (diff)
downloadcowlib-070b931ce5f4d911e23b1535457c4414a36610c6.tar.gz
cowlib-070b931ce5f4d911e23b1535457c4414a36610c6.tar.bz2
cowlib-070b931ce5f4d911e23b1535457c4414a36610c6.zip
Add cow_http:status_to_integer
-rw-r--r--src/cow_http.erl21
1 files changed, 18 insertions, 3 deletions
diff --git a/src/cow_http.erl b/src/cow_http.erl
index 74766e2..9a87f7d 100644
--- a/src/cow_http.erl
+++ b/src/cow_http.erl
@@ -16,6 +16,7 @@
%% @todo parse_request_line
-export([parse_status_line/1]).
+-export([status_to_integer/1]).
-export([parse_headers/1]).
-export([parse_fullpath/1]).
@@ -47,13 +48,27 @@ parse_status_line(<< "HTTP/1.1 ", Status/bits >>) ->
parse_status_line(<< "HTTP/1.0 ", Status/bits >>) ->
parse_status_line(Status, 'HTTP/1.0').
-parse_status_line(<< H, T, U, " ", Rest/bits >>, Version)
- when $0 =< H, H =< $9, $0 =< T, T =< $9, $0 =< U, U =< $9 ->
- Status = (H - $0) * 100 + (T - $0) * 10 + (U - $0),
+parse_status_line(<<H, T, U, " ", Rest/bits>>, Version) ->
+ Status = status_to_integer(H, T, U),
{Pos, _} = binary:match(Rest, <<"\r">>),
<< StatusStr:Pos/binary, "\r\n", Rest2/bits >> = Rest,
{Version, Status, StatusStr, Rest2}.
+-spec status_to_integer(status() | binary()) -> status().
+status_to_integer(Status) when is_integer(Status) ->
+ Status;
+status_to_integer(Status) ->
+ case Status of
+ <<H, T, U>> ->
+ status_to_integer(H, T, U);
+ <<H, T, U, " ", _/bits>> ->
+ status_to_integer(H, T, U)
+ end.
+
+status_to_integer(H, T, U)
+ when $0 =< H, H =< $9, $0 =< T, T =< $9, $0 =< U, U =< $9 ->
+ (H - $0) * 100 + (T - $0) * 10 + (U - $0).
+
-ifdef(TEST).
parse_status_line_test_() ->
Tests = [