From 070b931ce5f4d911e23b1535457c4414a36610c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 18 May 2018 18:01:53 +0200 Subject: Add cow_http:status_to_integer --- src/cow_http.erl | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'src/cow_http.erl') 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(<>, 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 + <> -> + status_to_integer(H, T, U); + <> -> + 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 = [ -- cgit v1.2.3