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).
|
2013-01-22 02:34:18 +01:00
|
|
|
-export([init/3, handle/2, terminate/3]).
|
2011-11-08 00:51:49 +01:00
|
|
|
|
|
|
|
init({_Transport, http}, Req, []) ->
|
|
|
|
{ok, Req, {}}.
|
|
|
|
|
|
|
|
handle(Req, State) ->
|
2014-02-06 19:36:25 +01:00
|
|
|
{Result, Req2} = acc_multipart(Req, []),
|
2012-08-27 13:28:57 +02:00
|
|
|
{ok, Req3} = cowboy_req:reply(200, [], term_to_binary(Result), Req2),
|
2012-01-23 07:23:44 +01:00
|
|
|
{ok, Req3, State}.
|
2011-11-08 00:51:49 +01:00
|
|
|
|
2013-01-22 02:34:18 +01:00
|
|
|
terminate(_, _, _) ->
|
2011-11-08 00:51:49 +01:00
|
|
|
ok.
|
|
|
|
|
2014-02-06 19:36:25 +01:00
|
|
|
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.
|