0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 04:10:24 +00:00

Make reply functions return Req

This commit is contained in:
Loïc Hoguin 2016-08-10 17:15:02 +02:00
parent aa6f2ab5a4
commit e30d120bd8
16 changed files with 109 additions and 108 deletions

View file

@ -5,8 +5,8 @@
-export([init/2]).
init(Req, Opts) ->
cowboy_req:stream_reply(200, Req),
init(Req0, Opts) ->
Req = cowboy_req:stream_reply(200, Req0),
cowboy_req:stream_body("Hello\r\n", nofin, Req),
timer:sleep(1000),
cowboy_req:stream_body("World\r\n", nofin, Req),

View file

@ -5,7 +5,7 @@
-export([init/2]).
init(Req, Opts) ->
init(Req0, Opts) ->
BigBody =
<<"A cowboy is an animal herder who tends cattle on ranches in North America,
traditionally on horseback, and often performs a multitude of other ranch-
@ -19,5 +19,5 @@ have established the ability to work at virtually identical tasks and obtained
considerable respect for their achievements. There are also cattle handlers
in many other parts of the world, particularly South America and Australia,
who perform work similar to the cowboy in their respective nations.\n">>,
Req2 = cowboy_req:reply(200, [], BigBody, Req),
{ok, Req2, Opts}.
Req = cowboy_req:reply(200, [], BigBody, Req0),
{ok, Req, Opts}.

View file

@ -5,17 +5,17 @@
-export([init/2]).
init(Req, Opts) ->
init(Req0, Opts) ->
NewValue = integer_to_list(rand:uniform(1000000)),
Req2 = cowboy_req:set_resp_cookie(<<"server">>, NewValue,
#{path => <<"/">>}, Req),
Req1 = cowboy_req:set_resp_cookie(<<"server">>, NewValue,
#{path => <<"/">>}, Req0),
#{client := ClientCookie, server := ServerCookie}
= cowboy_req:match_cookies([{client, [], <<>>}, {server, [], <<>>}], Req2),
{ok, Body} = toppage_dtl:render([
{client, ClientCookie},
{server, ServerCookie}
]),
cowboy_req:reply(200, #{
Req = cowboy_req:reply(200, #{
<<"content-type">> => <<"text/html">>
}, Body, Req2),
{ok, Req2, Opts}.
}, Body, Req1),
{ok, Req1, Opts}.

View file

@ -5,10 +5,10 @@
-export([init/2]).
init(Req, Opts) ->
Method = cowboy_req:method(Req),
#{echo := Echo} = cowboy_req:match_qs([echo], Req),
echo(Method, Echo, Req),
init(Req0, Opts) ->
Method = cowboy_req:method(Req0),
#{echo := Echo} = cowboy_req:match_qs([echo], Req0),
Req = echo(Method, Echo, Req0),
{ok, Req, Opts}.
echo(<<"GET">>, undefined, Req) ->

View file

@ -5,24 +5,21 @@
-export([init/2]).
init(Req, Opts) ->
Method = cowboy_req:method(Req),
HasBody = cowboy_req:has_body(Req),
Req2 = maybe_echo(Method, HasBody, Req),
{ok, Req2, Opts}.
init(Req0, Opts) ->
Method = cowboy_req:method(Req0),
HasBody = cowboy_req:has_body(Req0),
Req = maybe_echo(Method, HasBody, Req0),
{ok, Req, Opts}.
maybe_echo(<<"POST">>, true, Req) ->
{ok, PostVals, Req2} = cowboy_req:read_urlencoded_body(Req),
maybe_echo(<<"POST">>, true, Req0) ->
{ok, PostVals, Req} = cowboy_req:read_urlencoded_body(Req0),
Echo = proplists:get_value(<<"echo">>, PostVals),
echo(Echo, Req2),
Req2;
echo(Echo, Req);
maybe_echo(<<"POST">>, false, Req) ->
cowboy_req:reply(400, [], <<"Missing body.">>, Req),
Req;
cowboy_req:reply(400, [], <<"Missing body.">>, Req);
maybe_echo(_, _, Req) ->
%% Method not allowed.
cowboy_req:reply(405, Req),
Req.
cowboy_req:reply(405, Req).
echo(undefined, Req) ->
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);

View file

@ -6,10 +6,10 @@
-export([init/2]).
-export([info/3]).
init(Req, Opts) ->
cowboy_req:stream_reply(200, #{
init(Req0, Opts) ->
Req = cowboy_req:stream_reply(200, #{
<<"content-type">> => <<"text/event-stream">>
}, Req),
}, Req0),
erlang:send_after(1000, self(), {message, "Tick"}),
{cowboy_loop, Req, Opts, 5000}.

View file

@ -5,8 +5,8 @@
-export([init/2]).
init(Req, Opts) ->
cowboy_req:reply(200, #{
init(Req0, Opts) ->
Req = cowboy_req:reply(200, #{
<<"content-type">> => <<"text/plain">>
}, <<"Hello world!">>, Req),
}, <<"Hello world!">>, Req0),
{ok, Req, Opts}.

View file

@ -5,8 +5,8 @@
-export([init/2]).
init(Req, Opts) ->
cowboy_req:reply(200, #{
init(Req0, Opts) ->
Req = cowboy_req:reply(200, #{
<<"content-type">> => <<"text/plain">>
}, <<"Hello world!">>, Req),
}, <<"Hello world!">>, Req0),
{ok, Req, Opts}.