0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 20:50:24 +00:00
cowboy/test/http_SUITE_data/http_multipart.erl

25 lines
613 B
Erlang
Raw Normal View History

2011-11-08 00:51:49 +01:00
%% Feel free to use, reuse and abuse the code in this file.
2013-04-24 20:28:44 +02:00
-module(http_multipart).
2011-11-08 00:51:49 +01:00
-behaviour(cowboy_http_handler).
-export([init/3, handle/2, terminate/3]).
2011-11-08 00:51:49 +01:00
init({_Transport, http}, Req, []) ->
{ok, Req, {}}.
handle(Req, State) ->
{Result, Req2} = acc_multipart(Req, []),
{ok, cowboy_req:reply(200, [], term_to_binary(Result), Req2), State}.
2011-11-08 00:51:49 +01:00
terminate(_, _, _) ->
2011-11-08 00:51:49 +01:00
ok.
acc_multipart(Req, Acc) ->
case cowboy_req:part(Req) of
{ok, Headers, Req2} ->
{ok, Body, Req3} = cowboy_req:part_body(Req2),
acc_multipart(Req3, [{Headers, Body}|Acc]);
{done, Req2} ->
{lists:reverse(Acc), Req2}
end.