2011-04-08 16:30:37 +02:00
|
|
|
%% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.eu>
|
|
|
|
%%
|
|
|
|
%% Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
%% purpose with or without fee is hereby granted, provided that the above
|
|
|
|
%% copyright notice and this permission notice appear in all copies.
|
|
|
|
%%
|
|
|
|
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
-module(http_SUITE).
|
|
|
|
|
|
|
|
-include_lib("common_test/include/ct.hrl").
|
|
|
|
|
|
|
|
-export([all/0, groups/0, init_per_suite/1, end_per_suite/1,
|
|
|
|
init_per_group/2, end_per_group/2]). %% ct.
|
2011-05-08 17:26:21 +02:00
|
|
|
-export([chunked_response/1, headers_dupe/1, headers_huge/1,
|
2011-10-19 20:35:55 +02:00
|
|
|
keepalive_nl/1, nc_rand/1, nc_zero/1, pipeline/1, raw/1,
|
2011-10-10 09:09:15 +02:00
|
|
|
ws0/1, ws8/1, ws8_single_bytes/1, ws8_init_shutdown/1,
|
2011-10-24 15:06:51 +01:00
|
|
|
ws13/1, ws_timeout_hibernate/1]). %% http.
|
2011-08-23 16:24:02 +02:00
|
|
|
-export([http_200/1, http_404/1]). %% http and https.
|
2011-09-14 01:41:03 +02:00
|
|
|
-export([http_10_hostless/1]). %% misc.
|
2011-04-08 16:30:37 +02:00
|
|
|
|
|
|
|
%% ct.
|
|
|
|
|
|
|
|
all() ->
|
2011-09-14 01:41:03 +02:00
|
|
|
[{group, http}, {group, https}, {group, misc}].
|
2011-04-08 16:30:37 +02:00
|
|
|
|
|
|
|
groups() ->
|
|
|
|
BaseTests = [http_200, http_404],
|
2011-05-08 17:26:21 +02:00
|
|
|
[{http, [], [chunked_response, headers_dupe, headers_huge,
|
2011-10-19 20:35:55 +02:00
|
|
|
keepalive_nl, nc_rand, nc_zero, pipeline, raw,
|
2011-10-24 15:06:51 +01:00
|
|
|
ws0, ws8, ws8_single_bytes, ws8_init_shutdown, ws13,
|
2011-09-30 18:05:02 +01:00
|
|
|
ws_timeout_hibernate] ++ BaseTests},
|
2011-09-14 01:41:03 +02:00
|
|
|
{https, [], BaseTests}, {misc, [], [http_10_hostless]}].
|
2011-04-08 16:30:37 +02:00
|
|
|
|
|
|
|
init_per_suite(Config) ->
|
|
|
|
application:start(inets),
|
|
|
|
application:start(cowboy),
|
|
|
|
Config.
|
|
|
|
|
|
|
|
end_per_suite(_Config) ->
|
|
|
|
application:stop(cowboy),
|
|
|
|
application:stop(inets),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
init_per_group(http, Config) ->
|
|
|
|
Port = 33080,
|
|
|
|
cowboy:start_listener(http, 100,
|
|
|
|
cowboy_tcp_transport, [{port, Port}],
|
|
|
|
cowboy_http_protocol, [{dispatch, init_http_dispatch()}]
|
|
|
|
),
|
|
|
|
[{scheme, "http"}, {port, Port}|Config];
|
|
|
|
init_per_group(https, Config) ->
|
|
|
|
Port = 33081,
|
|
|
|
application:start(crypto),
|
|
|
|
application:start(public_key),
|
|
|
|
application:start(ssl),
|
|
|
|
DataDir = ?config(data_dir, Config),
|
|
|
|
cowboy:start_listener(https, 100,
|
|
|
|
cowboy_ssl_transport, [
|
|
|
|
{port, Port}, {certfile, DataDir ++ "cert.pem"},
|
|
|
|
{keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}],
|
|
|
|
cowboy_http_protocol, [{dispatch, init_https_dispatch()}]
|
|
|
|
),
|
2011-09-14 01:41:03 +02:00
|
|
|
[{scheme, "https"}, {port, Port}|Config];
|
|
|
|
init_per_group(misc, Config) ->
|
|
|
|
Port = 33082,
|
|
|
|
cowboy:start_listener(misc, 100,
|
|
|
|
cowboy_tcp_transport, [{port, Port}],
|
|
|
|
cowboy_http_protocol, [{dispatch, [{'_', [
|
|
|
|
{[], http_handler, []}
|
|
|
|
]}]}]),
|
|
|
|
[{port, Port}|Config].
|
2011-04-08 16:30:37 +02:00
|
|
|
|
|
|
|
end_per_group(https, _Config) ->
|
|
|
|
cowboy:stop_listener(https),
|
|
|
|
application:stop(ssl),
|
|
|
|
application:stop(public_key),
|
|
|
|
application:stop(crypto),
|
2011-09-14 01:41:03 +02:00
|
|
|
ok;
|
|
|
|
end_per_group(Listener, _Config) ->
|
|
|
|
cowboy:stop_listener(Listener),
|
2011-04-08 16:30:37 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%% Dispatch configuration.
|
|
|
|
|
|
|
|
init_http_dispatch() ->
|
|
|
|
[
|
2011-05-05 14:03:39 +02:00
|
|
|
{[<<"localhost">>], [
|
2011-05-08 17:26:21 +02:00
|
|
|
{[<<"chunked_response">>], chunked_handler, []},
|
2011-05-05 14:03:39 +02:00
|
|
|
{[<<"websocket">>], websocket_handler, []},
|
2011-09-22 21:33:56 +02:00
|
|
|
{[<<"ws_timeout_hibernate">>], ws_timeout_hibernate_handler, []},
|
2011-10-10 09:09:15 +02:00
|
|
|
{[<<"ws_init_shutdown">>], websocket_handler_init_shutdown, []},
|
2011-10-06 15:54:37 +02:00
|
|
|
{[<<"init_shutdown">>], http_handler_init_shutdown, []},
|
Add support for loops in standard HTTP handlers
Now init/3 can return one of the following values to enable loops:
- {loop, Req, State}
- {loop, Req, State, hibernate}
- {loop, Req, State, Timeout}
- {loop, Req, State, Timeout, hibernate}
Returning one of these tuples will activate looping in the HTTP handler.
When looping, handle/2 is never called. Instead, Cowboy will listen
for Erlang messages and forward them to the info/3 function of the
handler. If a timeout is defined, Cowboy will also close the connection
when no message has been received for Timeout milliseconds.
The info/3 function is defined as info(Msg, Req, State). It can return
either of the following tuples:
- {ok, Req, State}
- {loop, Req, State}
- {loop, Req, State, hibernate}
The first one ends the connection, calling terminate/2 before closing.
The others continue the loop.
Loops are useful when writing long-polling handlers that need to wait
and don't expect to receive anything. Therefore it is recommended to
set a timeout to close the connection if nothing arrives after a while
and to enable hibernate everywhere.
Normal HTTP handlers shouldn't need to use this and as such info/3
was made optional.
2011-10-10 17:27:52 +02:00
|
|
|
{[<<"long_polling">>], http_handler_long_polling, []},
|
2011-05-05 14:03:39 +02:00
|
|
|
{[<<"headers">>, <<"dupe">>], http_handler,
|
|
|
|
[{headers, [{<<"Connection">>, <<"close">>}]}]},
|
2011-04-14 01:26:02 +02:00
|
|
|
{[], http_handler, []}
|
|
|
|
]}
|
2011-04-08 16:30:37 +02:00
|
|
|
].
|
|
|
|
|
|
|
|
init_https_dispatch() ->
|
|
|
|
init_http_dispatch().
|
|
|
|
|
2011-04-09 12:59:53 +02:00
|
|
|
%% http.
|
|
|
|
|
2011-05-08 17:26:21 +02:00
|
|
|
chunked_response(Config) ->
|
|
|
|
{ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "chunked_handler\r\nworks fine!"}} =
|
|
|
|
httpc:request(build_url("/chunked_response", Config)).
|
|
|
|
|
2011-04-14 01:26:02 +02:00
|
|
|
headers_dupe(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
2011-04-14 21:21:17 +02:00
|
|
|
ok = gen_tcp:send(Socket, "GET /headers/dupe HTTP/1.1\r\n"
|
|
|
|
"Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
|
2011-04-14 01:26:02 +02:00
|
|
|
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{_Start, _Length} = binary:match(Data, <<"Connection: close">>),
|
|
|
|
nomatch = binary:match(Data, <<"Connection: keep-alive">>),
|
2011-10-06 12:40:04 +02:00
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 1000).
|
2011-04-14 01:26:02 +02:00
|
|
|
|
2011-05-05 17:11:27 +02:00
|
|
|
headers_huge(Config) ->
|
|
|
|
Cookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
|
2011-10-19 20:35:55 +02:00
|
|
|
"Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _N <- lists:seq(1, 40)]),
|
2011-05-05 17:11:27 +02:00
|
|
|
{_Packet, 200} = raw_req(["GET / HTTP/1.0\r\nHost: localhost\r\n"
|
|
|
|
"Set-Cookie: ", Cookie, "\r\n\r\n"], Config).
|
|
|
|
|
2011-09-13 23:41:34 +02:00
|
|
|
keepalive_nl(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = keepalive_nl_loop(Socket, 100),
|
|
|
|
ok = gen_tcp:close(Socket).
|
|
|
|
|
|
|
|
keepalive_nl_loop(_Socket, 0) ->
|
|
|
|
ok;
|
|
|
|
keepalive_nl_loop(Socket, N) ->
|
|
|
|
ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
|
|
|
|
"Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
|
|
|
|
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
|
|
|
|
nomatch = binary:match(Data, <<"Connection: close">>),
|
|
|
|
ok = gen_tcp:send(Socket, "\r\n"), %% extra nl
|
|
|
|
keepalive_nl_loop(Socket, N - 1).
|
|
|
|
|
2011-04-17 13:36:51 +02:00
|
|
|
nc_rand(Config) ->
|
2011-10-19 20:35:55 +02:00
|
|
|
nc_reqs(Config, "/dev/urandom").
|
|
|
|
|
|
|
|
nc_zero(Config) ->
|
|
|
|
nc_reqs(Config, "/dev/zero").
|
|
|
|
|
|
|
|
nc_reqs(Config, Input) ->
|
2011-04-17 13:36:51 +02:00
|
|
|
Cat = os:find_executable("cat"),
|
|
|
|
Nc = os:find_executable("nc"),
|
|
|
|
case {Cat, Nc} of
|
|
|
|
{false, _} ->
|
|
|
|
{skip, {notfound, cat}};
|
|
|
|
{_, false} ->
|
|
|
|
{skip, {notfound, nc}};
|
|
|
|
_Good ->
|
|
|
|
%% Throw garbage at the server then check if it's still up.
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
2011-10-19 20:35:55 +02:00
|
|
|
[nc_run_req(Port, Input) || _N <- lists:seq(1, 100)],
|
2011-04-17 13:36:51 +02:00
|
|
|
Packet = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n",
|
|
|
|
{Packet, 200} = raw_req(Packet, Config)
|
|
|
|
end.
|
|
|
|
|
2011-10-19 20:35:55 +02:00
|
|
|
nc_run_req(Port, Input) ->
|
|
|
|
os:cmd("cat " ++ Input ++ " | nc localhost " ++ integer_to_list(Port)).
|
2011-04-17 13:36:51 +02:00
|
|
|
|
2011-04-10 02:43:30 +02:00
|
|
|
pipeline(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket,
|
|
|
|
"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
|
|
|
|
"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
|
|
|
|
"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
|
|
|
|
"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
|
|
|
|
"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"),
|
|
|
|
Data = pipeline_recv(Socket, <<>>),
|
|
|
|
Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]),
|
|
|
|
5 = length(Reqs),
|
|
|
|
pipeline_check(Reqs).
|
|
|
|
|
|
|
|
pipeline_check([]) ->
|
|
|
|
ok;
|
|
|
|
pipeline_check([Req|Tail]) ->
|
|
|
|
<< "HTTP/1.1 200", _Rest/bits >> = Req,
|
|
|
|
pipeline_check(Tail).
|
|
|
|
|
|
|
|
pipeline_recv(Socket, SoFar) ->
|
|
|
|
case gen_tcp:recv(Socket, 0, 6000) of
|
|
|
|
{ok, Data} ->
|
|
|
|
pipeline_recv(Socket, << SoFar/binary, Data/binary >>);
|
|
|
|
{error, closed} ->
|
|
|
|
ok = gen_tcp:close(Socket),
|
|
|
|
SoFar
|
|
|
|
end.
|
|
|
|
|
2011-04-09 12:59:53 +02:00
|
|
|
raw_req(Packet, Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, Packet),
|
2011-10-04 10:54:30 +02:00
|
|
|
Res = case gen_tcp:recv(Socket, 0, 6000) of
|
|
|
|
{ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>} ->
|
|
|
|
list_to_integer(binary_to_list(Str));
|
|
|
|
{error, Reason} ->
|
|
|
|
Reason
|
|
|
|
end,
|
2011-04-09 12:59:53 +02:00
|
|
|
gen_tcp:close(Socket),
|
2011-10-04 10:54:30 +02:00
|
|
|
{Packet, Res}.
|
2011-04-09 12:59:53 +02:00
|
|
|
|
|
|
|
raw(Config) ->
|
2011-10-19 20:35:55 +02:00
|
|
|
Huge = [$0 || _N <- lists:seq(1, 5000)],
|
2011-04-09 12:59:53 +02:00
|
|
|
Tests = [
|
|
|
|
{"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
|
2011-04-09 15:45:25 +02:00
|
|
|
{"\n", 400},
|
2011-04-09 12:59:53 +02:00
|
|
|
{"Garbage\r\n\r\n", 400},
|
2011-04-09 15:28:41 +02:00
|
|
|
{"\r\n\r\n\r\n\r\n\r\n\r\n", 400},
|
2011-04-09 12:59:53 +02:00
|
|
|
{"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
|
2011-10-04 10:54:30 +02:00
|
|
|
{"", closed},
|
|
|
|
{"\r\n", closed},
|
|
|
|
{"\r\n\r\n", closed},
|
|
|
|
{"GET / HTTP/1.1", closed},
|
2011-04-09 13:53:22 +02:00
|
|
|
{"GET / HTTP/1.1\r\n", 408},
|
|
|
|
{"GET / HTTP/1.1\r\nHost: localhost", 408},
|
|
|
|
{"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
|
|
|
|
{"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
|
2011-04-09 12:59:53 +02:00
|
|
|
{"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
|
2011-10-06 15:54:37 +02:00
|
|
|
{"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505},
|
Add support for loops in standard HTTP handlers
Now init/3 can return one of the following values to enable loops:
- {loop, Req, State}
- {loop, Req, State, hibernate}
- {loop, Req, State, Timeout}
- {loop, Req, State, Timeout, hibernate}
Returning one of these tuples will activate looping in the HTTP handler.
When looping, handle/2 is never called. Instead, Cowboy will listen
for Erlang messages and forward them to the info/3 function of the
handler. If a timeout is defined, Cowboy will also close the connection
when no message has been received for Timeout milliseconds.
The info/3 function is defined as info(Msg, Req, State). It can return
either of the following tuples:
- {ok, Req, State}
- {loop, Req, State}
- {loop, Req, State, hibernate}
The first one ends the connection, calling terminate/2 before closing.
The others continue the loop.
Loops are useful when writing long-polling handlers that need to wait
and don't expect to receive anything. Therefore it is recommended to
set a timeout to close the connection if nothing arrives after a while
and to enable hibernate everywhere.
Normal HTTP handlers shouldn't need to use this and as such info/3
was made optional.
2011-10-10 17:27:52 +02:00
|
|
|
{"GET /init_shutdown HTTP/1.1\r\nHost: localhost\r\n\r\n", 666},
|
2011-10-19 20:35:55 +02:00
|
|
|
{"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n\r\n", 102},
|
|
|
|
{Huge, 413},
|
|
|
|
{"GET / HTTP/1.1\r\n" ++ Huge, 413}
|
2011-04-09 12:59:53 +02:00
|
|
|
],
|
|
|
|
[{Packet, StatusCode} = raw_req(Packet, Config)
|
|
|
|
|| {Packet, StatusCode} <- Tests].
|
|
|
|
|
2011-10-20 01:28:50 +02:00
|
|
|
%% This test makes sure the code works even if we wait for a reply
|
|
|
|
%% before sending the third challenge key in the GET body.
|
|
|
|
%%
|
|
|
|
%% This ensures that Cowboy will work fine with proxies on hixie.
|
2011-08-23 16:24:02 +02:00
|
|
|
ws0(Config) ->
|
2011-04-14 21:21:17 +02:00
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
2011-10-20 01:28:50 +02:00
|
|
|
ok = gen_tcp:send(Socket,
|
2011-04-14 21:21:17 +02:00
|
|
|
"GET /websocket HTTP/1.1\r\n"
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
"Connection: Upgrade\r\n"
|
|
|
|
"Upgrade: WebSocket\r\n"
|
|
|
|
"Origin: http://localhost\r\n"
|
|
|
|
"Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n"
|
|
|
|
"Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n"
|
2011-10-20 01:28:50 +02:00
|
|
|
"\r\n"),
|
2011-04-14 21:21:17 +02:00
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "WebSocket Protocol Handshake"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
2011-10-20 01:28:50 +02:00
|
|
|
[Headers, <<>>] = websocket_headers(
|
2011-08-23 16:24:02 +02:00
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
2011-04-14 21:21:17 +02:00
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "WebSocket"} = lists:keyfind('Upgrade', 1, Headers),
|
2011-06-27 13:02:07 +01:00
|
|
|
{"sec-websocket-location", "ws://localhost/websocket"}
|
2011-04-14 21:21:17 +02:00
|
|
|
= lists:keyfind("sec-websocket-location", 1, Headers),
|
|
|
|
{"sec-websocket-origin", "http://localhost"}
|
|
|
|
= lists:keyfind("sec-websocket-origin", 1, Headers),
|
2011-10-20 01:28:50 +02:00
|
|
|
ok = gen_tcp:send(Socket, <<15,245,8,18,2,204,133,33>>),
|
|
|
|
{ok, Body} = gen_tcp:recv(Socket, 0, 6000),
|
2011-04-14 21:21:17 +02:00
|
|
|
<<169,244,191,103,146,33,149,59,74,104,67,5,99,118,171,236>> = Body,
|
|
|
|
ok = gen_tcp:send(Socket, << 0, "client_msg", 255 >>),
|
|
|
|
{ok, << 0, "client_msg", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 0, "websocket_init", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 0, "websocket_handle", 255 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok = gen_tcp:send(Socket, << 255, 0 >>),
|
|
|
|
{ok, << 255, 0 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
2011-08-23 16:24:02 +02:00
|
|
|
ws8(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /websocket HTTP/1.1\r\n"
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
"Connection: Upgrade\r\n"
|
|
|
|
"Upgrade: websocket\r\n"
|
|
|
|
"Sec-WebSocket-Origin: http://localhost\r\n"
|
|
|
|
"Sec-WebSocket-Version: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
|
|
|
[Headers, <<>>] = websocket_headers(
|
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
|
|
|
|
16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping
|
|
|
|
{ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
|
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
2011-09-30 18:05:02 +01:00
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
ws8_single_bytes(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /websocket HTTP/1.1\r\n"
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
"Connection: Upgrade\r\n"
|
|
|
|
"Upgrade: websocket\r\n"
|
|
|
|
"Sec-WebSocket-Origin: http://localhost\r\n"
|
|
|
|
"Sec-WebSocket-Version: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
|
|
|
[Headers, <<>>] = websocket_headers(
|
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#81 >>), %% send one byte
|
|
|
|
ok = timer:sleep(100), %% sleep for a period
|
|
|
|
ok = gen_tcp:send(Socket, << 16#85 >>), %% send another and so on
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#37 >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#fa >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#21 >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#3d >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#7f >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#9f >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#4d >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#51 >>),
|
|
|
|
ok = timer:sleep(100),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#58 >>),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping
|
|
|
|
{ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
|
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
2011-08-23 16:24:02 +02:00
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
2011-09-22 21:33:56 +02:00
|
|
|
ws_timeout_hibernate(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /ws_timeout_hibernate HTTP/1.1\r\n"
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
"Connection: Upgrade\r\n"
|
|
|
|
"Upgrade: websocket\r\n"
|
|
|
|
"Sec-WebSocket-Origin: http://localhost\r\n"
|
|
|
|
"Sec-WebSocket-Version: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
|
|
|
[Headers, <<>>] = websocket_headers(
|
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
2011-10-10 09:09:15 +02:00
|
|
|
ws8_init_shutdown(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /ws_init_shutdown HTTP/1.1\r\n"
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
"Connection: Upgrade\r\n"
|
|
|
|
"Upgrade: websocket\r\n"
|
|
|
|
"Sec-WebSocket-Origin: http://localhost\r\n"
|
|
|
|
"Sec-WebSocket-Version: 8\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 403, "Forbidden"}, _Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
2011-10-24 15:06:51 +01:00
|
|
|
ws13(Config) ->
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{ok, Socket} = gen_tcp:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw}]),
|
|
|
|
ok = gen_tcp:send(Socket, [
|
|
|
|
"GET /websocket HTTP/1.1\r\n"
|
|
|
|
"Host: localhost\r\n"
|
|
|
|
"Connection: Upgrade\r\n"
|
|
|
|
"Origin: http://localhost\r\n"
|
|
|
|
"Sec-WebSocket-Version: 13\r\n"
|
|
|
|
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
|
|
|
|
"Upgrade: websocket\r\n"
|
|
|
|
"\r\n"]),
|
|
|
|
{ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
|
|
|
|
= erlang:decode_packet(http, Handshake, []),
|
|
|
|
[Headers, <<>>] = websocket_headers(
|
|
|
|
erlang:decode_packet(httph, Rest, []), []),
|
|
|
|
{'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
|
|
|
|
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
|
|
|
|
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
|
|
|
|
= lists:keyfind("sec-websocket-accept", 1, Headers),
|
|
|
|
ok = gen_tcp:send(Socket, << 16#81, 16#85, 16#37, 16#fa, 16#21, 16#3d,
|
|
|
|
16#7f, 16#9f, 16#4d, 16#51, 16#58 >>),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>}
|
|
|
|
= gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 0:8 >>), %% ping
|
|
|
|
{ok, << 1:1, 0:3, 10:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), %% pong
|
|
|
|
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
|
|
|
|
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
|
|
|
|
ok.
|
|
|
|
|
2011-04-14 21:21:17 +02:00
|
|
|
websocket_headers({ok, http_eoh, Rest}, Acc) ->
|
|
|
|
[Acc, Rest];
|
|
|
|
websocket_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) ->
|
|
|
|
F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end,
|
|
|
|
websocket_headers(erlang:decode_packet(httph, Rest, []),
|
|
|
|
[{F(Key), Value}|Acc]).
|
|
|
|
|
2011-04-09 12:59:53 +02:00
|
|
|
%% http and https.
|
2011-04-08 16:30:37 +02:00
|
|
|
|
|
|
|
build_url(Path, Config) ->
|
|
|
|
{scheme, Scheme} = lists:keyfind(scheme, 1, Config),
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
|
|
|
|
|
|
|
|
http_200(Config) ->
|
|
|
|
{ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
|
|
|
|
httpc:request(build_url("/", Config)).
|
|
|
|
|
|
|
|
http_404(Config) ->
|
|
|
|
{ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
|
|
|
|
httpc:request(build_url("/not/found", Config)).
|
2011-09-14 01:41:03 +02:00
|
|
|
|
|
|
|
%% misc.
|
|
|
|
|
|
|
|
http_10_hostless(Config) ->
|
|
|
|
Packet = "GET / HTTP/1.0\r\n\r\n",
|
|
|
|
{Packet, 200} = raw_req(Packet, Config).
|