diff options
author | Loïc Hoguin <[email protected]> | 2014-03-21 11:05:24 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2014-03-21 11:05:24 +0100 |
commit | aca97c9e967779557f19d1df4af6c7b51244fdc7 (patch) | |
tree | 394f4f17b91b89f29f5dcb792262802ad740e78a | |
parent | 24eb6f3f9e93a72063c57923fcb88cb5cb5e8c82 (diff) | |
download | cowlib-aca97c9e967779557f19d1df4af6c7b51244fdc7.tar.gz cowlib-aca97c9e967779557f19d1df4af6c7b51244fdc7.tar.bz2 cowlib-aca97c9e967779557f19d1df4af6c7b51244fdc7.zip |
Add cow_http:request/4 and cow_http:version/1
-rw-r--r-- | src/cow_http.erl | 26 |
1 files changed, 26 insertions, 0 deletions
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. |