From aca97c9e967779557f19d1df4af6c7b51244fdc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 21 Mar 2014 11:05:24 +0100 Subject: Add cow_http:request/4 and cow_http:version/1 --- src/cow_http.erl | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/cow_http.erl b/src/cow_http.erl index d10ff4f..f7e3cdd 100644 --- a/src/cow_http.erl +++ b/src/cow_http.erl @@ -22,6 +22,9 @@ -export([parse_fullpath/1]). -export([parse_version/1]). +-export([request/4]). +-export([version/1]). + -type version() :: 'HTTP/1.0' | 'HTTP/1.1'. -type status() :: 100..999. -type headers() :: [{binary(), iodata()}]. @@ -273,3 +276,26 @@ parse_version_test() -> {'EXIT', _} = (catch parse_version(<<"HTTP/1.2">>)), ok. -endif. + +%% @doc Return formatted request-line and headers. +%% @todo Add tests when the corresponding reverse functions are added. + +-spec request(binary(), iodata(), version(), headers()) -> iodata(). +request(Method, Path, Version, Headers) -> + [Method, <<" ">>, Path, <<" ">>, version(Version), <<"\r\n">>, + [[N, <<": ">>, V, <<"\r\n">>] || {N, V} <- Headers], + <<"\r\n">>]. + +%% @doc Return the version as a binary. + +-spec version(version()) -> binary(). +version('HTTP/1.1') -> <<"HTTP/1.1">>; +version('HTTP/1.0') -> <<"HTTP/1.0">>. + +-ifdef(TEST). +version_test() -> + <<"HTTP/1.1">> = version('HTTP/1.1'), + <<"HTTP/1.0">> = version('HTTP/1.0'), + {'EXIT', _} = (catch version('HTTP/1.2')), + ok. +-endif. -- cgit v1.2.3