2016-08-10 11:49:31 +02:00
|
|
|
%% This module echoes back the value the test is interested in.
|
|
|
|
|
|
|
|
-module(resp_h).
|
|
|
|
|
2018-10-31 10:50:57 +01:00
|
|
|
%% @todo Probably should have a separate handler for errors,
|
|
|
|
%% so that we can dialyze all the other correct calls.
|
|
|
|
-dialyzer({nowarn_function, do/3}).
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
-export([init/2]).
|
|
|
|
|
|
|
|
init(Req, Opts) ->
|
|
|
|
do(cowboy_req:binding(key, Req), Req, Opts).
|
|
|
|
|
|
|
|
do(<<"set_resp_cookie3">>, Req0, Opts) ->
|
|
|
|
Req = case cowboy_req:binding(arg, Req0) of
|
|
|
|
undefined ->
|
|
|
|
cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0);
|
|
|
|
<<"multiple">> ->
|
|
|
|
Req1 = cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0),
|
|
|
|
cowboy_req:set_resp_cookie(<<"yourcookie">>, <<"yourvalue">>, Req1);
|
|
|
|
<<"overwrite">> ->
|
|
|
|
Req1 = cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0),
|
|
|
|
cowboy_req:set_resp_cookie(<<"mycookie">>, <<"overwrite">>, Req1)
|
|
|
|
end,
|
2016-08-10 17:15:02 +02:00
|
|
|
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
|
2016-08-10 11:49:31 +02:00
|
|
|
do(<<"set_resp_cookie4">>, Req0, Opts) ->
|
2017-02-19 09:46:11 +01:00
|
|
|
Req = cowboy_req:set_resp_cookie(<<"mycookie">>, "myvalue", Req0,
|
|
|
|
#{path => cowboy_req:path(Req0)}),
|
2016-08-10 17:15:02 +02:00
|
|
|
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
|
2016-08-10 11:49:31 +02:00
|
|
|
do(<<"set_resp_header">>, Req0, Opts) ->
|
|
|
|
Req = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0),
|
2016-08-10 17:15:02 +02:00
|
|
|
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
|
2018-11-21 10:47:31 +01:00
|
|
|
do(<<"set_resp_header_server">>, Req0, Opts) ->
|
|
|
|
Req = cowboy_req:set_resp_header(<<"server">>, <<"nginx">>, Req0),
|
|
|
|
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
|
2016-11-13 15:39:40 +01:00
|
|
|
do(<<"set_resp_headers">>, Req0, Opts) ->
|
2017-01-04 19:45:35 +01:00
|
|
|
Req = cowboy_req:set_resp_headers(#{
|
|
|
|
<<"content-type">> => <<"text/plain">>,
|
2017-01-22 10:50:39 +01:00
|
|
|
<<"content-encoding">> => <<"compress">>
|
2017-01-04 19:45:35 +01:00
|
|
|
}, Req0),
|
2016-11-13 15:39:40 +01:00
|
|
|
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
|
|
|
|
do(<<"resp_header_defined">>, Req0, Opts) ->
|
2017-01-04 19:45:35 +01:00
|
|
|
Req1 = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0),
|
|
|
|
<<"text/plain">> = cowboy_req:resp_header(<<"content-type">>, Req1),
|
|
|
|
<<"text/plain">> = cowboy_req:resp_header(<<"content-type">>, Req1, default),
|
2016-11-13 15:39:40 +01:00
|
|
|
{ok, cowboy_req:reply(200, #{}, "OK", Req0), Opts};
|
2017-01-04 19:45:35 +01:00
|
|
|
do(<<"resp_header_default">>, Req, Opts) ->
|
|
|
|
undefined = cowboy_req:resp_header(<<"content-type">>, Req),
|
|
|
|
default = cowboy_req:resp_header(<<"content-type">>, Req, default),
|
|
|
|
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
|
|
|
|
do(<<"resp_headers">>, Req0, Opts) ->
|
|
|
|
Req1 = cowboy_req:set_resp_header(<<"server">>, <<"nginx">>, Req0),
|
|
|
|
Req = cowboy_req:set_resp_headers(#{
|
|
|
|
<<"content-type">> => <<"text/plain">>,
|
2017-01-22 10:50:39 +01:00
|
|
|
<<"content-encoding">> => <<"compress">>
|
2017-01-04 19:45:35 +01:00
|
|
|
}, Req1),
|
|
|
|
Headers = cowboy_req:resp_headers(Req),
|
|
|
|
true = maps:is_key(<<"server">>, Headers),
|
|
|
|
true = maps:is_key(<<"content-type">>, Headers),
|
|
|
|
true = maps:is_key(<<"content-encoding">>, Headers),
|
|
|
|
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
|
|
|
|
do(<<"resp_headers_empty">>, Req, Opts) ->
|
|
|
|
#{} = cowboy_req:resp_headers(Req),
|
|
|
|
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
|
2016-08-10 11:49:31 +02:00
|
|
|
do(<<"set_resp_body">>, Req0, Opts) ->
|
|
|
|
Arg = cowboy_req:binding(arg, Req0),
|
2016-08-10 17:15:02 +02:00
|
|
|
Req1 = case Arg of
|
2017-09-04 20:48:07 +02:00
|
|
|
<<"sendfile0">> ->
|
|
|
|
AppFile = code:where_is_file("cowboy.app"),
|
|
|
|
cowboy_req:set_resp_body({sendfile, 0, 0, AppFile}, Req0);
|
2016-08-10 11:49:31 +02:00
|
|
|
<<"sendfile">> ->
|
|
|
|
AppFile = code:where_is_file("cowboy.app"),
|
|
|
|
cowboy_req:set_resp_body({sendfile, 0, filelib:file_size(AppFile), AppFile}, Req0);
|
|
|
|
_ ->
|
|
|
|
cowboy_req:set_resp_body(<<"OK">>, Req0)
|
|
|
|
end,
|
2016-08-10 17:15:02 +02:00
|
|
|
Req = case Arg of
|
2016-08-10 11:49:31 +02:00
|
|
|
<<"override">> ->
|
2016-08-10 17:15:02 +02:00
|
|
|
cowboy_req:reply(200, #{}, <<"OVERRIDE">>, Req1);
|
2016-08-10 11:49:31 +02:00
|
|
|
_ ->
|
2016-08-10 17:15:02 +02:00
|
|
|
cowboy_req:reply(200, Req1)
|
2016-08-10 11:49:31 +02:00
|
|
|
end,
|
|
|
|
{ok, Req, Opts};
|
|
|
|
do(<<"has_resp_header">>, Req0, Opts) ->
|
|
|
|
false = cowboy_req:has_resp_header(<<"content-type">>, Req0),
|
|
|
|
Req = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0),
|
|
|
|
true = cowboy_req:has_resp_header(<<"content-type">>, Req),
|
2016-08-10 17:15:02 +02:00
|
|
|
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
|
2016-08-10 11:49:31 +02:00
|
|
|
do(<<"has_resp_body">>, Req0, Opts) ->
|
|
|
|
case cowboy_req:binding(arg, Req0) of
|
|
|
|
<<"sendfile">> ->
|
|
|
|
%% @todo Cases for sendfile. Note that sendfile 0 is unallowed.
|
|
|
|
false = cowboy_req:has_resp_body(Req0),
|
|
|
|
Req = cowboy_req:set_resp_body({sendfile, 0, 10, code:where_is_file("cowboy.app")}, Req0),
|
|
|
|
true = cowboy_req:has_resp_body(Req),
|
2016-08-10 17:15:02 +02:00
|
|
|
{ok, cowboy_req:reply(200, #{}, <<"OK">>, Req), Opts};
|
2016-08-10 11:49:31 +02:00
|
|
|
undefined ->
|
|
|
|
false = cowboy_req:has_resp_body(Req0),
|
|
|
|
Req = cowboy_req:set_resp_body(<<"OK">>, Req0),
|
|
|
|
true = cowboy_req:has_resp_body(Req),
|
2016-08-10 17:15:02 +02:00
|
|
|
{ok, cowboy_req:reply(200, #{}, Req), Opts}
|
2016-08-10 11:49:31 +02:00
|
|
|
end;
|
|
|
|
do(<<"delete_resp_header">>, Req0, Opts) ->
|
2018-01-22 14:00:05 +01:00
|
|
|
%% We try to delete first even though it hasn't been set to
|
|
|
|
%% make sure this noop is possible.
|
|
|
|
Req1 = cowboy_req:delete_resp_header(<<"content-type">>, Req0),
|
|
|
|
false = cowboy_req:has_resp_header(<<"content-type">>, Req1),
|
|
|
|
Req2 = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req1),
|
|
|
|
true = cowboy_req:has_resp_header(<<"content-type">>, Req2),
|
|
|
|
Req = cowboy_req:delete_resp_header(<<"content-type">>, Req2),
|
2016-08-10 11:49:31 +02:00
|
|
|
false = cowboy_req:has_resp_header(<<"content-type">>, Req),
|
2016-08-10 17:15:02 +02:00
|
|
|
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
|
2017-10-29 19:52:27 +00:00
|
|
|
do(<<"inform2">>, Req0, Opts) ->
|
|
|
|
case cowboy_req:binding(arg, Req0) of
|
|
|
|
<<"binary">> ->
|
|
|
|
cowboy_req:inform(<<"102 On my way">>, Req0);
|
|
|
|
<<"error">> ->
|
|
|
|
ct_helper:ignore(cowboy_req, inform, 3),
|
|
|
|
cowboy_req:inform(ok, Req0);
|
|
|
|
<<"twice">> ->
|
|
|
|
cowboy_req:inform(102, Req0),
|
|
|
|
cowboy_req:inform(102, Req0);
|
|
|
|
Status ->
|
|
|
|
cowboy_req:inform(binary_to_integer(Status), Req0)
|
|
|
|
end,
|
|
|
|
Req = cowboy_req:reply(200, Req0),
|
|
|
|
{ok, Req, Opts};
|
|
|
|
do(<<"inform3">>, Req0, Opts) ->
|
|
|
|
Headers = #{<<"ext-header">> => <<"ext-value">>},
|
|
|
|
case cowboy_req:binding(arg, Req0) of
|
|
|
|
<<"binary">> ->
|
|
|
|
cowboy_req:inform(<<"102 On my way">>, Headers, Req0);
|
|
|
|
<<"error">> ->
|
|
|
|
ct_helper:ignore(cowboy_req, inform, 3),
|
|
|
|
cowboy_req:inform(ok, Headers, Req0);
|
|
|
|
<<"twice">> ->
|
|
|
|
cowboy_req:inform(102, Headers, Req0),
|
|
|
|
cowboy_req:inform(102, Headers, Req0);
|
|
|
|
Status ->
|
|
|
|
cowboy_req:inform(binary_to_integer(Status), Headers, Req0)
|
|
|
|
end,
|
|
|
|
Req = cowboy_req:reply(200, Req0),
|
|
|
|
{ok, Req, Opts};
|
2016-08-10 17:15:02 +02:00
|
|
|
do(<<"reply2">>, Req0, Opts) ->
|
|
|
|
Req = case cowboy_req:binding(arg, Req0) of
|
2016-08-10 11:49:31 +02:00
|
|
|
<<"binary">> ->
|
2016-08-10 17:15:02 +02:00
|
|
|
cowboy_req:reply(<<"200 GOOD">>, Req0);
|
2016-08-10 11:49:31 +02:00
|
|
|
<<"error">> ->
|
|
|
|
ct_helper:ignore(cowboy_req, reply, 4),
|
2016-08-10 17:15:02 +02:00
|
|
|
cowboy_req:reply(ok, Req0);
|
2016-08-10 11:49:31 +02:00
|
|
|
<<"twice">> ->
|
2016-08-10 17:15:02 +02:00
|
|
|
ct_helper:ignore(cowboy_req, reply, 4),
|
|
|
|
Req1 = cowboy_req:reply(200, Req0),
|
|
|
|
cowboy_req:reply(200, Req1);
|
2016-08-10 11:49:31 +02:00
|
|
|
Status ->
|
2016-08-10 17:15:02 +02:00
|
|
|
cowboy_req:reply(binary_to_integer(Status), Req0)
|
2016-08-10 11:49:31 +02:00
|
|
|
end,
|
|
|
|
{ok, Req, Opts};
|
2016-08-10 17:15:02 +02:00
|
|
|
do(<<"reply3">>, Req0, Opts) ->
|
|
|
|
Req = case cowboy_req:binding(arg, Req0) of
|
2016-08-10 11:49:31 +02:00
|
|
|
<<"error">> ->
|
|
|
|
ct_helper:ignore(cowboy_req, reply, 4),
|
2016-08-10 17:15:02 +02:00
|
|
|
cowboy_req:reply(200, ok, Req0);
|
2016-08-10 11:49:31 +02:00
|
|
|
Status ->
|
|
|
|
cowboy_req:reply(binary_to_integer(Status),
|
2016-08-10 17:15:02 +02:00
|
|
|
#{<<"content-type">> => <<"text/plain">>}, Req0)
|
2016-08-10 11:49:31 +02:00
|
|
|
end,
|
|
|
|
{ok, Req, Opts};
|
2016-08-10 17:15:02 +02:00
|
|
|
do(<<"reply4">>, Req0, Opts) ->
|
|
|
|
Req = case cowboy_req:binding(arg, Req0) of
|
2016-08-10 11:49:31 +02:00
|
|
|
<<"error">> ->
|
|
|
|
ct_helper:ignore(erlang, iolist_size, 1),
|
2016-08-10 17:15:02 +02:00
|
|
|
cowboy_req:reply(200, #{}, ok, Req0);
|
2016-08-10 11:49:31 +02:00
|
|
|
Status ->
|
2016-08-10 17:15:02 +02:00
|
|
|
cowboy_req:reply(binary_to_integer(Status), #{}, <<"OK">>, Req0)
|
2016-08-10 11:49:31 +02:00
|
|
|
end,
|
|
|
|
{ok, Req, Opts};
|
2016-08-10 17:15:02 +02:00
|
|
|
do(<<"stream_reply2">>, Req0, Opts) ->
|
2018-05-18 18:03:56 +02:00
|
|
|
case cowboy_req:binding(arg, Req0) of
|
2016-08-10 11:49:31 +02:00
|
|
|
<<"binary">> ->
|
2018-05-18 18:03:56 +02:00
|
|
|
Req = cowboy_req:stream_reply(<<"200 GOOD">>, Req0),
|
|
|
|
stream_body(Req),
|
|
|
|
{ok, Req, Opts};
|
2016-08-10 11:49:31 +02:00
|
|
|
<<"error">> ->
|
|
|
|
ct_helper:ignore(cowboy_req, stream_reply, 3),
|
2018-05-18 18:03:56 +02:00
|
|
|
Req = cowboy_req:stream_reply(ok, Req0),
|
|
|
|
stream_body(Req),
|
|
|
|
{ok, Req, Opts};
|
|
|
|
<<"204">> ->
|
|
|
|
Req = cowboy_req:stream_reply(204, Req0),
|
|
|
|
{ok, Req, Opts};
|
2018-11-14 19:24:39 +01:00
|
|
|
<<"304">> ->
|
|
|
|
Req = cowboy_req:stream_reply(304, Req0),
|
|
|
|
{ok, Req, Opts};
|
2016-08-10 11:49:31 +02:00
|
|
|
Status ->
|
2018-05-18 18:03:56 +02:00
|
|
|
Req = cowboy_req:stream_reply(binary_to_integer(Status), Req0),
|
|
|
|
stream_body(Req),
|
|
|
|
{ok, Req, Opts}
|
|
|
|
end;
|
2016-08-10 17:15:02 +02:00
|
|
|
do(<<"stream_reply3">>, Req0, Opts) ->
|
|
|
|
Req = case cowboy_req:binding(arg, Req0) of
|
2016-08-10 11:49:31 +02:00
|
|
|
<<"error">> ->
|
|
|
|
ct_helper:ignore(cowboy_req, stream_reply, 3),
|
2016-08-10 17:15:02 +02:00
|
|
|
cowboy_req:stream_reply(200, ok, Req0);
|
2016-08-10 11:49:31 +02:00
|
|
|
Status ->
|
|
|
|
cowboy_req:stream_reply(binary_to_integer(Status),
|
2016-08-10 17:15:02 +02:00
|
|
|
#{<<"content-type">> => <<"text/plain">>}, Req0)
|
2016-08-10 11:49:31 +02:00
|
|
|
end,
|
|
|
|
stream_body(Req),
|
|
|
|
{ok, Req, Opts};
|
2017-11-01 15:33:10 +00:00
|
|
|
do(<<"stream_body">>, Req0, Opts) ->
|
|
|
|
case cowboy_req:binding(arg, Req0) of
|
|
|
|
<<"fin0">> ->
|
|
|
|
Req = cowboy_req:stream_reply(200, Req0),
|
|
|
|
cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
|
|
|
|
cowboy_req:stream_body(<<>>, fin, Req),
|
|
|
|
{ok, Req, Opts};
|
2017-11-19 22:17:57 +01:00
|
|
|
<<"multiple">> ->
|
|
|
|
Req = cowboy_req:stream_reply(200, Req0),
|
|
|
|
cowboy_req:stream_body(<<"Hello ">>, nofin, Req),
|
|
|
|
cowboy_req:stream_body(<<"world">>, nofin, Req),
|
|
|
|
cowboy_req:stream_body(<<"!">>, fin, Req),
|
|
|
|
{ok, Req, Opts};
|
2017-11-01 15:33:10 +00:00
|
|
|
<<"nofin">> ->
|
|
|
|
Req = cowboy_req:stream_reply(200, Req0),
|
|
|
|
cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
|
|
|
|
{ok, Req, Opts};
|
2018-11-09 17:42:37 +01:00
|
|
|
<<"sendfile">> ->
|
|
|
|
AppFile = code:where_is_file("cowboy.app"),
|
|
|
|
AppSize = filelib:file_size(AppFile),
|
|
|
|
Req = cowboy_req:stream_reply(200, Req0),
|
|
|
|
cowboy_req:stream_body(<<"Hello ">>, nofin, Req),
|
|
|
|
cowboy_req:stream_body({sendfile, 0, AppSize, AppFile}, nofin, Req),
|
|
|
|
cowboy_req:stream_body(<<" interspersed ">>, nofin, Req),
|
|
|
|
cowboy_req:stream_body({sendfile, 0, AppSize, AppFile}, nofin, Req),
|
|
|
|
cowboy_req:stream_body(<<" world!">>, fin, Req),
|
|
|
|
{ok, Req, Opts};
|
|
|
|
<<"sendfile_fin">> ->
|
|
|
|
AppFile = code:where_is_file("cowboy.app"),
|
|
|
|
AppSize = filelib:file_size(AppFile),
|
|
|
|
Req = cowboy_req:stream_reply(200, Req0),
|
|
|
|
cowboy_req:stream_body(<<"Hello! ">>, nofin, Req),
|
|
|
|
cowboy_req:stream_body({sendfile, 0, AppSize, AppFile}, fin, Req),
|
|
|
|
{ok, Req, Opts};
|
2017-11-01 15:33:10 +00:00
|
|
|
_ ->
|
|
|
|
%% Call stream_body without initiating streaming.
|
|
|
|
cowboy_req:stream_body(<<0:800000>>, fin, Req0),
|
|
|
|
{ok, Req0, Opts}
|
|
|
|
end;
|
2018-05-16 13:28:49 +02:00
|
|
|
do(<<"stream_body_content_length">>, Req0, Opts) ->
|
|
|
|
case cowboy_req:binding(arg, Req0) of
|
|
|
|
<<"fin0">> ->
|
|
|
|
Req1 = cowboy_req:set_resp_header(<<"content-length">>, <<"12">>, Req0),
|
|
|
|
Req = cowboy_req:stream_reply(200, Req1),
|
|
|
|
cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
|
|
|
|
cowboy_req:stream_body(<<>>, fin, Req),
|
|
|
|
{ok, Req, Opts};
|
|
|
|
<<"multiple">> ->
|
|
|
|
Req1 = cowboy_req:set_resp_header(<<"content-length">>, <<"12">>, Req0),
|
|
|
|
Req = cowboy_req:stream_reply(200, Req1),
|
|
|
|
cowboy_req:stream_body(<<"Hello ">>, nofin, Req),
|
|
|
|
cowboy_req:stream_body(<<"world">>, nofin, Req),
|
|
|
|
cowboy_req:stream_body(<<"!">>, fin, Req),
|
|
|
|
{ok, Req, Opts};
|
|
|
|
<<"nofin">> ->
|
|
|
|
Req1 = cowboy_req:set_resp_header(<<"content-length">>, <<"12">>, Req0),
|
|
|
|
Req = cowboy_req:stream_reply(200, Req1),
|
|
|
|
cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
|
|
|
|
{ok, Req, Opts};
|
|
|
|
<<"nofin-error">> ->
|
|
|
|
Req1 = cowboy_req:set_resp_header(<<"content-length">>, <<"12">>, Req0),
|
|
|
|
Req = cowboy_req:stream_reply(200, Req1),
|
|
|
|
cowboy_req:stream_body(<<"Hello">>, nofin, Req),
|
|
|
|
{ok, Req, Opts}
|
|
|
|
end;
|
2018-06-27 13:40:11 +02:00
|
|
|
do(<<"stream_events">>, Req0, Opts) ->
|
|
|
|
case cowboy_req:binding(arg, Req0) of
|
|
|
|
%%<<"single">>
|
|
|
|
%%<<"list">>
|
|
|
|
<<"single">> ->
|
|
|
|
Req = cowboy_req:stream_reply(200,
|
|
|
|
#{<<"content-type">> => <<"text/event-stream">>},
|
|
|
|
Req0),
|
|
|
|
cowboy_req:stream_events(#{
|
|
|
|
event => <<"add_comment">>,
|
|
|
|
data => <<"Comment text.\nWith many lines.">>
|
|
|
|
}, fin, Req),
|
|
|
|
{ok, Req, Opts};
|
|
|
|
<<"list">> ->
|
|
|
|
Req = cowboy_req:stream_reply(200,
|
|
|
|
#{<<"content-type">> => <<"text/event-stream">>},
|
|
|
|
Req0),
|
|
|
|
cowboy_req:stream_events([
|
|
|
|
#{
|
|
|
|
event => <<"add_comment">>,
|
|
|
|
data => <<"Comment text.\nWith many lines.">>
|
|
|
|
},
|
|
|
|
#{
|
|
|
|
comment => <<"Set retry higher\nwith many lines also.">>,
|
|
|
|
retry => 10000
|
|
|
|
},
|
|
|
|
#{
|
|
|
|
id => <<"123">>,
|
|
|
|
event => <<"add_comment">>,
|
|
|
|
data => <<"Closing!">>
|
|
|
|
}
|
|
|
|
], fin, Req),
|
|
|
|
{ok, Req, Opts};
|
|
|
|
<<"multiple">> ->
|
|
|
|
Req = cowboy_req:stream_reply(200,
|
|
|
|
#{<<"content-type">> => <<"text/event-stream">>},
|
|
|
|
Req0),
|
|
|
|
cowboy_req:stream_events(#{
|
|
|
|
event => <<"add_comment">>,
|
|
|
|
data => <<"Comment text.\nWith many lines.">>
|
|
|
|
}, nofin, Req),
|
|
|
|
cowboy_req:stream_events(#{
|
|
|
|
comment => <<"Set retry higher\nwith many lines also.">>,
|
|
|
|
retry => 10000
|
|
|
|
}, nofin, Req),
|
|
|
|
cowboy_req:stream_events(#{
|
|
|
|
id => <<"123">>,
|
|
|
|
event => <<"add_comment">>,
|
|
|
|
data => <<"Closing!">>
|
|
|
|
}, fin, Req),
|
|
|
|
{ok, Req, Opts}
|
|
|
|
end;
|
2017-11-15 14:58:49 +01:00
|
|
|
do(<<"stream_trailers">>, Req0, Opts) ->
|
|
|
|
case cowboy_req:binding(arg, Req0) of
|
2017-11-20 15:46:23 +01:00
|
|
|
<<"large">> ->
|
|
|
|
Req = cowboy_req:stream_reply(200, #{
|
|
|
|
<<"trailer">> => <<"grpc-status">>
|
|
|
|
}, Req0),
|
|
|
|
cowboy_req:stream_body(<<0:800000>>, nofin, Req),
|
|
|
|
cowboy_req:stream_trailers(#{
|
|
|
|
<<"grpc-status">> => <<"0">>
|
|
|
|
}, Req),
|
|
|
|
{ok, Req, Opts};
|
2017-11-15 14:58:49 +01:00
|
|
|
_ ->
|
|
|
|
Req = cowboy_req:stream_reply(200, #{
|
|
|
|
<<"trailer">> => <<"grpc-status">>
|
|
|
|
}, Req0),
|
|
|
|
cowboy_req:stream_body(<<"Hello world!">>, nofin, Req),
|
|
|
|
cowboy_req:stream_trailers(#{
|
|
|
|
<<"grpc-status">> => <<"0">>
|
|
|
|
}, Req),
|
|
|
|
{ok, Req, Opts}
|
|
|
|
end;
|
2016-08-10 11:49:31 +02:00
|
|
|
do(<<"push">>, Req, Opts) ->
|
|
|
|
case cowboy_req:binding(arg, Req) of
|
|
|
|
<<"method">> ->
|
|
|
|
cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req,
|
|
|
|
#{method => <<"HEAD">>});
|
|
|
|
<<"origin">> ->
|
|
|
|
cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req,
|
|
|
|
#{scheme => <<"ftp">>, host => <<"127.0.0.1">>, port => 21});
|
|
|
|
<<"qs">> ->
|
|
|
|
cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req,
|
|
|
|
#{qs => <<"server=cowboy&version=2.0">>});
|
|
|
|
_ ->
|
|
|
|
cowboy_req:push("/static/style.css", #{<<"accept">> => <<"text/css">>}, Req),
|
|
|
|
%% The text/plain mime is not defined by default, so a 406 will be returned.
|
|
|
|
cowboy_req:push("/static/plain.txt", #{<<"accept">> => <<"text/plain">>}, Req)
|
|
|
|
end,
|
2016-08-10 17:15:02 +02:00
|
|
|
{ok, cowboy_req:reply(200, Req), Opts}.
|
2016-08-10 11:49:31 +02:00
|
|
|
|
|
|
|
stream_body(Req) ->
|
|
|
|
_ = [cowboy_req:stream_body(<<0:800000>>, nofin, Req) || _ <- lists:seq(1,9)],
|
|
|
|
cowboy_req:stream_body(<<0:800000>>, fin, Req).
|