mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Merge branch 'master' of https://github.com/bfrog/cowboy
This commit is contained in:
commit
dd08a90568
2 changed files with 71 additions and 3 deletions
|
@ -29,7 +29,7 @@
|
||||||
file_200/1, file_403/1, dir_403/1, file_404/1,
|
file_200/1, file_403/1, dir_403/1, file_404/1,
|
||||||
file_400/1]). %% http and https.
|
file_400/1]). %% http and https.
|
||||||
-export([http_10_hostless/1]). %% misc.
|
-export([http_10_hostless/1]). %% misc.
|
||||||
-export([rest_simple/1, rest_keepalive/1]). %% rest.
|
-export([rest_simple/1, rest_keepalive/1, rest_keepalive_post/1]). %% rest.
|
||||||
|
|
||||||
%% ct.
|
%% ct.
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ groups() ->
|
||||||
static_function_etag, multipart] ++ BaseTests},
|
static_function_etag, multipart] ++ BaseTests},
|
||||||
{https, [], BaseTests},
|
{https, [], BaseTests},
|
||||||
{misc, [], [http_10_hostless]},
|
{misc, [], [http_10_hostless]},
|
||||||
{rest, [], [rest_simple, rest_keepalive]}].
|
{rest, [], [rest_simple, rest_keepalive, rest_keepalive_post]}].
|
||||||
|
|
||||||
init_per_suite(Config) ->
|
init_per_suite(Config) ->
|
||||||
application:start(inets),
|
application:start(inets),
|
||||||
|
@ -95,7 +95,9 @@ init_per_group(rest, Config) ->
|
||||||
cowboy:start_listener(reset, 100,
|
cowboy:start_listener(reset, 100,
|
||||||
cowboy_tcp_transport, [{port, Port}],
|
cowboy_tcp_transport, [{port, Port}],
|
||||||
cowboy_http_protocol, [{dispatch, [{'_', [
|
cowboy_http_protocol, [{dispatch, [{'_', [
|
||||||
{[<<"simple">>], rest_simple_resource, []}
|
{[<<"simple">>], rest_simple_resource, []},
|
||||||
|
{[<<"forbidden_post">>], rest_forbidden_resource, [true]},
|
||||||
|
{[<<"simple_post">>], rest_forbidden_resource, [false]}
|
||||||
]}]}]),
|
]}]}]),
|
||||||
[{port, Port}|Config].
|
[{port, Port}|Config].
|
||||||
|
|
||||||
|
@ -583,3 +585,29 @@ rest_keepalive_loop(Socket, N) ->
|
||||||
{0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
|
{0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
|
||||||
nomatch = binary:match(Data, <<"Connection: close">>),
|
nomatch = binary:match(Data, <<"Connection: close">>),
|
||||||
rest_keepalive_loop(Socket, N - 1).
|
rest_keepalive_loop(Socket, N - 1).
|
||||||
|
|
||||||
|
rest_keepalive_post(Config) ->
|
||||||
|
{port, Port} = lists:keyfind(port, 1, Config),
|
||||||
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
||||||
|
[binary, {active, false}, {packet, raw}]),
|
||||||
|
ok = rest_keepalive_post_loop(Socket, 10, forbidden_post),
|
||||||
|
ok = gen_tcp:close(Socket).
|
||||||
|
|
||||||
|
rest_keepalive_post_loop(_Socket, 0, _) ->
|
||||||
|
ok;
|
||||||
|
rest_keepalive_post_loop(Socket, N, simple_post) ->
|
||||||
|
ok = gen_tcp:send(Socket, "POST /simple_post HTTP/1.1\r\n"
|
||||||
|
"Host: localhost\r\nConnection: keep-alive\r\n"
|
||||||
|
"Content-Length: 5\r\nContent-Type: text/plain\r\n\r\n12345"),
|
||||||
|
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
|
||||||
|
{0, 12} = binary:match(Data, <<"HTTP/1.1 303">>),
|
||||||
|
nomatch = binary:match(Data, <<"Connection: close">>),
|
||||||
|
rest_keepalive_post_loop(Socket, N - 1, forbidden_post);
|
||||||
|
rest_keepalive_post_loop(Socket, N, forbidden_post) ->
|
||||||
|
ok = gen_tcp:send(Socket, "POST /forbidden_post HTTP/1.1\r\n"
|
||||||
|
"Host: localhost\r\nConnection: keep-alive\r\n"
|
||||||
|
"Content-Length: 5\r\nContent-Type: text/plain\r\n\r\n12345"),
|
||||||
|
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
|
||||||
|
{0, 12} = binary:match(Data, <<"HTTP/1.1 403">>),
|
||||||
|
nomatch = binary:match(Data, <<"Connection: close">>),
|
||||||
|
rest_keepalive_post_loop(Socket, N - 1, simple_post).
|
||||||
|
|
40
test/rest_forbidden_resource.erl
Normal file
40
test/rest_forbidden_resource.erl
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
-module(rest_forbidden_resource).
|
||||||
|
-export([init/3, rest_init/2, allowed_methods/2, forbidden/2,
|
||||||
|
content_types_provided/2, content_types_accepted/2,
|
||||||
|
post_is_create/2, create_path/2, to_text/2, from_text/2]).
|
||||||
|
|
||||||
|
init(_Transport, _Req, _Opts) ->
|
||||||
|
{upgrade, protocol, cowboy_http_rest}.
|
||||||
|
|
||||||
|
rest_init(Req, [Forbidden]) ->
|
||||||
|
{ok, Req, Forbidden}.
|
||||||
|
|
||||||
|
allowed_methods(Req, State) ->
|
||||||
|
{['GET', 'HEAD', 'POST'], Req, State}.
|
||||||
|
|
||||||
|
forbidden(Req, State=true) ->
|
||||||
|
{true, Req, State};
|
||||||
|
forbidden(Req, State=false) ->
|
||||||
|
{false, Req, State}.
|
||||||
|
|
||||||
|
content_types_provided(Req, State) ->
|
||||||
|
{[{{<<"text">>, <<"plain">>, []}, to_text}], Req, State}.
|
||||||
|
|
||||||
|
content_types_accepted(Req, State) ->
|
||||||
|
{[{{<<"text">>, <<"plain">>, []}, from_text}], Req, State}.
|
||||||
|
|
||||||
|
post_is_create(Req, State) ->
|
||||||
|
{true, Req, State}.
|
||||||
|
|
||||||
|
create_path(Req, State) ->
|
||||||
|
{Path, Req2} = cowboy_http_req:raw_path(Req),
|
||||||
|
{Path, Req2, State}.
|
||||||
|
|
||||||
|
to_text(Req, State) ->
|
||||||
|
{<<"This is REST!">>, Req, State}.
|
||||||
|
|
||||||
|
from_text(Req, State) ->
|
||||||
|
{true, Req, State}.
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue