diff options
61 files changed, 1961 insertions, 3044 deletions
@@ -1,12 +1,7 @@ .cowboy.plt .erlang.mk.packages.* -.eunit _rel deps -doc/*.css -doc/*.html -doc/*.png -doc/edoc-info ebin logs relx @@ -1,4 +1,4 @@ -Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above @@ -4,14 +4,17 @@ PROJECT = cowboy # Options. +ERLC_OPTS ?= -Werror +debug_info +warn_export_all +warn_export_vars \ + +warn_shadow_vars +warn_obsolete_guard +warn_missing_spec COMPILE_FIRST = cowboy_middleware cowboy_sub_protocol -CT_SUITES = eunit http spdy ws +CT_SUITES = eunit http loop_handler spdy ws +CT_OPTS += -pa test -ct_hooks cowboy_ct_hook [] PLT_APPS = crypto public_key ssl # Dependencies. DEPS = cowlib ranch -dep_cowlib = pkg://cowlib 0.4.0 +dep_cowlib = pkg://cowlib 0.6.1 dep_ranch = pkg://ranch 0.9.0 TEST_DEPS = ct_helper gun @@ -21,11 +24,3 @@ dep_gun = pkg://gun master # Standard targets. include erlang.mk - -# Extra targets. - -.PHONY: autobahn - -autobahn: clean clean-deps deps app build-tests - @mkdir -p logs/ - @$(CT_RUN) -suite autobahn_SUITE @@ -7,7 +7,7 @@ Goals ----- Cowboy aims to provide a **complete** HTTP stack in a **small** code base. -It is optimized for **low latency** and **low memory usage**, in parts +It is optimized for **low latency** and **low memory usage**, in part because it uses **binary strings**. Cowboy provides **routing** capabilities, selectively dispatching requests @@ -21,8 +21,11 @@ No parameterized module. No process dictionary. **Clean** Erlang code. Sponsors -------- -The SPDY protocol development is sponsored -by [LeoFS Cloud Storage](http://www.leofs.org). +The SPDY implementation was sponsored by +[LeoFS Cloud Storage](http://www.leofs.org). + +The project is currently sponsored by +[Kato.im](https://kato.im). Getting Started --------------- @@ -1,7 +1,7 @@ #!/bin/sh KERL_INSTALL_PATH=~/erlang -KERL_RELEASES="r15b01 r15b02 r15b03 r16b r16b01 r16b02 r16b03 maint master" +KERL_RELEASES="r15b01 r15b02 r15b03 r16b r16b01 r16b02 r16b03-1 17.0 17.0_native" make build-tests @@ -1,4 +1,4 @@ -# Copyright (c) 2013, Loïc Hoguin <[email protected]> +# Copyright (c) 2013-2014, Loïc Hoguin <[email protected]> # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -58,7 +58,7 @@ ifneq ($(wildcard $(RELX_CONFIG)),) RELX ?= $(CURDIR)/relx export RELX -RELX_URL ?= https://github.com/erlware/relx/releases/download/v0.5.2/relx +RELX_URL ?= https://github.com/erlware/relx/releases/download/v0.6.0/relx RELX_OPTS ?= define get_relx @@ -132,7 +132,7 @@ define compile_dtl Compile = fun(F) -> \ Module = list_to_atom( \ string:to_lower(filename:basename(F, ".dtl")) ++ "_dtl"), \ - erlydtl_compiler:compile(F, Module, [{out_dir, "ebin/"}]) \ + erlydtl:compile(F, Module, [{out_dir, "ebin/"}]) \ end, \ _ = [Compile(F) || F <- string:tokens("$(1)", " ")], \ init:stop()' @@ -209,11 +209,14 @@ clean-docs: $(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep)))) +TEST_ERLC_OPTS ?= +debug_info +warn_export_vars +warn_shadow_vars +warn_obsolete_guard +TEST_ERLC_OPTS += -DTEST=1 -DEXTRA=1 +'{parse_transform, eunit_autoexport}' + build-test-deps: $(ALL_TEST_DEPS_DIRS) @for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done build-tests: build-test-deps - $(gen_verbose) erlc -v $(ERLC_OPTS) -o test/ \ + $(gen_verbose) erlc -v $(TEST_ERLC_OPTS) -o test/ \ $(wildcard test/*.erl test/*/*.erl) -pa ebin/ CT_OPTS ?= @@ -228,7 +231,7 @@ CT_RUN = ct_run \ CT_SUITES ?= define test_target -test_$(1): ERLC_OPTS += -DTEST=1 +'{parse_transform, eunit_autoexport}' +test_$(1): ERLC_OPTS = $(TEST_ERLC_OPTS) test_$(1): clean deps app build-tests @if [ -d "test" ] ; \ then \ @@ -240,7 +243,7 @@ endef $(foreach test,$(CT_SUITES),$(eval $(call test_target,$(test)))) -tests: ERLC_OPTS += -DTEST=1 +'{parse_transform, eunit_autoexport}' +tests: ERLC_OPTS = $(TEST_ERLC_OPTS) tests: clean deps app build-tests @if [ -d "test" ] ; \ then \ @@ -251,16 +254,18 @@ tests: clean deps app build-tests # Dialyzer. +DIALYZER_PLT ?= $(CURDIR)/.$(PROJECT).plt +export DIALYZER_PLT + PLT_APPS ?= DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions \ -Wunmatched_returns # -Wunderspecs build-plt: deps app - @dialyzer --build_plt --output_plt .$(PROJECT).plt \ - --apps erts kernel stdlib $(PLT_APPS) $(ALL_DEPS_DIRS) + @dialyzer --build_plt --apps erts kernel stdlib $(PLT_APPS) $(ALL_DEPS_DIRS) dialyze: - @dialyzer --src src --plt .$(PROJECT).plt --no_native $(DIALYZER_OPTS) + @dialyzer --src src --no_native $(DIALYZER_OPTS) # Packages. diff --git a/guide/multipart_req.md b/guide/multipart_req.md new file mode 100644 index 0000000..a4d0137 --- /dev/null +++ b/guide/multipart_req.md @@ -0,0 +1,112 @@ +Multipart requests +================== + +You can read and parse multipart messages using the +Req object directly. + +Cowboy defines two functions that allows you to get +information about each part and read their contents. + +Checking the content-type +------------------------- + +While there is a variety of multipart messages, the +most common on the Web is `multipart/form-data`. It's +the type of message being sent when an HTML form +allows uploading files. + +You can quickly figure out if a multipart message +has been sent by parsing the `content-type` header. + +``` erlang +{ok, {<<"multipart">>, <<"form-data">>, _}, Req2} + = cowboy_req:parse_header(<<"content-type">>, Req). +``` + +Reading a multipart message +--------------------------- + +To read a message you have to iterate over all its +parts. Then, for each part, you can inspect its headers +and read its body. + +``` erlang +multipart(Req) -> + case cowboy_req:part(Req) of + {ok, _Headers, Req2} -> + {ok, _Body, Req3} = cowboy_req:part_body(Req2), + multipart(Req3); + {done, Req2} -> + Req2 + end. +``` + +Parts do not have a size limit. When a part body is +too big, Cowboy will return what it read so far and +allow you to continue if you wish to do so. + +The function `cow_multipart:form_data/1` can be used +to quickly obtain information about a part from a +`multipart/form-data` message. This function will +tell you if the part is for a normal field or if it +is a file being uploaded. + +This can be used for example to allow large part bodies +for files but crash when a normal field is too large. + +``` erlang +multipart(Req) -> + case cowboy_req:part(Req) of + {ok, Headers, Req2} -> + Req4 = case cow_multipart:form_data(Headers) of + {data, _FieldName} -> + {ok, _Body, Req3} = cowboy_req:part_body(Req2), + Req3; + {file, _FieldName, _Filename, _CType, _CTransferEncoding} -> + stream_file(Req2) + end, + multipart(Req4); + {done, Req2} -> + Req2 + end. + +stream_file(Req) -> + case cowboy_req:part_body(Req) of + {ok, _Body, Req2} -> + Req2; + {more, _Body, Req2} -> + stream_file(Req2) + end. +``` + +By default the body chunk Cowboy will return is limited +to 8MB. This can of course be overriden. + +Skipping unwanted parts +----------------------- + +If you do not want to read a part's body, you can skip it. +Skipping is easy. If you do not call the function to read +the part's body, Cowboy will automatically skip it when +you request the next part. + +The following snippet reads all part headers and skips +all bodies: + +``` erlang +multipart(Req) -> + case cowboy_req:part(Req) of + {ok, _Headers, Req2} -> + multipart(Req2); + {done, Req2} -> + Req2 + end. +``` + +Similarly, if you start reading the body and it ends up +being too big, you can simply continue with the next part, +Cowboy will automatically skip what remains. + +And if you started reading the message but decide that you +do not need the remaining parts, you can simply stop reading +entirely and Cowboy will automatically figure out what to do. diff --git a/guide/req.md b/guide/req.md index 2b59152..f93623a 100644 --- a/guide/req.md +++ b/guide/req.md @@ -62,8 +62,10 @@ or `{error, atom()}`. This includes the following functions: also includes the `chunk/2` function which always returns `ok`. -The final group modifies the Req object, so it always return -a new `Req`. It includes the following functions: `compact/1`, +The final group modifies the Req object state without +performing any immediate operations. As these functions +can't fail, they always return a new `Req` directly. +This includes the following functions: `compact/1`, `delete_resp_header/2`, `set_meta/3`, `set_resp_body/2`, `set_resp_body_fun/{2,3}`, `set_resp_cookie/4`, `set_resp_header/3`. diff --git a/guide/toc.md b/guide/toc.md index a0c9a8c..26925c5 100644 --- a/guide/toc.md +++ b/guide/toc.md @@ -48,7 +48,7 @@ Multipart --------- * Understanding multipart - * Multipart requests + * [Multipart requests](multipart_req.md) * Multipart responses Server push technologies @@ -61,7 +61,7 @@ Server push technologies Using Websocket --------------- - * The Websocket protocol + * [The Websocket protocol](ws_protocol.md) * [Handling Websocket connections](ws_handlers.md) Advanced HTTP diff --git a/guide/ws_handlers.md b/guide/ws_handlers.md index 94bc95e..99f69dc 100644 --- a/guide/ws_handlers.md +++ b/guide/ws_handlers.md @@ -1,75 +1,230 @@ -Websocket handlers -================== +Handling Websocket connections +============================== -Purpose -------- +A special handler is required for handling Websocket connections. +Websocket handlers allow you to initialize the connection, +handle incoming frames from the socket, handle incoming Erlang +messages and then clean up on termination. -Websocket is an extension to HTTP to emulate plain TCP connections -between the user's browser and the server. Requests that are upgraded -are then handled by websocket handlers. +Websocket handlers essentially act as a bridge between the client +and the Erlang system. They will typically do little more than +socket communication and decoding/encoding of frames. -Both sides of the socket can send data at any time asynchronously. +Initialization +-------------- -Websocket is an IETF standard. Cowboy supports the standard and all -the drafts that were previously implemented by browsers. Websocket -is implemented by most browsers today, although for backward -compatibility reasons a solution like [Bullet](https://github.com/extend/bullet) -might be preferred. +First, the `init/3` callback is called. This callback is common +to all handlers. To establish a Websocket connection, this function +must return an `upgrade` tuple. -Usage ------ +``` erlang +init(_, Req, Opts) -> + {upgrade, protocol, cowboy_websocket}. +``` + +It is also possible to return an update Req object and options +using the longer form of this tuple. + +``` erlang +init(_Type, Req, Opts) -> + {upgrade, protocol, cowboy_websocket, Req, Opts}. +``` -Websocket handlers are a bridge between the client and your system. -They can receive data from the client, through `websocket_handle/3`, -or from the system, through `websocket_info/3`. It is up to the -handler to decide to process this data, and optionally send a reply -to the client. +Upon receiving this tuple, Cowboy will switch to the code +that handles Websocket connections. It does not immediately +perform the handshake however. First, it calls the `websocket_init/3` +callback. -The first thing to do to be able to handle websockets is to tell -Cowboy that it should upgrade the connection to use the Websocket -protocol, as follow. +This function must be used to initialize the state, and can +also be used to register the process, start a timer, etc. +As long as the function returns an `ok` tuple, then Cowboy +performs the Websocket handshake. ``` erlang -init({tcp, http}, Req, Opts) -> - {upgrade, protocol, cowboy_websocket}. +websocket_init(_Type, Req, _Opts) -> + {ok, Req, #state{}}. ``` -Cowboy will then switch the protocol and call `websocket_init`, -followed by zero or more calls to `websocket_handle` and -`websocket_info`. Then, when the connection is shutting down, -`websocket_terminate` will be called. +A `shutdown` tuple can be returned to refuse to perform the +handshake. When doing so, Cowboy will send a `400 Bad Request` +response to the client and close the connection. + +``` erlang +websocket_init(_Type, Req, _Opts) -> + {shutdown, Req}. +``` + +It is also possible to perform a `cowboy_req:reply/{2,3,4}` +before returning a `shutdown` tuple, allowing you to override +the response sent back to the client. + +Note that browser support for handling Websocket connection +failures may vary. + +If the sec-websocket-protocol header was sent with the request +for establishing a Websocket connection, then the Websocket +handler *must* select one of these subprotocol and send it +back to the client, otherwise the client might decide to close +the connection, assuming no correct subprotocol was found. + +``` erlang +websocket_init(_Type, Req, _Opts) -> + case cowboy_req:parse_header(<<"sec-websocket-protocol">>, Req) of + {ok, undefined, Req2} -> + {ok, Req, #state{}}; + {ok, Subprotocols, Req2} -> + case lists:keymember(<<"mychat2">>, 1, Subprotocols) of + true -> + Req3 = cowboy:set_resp_header(<<"sec-websocket-protocol">>, + <<"mychat2">>, Req2), + {ok, Req3, #state{}}; + false -> + {shutdown, Req2} + end + end. +``` + +It is not recommended to wait too long inside the `websocket_init/3` +function. Any extra initialization may be done after returning by +sending yourself a message before doing anything. Any message sent +to `self()` from `websocket_init/3` is guaranteed to arrive before +any frames from the client. + +It is also very easy to ensure that this message arrives before +any message from other processes by sending it before registering +or enabling timers. + +``` erlang +websocket_init(_Type, Req, _Opts) -> + self() ! post_init, + %% Register process here... + {ok, Req, #state{}}. + +websocket_info(post_init, Req, State) -> + %% Perform post_init initialization here... + {ok, Req, State}. +``` + +Handling frames from the client +------------------------------- + +Cowboy will call `websocket_handle/3` whenever a text, binary, +ping or pong frame arrives from the client. Note that in the +case of ping and pong frames, no action is expected as Cowboy +automatically replies to ping frames. + +The handler can decide to send frames to the socket, shutdown +or just continue without sending anything. -The following handler sends a message every second. It also echoes -back what it receives. +The following snippet echoes back any text frame received and +ignores all others. ``` erlang --module(my_ws_handler). --behaviour(cowboy_websocket_handler). - --export([init/3]). --export([websocket_init/3]). --export([websocket_handle/3]). --export([websocket_info/3]). --export([websocket_terminate/3]). - -init({tcp, http}, Req, Opts) -> - {upgrade, protocol, cowboy_websocket}. - -websocket_init(TransportName, Req, _Opts) -> - erlang:start_timer(1000, self(), <<"Hello!">>), - {ok, Req, undefined_state}. - -websocket_handle({text, Msg}, Req, State) -> - {reply, {text, << "That's what she said! ", Msg/binary >>}, Req, State}; -websocket_handle(_Data, Req, State) -> - {ok, Req, State}. - -websocket_info({timeout, _Ref, Msg}, Req, State) -> - erlang:start_timer(1000, self(), <<"How' you doin'?">>), - {reply, {text, Msg}, Req, State}; +websocket_handle(Frame = {text, _}, Req, State) -> + {reply, Frame, Req, State}; +websocket_handle(_Frame, Req, State) -> + {ok, Req, State}. +``` + +Handling Erlang messages +------------------------ + +Cowboy will call `websocket_info/3` whenever an Erlang message +arrives. + +The handler can decide to send frames to the socket, shutdown +or just continue without sending anything. + +The following snippet forwards any `log` message to the socket +and ignores all others. + +``` erlang +websocket_info({log, Text}, Req, State) -> + {reply, {text, Text}, Req, State}; websocket_info(_Info, Req, State) -> - {ok, Req, State}. + {ok, Req, State}. +``` + +Sending frames to the socket +---------------------------- + +Cowboy allows sending either a single frame or a list of +frames to the socket. Any frame can be sent: text, binary, ping, +pong or close frames. + +The following example sends three frames using a single `reply` +tuple. -websocket_terminate(_Reason, _Req, _State) -> - ok. +``` erlang +websocket_info(hello_world, Req, State) -> + {reply, [ + {text, "Hello"}, + {text, <<"world!">>}, + {binary, <<0:8000>>} + ], Req, State}; +%% More websocket_info/3 clauses here... +``` + +Note that the payload for text and binary frames is of type +`iodata()`, meaning it can be either a `binary()` or an +`iolist()`. + +Sending a `close` frame will immediately initiate the closing +of the Websocket connection. Be aware that any additional +frames sent by the client or any Erlang messages waiting to +be received will not be processed. Also note that when replying +a list of frames that includes close, any frame found after the +close frame will not be sent. + +Ping and timeout +---------------- + +The biggest performance improvement you can do when dealing +with a huge number of Websocket connections is to reduce the +number of timers that are started on the server. A common use +of timers when dealing with connections is for sending a ping +every once in a while. This should be done exclusively on the +client side. Indeed, a server handling one million Websocket +connections will perform a lot better when it doesn't have to +handle one million extra timers too! + +Cowboy will automatically respond to ping frames sent by the +client. It will still forward the frame to the handler for +informative purpose, but no further action is required. + +Cowboy can be configured to automatically close the Websocket +connection when no data arrives on the socket. It is highly +recommended to configure a timeout for it, as otherwise you +may end up with zombie "half-connected" sockets that may +leave the process alive forever. + +A good timeout value is 60 seconds. + +``` erlang +websocket_init(_Type, Req, _Opts) -> + {ok, Req, #state{}, 60000}. ``` + +This value cannot be changed once it is set. It defaults to +`infinity`. + +Hibernate +--------- + +Most tuples returned from handler callbacks can include an +extra value `hibernate`. After doing any necessary operations +following the return of the callback, Cowboy will hibernate +the process. + +It is highly recommended to hibernate processes that do not +handle much traffic. It is a good idea to hibernate all +connections by default and investigate only when you start +noticing increased CPU usage. + +Supporting older browsers +------------------------- + +Unfortunately Websocket is a relatively recent technology, +which means that not all browsers support it. A library like +[Bullet](https://github.com/extend/bullet) can be used to +emulate Websocket connections on older browsers. diff --git a/guide/ws_protocol.md b/guide/ws_protocol.md new file mode 100644 index 0000000..390751e --- /dev/null +++ b/guide/ws_protocol.md @@ -0,0 +1,45 @@ +The Websocket protocol +====================== + +This chapter explains what Websocket is and why it is +a vital component of soft realtime Web applications. + +Description +----------- + +Websocket is an extension to HTTP that emulates plain TCP +connections between the client, typically a Web browser, +and the server. It uses the HTTP Upgrade mechanism to +establish the connection. + +Websocket connections are asynchronous, unlike HTTP. This +means that not only can the client send frames to the server +at any time, but the server can also send frames to the client +without the client initiating anything other than the +Websocket connection itself. This allows the server to push +data to the client directly. + +Websocket is an IETF standard. Cowboy supports the standard +and all drafts that were previously implemented by browsers, +excluding the initial flawed draft sometimes known as +"version 0". + +Implementation +-------------- + +Cowboy implements Websocket as a protocol upgrade. Once the +upgrade is performed from the `init/3` callback, Cowboy +switches to Websocket. Please consult the next chapter for +more information on initiating and handling Websocket +connections. + +The implementation of Websocket in Cowboy is validated using +the Autobahn test suite, which is an extensive suite of tests +covering all aspects of the protocol. Cowboy passes the +suite with 100% success, including all optional tests. + +Cowboy's Websocket implementation also includes the +x-webkit-deflate-frame compression draft which is being used +by some browsers to reduce the size of data being transmitted. +Cowboy will automatically use compression as long as the +`compress` protocol option is set when starting the listener. diff --git a/manual/cowboy.md b/manual/cowboy.md index c2ef859..0eab0f8 100644 --- a/manual/cowboy.md +++ b/manual/cowboy.md @@ -76,7 +76,7 @@ Exports > Start listening for SPDY connections. Returns the pid for this > listener's supervisor. -### stop_listener(Ref) -> ok +### stop_listener(Ref) -> ok | {error, not_found} > Types: > * Ref = ranch:ref() diff --git a/manual/cowboy_req.md b/manual/cowboy_req.md index d7af41f..91139ac 100644 --- a/manual/cowboy_req.md +++ b/manual/cowboy_req.md @@ -408,6 +408,51 @@ Request body related exports > will perform all the required initialization when it is > called the first time. +### part(Req) -> {ok, Headers, Req2} | {done, Req2} + +> Types: +> * Headers = cow_multipart:headers() +> +> Read the headers for the next part of the multipart message. +> +> Cowboy will skip any data remaining until the beginning of +> the next part. This includes the preamble to the multipart +> message but also the body of a previous part if it hasn't +> been read. Both are skipped automatically when calling this +> function. +> +> The headers returned are MIME headers, NOT HTTP headers. +> They can be parsed using the functions from the `cow_multipart` +> module. In addition, the `cow_multipart:form_data/1` function +> can be used to quickly figure out `multipart/form-data` messages. +> It takes the list of headers and returns whether this part is +> a simple form field or a file being uploaded. +> +> Note that once a part has been read, or skipped, it cannot +> be read again. + +### part_body(Req) -> part_body(8000000, Req) +### part_body(MaxReadSize, Req) -> {ok, Data, Req2} | {more, Data, Req2} + +> Types: +> * MaxReadSize = non_neg_integer() +> * Data = binary() +> +> Read the body of the current part of the multipart message. +> +> This function will read the body up to `MaxReadSize` bytes. +> This is a soft limit. If there are more data to be read +> from the socket for this part, the function will return +> what it could read inside a `more` tuple. Otherwise, it +> will return an `ok` tuple. +> +> Calling this function again after receiving a `more` tuple +> will return another chunk of body. The last chunk will be +> returned inside an `ok` tuple. +> +> Note that once the body has been read, fully or partially, +> it cannot be read again. + ### skip_body(Req) -> {ok, Req2} | {error, Reason} > Types: @@ -419,26 +464,31 @@ Request body related exports > read before. ### stream_body(Req) -> stream_body(1000000, Req) -### stream_body(MaxSegmentSize, Req) -> {ok, Data, Req2} +### stream_body(MaxReadSize, Req) -> {ok, Data, Req2} | {done, Req2} | {error, Reason} > Types: -> * MaxSegmentSize = non_neg_integer() +> * MaxReadSize = non_neg_integer() > * Data = binary() > * Reason = atom() > > Stream the request body. > -> This function will return a segment of the request body -> with a size of up to `MaxSegmentSize`, or 1MB by default. -> This function can be called repeatedly until a `done` tuple -> is returned, indicating the body has been fully received. +> This function will return the next segment of the body. > > Cowboy will properly handle chunked transfer-encoding by > default. If any other transfer-encoding or content-encoding > has been used for the request, custom decoding functions > can be used. They must be specified using `init_stream/4`. > +> The amount of data returned by this function may vary +> depending on the current state of the request. If data +> is already available in the buffer then it is used fully, +> otherwise Cowboy will read up to `MaxReadSize` bytes from +> the socket. By default Cowboy will read up to 1MB of data. +> It is then decoded, which may grow or shrink it, depending +> on the encoding headers, before it is finally returned. +> > After the body has been streamed fully, Cowboy will remove > the transfer-encoding header from the `Req` object, and add > the content-length header if it wasn't already there. @@ -564,7 +614,8 @@ Response related exports > > If a `Length` is provided, it will be sent in the > content-length header in the response. It is recommended -> to set the length if it can be known in advance. +> to set the length if it can be known in advance. Otherwise, +> the transfer-encoding header will be set to identity. > > This function will only be called if the response is sent > using the `reply/2` or `reply/3` function. diff --git a/manual/cowboy_rest.md b/manual/cowboy_rest.md index 110e224..7a38bb7 100644 --- a/manual/cowboy_rest.md +++ b/manual/cowboy_rest.md @@ -202,7 +202,7 @@ REST callbacks description ### content_types_provided -> * Methods: GET, HEAD +> * Methods: GET, HEAD, POST, PUT, PATCH, DELETE > * Default value: [{{<<"text">>, <<"html">>, '*'}, to_html}] > > Types: @@ -226,8 +226,11 @@ REST callbacks description > `charset` parameter, if present, which is case insensitive. > > The `ProvideResource` value is the name of the callback that will -> be called if the content-type matches. It is defined as follow. +> be called if the content-type matches. It will only be called when +> a representation of the resource needs to be returned. It is defined +> as follow. > +> * Methods: GET, HEAD > * Value type: iodata() | {stream, Fun} | {stream, Len, Fun} | {chunked, ChunkedFun} > * No default > diff --git a/rebar.config b/rebar.config index d234ae2..249620b 100644 --- a/rebar.config +++ b/rebar.config @@ -1,4 +1,4 @@ {deps, [ - {cowlib, ".*", {git, "git://github.com/extend/cowlib.git", "0.4.0"}}, + {cowlib, ".*", {git, "git://github.com/extend/cowlib.git", "0.6.1"}}, {ranch, ".*", {git, "git://github.com/extend/ranch.git", "0.9.0"}} ]}. diff --git a/src/cowboy.app.src b/src/cowboy.app.src index e98eed3..39c69f1 100644 --- a/src/cowboy.app.src +++ b/src/cowboy.app.src @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above diff --git a/src/cowboy.erl b/src/cowboy.erl index abc7911..2b50dfb 100644 --- a/src/cowboy.erl +++ b/src/cowboy.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,7 +12,6 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc Convenience API to start and stop HTTP/HTTPS listeners. -module(cowboy). -export([start_http/4]). @@ -37,7 +36,6 @@ fun((http_status(), http_headers(), iodata(), Req) -> Req). -export_type([onresponse_fun/0]). -%% @doc Start an HTTP listener. -spec start_http(ranch:ref(), non_neg_integer(), ranch_tcp:opts(), cowboy_protocol:opts()) -> {ok, pid()} | {error, any()}. start_http(Ref, NbAcceptors, TransOpts, ProtoOpts) @@ -45,7 +43,6 @@ start_http(Ref, NbAcceptors, TransOpts, ProtoOpts) ranch:start_listener(Ref, NbAcceptors, ranch_tcp, TransOpts, cowboy_protocol, ProtoOpts). -%% @doc Start an HTTPS listener. -spec start_https(ranch:ref(), non_neg_integer(), ranch_ssl:opts(), cowboy_protocol:opts()) -> {ok, pid()} | {error, any()}. start_https(Ref, NbAcceptors, TransOpts, ProtoOpts) @@ -53,7 +50,6 @@ start_https(Ref, NbAcceptors, TransOpts, ProtoOpts) ranch:start_listener(Ref, NbAcceptors, ranch_ssl, TransOpts, cowboy_protocol, ProtoOpts). -%% @doc Start a SPDY listener. -spec start_spdy(ranch:ref(), non_neg_integer(), ranch_ssl:opts(), cowboy_spdy:opts()) -> {ok, pid()} | {error, any()}. start_spdy(Ref, NbAcceptors, TransOpts, ProtoOpts) @@ -66,20 +62,14 @@ start_spdy(Ref, NbAcceptors, TransOpts, ProtoOpts) ranch:start_listener(Ref, NbAcceptors, ranch_ssl, TransOpts2, cowboy_spdy, ProtoOpts). -%% @doc Stop a listener. --spec stop_listener(ranch:ref()) -> ok. +-spec stop_listener(ranch:ref()) -> ok | {error, not_found}. stop_listener(Ref) -> ranch:stop_listener(Ref). -%% @doc Convenience function for setting an environment value. -%% -%% Allows you to update live an environment value used by middlewares. -%% This function is primarily intended to simplify updating the dispatch -%% list used for routing. -spec set_env(ranch:ref(), atom(), any()) -> ok. set_env(Ref, Name, Value) -> Opts = ranch:get_protocol_options(Ref), {_, Env} = lists:keyfind(env, 1, Opts), - Env2 = [{Name, Value}|lists:keydelete(Name, 1, Env)], - Opts2 = lists:keyreplace(env, 1, Opts, {env, Env2}), + Opts2 = lists:keyreplace(env, 1, Opts, + {env, lists:keystore(Name, 1, Env, {Name, Value})}), ok = ranch:set_protocol_options(Ref, Opts2). diff --git a/src/cowboy_app.erl b/src/cowboy_app.erl index b46ba1d..1161d91 100644 --- a/src/cowboy_app.erl +++ b/src/cowboy_app.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,18 +12,16 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @private -module(cowboy_app). -behaviour(application). -%% API. -export([start/2]). -export([stop/1]). -%% API. - -start(_Type, _Args) -> +-spec start(_, _) -> {ok, pid()}. +start(_, _) -> cowboy_sup:start_link(). -stop(_State) -> +-spec stop(_) -> ok. +stop(_) -> ok. diff --git a/src/cowboy_bstr.erl b/src/cowboy_bstr.erl index 0c1f66a..98d2cf7 100644 --- a/src/cowboy_bstr.erl +++ b/src/cowboy_bstr.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,7 +12,6 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc Binary string manipulation. -module(cowboy_bstr). %% Binary strings. @@ -24,11 +23,9 @@ -export([char_to_lower/1]). -export([char_to_upper/1]). -%% @doc Capitalize a token. -%% %% The first letter and all letters after a dash are capitalized. %% This is the form seen for header names in the HTTP/1.1 RFC and -%% others. Note that using this form isn't required, as header name +%% others. Note that using this form isn't required, as header names %% are case insensitive, and it is only provided for use with eventual %% badly implemented clients. -spec capitalize_token(B) -> B when B::binary(). @@ -43,19 +40,14 @@ capitalize_token(<< C, Rest/bits >>, true, Acc) -> capitalize_token(<< C, Rest/bits >>, false, Acc) -> capitalize_token(Rest, false, << Acc/binary, (char_to_lower(C)) >>). -%% @doc Convert a binary string to lowercase. -spec to_lower(B) -> B when B::binary(). to_lower(B) -> << << (char_to_lower(C)) >> || << C >> <= B >>. -%% @doc Convert a binary string to uppercase. -spec to_upper(B) -> B when B::binary(). to_upper(B) -> << << (char_to_upper(C)) >> || << C >> <= B >>. -%% @doc Convert [A-Z] characters to lowercase. -%% @end -%% We gain noticeable speed by matching each value directly. -spec char_to_lower(char()) -> char(). char_to_lower($A) -> $a; char_to_lower($B) -> $b; @@ -85,7 +77,6 @@ char_to_lower($Y) -> $y; char_to_lower($Z) -> $z; char_to_lower(Ch) -> Ch. -%% @doc Convert [a-z] characters to uppercase. -spec char_to_upper(char()) -> char(). char_to_upper($a) -> $A; char_to_upper($b) -> $B; @@ -118,9 +109,7 @@ char_to_upper(Ch) -> Ch. %% Tests. -ifdef(TEST). - capitalize_token_test_() -> - %% {Header, Result} Tests = [ {<<"heLLo-woRld">>, <<"Hello-World">>}, {<<"Sec-Websocket-Version">>, <<"Sec-Websocket-Version">>}, @@ -131,5 +120,4 @@ capitalize_token_test_() -> {<<"Sec-WebSocket---Version">>, <<"Sec-Websocket---Version">>} ], [{H, fun() -> R = capitalize_token(H) end} || {H, R} <- Tests]. - -endif. diff --git a/src/cowboy_client.erl b/src/cowboy_client.erl deleted file mode 100644 index 10aaa9c..0000000 --- a/src/cowboy_client.erl +++ /dev/null @@ -1,275 +0,0 @@ -%% Copyright (c) 2012-2013, Loïc Hoguin <[email protected]> -%% -%% 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. - -%% @private --module(cowboy_client). - --export([init/1]). --export([state/1]). --export([transport/1]). - --export([connect/4]). --export([raw_request/2]). --export([request/3]). --export([request/4]). --export([request/5]). --export([response/1]). --export([response_body/1]). --export([skip_body/1]). --export([stream_status/1]). --export([stream_headers/1]). --export([stream_header/1]). --export([stream_body/1]). - --record(client, { - state = wait :: wait | request | response | response_body, - opts = [] :: [any()], - socket = undefined :: undefined | inet:socket(), - transport = undefined :: module(), - timeout = 5000 :: timeout(), %% @todo Configurable. - buffer = <<>> :: binary(), - connection = keepalive :: keepalive | close, - version = 'HTTP/1.1' :: cowboy:http_version(), - response_body = undefined :: undefined | non_neg_integer() -}). - -init(Opts) -> - {ok, #client{opts=Opts}}. - -state(#client{state=State}) -> - State. - -transport(#client{socket=undefined}) -> - {error, notconnected}; -transport(#client{transport=Transport, socket=Socket}) -> - {ok, Transport, Socket}. - -connect(Transport, Host, Port, Client) - when is_binary(Host) -> - connect(Transport, binary_to_list(Host), Port, Client); -connect(Transport, Host, Port, Client=#client{state=State, opts=Opts}) - when is_atom(Transport), is_list(Host), - is_integer(Port), is_record(Client, client), - State =:= wait -> - {ok, Socket} = Transport:connect(Host, Port, Opts), - {ok, Client#client{state=request, socket=Socket, transport=Transport}}. - -raw_request(Data, Client=#client{state=response_body}) -> - {done, Client2} = skip_body(Client), - raw_request(Data, Client2); -raw_request(Data, Client=#client{ - state=State, socket=Socket, transport=Transport}) - when State =:= request -> - ok = Transport:send(Socket, Data), - {ok, Client}. - -request(Method, URL, Client) -> - request(Method, URL, [], <<>>, Client). - -request(Method, URL, Headers, Client) -> - request(Method, URL, Headers, <<>>, Client). - -request(Method, URL, Headers, Body, Client=#client{state=response_body}) -> - {done, Client2} = skip_body(Client), - request(Method, URL, Headers, Body, Client2); -request(Method, URL, Headers, Body, Client=#client{ - state=State, version=Version}) - when State =:= wait; State =:= request -> - {Transport, FullHost, Host, Port, Path} = parse_url(URL), - {ok, Client2} = case State of - wait -> connect(Transport, Host, Port, Client); - request -> {ok, Client} - end, - VersionBin = atom_to_binary(Version, latin1), - %% @todo do keepalive too, allow override... - Headers2 = case lists:keyfind(<<"host">>, 1, Headers) of - false -> [{<<"host">>, FullHost}|Headers]; - _ -> Headers - end, - Headers3 = [ - {<<"user-agent">>, <<"Cow">>} - |Headers2], - Headers4 = case iolist_size(Body) of - 0 -> Headers3; - Length -> [{<<"content-length">>, integer_to_list(Length)}|Headers3] - end, - HeadersData = [[Name, <<": ">>, Value, <<"\r\n">>] - || {Name, Value} <- Headers4], - Data = [Method, <<" ">>, Path, <<" ">>, VersionBin, <<"\r\n">>, - HeadersData, <<"\r\n">>, Body], - raw_request(Data, Client2). - -parse_url(<< "https://", Rest/binary >>) -> - parse_url(Rest, ranch_ssl); -parse_url(<< "http://", Rest/binary >>) -> - parse_url(Rest, ranch_tcp); -parse_url(URL) -> - parse_url(URL, ranch_tcp). - -parse_url(URL, Transport) -> - case binary:split(URL, <<"/">>) of - [Peer] -> - {Host, Port} = parse_peer(Peer, Transport), - {Transport, Peer, Host, Port, <<"/">>}; - [Peer, Path] -> - {Host, Port} = parse_peer(Peer, Transport), - {Transport, Peer, Host, Port, [<<"/">>, Path]} - end. - -parse_peer(Peer, Transport) -> - case binary:split(Peer, <<":">>) of - [Host] when Transport =:= ranch_tcp -> - {binary_to_list(Host), 80}; - [Host] when Transport =:= ranch_ssl -> - {binary_to_list(Host), 443}; - [Host, Port] -> - {binary_to_list(Host), list_to_integer(binary_to_list(Port))} - end. - -response(Client=#client{state=response_body}) -> - {done, Client2} = skip_body(Client), - response(Client2); -response(Client=#client{state=request}) -> - case stream_status(Client) of - {ok, Status, _, Client2} -> - case stream_headers(Client2) of - {ok, Headers, Client3} -> - {ok, Status, Headers, Client3}; - {error, Reason} -> - {error, Reason} - end; - {error, Reason} -> - {error, Reason} - end. - -response_body(Client=#client{state=response_body}) -> - response_body_loop(Client, <<>>). - -response_body_loop(Client, Acc) -> - case stream_body(Client) of - {ok, Data, Client2} -> - response_body_loop(Client2, << Acc/binary, Data/binary >>); - {done, Client2} -> - {ok, Acc, Client2}; - {error, Reason} -> - {error, Reason} - end. - -skip_body(Client=#client{state=response_body}) -> - case stream_body(Client) of - {ok, _, Client2} -> skip_body(Client2); - Done -> Done - end. - -stream_status(Client=#client{state=State, buffer=Buffer}) - when State =:= request -> - case binary:split(Buffer, <<"\r\n">>) of - [Line, Rest] -> - parse_version(Client#client{state=response, buffer=Rest}, Line); - _ -> - case recv(Client) of - {ok, Data} -> - Buffer2 = << Buffer/binary, Data/binary >>, - stream_status(Client#client{buffer=Buffer2}); - {error, Reason} -> - {error, Reason} - end - end. - -parse_version(Client, << "HTTP/1.1 ", Rest/binary >>) -> - parse_status(Client, Rest, 'HTTP/1.1'); -parse_version(Client, << "HTTP/1.0 ", Rest/binary >>) -> - parse_status(Client, Rest, 'HTTP/1.0'). - -parse_status(Client, << S3, S2, S1, " ", StatusStr/binary >>, Version) - when S3 >= $0, S3 =< $9, S2 >= $0, S2 =< $9, S1 >= $0, S1 =< $9 -> - Status = (S3 - $0) * 100 + (S2 - $0) * 10 + S1 - $0, - {ok, Status, StatusStr, Client#client{version=Version}}. - -stream_headers(Client=#client{state=State}) - when State =:= response -> - stream_headers(Client, []). - -stream_headers(Client, Acc) -> - case stream_header(Client) of - {ok, Name, Value, Client2} -> - stream_headers(Client2, [{Name, Value}|Acc]); - {done, Client2} -> - {ok, Acc, Client2}; - {error, Reason} -> - {error, Reason} - end. - -stream_header(Client=#client{state=State, buffer=Buffer, - response_body=RespBody}) when State =:= response -> - case binary:split(Buffer, <<"\r\n">>) of - [<<>>, Rest] -> - %% If we have a body, set response_body. - Client2 = case RespBody of - undefined -> Client#client{state=request}; - 0 -> Client#client{state=request}; - _ -> Client#client{state=response_body} - end, - {done, Client2#client{buffer=Rest}}; - [Line, Rest] -> - %% @todo Do a better parsing later on. - [Name, Value] = binary:split(Line, <<": ">>), - Name2 = cowboy_bstr:to_lower(Name), - Client2 = case Name2 of - <<"content-length">> -> - Length = list_to_integer(binary_to_list(Value)), - if Length >= 0 -> ok end, - Client#client{response_body=Length}; - _ -> - Client - end, - {ok, Name2, Value, Client2#client{buffer=Rest}}; - _ -> - case recv(Client) of - {ok, Data} -> - Buffer2 = << Buffer/binary, Data/binary >>, - stream_header(Client#client{buffer=Buffer2}); - {error, Reason} -> - {error, Reason} - end - end. - -stream_body(Client=#client{state=response_body, response_body=RespBody}) - when RespBody =:= undefined; RespBody =:= 0 -> - {done, Client#client{state=request, response_body=undefined}}; -stream_body(Client=#client{state=response_body, buffer=Buffer, - response_body=Length}) when is_integer(Length) -> - case byte_size(Buffer) of - 0 -> - case recv(Client) of - {ok, Body} when byte_size(Body) =< Length -> - Length2 = Length - byte_size(Body), - {ok, Body, Client#client{response_body=Length2}}; - {ok, Data} -> - << Body:Length/binary, Rest/binary >> = Data, - {ok, Body, Client#client{buffer=Rest, - response_body=undefined}}; - {error, Reason} -> - {error, Reason} - end; - N when N =< Length -> - Length2 = Length - N, - {ok, Buffer, Client#client{buffer= <<>>, response_body=Length2}}; - _ -> - << Body:Length/binary, Rest/binary >> = Buffer, - {ok, Body, Client#client{buffer=Rest, response_body=undefined}} - end. - -recv(#client{socket=Socket, transport=Transport, timeout=Timeout}) -> - Transport:recv(Socket, 0, Timeout). diff --git a/src/cowboy_clock.erl b/src/cowboy_clock.erl index 6fb522b..2de3470 100644 --- a/src/cowboy_clock.erl +++ b/src/cowboy_clock.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,8 +12,6 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc Date and time related functions. -%% %% While a gen_server process runs in the background to update %% the cache of formatted dates every second, all API calls are %% local and directly read from the ETS cache table, providing @@ -41,68 +39,63 @@ tref = undefined :: undefined | timer:tref() }). --define(SERVER, ?MODULE). --define(TABLE, ?MODULE). - %% API. -%% @private -spec start_link() -> {ok, pid()}. start_link() -> - gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). -%% @private -spec stop() -> stopped. stop() -> - gen_server:call(?SERVER, stop). + gen_server:call(?MODULE, stop). -%% @doc Return the current date and time formatted according to RFC-1123. -spec rfc1123() -> binary(). rfc1123() -> - ets:lookup_element(?TABLE, rfc1123, 2). + ets:lookup_element(?MODULE, rfc1123, 2). -%% @doc Return the given date and time formatted according to RFC-1123. -spec rfc1123(calendar:datetime()) -> binary(). rfc1123(DateTime) -> update_rfc1123(<<>>, undefined, DateTime). %% gen_server. -%% @private +-spec init([]) -> {ok, #state{}}. init([]) -> - ?TABLE = ets:new(?TABLE, [set, protected, + ?MODULE = ets:new(?MODULE, [set, protected, named_table, {read_concurrency, true}]), T = erlang:universaltime(), B = update_rfc1123(<<>>, undefined, T), {ok, TRef} = timer:send_interval(1000, update), - ets:insert(?TABLE, {rfc1123, B}), + ets:insert(?MODULE, {rfc1123, B}), {ok, #state{universaltime=T, rfc1123=B, tref=TRef}}. -%% @private +-spec handle_call(any(), _, State) + -> {reply, ignored, State} | {stop, normal, stopped, State} + when State::#state{}. handle_call(stop, _From, State=#state{tref=TRef}) -> {ok, cancel} = timer:cancel(TRef), {stop, normal, stopped, State}; handle_call(_Request, _From, State) -> {reply, ignored, State}. -%% @private +-spec handle_cast(_, State) -> {noreply, State} when State::#state{}. handle_cast(_Msg, State) -> {noreply, State}. -%% @private +-spec handle_info(any(), State) -> {noreply, State} when State::#state{}. handle_info(update, #state{universaltime=Prev, rfc1123=B1, tref=TRef}) -> T = erlang:universaltime(), B2 = update_rfc1123(B1, Prev, T), - ets:insert(?TABLE, {rfc1123, B2}), + ets:insert(?MODULE, {rfc1123, B2}), {noreply, #state{universaltime=T, rfc1123=B2, tref=TRef}}; handle_info(_Info, State) -> {noreply, State}. -%% @private +-spec terminate(_, _) -> ok. terminate(_Reason, _State) -> ok. -%% @private +-spec code_change(_, State, _) -> {ok, State} when State::#state{}. code_change(_OldVsn, State, _Extra) -> {ok, State}. @@ -174,7 +167,6 @@ month(12) -> <<"Dec">>. %% Tests. -ifdef(TEST). - update_rfc1123_test_() -> Tests = [ {<<"Sat, 14 May 2011 14:25:33 GMT">>, undefined, @@ -215,5 +207,4 @@ pad_int_test_() -> {56, <<"56">>}, {57, <<"57">>}, {58, <<"58">>}, {59, <<"59">>} ], [{I, fun() -> O = pad_int(I) end} || {I, O} <- Tests]. - -endif. diff --git a/src/cowboy_handler.erl b/src/cowboy_handler.erl index e431ba8..7780e20 100644 --- a/src/cowboy_handler.erl +++ b/src/cowboy_handler.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,7 +12,7 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc Handler middleware. +%% Handler middleware. %% %% Execute the handler given by the <em>handler</em> and <em>handler_opts</em> %% environment values. The result of this execution is added to the @@ -27,8 +27,6 @@ %% by default. This can be configured through the <em>loop_max_buffer</em> %% environment value. The request will be terminated with an %% <em>{error, overflow}</em> reason if this threshold is reached. -%% -%% @see cowboy_http_handler -module(cowboy_handler). -behaviour(cowboy_middleware). @@ -45,7 +43,6 @@ resp_sent = false :: boolean() }). -%% @private -spec execute(Req, Env) -> {ok, Req, Env} | {error, 500, Req} | {suspend, ?MODULE, handler_loop, [any()]} @@ -148,8 +145,6 @@ handler_after_callback(Req, State=#state{resp_sent=false}, Handler, handler_after_callback(Req, State, Handler, HandlerState) -> handler_before_loop(Req, State, Handler, HandlerState). -%% We don't listen for Transport closes because that would force us -%% to receive data and buffer it indefinitely. -spec handler_before_loop(Req, #state{}, module(), any()) -> {ok, Req, cowboy_middleware:env()} | {error, 500, Req} | {suspend, module(), atom(), [any()]} @@ -177,7 +172,6 @@ handler_loop_timeout(State=#state{loop_timeout=Timeout, TRef = erlang:start_timer(Timeout, self(), ?MODULE), State#state{loop_timeout_ref=TRef}. -%% @private -spec handler_loop(Req, #state{}, module(), any()) -> {ok, Req, cowboy_middleware:env()} | {error, 500, Req} | {suspend, module(), atom(), [any()]} diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl index ea454d6..1cf73bf 100644 --- a/src/cowboy_http.erl +++ b/src/cowboy_http.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% Copyright (c) 2011, Anthony Ramine <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any @@ -13,7 +13,7 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc Core HTTP parsing API. +%% Deprecated HTTP parsing API. -module(cowboy_http). %% Parsing. @@ -40,19 +40,10 @@ -export([parameterized_tokens/1]). %% Decoding. --export([te_chunked/2]). --export([te_identity/2]). -export([ce_identity/1]). -%% Interpretation. --export([urldecode/1]). --export([urldecode/2]). --export([urlencode/1]). --export([urlencode/2]). - %% Parsing. -%% @doc Parse a non-empty list of the given type. -spec nonempty_list(binary(), fun()) -> [any(), ...] | {error, badarg}. nonempty_list(Data, Fun) -> case list(Data, Fun, []) of @@ -61,7 +52,6 @@ nonempty_list(Data, Fun) -> L -> lists:reverse(L) end. -%% @doc Parse a list of the given type. -spec list(binary(), fun()) -> list() | {error, badarg}. list(Data, Fun) -> case list(Data, Fun, []) of @@ -89,8 +79,6 @@ list(Data, Fun, Acc) -> end) end). -%% @doc Parse a content type. -%% %% We lowercase the charset header as we know it's case insensitive. -spec content_type(binary()) -> any(). content_type(Data) -> @@ -112,7 +100,6 @@ content_type(Data) -> end) end). -%% @doc Parse a media range. -spec media_range(binary(), fun()) -> any(). media_range(Data, Fun) -> media_type(Data, @@ -155,7 +142,6 @@ media_range_param_value(Data, Fun, Type, SubType, Acc, Attr) -> Type, SubType, [{Attr, Value}|Acc]) end). -%% @doc Parse a media type. -spec media_type(binary(), fun()) -> any(). media_type(Data, Fun) -> token_ci(Data, @@ -215,8 +201,6 @@ accept_ext_value(Data, Fun, Type, SubType, Params, Quality, Acc, Attr) -> Type, SubType, Params, Quality, [{Attr, Value}|Acc]) end). -%% @doc Parse a conneg header (Accept-Charset, Accept-Encoding), -%% followed by an optional quality value. -spec conneg(binary(), fun()) -> any(). conneg(Data, Fun) -> token_ci(Data, @@ -228,7 +212,6 @@ conneg(Data, Fun) -> end) end). -%% @doc Parse a language range, followed by an optional quality value. -spec language_range(binary(), fun()) -> any(). language_range(<< $*, Rest/binary >>, Fun) -> language_range_ret(Rest, Fun, '*'); @@ -289,12 +272,10 @@ maybe_qparam(Data, Fun) -> Fun(Rest, 1000) end). -%% @doc Parse a quality parameter string (for example q=0.500). -spec qparam(binary(), fun()) -> any(). qparam(<< Q, $=, Data/binary >>, Fun) when Q =:= $q; Q =:= $Q -> qvalue(Data, Fun). -%% @doc Parse either a list of entity tags or a "*". -spec entity_tag_match(binary()) -> any(). entity_tag_match(<< $*, Rest/binary >>) -> whitespace(Rest, @@ -304,7 +285,6 @@ entity_tag_match(<< $*, Rest/binary >>) -> entity_tag_match(Data) -> nonempty_list(Data, fun entity_tag/2). -%% @doc Parse an entity-tag. -spec entity_tag(binary(), fun()) -> any(). entity_tag(<< "W/", Rest/binary >>, Fun) -> opaque_tag(Rest, Fun, weak); @@ -318,7 +298,6 @@ opaque_tag(Data, Fun, Strength) -> (Rest, OpaqueTag) -> Fun(Rest, {Strength, OpaqueTag}) end). -%% @doc Parse an expectation. -spec expectation(binary(), fun()) -> any(). expectation(Data, Fun) -> token_ci(Data, @@ -334,7 +313,6 @@ expectation(Data, Fun) -> Fun(Rest, Expectation) end). -%% @doc Parse a list of parameters (a=b;c=d). -spec params(binary(), fun()) -> any(). params(Data, Fun) -> params(Data, Fun, []). @@ -366,9 +344,6 @@ param(Data, Fun) -> end) end). -%% @doc Parse an HTTP date (RFC1123, RFC850 or asctime date). -%% @end -%% %% While this may not be the most efficient date parsing we can do, %% it should work fine for our purposes because all HTTP dates should %% be sent as RFC1123 dates in HTTP/1.1. @@ -391,7 +366,6 @@ http_date(Data) -> HTTPDate end. -%% @doc Parse an RFC1123 date. -spec rfc1123_date(binary()) -> any(). rfc1123_date(Data) -> wkday(Data, @@ -411,7 +385,6 @@ rfc1123_date(Data) -> {error, badarg} end). -%% @doc Parse an RFC850 date. -spec rfc850_date(binary()) -> any(). %% From the RFC: %% HTTP/1.1 clients and caches SHOULD assume that an RFC-850 date @@ -435,7 +408,6 @@ rfc850_date(Data) -> {error, badarg} end). -%% @doc Parse an asctime date. -spec asctime_date(binary()) -> any(). asctime_date(Data) -> wkday(Data, @@ -594,7 +566,6 @@ time(<< H1, H2, ":", M1, M2, ":", S1, S2, Rest/binary >>, Fun) {error, badarg} end. -%% @doc Skip whitespace. -spec whitespace(binary(), fun()) -> any(). whitespace(<< C, Rest/binary >>, Fun) when C =:= $\s; C =:= $\t -> @@ -602,7 +573,6 @@ whitespace(<< C, Rest/binary >>, Fun) whitespace(Data, Fun) -> Fun(Data). -%% @doc Parse a list of digits as a non negative integer. -spec digits(binary()) -> non_neg_integer() | {error, badarg}. digits(Data) -> digits(Data, @@ -629,8 +599,6 @@ digits(<< C, Rest/binary >>, Fun, Acc) digits(Data, Fun, Acc) -> Fun(Data, Acc). -%% @doc Parse a list of case-insensitive alpha characters. -%% %% Changes all characters to lowercase. -spec alpha(binary(), fun()) -> any(). alpha(Data, Fun) -> @@ -673,14 +641,11 @@ word(Data, Fun) -> (Rest, Token) -> Fun(Rest, Token) end). -%% @doc Parse a case-insensitive token. -%% %% Changes all characters to lowercase. -spec token_ci(binary(), fun()) -> any(). token_ci(Data, Fun) -> token(Data, Fun, ci, <<>>). -%% @doc Parse a token. -spec token(binary(), fun()) -> any(). token(Data, Fun) -> token(Data, Fun, cs, <<>>). @@ -701,7 +666,6 @@ token(<< C, Rest/binary >>, Fun, Case = ci, Acc) -> token(<< C, Rest/binary >>, Fun, Case, Acc) -> token(Rest, Fun, Case, << Acc/binary, C >>). -%% @doc Parse a quoted string. -spec quoted_string(binary(), fun()) -> any(). quoted_string(<< $", Rest/binary >>, Fun) -> quoted_string(Rest, Fun, <<>>). @@ -716,7 +680,6 @@ quoted_string(<< $\\, C, Rest/binary >>, Fun, Acc) -> quoted_string(<< C, Rest/binary >>, Fun, Acc) -> quoted_string(Rest, Fun, << Acc/binary, C >>). -%% @doc Parse a quality value. -spec qvalue(binary(), fun()) -> any(). qvalue(<< $0, $., Rest/binary >>, Fun) -> qvalue(Rest, Fun, 0, 100); @@ -745,8 +708,7 @@ qvalue(<< C, Rest/binary >>, Fun, Q, M) qvalue(Data, Fun, Q, _M) -> Fun(Data, Q). -%% @doc Parse authorization value according rfc 2617. -%% Only Basic authorization is supported so far. +%% Only RFC2617 Basic authorization is supported so far. -spec authorization(binary(), binary()) -> {binary(), any()} | {error, badarg}. authorization(UserPass, Type = <<"basic">>) -> whitespace(UserPass, @@ -762,7 +724,6 @@ authorization(UserPass, Type = <<"basic">>) -> authorization(String, Type) -> whitespace(String, fun(Rest) -> {Type, Rest} end). -%% @doc Parse user credentials. -spec authorization_basic_userid(binary(), fun()) -> any(). authorization_basic_userid(Data, Fun) -> authorization_basic_userid(Data, Fun, <<>>). @@ -781,8 +742,6 @@ authorization_basic_userid(<<C, Rest/binary>>, Fun, Acc) -> authorization_basic_password(Data, Fun) -> authorization_basic_password(Data, Fun, <<>>). -authorization_basic_password(<<>>, _Fun, <<>>) -> - {error, badarg}; authorization_basic_password(<<C, _Rest/binary>>, _Fun, _Acc) when C < 32; C=:= 127 -> {error, badarg}; @@ -791,7 +750,6 @@ authorization_basic_password(<<>>, Fun, Acc) -> authorization_basic_password(<<C, Rest/binary>>, Fun, Acc) -> authorization_basic_password(Rest, Fun, <<Acc/binary, C>>). -%% @doc Parse range header according rfc 2616. -spec range(binary()) -> {Unit, [Range]} | {error, badarg} when Unit :: binary(), Range :: {non_neg_integer(), non_neg_integer() | infinity} | neg_integer(). @@ -849,7 +807,6 @@ range_digits(Data, Default, Fun) -> Fun(Data, Default) end). -%% @doc Parse a non empty list of tokens followed with optional parameters. -spec parameterized_tokens(binary()) -> any(). parameterized_tokens(Data) -> nonempty_list(Data, @@ -894,170 +851,15 @@ parameterized_tokens_param(Data, Fun) -> %% Decoding. -%% @doc Decode a stream of chunks. --spec te_chunked(Bin, TransferState) - -> more | {more, non_neg_integer(), Bin, TransferState} - | {ok, Bin, Bin, TransferState} - | {done, non_neg_integer(), Bin} | {error, badarg} - when Bin::binary(), TransferState::{non_neg_integer(), non_neg_integer()}. -te_chunked(<< "0\r\n\r\n", Rest/binary >>, {0, Streamed}) -> - {done, Streamed, Rest}; -te_chunked(Data, {0, Streamed}) -> - %% @todo We are expecting an hex size, not a general token. - token(Data, - fun (<< "\r\n", Rest/binary >>, BinLen) -> - case list_to_integer(binary_to_list(BinLen), 16) of - %% Final chunk is parsed in one go above. Rest would be - %% <<\r\n">> if complete. - 0 when byte_size(Rest) < 2 -> - more; - %% Normal chunk. Add 2 to Len for trailing <<"\r\n">>. Note - %% that repeated <<"-2\r\n">> would be streamed, and - %% accumulated, until out of memory if Len could be -2. - Len when Len > 0 -> - te_chunked(Rest, {Len + 2, Streamed}) - end; - %% Chunk size shouldn't take too many bytes, - %% don't try to stream forever. - (Rest, _) when byte_size(Rest) < 16 -> - more; - (_, _) -> - {error, badarg} - end); -%% <<"\n">> from trailing <<"\r\n">>. -te_chunked(<< "\n", Rest/binary>>, {1, Streamed}) -> - {ok, <<>>, Rest, {0, Streamed}}; -te_chunked(<<>>, State={1, _Streamed}) -> - {more, 1, <<>>, State}; -%% Remainder of chunk (if any) and as much of trailing <<"\r\n">> as possible. -te_chunked(Data, {ChunkRem, Streamed}) when byte_size(Data) >= ChunkRem - 2 -> - ChunkSize = ChunkRem - 2, - Streamed2 = Streamed + ChunkSize, - case Data of - << Chunk:ChunkSize/binary, "\r\n", Rest/binary >> -> - {ok, Chunk, Rest, {0, Streamed2}}; - << Chunk:ChunkSize/binary, "\r" >> -> - {more, 1, Chunk, {1, Streamed2}}; - << Chunk:ChunkSize/binary >> -> - {more, 2, Chunk, {2, Streamed2}} - end; -%% Incomplete chunk. -te_chunked(Data, {ChunkRem, Streamed}) -> - ChunkRem2 = ChunkRem - byte_size(Data), - Streamed2 = Streamed + byte_size(Data), - {more, ChunkRem2, Data, {ChunkRem2, Streamed2}}. - -%% @doc Decode an identity stream. --spec te_identity(Bin, TransferState) - -> {more, non_neg_integer(), Bin, TransferState} - | {done, Bin, non_neg_integer(), Bin} - when Bin::binary(), TransferState::{non_neg_integer(), non_neg_integer()}. -te_identity(Data, {Streamed, Total}) - when Streamed + byte_size(Data) < Total -> - Streamed2 = Streamed + byte_size(Data), - {more, Total - Streamed2, Data, {Streamed2, Total}}; -te_identity(Data, {Streamed, Total}) -> - Size = Total - Streamed, - << Data2:Size/binary, Rest/binary >> = Data, - {done, Data2, Total, Rest}. - -%% @doc Decode an identity content. +%% @todo Move this to cowlib too I suppose. :-) -spec ce_identity(binary()) -> {ok, binary()}. ce_identity(Data) -> {ok, Data}. -%% Interpretation. - -%% @doc Decode a URL encoded binary. -%% @equiv urldecode(Bin, crash) --spec urldecode(binary()) -> binary(). -urldecode(Bin) when is_binary(Bin) -> - urldecode(Bin, <<>>, crash). - -%% @doc Decode a URL encoded binary. -%% The second argument specifies how to handle percent characters that are not -%% followed by two valid hex characters. Use `skip' to ignore such errors, -%% if `crash' is used the function will fail with the reason `badarg'. --spec urldecode(binary(), crash | skip) -> binary(). -urldecode(Bin, OnError) when is_binary(Bin) -> - urldecode(Bin, <<>>, OnError). - --spec urldecode(binary(), binary(), crash | skip) -> binary(). -urldecode(<<$%, H, L, Rest/binary>>, Acc, OnError) -> - G = unhex(H), - M = unhex(L), - if G =:= error; M =:= error -> - case OnError of skip -> ok; crash -> erlang:error(badarg) end, - urldecode(<<H, L, Rest/binary>>, <<Acc/binary, $%>>, OnError); - true -> - urldecode(Rest, <<Acc/binary, (G bsl 4 bor M)>>, OnError) - end; -urldecode(<<$%, Rest/binary>>, Acc, OnError) -> - case OnError of skip -> ok; crash -> erlang:error(badarg) end, - urldecode(Rest, <<Acc/binary, $%>>, OnError); -urldecode(<<$+, Rest/binary>>, Acc, OnError) -> - urldecode(Rest, <<Acc/binary, $ >>, OnError); -urldecode(<<C, Rest/binary>>, Acc, OnError) -> - urldecode(Rest, <<Acc/binary, C>>, OnError); -urldecode(<<>>, Acc, _OnError) -> - Acc. - --spec unhex(byte()) -> byte() | error. -unhex(C) when C >= $0, C =< $9 -> C - $0; -unhex(C) when C >= $A, C =< $F -> C - $A + 10; -unhex(C) when C >= $a, C =< $f -> C - $a + 10; -unhex(_) -> error. - - -%% @doc URL encode a string binary. -%% @equiv urlencode(Bin, []) --spec urlencode(binary()) -> binary(). -urlencode(Bin) -> - urlencode(Bin, []). - -%% @doc URL encode a string binary. -%% The `noplus' option disables the default behaviour of quoting space -%% characters, `\s', as `+'. The `upper' option overrides the default behaviour -%% of writing hex numbers using lowecase letters to using uppercase letters -%% instead. --spec urlencode(binary(), [noplus|upper]) -> binary(). -urlencode(Bin, Opts) -> - Plus = not lists:member(noplus, Opts), - Upper = lists:member(upper, Opts), - urlencode(Bin, <<>>, Plus, Upper). - --spec urlencode(binary(), binary(), boolean(), boolean()) -> binary(). -urlencode(<<C, Rest/binary>>, Acc, P=Plus, U=Upper) -> - if C >= $0, C =< $9 -> urlencode(Rest, <<Acc/binary, C>>, P, U); - C >= $A, C =< $Z -> urlencode(Rest, <<Acc/binary, C>>, P, U); - C >= $a, C =< $z -> urlencode(Rest, <<Acc/binary, C>>, P, U); - C =:= $.; C =:= $-; C =:= $~; C =:= $_ -> - urlencode(Rest, <<Acc/binary, C>>, P, U); - C =:= $ , Plus -> - urlencode(Rest, <<Acc/binary, $+>>, P, U); - true -> - H = C band 16#F0 bsr 4, L = C band 16#0F, - H1 = if Upper -> tohexu(H); true -> tohexl(H) end, - L1 = if Upper -> tohexu(L); true -> tohexl(L) end, - urlencode(Rest, <<Acc/binary, $%, H1, L1>>, P, U) - end; -urlencode(<<>>, Acc, _Plus, _Upper) -> - Acc. - --spec tohexu(byte()) -> byte(). -tohexu(C) when C < 10 -> $0 + C; -tohexu(C) when C < 16 -> $A + C - 10. - --spec tohexl(byte()) -> byte(). -tohexl(C) when C < 10 -> $0 + C; -tohexl(C) when C < 16 -> $a + C - 10. - %% Tests. -ifdef(TEST). - nonempty_charset_list_test_() -> - %% {Value, Result} Tests = [ {<<>>, {error, badarg}}, {<<"iso-8859-5, unicode-1-1;q=0.8">>, [ @@ -1074,7 +876,6 @@ nonempty_charset_list_test_() -> [{V, fun() -> R = nonempty_list(V, fun conneg/2) end} || {V, R} <- Tests]. nonempty_language_range_list_test_() -> - %% {Value, Result} Tests = [ {<<"da, en-gb;q=0.8, en;q=0.7">>, [ {<<"da">>, 1000}, @@ -1094,7 +895,6 @@ nonempty_language_range_list_test_() -> || {V, R} <- Tests]. nonempty_token_list_test_() -> - %% {Value, Result} Tests = [ {<<>>, {error, badarg}}, {<<" ">>, {error, badarg}}, @@ -1110,7 +910,6 @@ nonempty_token_list_test_() -> [{V, fun() -> R = nonempty_list(V, fun token/2) end} || {V, R} <- Tests]. media_range_list_test_() -> - %% {Tokens, Result} Tests = [ {<<"audio/*; q=0.2, audio/basic">>, [ {{<<"audio">>, <<"*">>, []}, 200, []}, @@ -1155,7 +954,6 @@ media_range_list_test_() -> [{V, fun() -> R = list(V, fun media_range/2) end} || {V, R} <- Tests]. entity_tag_match_test_() -> - %% {Tokens, Result} Tests = [ {<<"\"xyzzy\"">>, [{strong, <<"xyzzy">>}]}, {<<"\"xyzzy\", W/\"r2d2xxxx\", \"c3piozzzz\"">>, @@ -1167,7 +965,6 @@ entity_tag_match_test_() -> [{V, fun() -> R = entity_tag_match(V) end} || {V, R} <- Tests]. http_date_test_() -> - %% {Tokens, Result} Tests = [ {<<"Sun, 06 Nov 1994 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}}, {<<"Sunday, 06-Nov-94 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}}, @@ -1176,28 +973,24 @@ http_date_test_() -> [{V, fun() -> R = http_date(V) end} || {V, R} <- Tests]. rfc1123_date_test_() -> - %% {Tokens, Result} Tests = [ {<<"Sun, 06 Nov 1994 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}} ], [{V, fun() -> R = rfc1123_date(V) end} || {V, R} <- Tests]. rfc850_date_test_() -> - %% {Tokens, Result} Tests = [ {<<"Sunday, 06-Nov-94 08:49:37 GMT">>, {{1994, 11, 6}, {8, 49, 37}}} ], [{V, fun() -> R = rfc850_date(V) end} || {V, R} <- Tests]. asctime_date_test_() -> - %% {Tokens, Result} Tests = [ {<<"Sun Nov 6 08:49:37 1994">>, {{1994, 11, 6}, {8, 49, 37}}} ], [{V, fun() -> R = asctime_date(V) end} || {V, R} <- Tests]. content_type_test_() -> - %% {ContentType, Result} Tests = [ {<<"text/plain; charset=iso-8859-4">>, {<<"text">>, <<"plain">>, [{<<"charset">>, <<"iso-8859-4">>}]}}, @@ -1214,7 +1007,6 @@ content_type_test_() -> [{V, fun () -> R = content_type(V) end} || {V, R} <- Tests]. parameterized_tokens_test_() -> - %% {ParameterizedTokens, Result} Tests = [ {<<"foo">>, [{<<"foo">>, []}]}, {<<"bar; baz=2">>, [{<<"bar">>, [{<<"baz">>, <<"2">>}]}]}, @@ -1225,7 +1017,6 @@ parameterized_tokens_test_() -> [{V, fun () -> R = parameterized_tokens(V) end} || {V, R} <- Tests]. digits_test_() -> - %% {Digits, Result} Tests = [ {<<"42 ">>, 42}, {<<"69\t">>, 69}, @@ -1233,45 +1024,12 @@ digits_test_() -> ], [{V, fun() -> R = digits(V) end} || {V, R} <- Tests]. -urldecode_test_() -> - F = fun(Qs, O) -> - try urldecode(Qs, O) of - R -> - {ok, R} - catch _:E -> - {error, E} - end - end, - Tests = [ - {<<"%20">>, crash, {ok, <<" ">>}}, - {<<"+">>, crash, {ok, <<" ">>}}, - {<<"%00">>, crash, {ok, <<0>>}}, - {<<"%fF">>, crash, {ok, <<255>>}}, - {<<"123">>, crash, {ok, <<"123">>}}, - {<<"%i5">>, skip, {ok, <<"%i5">>}}, - {<<"%5">>, skip, {ok, <<"%5">>}}, - {<<"%i5">>, crash, {error, badarg}}, - {<<"%5">>, crash, {error, badarg}} - ], - [{Qs, fun() -> R = F(Qs,O) end} || {Qs, O, R} <- Tests]. - -urlencode_test_() -> - Tests = [ - {<<255,0>>, [], <<"%ff%00">>}, - {<<255,0>>, [upper], <<"%FF%00">>}, - {<<" ">>, [], <<"+">>}, - {<<" ">>, [noplus], <<"%20">>}, - {<<"aBc">>, [], <<"aBc">>}, - {<<".-~_">>, [], <<".-~_">>} - ], - Tests2 = [{<<255, " ">>,<<"%ff+">>}], - [{V, fun() -> R = urlencode(V, O) end} || {V, O, R} <- Tests] ++ - [{V, fun() -> R = urlencode(V) end} || {V, R} <- Tests2]. - http_authorization_test_() -> Tests = [ {<<"basic">>, <<"QWxsYWRpbjpvcGVuIHNlc2FtZQ==">>, {<<"basic">>, {<<"Alladin">>, <<"open sesame">>}}}, + {<<"basic">>, <<"dXNlcm5hbWU6">>, + {<<"basic">>, {<<"username">>, <<>>}}}, {<<"basic">>, <<"dXNlcm5hbWUK">>, {error, badarg}}, {<<"basic">>, <<"_[]@#$%^&*()-AA==">>, @@ -1305,5 +1063,4 @@ http_range_test_() -> {error, badarg}} ], [fun() -> R = range(V) end ||{V, R} <- Tests]. - -endif. diff --git a/src/cowboy_http_handler.erl b/src/cowboy_http_handler.erl index 3ad8f88..14c7987 100644 --- a/src/cowboy_http_handler.erl +++ b/src/cowboy_http_handler.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,23 +12,6 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc Behaviour for short-lived HTTP handlers. -%% -%% <em>init/3</em> allows you to initialize a state for all subsequent -%% callbacks, and indicate to Cowboy whether you accept to handle the -%% request or want to shutdown without handling it, in which case the -%% <em>handle/2</em> call will simply be skipped. -%% -%% <em>handle/2</em> allows you to handle the request. It receives the -%% state previously defined. -%% -%% <em>terminate/3</em> allows you to clean up. It receives the -%% termination reason and the state previously defined. -%% -%% There is no required operation to perform in any of these callbacks -%% other than returning the proper values. Make sure you always return -%% the last modified Req so that Cowboy has the up to date information -%% about the request. -module(cowboy_http_handler). -type opts() :: any(). diff --git a/src/cowboy_loop_handler.erl b/src/cowboy_loop_handler.erl index af49e57..edef77f 100644 --- a/src/cowboy_loop_handler.erl +++ b/src/cowboy_loop_handler.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,29 +12,6 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc Behaviour for long-lived HTTP handlers. -%% -%% <em>init/3</em> allows you to initialize a state for all subsequent -%% callbacks, and indicate to Cowboy whether you accept to handle the -%% request or want to shutdown without handling it, in which case the -%% receive loop and <em>info/3</em> calls will simply be skipped. -%% -%% <em>info/3</em> allows you to handle the messages this process will -%% receive. It receives the message and the state previously defined. -%% It can decide to stop the receive loop or continue receiving. -%% -%% <em>terminate/3</em> allows you to clean up. It receives the -%% termination reason and the state previously defined. -%% -%% There is no required operation to perform in any of these callbacks -%% other than returning the proper values. Make sure you always return -%% the last modified Req so that Cowboy has the up to date information -%% about the request. -%% -%% It is recommended to use hibernate if this process is not going to -%% receive a lot of messages. It is also recommended to use a timeout -%% value so that the connection gets closed after a long period of -%% inactivity. -module(cowboy_loop_handler). -type opts() :: any(). diff --git a/src/cowboy_middleware.erl b/src/cowboy_middleware.erl index 40c9407..fa0f5bc 100644 --- a/src/cowboy_middleware.erl +++ b/src/cowboy_middleware.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2013-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,17 +12,6 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc Behaviour for middlewares. -%% -%% Only one function needs to be implemented, <em>execute/2</em>. -%% It receives the Req and the environment and returns them -%% optionally modified. It can decide to stop the processing with -%% or without an error. It is also possible to hibernate the process -%% if needed. -%% -%% A middleware can perform any operation. Make sure you always return -%% the last modified Req so that Cowboy has the up to date information -%% about the request. -module(cowboy_middleware). -type env() :: [{atom(), any()}]. diff --git a/src/cowboy_multipart.erl b/src/cowboy_multipart.erl deleted file mode 100644 index 4df5a27..0000000 --- a/src/cowboy_multipart.erl +++ /dev/null @@ -1,313 +0,0 @@ -%% Copyright (c) 2011, Anthony Ramine <[email protected]> -%% -%% 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. - -%% @doc Multipart parser. --module(cowboy_multipart). - --export([parser/1]). --export([content_disposition/1]). - --type part_parser() :: parser(more(part_result())). --type parser(T) :: fun((binary()) -> T). --type more(T) :: T | {more, parser(T)}. --type part_result() :: headers() | eof. --type headers() :: {headers, http_headers(), body_cont()}. --type http_headers() :: [{binary(), binary()}]. --type body_cont() :: cont(more(body_result())). --type cont(T) :: fun(() -> T). --type body_result() :: {body, binary(), body_cont()} | end_of_part(). --type end_of_part() :: {end_of_part, cont(more(part_result()))}. --type disposition() :: {binary(), [{binary(), binary()}]}. - -%% API. - -%% @doc Return a multipart parser for the given boundary. --spec parser(binary()) -> part_parser(). -parser(Boundary) when is_binary(Boundary) -> - fun (Bin) when is_binary(Bin) -> parse(Bin, Boundary) end. - -%% @doc Parse a content disposition. -%% @todo Parse the MIME header instead of the HTTP one. --spec content_disposition(binary()) -> disposition(). -content_disposition(Data) -> - cowboy_http:token_ci(Data, - fun (_Rest, <<>>) -> {error, badarg}; - (Rest, Disposition) -> - cowboy_http:params(Rest, - fun (<<>>, Params) -> {Disposition, Params}; - (_Rest2, _) -> {error, badarg} - end) - end). - -%% Internal. - -%% @doc Entry point of the multipart parser, skips over the preamble if any. --spec parse(binary(), binary()) -> more(part_result()). -parse(Bin, Boundary) when byte_size(Bin) >= byte_size(Boundary) + 2 -> - BoundarySize = byte_size(Boundary), - Pattern = pattern(Boundary), - case Bin of - <<"--", Boundary:BoundarySize/binary, Rest/binary>> -> - % Data starts with initial boundary, skip preamble parsing. - parse_boundary_tail(Rest, Pattern); - _ -> - % Parse preamble. - skip(Bin, Pattern) - end; -parse(Bin, Boundary) -> - % Not enough data to know if the data begins with a boundary. - more(Bin, fun (NewBin) -> parse(NewBin, Boundary) end). - --type pattern() :: {binary:cp(), non_neg_integer()}. --type patterns() :: {pattern(), pattern()}. - -%% @doc Return two compiled binary patterns with their sizes in bytes. -%% The boundary pattern is the boundary prepended with "\r\n--". -%% The boundary suffix pattern matches all prefixes of the boundary. --spec pattern(binary()) -> patterns(). -pattern(Boundary) -> - MatchPattern = <<"\r\n--", Boundary/binary>>, - MatchPrefixes = prefixes(MatchPattern), - {{binary:compile_pattern(MatchPattern), byte_size(MatchPattern)}, - {binary:compile_pattern(MatchPrefixes), byte_size(MatchPattern)}}. - -%% @doc Return all prefixes of a binary string. -%% The list of prefixes includes the full string. --spec prefixes(binary()) -> [binary()]. -prefixes(<<C, Rest/binary>>) -> - prefixes(Rest, <<C>>). - --spec prefixes(binary(), binary()) -> [binary()]. -prefixes(<<C, Rest/binary>>, Acc) -> - [Acc|prefixes(Rest, <<Acc/binary, C>>)]; -prefixes(<<>>, Acc) -> - [Acc]. - -%% @doc Test if a boundary is a possble suffix. -%% The patterns are expected to have been returned from `pattern/1'. --spec suffix_match(binary(), patterns()) -> nomatch | {integer(), integer()}. -suffix_match(Bin, {_Boundary, {Pat, Len}}) -> - Size = byte_size(Bin), - suffix_match(Bin, Pat, Size, max(-Size, -Len)). - --spec suffix_match(binary(), binary:cp(), non_neg_integer(), 0|neg_integer()) -> - nomatch | {integer(), integer()}. -suffix_match(_Bin, _Pat, _Size, _Match=0) -> - nomatch; -suffix_match(Bin, Pat, Size, Match) when Match < 0 -> - case binary:match(Bin, Pat, [{scope, {Size, Match}}]) of - {Pos, Len}=Part when Pos + Len =:= Size -> Part; - {_, Len} -> suffix_match(Bin, Pat, Size, Match + Len); - nomatch -> nomatch - end. - -%% @doc Parse remaining characters of a line beginning with the boundary. -%% If followed by "--", <em>eof</em> is returned and parsing is finished. --spec parse_boundary_tail(binary(), patterns()) -> more(part_result()). -parse_boundary_tail(Bin, Pattern) when byte_size(Bin) >= 2 -> - case Bin of - <<"--", _Rest/binary>> -> - % Boundary is followed by "--", end parsing. - eof; - _ -> - % No dash after boundary, proceed with unknown chars and lwsp - % removal. - parse_boundary_eol(Bin, Pattern) - end; -parse_boundary_tail(Bin, Pattern) -> - % Boundary may be followed by "--", need more data. - more(Bin, fun (NewBin) -> parse_boundary_tail(NewBin, Pattern) end). - -%% @doc Skip whitespace and unknown chars until CRLF. --spec parse_boundary_eol(binary(), patterns()) -> more(part_result()). -parse_boundary_eol(Bin, Pattern) -> - case binary:match(Bin, <<"\r\n">>) of - {CrlfStart, _Length} -> - % End of line found, remove optional whitespace. - <<_:CrlfStart/binary, Rest/binary>> = Bin, - Fun = fun (Rest2) -> parse_boundary_crlf(Rest2, Pattern) end, - cowboy_http:whitespace(Rest, Fun); - nomatch -> - % CRLF not found in the given binary. - RestStart = max(byte_size(Bin) - 1, 0), - <<_:RestStart/binary, Rest/binary>> = Bin, - more(Rest, fun (NewBin) -> parse_boundary_eol(NewBin, Pattern) end) - end. - --spec parse_boundary_crlf(binary(), patterns()) -> more(part_result()). -parse_boundary_crlf(<<"\r\n", Rest/binary>>, Pattern) -> - % The binary is at least 2 bytes long as this function is only called by - % parse_boundary_eol/3 when CRLF has been found so a more tuple will never - % be returned from here. - parse_headers(Rest, Pattern); -parse_boundary_crlf(Bin, Pattern) -> - % Unspecified behaviour here: RFC 2046 doesn't say what to do when LWSP is - % not followed directly by a new line. In this implementation it is - % considered part of the boundary so EOL needs to be searched again. - parse_boundary_eol(Bin, Pattern). - --spec parse_headers(binary(), patterns()) -> more(part_result()). -parse_headers(Bin, Pattern) -> - parse_headers(Bin, Pattern, []). - --spec parse_headers(binary(), patterns(), http_headers()) -> more(part_result()). -parse_headers(Bin, Pattern, Acc) -> - case erlang:decode_packet(httph_bin, Bin, []) of - {ok, {http_header, _, Name, _, Value}, Rest} -> - Name2 = case is_atom(Name) of - true -> cowboy_bstr:to_lower(atom_to_binary(Name, latin1)); - false -> cowboy_bstr:to_lower(Name) - end, - parse_headers(Rest, Pattern, [{Name2, Value} | Acc]); - {ok, http_eoh, Rest} -> - Headers = lists:reverse(Acc), - {headers, Headers, fun () -> parse_body(Rest, Pattern) end}; - {ok, {http_error, _}, _} -> - % Skip malformed parts. - skip(Bin, Pattern); - {more, _} -> - more(Bin, fun (NewBin) -> parse_headers(NewBin, Pattern, Acc) end) - end. - --spec parse_body(binary(), patterns()) -> more(body_result()). -parse_body(Bin, Pattern = {{P, PSize}, _}) when byte_size(Bin) >= PSize -> - case binary:match(Bin, P) of - {0, _Length} -> - <<_:PSize/binary, Rest/binary>> = Bin, - end_of_part(Rest, Pattern); - {BoundaryStart, _Length} -> - % Boundary found, this is the latest partial body that will be - % returned for this part. - <<PBody:BoundaryStart/binary, _:PSize/binary, Rest/binary>> = Bin, - FResult = end_of_part(Rest, Pattern), - {body, PBody, fun () -> FResult end}; - nomatch -> - case suffix_match(Bin, Pattern) of - nomatch -> - %% Prefix of boundary not found at end of input. it's - %% safe to return the whole binary. Saves copying of - %% next input onto tail of current input binary. - {body, Bin, fun () -> parse_body(<<>>, Pattern) end}; - {BoundaryStart, Len} -> - PBody = binary:part(Bin, 0, BoundaryStart), - Rest = binary:part(Bin, BoundaryStart, Len), - {body, PBody, fun () -> parse_body(Rest, Pattern) end} - end - end; -parse_body(Bin, Pattern) -> - more(Bin, fun (NewBin) -> parse_body(NewBin, Pattern) end). - --spec end_of_part(binary(), patterns()) -> end_of_part(). -end_of_part(Bin, Pattern) -> - {end_of_part, fun () -> parse_boundary_tail(Bin, Pattern) end}. - --spec skip(binary(), patterns()) -> more(part_result()). -skip(Bin, Pattern = {{P, PSize}, _}) -> - case binary:match(Bin, P) of - {BoundaryStart, _Length} -> - % Boundary found, proceed with parsing of the next part. - RestStart = BoundaryStart + PSize, - <<_:RestStart/binary, Rest/binary>> = Bin, - parse_boundary_tail(Rest, Pattern); - nomatch -> - % Boundary not found, need more data. - RestStart = max(byte_size(Bin) - PSize + 1, 0), - <<_:RestStart/binary, Rest/binary>> = Bin, - more(Rest, fun (NewBin) -> skip(NewBin, Pattern) end) - end. - --spec more(binary(), parser(T)) -> {more, parser(T)}. -more(<<>>, F) -> - {more, F}; -more(Bin, InnerF) -> - F = fun (NewData) when is_binary(NewData) -> - InnerF(<<Bin/binary, NewData/binary>>) - end, - {more, F}. - -%% Tests. - --ifdef(TEST). - -multipart_test_() -> - %% {Body, Result} - Tests = [ - {<<"--boundary--">>, []}, - {<<"preamble\r\n--boundary--">>, []}, - {<<"--boundary--\r\nepilogue">>, []}, - {<<"\r\n--boundary\r\nA:b\r\nC:d\r\n\r\n\r\n--boundary--">>, - [{[{<<"a">>, <<"b">>}, {<<"c">>, <<"d">>}], <<>>}]}, - { - << - "--boundary\r\nX-Name:answer\r\n\r\n42" - "\r\n--boundary\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n" - "\r\n--boundary--" - >>, - [ - {[{<<"x-name">>, <<"answer">>}], <<"42">>}, - {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>} - ] - } - ], - [{title(V), fun () -> R = acc_multipart(V) end} || {V, R} <- Tests]. - -acc_multipart(V) -> - acc_multipart((parser(<<"boundary">>))(V), []). - -acc_multipart({headers, Headers, Cont}, Acc) -> - acc_multipart(Cont(), [{Headers, []}|Acc]); -acc_multipart({body, Body, Cont}, [{Headers, BodyAcc}|Acc]) -> - acc_multipart(Cont(), [{Headers, [Body|BodyAcc]}|Acc]); -acc_multipart({end_of_part, Cont}, [{Headers, BodyAcc}|Acc]) -> - Body = list_to_binary(lists:reverse(BodyAcc)), - acc_multipart(Cont(), [{Headers, Body}|Acc]); -acc_multipart(eof, Acc) -> - lists:reverse(Acc). - -content_disposition_test_() -> - %% {Disposition, Result} - Tests = [ - {<<"form-data; name=id">>, {<<"form-data">>, [{<<"name">>, <<"id">>}]}}, - {<<"inline">>, {<<"inline">>, []}}, - {<<"attachment; \tfilename=brackets-slides.pdf">>, - {<<"attachment">>, [{<<"filename">>, <<"brackets-slides.pdf">>}]}} - ], - [{title(V), fun () -> R = content_disposition(V) end} || {V, R} <- Tests]. - -title(Bin) -> - Title = lists:foldl( - fun ({T, R}, V) -> re:replace(V, T, R, [global]) end, - Bin, - [{"\t", "\\\\t"}, {"\r", "\\\\r"}, {"\n", "\\\\n"}] - ), - iolist_to_binary(Title). - -suffix_test_() -> - Tests = [ - {nomatch, <<>>, <<"ABC">>}, - {{0, 1}, <<"\r">>, <<"ABC">>}, - {{0, 2}, <<"\r\n">>, <<"ABC">>}, - {{0, 4}, <<"\r\n--">>, <<"ABC">>}, - {{0, 5}, <<"\r\n--A">>, <<"ABC">>}, - {{0, 6}, <<"\r\n--AB">>, <<"ABC">>}, - {{0, 7}, <<"\r\n--ABC">>, <<"ABC">>}, - {nomatch, <<"\r\n--AB1">>, <<"ABC">>}, - {{1, 1}, <<"1\r">>, <<"ABC">>}, - {{2, 2}, <<"12\r\n">>, <<"ABC">>}, - {{3, 4}, <<"123\r\n--">>, <<"ABC">>} - ], - [fun() -> Part = suffix_match(Packet, pattern(Boundary)) end || - {Part, Packet, Boundary} <- Tests]. - --endif. diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl index fdc1126..22faf1b 100644 --- a/src/cowboy_protocol.erl +++ b/src/cowboy_protocol.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% Copyright (c) 2011, Anthony Ramine <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any @@ -13,39 +13,6 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc HTTP protocol handler. -%% -%% The available options are: -%% <dl> -%% <dt>compress</dt><dd>Whether to automatically compress the response -%% body when the conditions are met. Disabled by default.</dd> -%% <dt>env</dt><dd>The environment passed and optionally modified -%% by middlewares.</dd> -%% <dt>max_empty_lines</dt><dd>Max number of empty lines before a request. -%% Defaults to 5.</dd> -%% <dt>max_header_name_length</dt><dd>Max length allowed for header names. -%% Defaults to 64.</dd> -%% <dt>max_header_value_length</dt><dd>Max length allowed for header values. -%% Defaults to 4096.</dd> -%% <dt>max_headers</dt><dd>Max number of headers allowed. -%% Defaults to 100.</dd> -%% <dt>max_keepalive</dt><dd>Max number of requests allowed in a single -%% keep-alive session. Defaults to 100.</dd> -%% <dt>max_request_line_length</dt><dd>Max length allowed for the request -%% line. Defaults to 4096.</dd> -%% <dt>middlewares</dt><dd>The list of middlewares to execute when a -%% request is received.</dd> -%% <dt>onrequest</dt><dd>Optional fun that allows Req interaction before -%% any dispatching is done. Host info, path info and bindings are thus -%% not available at this point.</dd> -%% <dt>onresponse</dt><dd>Optional fun that allows replacing a response -%% sent by the application.</dd> -%% <dt>timeout</dt><dd>Time in milliseconds a client has to send the -%% full request line and headers. Defaults to 5000 milliseconds.</dd> -%% </dl> -%% -%% Note that there is no need to monitor these processes when using Cowboy as -%% an application as it already supervises them under the listener supervisor. -module(cowboy_protocol). %% API. @@ -89,9 +56,10 @@ until :: non_neg_integer() | infinity }). +-include_lib("cowlib/include/cow_inline.hrl"). + %% API. -%% @doc Start an HTTP protocol process. -spec start_link(ranch:ref(), inet:socket(), module(), opts()) -> {ok, pid()}. start_link(Ref, Socket, Transport, Opts) -> Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts]), @@ -99,15 +67,13 @@ start_link(Ref, Socket, Transport, Opts) -> %% Internal. -%% @doc Faster alternative to proplists:get_value/3. -%% @private +%% Faster alternative to proplists:get_value/3. get_value(Key, Opts, Default) -> case lists:keyfind(Key, 1, Opts) of {_, Value} -> Value; _ -> Default end. -%% @private -spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok. init(Ref, Socket, Transport, Opts) -> Compress = get_value(compress, Opts, false), @@ -170,7 +136,6 @@ wait_request(Buffer, State=#state{socket=Socket, transport=Transport, terminate(State) end. -%% @private -spec parse_request(binary(), #state{}, non_neg_integer()) -> ok. %% Empty lines must be using \r\n. parse_request(<< $\n, _/binary >>, State, _) -> @@ -292,44 +257,12 @@ match_colon(<< _, Rest/bits >>, N) -> match_colon(_, _) -> nomatch. -%% I know, this isn't exactly pretty. But this is the most critical -%% code path and as such needs to be optimized to death. -%% -%% ... Sorry for your eyes. -%% -%% But let's be honest, that's still pretty readable. parse_hd_name(<< C, Rest/bits >>, S, M, P, Q, V, H, SoFar) -> case C of $: -> parse_hd_before_value(Rest, S, M, P, Q, V, H, SoFar); $\s -> parse_hd_name_ws(Rest, S, M, P, Q, V, H, SoFar); $\t -> parse_hd_name_ws(Rest, S, M, P, Q, V, H, SoFar); - $A -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $a >>); - $B -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $b >>); - $C -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $c >>); - $D -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $d >>); - $E -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $e >>); - $F -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $f >>); - $G -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $g >>); - $H -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $h >>); - $I -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $i >>); - $J -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $j >>); - $K -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $k >>); - $L -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $l >>); - $M -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $m >>); - $N -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $n >>); - $O -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $o >>); - $P -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $p >>); - $Q -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $q >>); - $R -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $r >>); - $S -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $s >>); - $T -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $t >>); - $U -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $u >>); - $V -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $v >>); - $W -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $w >>); - $X -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $x >>); - $Y -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $y >>); - $Z -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, $z >>); - C -> parse_hd_name(Rest, S, M, P, Q, V, H, << SoFar/binary, C >>) + ?INLINE_LOWERCASE(parse_hd_name, Rest, S, M, P, Q, V, H, SoFar) end. parse_hd_name_ws(<< C, Rest/bits >>, S, M, P, Q, V, H, Name) -> @@ -405,7 +338,8 @@ parse_hd_value(<< $\r, Rest/bits >>, S, M, P, Q, V, Headers, Name, SoFar) -> << $\n >> -> wait_hd_value_nl(<<>>, S, M, P, Q, V, Headers, Name, SoFar); << $\n, C, Rest2/bits >> when C =:= $\s; C =:= $\t -> - parse_hd_value(Rest2, S, M, P, Q, V, Headers, Name, SoFar); + parse_hd_value(Rest2, S, M, P, Q, V, Headers, Name, + << SoFar/binary, C >>); << $\n, Rest2/bits >> -> parse_header(Rest2, S, M, P, Q, V, [{Name, SoFar}|Headers]) end; @@ -441,8 +375,6 @@ request(B, State=#state{transport=Transport}, M, P, Q, Version, Headers) -> default_port(ssl) -> 443; default_port(_) -> 80. -%% Another hurtful block of code. :) -%% %% Same code as cow_http:parse_fullhost/1, but inline because we %% really want this to go fast. parse_host(<< $[, Rest/bits >>, false, <<>>) -> @@ -455,33 +387,7 @@ parse_host(<< $], Rest/bits >>, true, Acc) -> parse_host(Rest, false, << Acc/binary, $] >>); parse_host(<< C, Rest/bits >>, E, Acc) -> case C of - $A -> parse_host(Rest, E, << Acc/binary, $a >>); - $B -> parse_host(Rest, E, << Acc/binary, $b >>); - $C -> parse_host(Rest, E, << Acc/binary, $c >>); - $D -> parse_host(Rest, E, << Acc/binary, $d >>); - $E -> parse_host(Rest, E, << Acc/binary, $e >>); - $F -> parse_host(Rest, E, << Acc/binary, $f >>); - $G -> parse_host(Rest, E, << Acc/binary, $g >>); - $H -> parse_host(Rest, E, << Acc/binary, $h >>); - $I -> parse_host(Rest, E, << Acc/binary, $i >>); - $J -> parse_host(Rest, E, << Acc/binary, $j >>); - $K -> parse_host(Rest, E, << Acc/binary, $k >>); - $L -> parse_host(Rest, E, << Acc/binary, $l >>); - $M -> parse_host(Rest, E, << Acc/binary, $m >>); - $N -> parse_host(Rest, E, << Acc/binary, $n >>); - $O -> parse_host(Rest, E, << Acc/binary, $o >>); - $P -> parse_host(Rest, E, << Acc/binary, $p >>); - $Q -> parse_host(Rest, E, << Acc/binary, $q >>); - $R -> parse_host(Rest, E, << Acc/binary, $r >>); - $S -> parse_host(Rest, E, << Acc/binary, $s >>); - $T -> parse_host(Rest, E, << Acc/binary, $t >>); - $U -> parse_host(Rest, E, << Acc/binary, $u >>); - $V -> parse_host(Rest, E, << Acc/binary, $v >>); - $W -> parse_host(Rest, E, << Acc/binary, $w >>); - $X -> parse_host(Rest, E, << Acc/binary, $x >>); - $Y -> parse_host(Rest, E, << Acc/binary, $y >>); - $Z -> parse_host(Rest, E, << Acc/binary, $z >>); - _ -> parse_host(Rest, E, << Acc/binary, C >>) + ?INLINE_LOWERCASE(parse_host, Rest, E, Acc) end. %% End of request parsing. @@ -538,7 +444,6 @@ execute(Req, State, Env, [Middleware|Tail]) -> error_terminate(Code, Req2, State) end. -%% @private -spec resume(#state{}, cowboy_middleware:env(), [module()], module(), module(), [any()]) -> ok. resume(State, Env, Tail, Module, Function, Args) -> @@ -569,8 +474,8 @@ next_request(Req, State=#state{req_keepalive=Keepalive, timeout=Timeout}, _ -> close end, %% Flush the resp_sent message before moving on. - receive {cowboy_req, resp_sent} -> ok after 0 -> ok end, if HandlerRes =:= ok, Buffer =/= close -> + receive {cowboy_req, resp_sent} -> ok after 0 -> ok end, ?MODULE:parse_request(Buffer, State#state{req_keepalive=Keepalive + 1, until=until(Timeout)}, 0); diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl index d98e395..eec4e88 100644 --- a/src/cowboy_req.erl +++ b/src/cowboy_req.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% Copyright (c) 2011, Anthony Ramine <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any @@ -13,31 +13,6 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc HTTP request manipulation API. -%% -%% The functions in this module try to follow this pattern for their -%% return types: -%% <dl> -%% <dt>access:</dt> -%% <dd><em>{Value, Req}</em></dd> -%% <dt>action:</dt> -%% <dd><em>{Result, Req} | {Result, Value, Req} | {error, atom()}</em></dd> -%% <dt>modification:</dt> -%% <dd><em>Req</em></dd> -%% <dt>question (<em>has_*</em> or <em>is_*</em>):</dt> -%% <dd><em>boolean()</em></dd> -%% </dl> -%% -%% Exceptions include <em>chunk/2</em> which always returns <em>'ok'</em>, -%% and <em>to_list/1</em> which returns a list of key/values. -%% -%% Also note that all body reading functions perform actions, as Cowboy -%% doesn't read the request body until they are called. -%% -%% Whenever <em>Req</em> is returned, it should always be kept in place of -%% the one given as argument in your function call, because it keeps -%% track of the request and response state. Doing so allows Cowboy to do -%% some lazy evaluation and cache results when possible. -module(cowboy_req). %% Request API. @@ -82,8 +57,11 @@ -export([body/2]). -export([body_qs/1]). -export([body_qs/2]). --export([multipart_data/1]). --export([multipart_skip/1]). + +%% Multipart API. +-export([part/1]). +-export([part_body/1]). +-export([part_body/2]). %% Response API. -export([set_resp_cookie/4]). @@ -122,11 +100,7 @@ -> {ok, binary()} | {error, atom()}). -type transfer_decode_fun() :: fun((binary(), any()) - -> {ok, binary(), binary(), any()} - | more | {more, non_neg_integer(), binary(), any()} - | {done, non_neg_integer(), binary()} - | {done, binary(), non_neg_integer(), binary()} - | {error, atom()}). + -> cow_http_te:decode_ret()). -type resp_body_fun() :: fun((any(), module()) -> ok). -type send_chunk_fun() :: fun((iodata()) -> ok | {error, atom()}). @@ -159,8 +133,8 @@ %% Request body. body_state = waiting :: waiting | done | {stream, non_neg_integer(), transfer_decode_fun(), any(), content_decode_fun()}, - multipart = undefined :: undefined | {non_neg_integer(), fun()}, buffer = <<>> :: binary(), + multipart = undefined :: undefined | {binary(), binary()}, %% Response. resp_compress = false :: boolean(), @@ -181,13 +155,6 @@ %% Request API. -%% @doc Create a new HTTP Req object. -%% -%% This function takes care of setting the owner's pid to self(). -%% @private -%% -%% Since we always need to parse the Connection header, we do it -%% in an optimized way and add the parsed value to p_headers' cache. -spec new(any(), module(), undefined | {inet:ip_address(), inet:port_number()}, binary(), binary(), binary(), @@ -210,72 +177,58 @@ new(Socket, Transport, Peer, Method, Path, Query, false -> Req; %% keepalive {_, ConnectionHeader} -> - Tokens = parse_connection_before(ConnectionHeader, []), + Tokens = cow_http_hd:parse_connection(ConnectionHeader), Connection = connection_to_atom(Tokens), Req#http_req{connection=Connection, p_headers=[{<<"connection">>, Tokens}]} end end. -%% @doc Return the HTTP method of the request. -spec method(Req) -> {binary(), Req} when Req::req(). method(Req) -> {Req#http_req.method, Req}. -%% @doc Return the HTTP version used for the request. -spec version(Req) -> {cowboy:http_version(), Req} when Req::req(). version(Req) -> {Req#http_req.version, Req}. -%% @doc Return the peer address and port number of the remote host. -spec peer(Req) -> {{inet:ip_address(), inet:port_number()}, Req} when Req::req(). peer(Req) -> {Req#http_req.peer, Req}. -%% @doc Return the host binary string. -spec host(Req) -> {binary(), Req} when Req::req(). host(Req) -> {Req#http_req.host, Req}. -%% @doc Return the extra host information obtained from partially matching -%% the hostname using <em>'...'</em>. -spec host_info(Req) -> {cowboy_router:tokens() | undefined, Req} when Req::req(). host_info(Req) -> {Req#http_req.host_info, Req}. -%% @doc Return the port used for this request. -spec port(Req) -> {inet:port_number(), Req} when Req::req(). port(Req) -> {Req#http_req.port, Req}. -%% @doc Return the path binary string. -spec path(Req) -> {binary(), Req} when Req::req(). path(Req) -> {Req#http_req.path, Req}. -%% @doc Return the extra path information obtained from partially matching -%% the patch using <em>'...'</em>. -spec path_info(Req) -> {cowboy_router:tokens() | undefined, Req} when Req::req(). path_info(Req) -> {Req#http_req.path_info, Req}. -%% @doc Return the raw query string directly taken from the request. -spec qs(Req) -> {binary(), Req} when Req::req(). qs(Req) -> {Req#http_req.qs, Req}. -%% @equiv qs_val(Name, Req, undefined) -spec qs_val(binary(), Req) -> {binary() | true | undefined, Req} when Req::req(). qs_val(Name, Req) when is_binary(Name) -> qs_val(Name, Req, undefined). -%% @doc Return the query string value for the given key, or a default if -%% missing. -spec qs_val(binary(), Req, Default) -> {binary() | true | Default, Req} when Req::req(), Default::any(). qs_val(Name, Req=#http_req{qs=RawQs, qs_vals=undefined}, Default) @@ -288,7 +241,6 @@ qs_val(Name, Req, Default) -> false -> {Default, Req} end. -%% @doc Return the full list of query string values. -spec qs_vals(Req) -> {list({binary(), binary() | true}), Req} when Req::req(). qs_vals(Req=#http_req{qs=RawQs, qs_vals=undefined}) -> QsVals = cow_qs:parse_qs(RawQs), @@ -296,10 +248,7 @@ qs_vals(Req=#http_req{qs=RawQs, qs_vals=undefined}) -> qs_vals(Req=#http_req{qs_vals=QsVals}) -> {QsVals, Req}. -%% @doc Return the request URL as a binary without the path and query string. -%% %% The URL includes the scheme, host and port only. -%% @see cowboy_req:url/1 -spec host_url(Req) -> {undefined | binary(), Req} when Req::req(). host_url(Req=#http_req{port=undefined}) -> {undefined, Req}; @@ -316,8 +265,6 @@ host_url(Req=#http_req{transport=Transport, host=Host, port=Port}) -> end, {<< "http", Secure/binary, "://", Host/binary, PortBin/binary >>, Req}. -%% @doc Return the full request URL as a binary. -%% %% The URL includes the scheme, host, port, path and query string. -spec url(Req) -> {undefined | binary(), Req} when Req::req(). url(Req=#http_req{}) -> @@ -333,13 +280,10 @@ url(HostURL, Req=#http_req{path=Path, qs=QS}) -> end, {<< HostURL/binary, Path/binary, QS2/binary >>, Req}. -%% @equiv binding(Name, Req, undefined) -spec binding(atom(), Req) -> {any() | undefined, Req} when Req::req(). binding(Name, Req) when is_atom(Name) -> binding(Name, Req, undefined). -%% @doc Return the binding value for the given key obtained when matching -%% the host and path against the dispatch list, or a default if missing. -spec binding(atom(), Req, Default) -> {any() | Default, Req} when Req::req(), Default::any(). binding(Name, Req, Default) when is_atom(Name) -> @@ -348,18 +292,15 @@ binding(Name, Req, Default) when is_atom(Name) -> false -> {Default, Req} end. -%% @doc Return the full list of binding values. -spec bindings(Req) -> {[{atom(), any()}], Req} when Req::req(). bindings(Req) -> {Req#http_req.bindings, Req}. -%% @equiv header(Name, Req, undefined) -spec header(binary(), Req) -> {binary() | undefined, Req} when Req::req(). header(Name, Req) -> header(Name, Req, undefined). -%% @doc Return the header value for the given key, or a default if missing. -spec header(binary(), Req, Default) -> {binary() | Default, Req} when Req::req(), Default::any(). header(Name, Req, Default) -> @@ -368,16 +309,10 @@ header(Name, Req, Default) -> false -> {Default, Req} end. -%% @doc Return the full list of headers. -spec headers(Req) -> {cowboy:http_headers(), Req} when Req::req(). headers(Req) -> {Req#http_req.headers, Req}. -%% @doc Semantically parse headers. -%% -%% When the value isn't found, a proper default value for the type -%% returned is used as a return value. -%% @see parse_header/3 -spec parse_header(binary(), Req) -> {ok, any(), Req} | {undefined, binary(), Req} | {error, badarg} when Req::req(). @@ -387,14 +322,10 @@ parse_header(Name, Req=#http_req{p_headers=PHeaders}) -> {Name, Value} -> {ok, Value, Req} end. -%% @doc Default values for semantic header parsing. -spec parse_header_default(binary()) -> any(). parse_header_default(<<"transfer-encoding">>) -> [<<"identity">>]; parse_header_default(_Name) -> undefined. -%% @doc Semantically parse headers. -%% -%% When the header is unknown, the value is returned directly without parsing. -spec parse_header(binary(), Req, any()) -> {ok, any(), Req} | {undefined, binary(), Req} | {error, badarg} when Req::req(). @@ -424,7 +355,7 @@ parse_header(Name = <<"authorization">>, Req, Default) -> cowboy_http:token_ci(Value, fun cowboy_http:authorization/2) end); parse_header(Name = <<"content-length">>, Req, Default) -> - parse_header(Name, Req, Default, fun cowboy_http:digits/1); + parse_header(Name, Req, Default, fun cow_http_hd:parse_content_length/1); parse_header(Name = <<"content-type">>, Req, Default) -> parse_header(Name, Req, Default, fun cowboy_http:content_type/1); parse_header(Name = <<"cookie">>, Req, Default) -> @@ -451,10 +382,10 @@ parse_header(Name, Req, Default) fun (Value) -> cowboy_http:nonempty_list(Value, fun cowboy_http:token/2) end); -%% @todo Extension parameters. -parse_header(Name, Req, Default) - when Name =:= <<"transfer-encoding">>; - Name =:= <<"upgrade">> -> +parse_header(Name = <<"transfer-encoding">>, Req, Default) -> + parse_header(Name, Req, Default, fun cow_http_hd:parse_transfer_encoding/1); +%% @todo Product version. +parse_header(Name = <<"upgrade">>, Req, Default) -> parse_header(Name, Req, Default, fun (Value) -> cowboy_http:nonempty_list(Value, fun cowboy_http:token_ci/2) @@ -478,14 +409,11 @@ parse_header(Name, Req=#http_req{p_headers=PHeaders}, Default, Fun) -> end end. -%% @equiv cookie(Name, Req, undefined) -spec cookie(binary(), Req) -> {binary() | undefined, Req} when Req::req(). cookie(Name, Req) when is_binary(Name) -> cookie(Name, Req, undefined). -%% @doc Return the cookie value for the given key, or a default if -%% missing. -spec cookie(binary(), Req, Default) -> {binary() | Default, Req} when Req::req(), Default::any(). cookie(Name, Req=#http_req{cookies=undefined}, Default) when is_binary(Name) -> @@ -501,7 +429,6 @@ cookie(Name, Req, Default) -> false -> {Default, Req} end. -%% @doc Return the full list of cookie values. -spec cookies(Req) -> {list({binary(), binary()}), Req} when Req::req(). cookies(Req=#http_req{cookies=undefined}) -> case parse_header(<<"cookie">>, Req) of @@ -516,16 +443,10 @@ cookies(Req=#http_req{cookies=undefined}) -> cookies(Req=#http_req{cookies=Cookies}) -> {Cookies, Req}. -%% @equiv meta(Name, Req, undefined) -spec meta(atom(), Req) -> {any() | undefined, Req} when Req::req(). meta(Name, Req) -> meta(Name, Req, undefined). -%% @doc Return metadata information about the request. -%% -%% Metadata information varies from one protocol to another. Websockets -%% would define the protocol version here, while REST would use it to -%% indicate which media type, language and charset were retained. -spec meta(atom(), Req, any()) -> {any(), Req} when Req::req(). meta(Name, Req, Default) -> case lists:keyfind(Name, 1, Req#http_req.meta) of @@ -533,18 +454,12 @@ meta(Name, Req, Default) -> false -> {Default, Req} end. -%% @doc Set metadata information. -%% -%% You can use this function to attach information about the request. -%% -%% If the value already exists it will be overwritten. -spec set_meta(atom(), any(), Req) -> Req when Req::req(). set_meta(Name, Value, Req=#http_req{meta=Meta}) -> - Req#http_req{meta=[{Name, Value}|lists:keydelete(Name, 1, Meta)]}. + Req#http_req{meta=lists:keystore(Name, 1, Meta, {Name, Value})}. %% Request Body API. -%% @doc Return whether the request message has a body. -spec has_body(req()) -> boolean(). has_body(Req) -> case lists:keyfind(<<"content-length">>, 1, Req#http_req.headers) of @@ -556,8 +471,6 @@ has_body(Req) -> lists:keymember(<<"transfer-encoding">>, 1, Req#http_req.headers) end. -%% @doc Return the request message body length, if known. -%% %% The length may not be known if Transfer-Encoding is not identity, %% and the body hasn't been read at the time of the call. -spec body_length(Req) -> {undefined | non_neg_integer(), Req} when Req::req(). @@ -570,12 +483,6 @@ body_length(Req) -> {undefined, Req2} end. -%% @doc Initialize body streaming and set custom decoding functions. -%% -%% Calling this function is optional. It should only be used if you -%% need to override the default behavior of Cowboy. Otherwise you -%% should call stream_body/{1,2} directly. -%% %% Two decodings happen. First a decoding function is applied to the %% transferred data, and then another is applied to the actual content. %% @@ -584,34 +491,17 @@ body_length(Req) -> %% also initialized through this function. %% %% Content encoding is generally used for compression. -%% -%% Standard encodings can be found in cowboy_http. -spec init_stream(transfer_decode_fun(), any(), content_decode_fun(), Req) -> {ok, Req} when Req::req(). init_stream(TransferDecode, TransferState, ContentDecode, Req) -> {ok, Req#http_req{body_state= {stream, 0, TransferDecode, TransferState, ContentDecode}}}. -%% @equiv stream_body(1000000, Req) -spec stream_body(Req) -> {ok, binary(), Req} | {done, Req} | {error, atom()} when Req::req(). stream_body(Req) -> stream_body(1000000, Req). -%% @doc Stream the request's body. -%% -%% This is the most low level function to read the request body. -%% -%% In most cases, if they weren't defined before using init_stream/4, -%% this function will guess which transfer and content encodings were -%% used for building the request body, and configure the decoding -%% functions that will be used when streaming. -%% -%% It then starts streaming the body, returning {ok, Data, Req} -%% for each streamed part, and {done, Req} when it's finished streaming. -%% -%% You can limit the size of the chunks being returned by using the -%% first argument which is the size in bytes. It defaults to 1000000 bytes. -spec stream_body(non_neg_integer(), Req) -> {ok, binary(), Req} | {done, Req} | {error, atom()} when Req::req(). stream_body(MaxLength, Req=#http_req{body_state=waiting, version=Version, @@ -629,7 +519,7 @@ stream_body(MaxLength, Req=#http_req{body_state=waiting, version=Version, {ok, [<<"chunked">>], Req2} -> stream_body(MaxLength, Req2#http_req{body_state= {stream, 0, - fun cowboy_http:te_chunked/2, {0, 0}, + fun cow_http_te:stream_chunked/2, {0, 0}, fun cowboy_http:ce_identity/1}}); {ok, [<<"identity">>], Req2} -> {Length, Req3} = body_length(Req2), @@ -639,7 +529,7 @@ stream_body(MaxLength, Req=#http_req{body_state=waiting, version=Version, Length -> stream_body(MaxLength, Req3#http_req{body_state= {stream, Length, - fun cowboy_http:te_identity/2, {0, Length}, + fun cow_http_te:stream_identity/2, {0, Length}, fun cowboy_http:ce_identity/1}}) end end; @@ -663,31 +553,34 @@ stream_body_recv(MaxLength, Req=#http_req{ {error, Reason} -> {error, Reason} end. +%% @todo Handle chunked after-the-facts headers. +%% @todo Depending on the length returned we might want to 0 or +5 it. -spec transfer_decode(binary(), Req) -> {ok, binary(), Req} | {error, atom()} when Req::req(). transfer_decode(Data, Req=#http_req{body_state={stream, _, TransferDecode, TransferState, ContentDecode}}) -> case TransferDecode(Data, TransferState) of - {ok, Data2, Rest, TransferState2} -> - content_decode(ContentDecode, Data2, - Req#http_req{buffer=Rest, body_state={stream, 0, - TransferDecode, TransferState2, ContentDecode}}); - %% @todo {header(s) for chunked more -> stream_body_recv(0, Req#http_req{buffer=Data, body_state={stream, 0, TransferDecode, TransferState, ContentDecode}}); - {more, Length, Data2, TransferState2} -> + {more, Data2, TransferState2} -> + content_decode(ContentDecode, Data2, + Req#http_req{body_state={stream, 0, + TransferDecode, TransferState2, ContentDecode}}); + {more, Data2, Length, TransferState2} when is_integer(Length) -> content_decode(ContentDecode, Data2, Req#http_req{body_state={stream, Length, TransferDecode, TransferState2, ContentDecode}}); + {more, Data2, Rest, TransferState2} -> + content_decode(ContentDecode, Data2, + Req#http_req{buffer=Rest, body_state={stream, 0, + TransferDecode, TransferState2, ContentDecode}}); {done, Length, Rest} -> Req2 = transfer_decode_done(Length, Rest, Req), {done, Req2}; {done, Data2, Length, Rest} -> Req2 = transfer_decode_done(Length, Rest, Req), - content_decode(ContentDecode, Data2, Req2); - {error, Reason} -> - {error, Reason} + content_decode(ContentDecode, Data2, Req2) end. -spec transfer_decode_done(non_neg_integer(), binary(), Req) @@ -704,7 +597,6 @@ transfer_decode_done(Length, Rest, Req=#http_req{ Req#http_req{buffer=Rest, body_state=done, headers=Headers3, p_headers=PHeaders3}. -%% @todo Probably needs a Rest. -spec content_decode(content_decode_fun(), binary(), Req) -> {ok, binary(), Req} | {error, atom()} when Req::req(). content_decode(ContentDecode, Data, Req) -> @@ -713,12 +605,10 @@ content_decode(ContentDecode, Data, Req) -> {error, Reason} -> {error, Reason} end. -%% @equiv body(8000000, Req) -spec body(Req) -> {ok, binary(), Req} | {error, atom()} when Req::req(). body(Req) -> body(8000000, Req). -%% @doc Return the body sent with the request. -spec body(non_neg_integer() | infinity, Req) -> {ok, binary(), Req} | {error, atom()} when Req::req(). body(MaxBodyLength, Req) -> @@ -754,15 +644,12 @@ skip_body(Req) -> {error, Reason} -> {error, Reason} end. -%% @equiv body_qs(16000, Req) -spec body_qs(Req) -> {ok, [{binary(), binary() | true}], Req} | {error, atom()} when Req::req(). body_qs(Req) -> body_qs(16000, Req). -%% @doc Return the body sent with the request, parsed as an -%% application/x-www-form-urlencoded string. %% Essentially a POST query string. -spec body_qs(non_neg_integer() | infinity, Req) -> {ok, [{binary(), binary() | true}], Req} | {error, atom()} @@ -775,65 +662,78 @@ body_qs(MaxBodyLength, Req) -> {error, Reason} end. -%% Multipart Request API. +%% Multipart API. -%% @doc Return data from the multipart parser. -%% -%% Use this function for multipart streaming. For each part in the request, -%% this function returns <em>{headers, Headers, Req}</em> followed by a sequence of -%% <em>{body, Data, Req}</em> tuples and finally <em>{end_of_part, Req}</em>. When there -%% is no part to parse anymore, <em>{eof, Req}</em> is returned. --spec multipart_data(Req) - -> {headers, cowboy:http_headers(), Req} | {body, binary(), Req} - | {end_of_part | eof, Req} when Req::req(). -multipart_data(Req=#http_req{body_state=waiting}) -> - {ok, {<<"multipart">>, _SubType, Params}, Req2} = - parse_header(<<"content-type">>, Req), - {_, Boundary} = lists:keyfind(<<"boundary">>, 1, Params), - {ok, Length, Req3} = parse_header(<<"content-length">>, Req2), - multipart_data(Req3, Length, {more, cowboy_multipart:parser(Boundary)}); -multipart_data(Req=#http_req{multipart={Length, Cont}}) -> - multipart_data(Req, Length, Cont()); -multipart_data(Req=#http_req{body_state=done}) -> - {eof, Req}. - -multipart_data(Req, Length, {headers, Headers, Cont}) -> - {headers, Headers, Req#http_req{multipart={Length, Cont}}}; -multipart_data(Req, Length, {body, Data, Cont}) -> - {body, Data, Req#http_req{multipart={Length, Cont}}}; -multipart_data(Req, Length, {end_of_part, Cont}) -> - {end_of_part, Req#http_req{multipart={Length, Cont}}}; -multipart_data(Req, 0, eof) -> - {eof, Req#http_req{body_state=done, multipart=undefined}}; -multipart_data(Req=#http_req{socket=Socket, transport=Transport}, - Length, eof) -> - %% We just want to skip so no need to stream data here. - {ok, _Data} = Transport:recv(Socket, Length, 5000), - {eof, Req#http_req{body_state=done, multipart=undefined}}; -multipart_data(Req, Length, {more, Parser}) when Length > 0 -> - case stream_body(Req) of - {ok, << Data:Length/binary, Buffer/binary >>, Req2} -> - multipart_data(Req2#http_req{buffer=Buffer}, 0, Parser(Data)); - {ok, Data, Req2} -> - multipart_data(Req2, Length - byte_size(Data), Parser(Data)) +-spec part(Req) + -> {ok, cow_multipart:headers(), Req} | {done, Req} + when Req::req(). +part(Req=#http_req{multipart=undefined}) -> + part(init_multipart(Req)); +part(Req) -> + {ok, Data, Req2} = stream_multipart(Req), + part(Data, Req2). + +part(Buffer, Req=#http_req{multipart={Boundary, _}}) -> + case cow_multipart:parse_headers(Buffer, Boundary) of + more -> + {ok, Data, Req2} = stream_multipart(Req), + part(<< Buffer/binary, Data/binary >>, Req2); + {more, Buffer2} -> + {ok, Data, Req2} = stream_multipart(Req), + part(<< Buffer2/binary, Data/binary >>, Req2); + {ok, Headers, Rest} -> + {ok, Headers, Req#http_req{multipart={Boundary, Rest}}}; + %% Ignore epilogue. + {done, _} -> + {done, Req#http_req{multipart=undefined}} end. -%% @doc Skip a part returned by the multipart parser. -%% -%% This function repeatedly calls <em>multipart_data/1</em> until -%% <em>{end_of_part, Req}</em> or <em>{eof, Req}</em> is parsed. --spec multipart_skip(Req) -> {ok, Req} when Req::req(). -multipart_skip(Req) -> - case multipart_data(Req) of - {end_of_part, Req2} -> {ok, Req2}; - {eof, Req2} -> {ok, Req2}; - {_, _, Req2} -> multipart_skip(Req2) +-spec part_body(Req) + -> {ok, binary(), Req} | {more, binary(), Req} + when Req::req(). +part_body(Req) -> + part_body(8000000, Req). + +-spec part_body(non_neg_integer(), Req) + -> {ok, binary(), Req} | {more, binary(), Req} + when Req::req(). +part_body(MaxLength, Req=#http_req{multipart=undefined}) -> + part_body(MaxLength, init_multipart(Req)); +part_body(MaxLength, Req) -> + part_body(<<>>, MaxLength, Req, <<>>). + +part_body(Buffer, MaxLength, Req=#http_req{multipart={Boundary, _}}, Acc) + when byte_size(Acc) > MaxLength -> + {more, Acc, Req#http_req{multipart={Boundary, Buffer}}}; +part_body(Buffer, MaxLength, Req=#http_req{multipart={Boundary, _}}, Acc) -> + {ok, Data, Req2} = stream_multipart(Req), + case cow_multipart:parse_body(<< Buffer/binary, Data/binary >>, Boundary) of + {ok, Body} -> + part_body(<<>>, MaxLength, Req2, << Acc/binary, Body/binary >>); + {ok, Body, Rest} -> + part_body(Rest, MaxLength, Req2, << Acc/binary, Body/binary >>); + done -> + {ok, Acc, Req2}; + {done, Body} -> + {ok, << Acc/binary, Body/binary >>, Req2}; + {done, Body, Rest} -> + {ok, << Acc/binary, Body/binary >>, + Req2#http_req{multipart={Boundary, Rest}}} end. +init_multipart(Req) -> + {ok, {<<"multipart">>, _, Params}, Req2} + = parse_header(<<"content-type">>, Req), + {_, Boundary} = lists:keyfind(<<"boundary">>, 1, Params), + Req2#http_req{multipart={Boundary, <<>>}}. + +stream_multipart(Req=#http_req{multipart={_, <<>>}}) -> + stream_body(Req); +stream_multipart(Req=#http_req{multipart={Boundary, Buffer}}) -> + {ok, Buffer, Req#http_req{multipart={Boundary, <<>>}}}. + %% Response API. -%% @doc Add a cookie header to the response. -%% %% The cookie name cannot contain any of the following characters: %% =,;\s\t\r\n\013\014 %% @@ -845,46 +745,19 @@ set_resp_cookie(Name, Value, Opts, Req) -> Cookie = cow_cookie:setcookie(Name, Value, Opts), set_resp_header(<<"set-cookie">>, Cookie, Req). -%% @doc Add a header to the response. -spec set_resp_header(binary(), iodata(), Req) -> Req when Req::req(). set_resp_header(Name, Value, Req=#http_req{resp_headers=RespHeaders}) -> Req#http_req{resp_headers=[{Name, Value}|RespHeaders]}. -%% @doc Add a body to the response. -%% -%% The body set here is ignored if the response is later sent using -%% anything other than reply/2 or reply/3. The response body is expected -%% to be a binary or an iolist. -spec set_resp_body(iodata(), Req) -> Req when Req::req(). set_resp_body(Body, Req) -> Req#http_req{resp_body=Body}. -%% @doc Add a body stream function to the response. -%% -%% The body set here is ignored if the response is later sent using -%% anything other than reply/2 or reply/3. -%% -%% Setting a response stream function without a length means that the -%% body will be sent until the connection is closed. Cowboy will make -%% sure that the connection is closed with no extra step required. -%% -%% To inform the client that a body has been sent with this request, -%% Cowboy will add a "Transfer-Encoding: identity" header to the -%% response. -spec set_resp_body_fun(resp_body_fun(), Req) -> Req when Req::req(). set_resp_body_fun(StreamFun, Req) when is_function(StreamFun) -> Req#http_req{resp_body=StreamFun}. -%% @doc Add a body function to the response. -%% -%% The body set here is ignored if the response is later sent using -%% anything other than reply/2 or reply/3. -%% -%% Cowboy will call the given response stream function after sending the -%% headers. This function must send the specified number of bytes to the -%% socket it will receive as argument. -%% %% If the body function crashes while writing the response body or writes %% fewer bytes than declared the behaviour is undefined. -spec set_resp_body_fun(non_neg_integer(), resp_body_fun(), Req) @@ -898,12 +771,10 @@ set_resp_body_fun(chunked, StreamFun, Req) when is_function(StreamFun) -> Req#http_req{resp_body={chunked, StreamFun}}. -%% @doc Return whether the given header has been set for the response. -spec has_resp_header(binary(), req()) -> boolean(). has_resp_header(Name, #http_req{resp_headers=RespHeaders}) -> lists:keymember(Name, 1, RespHeaders). -%% @doc Return whether a body has been set for the response. -spec has_resp_body(req()) -> boolean(). has_resp_body(#http_req{resp_body=RespBody}) when is_function(RespBody) -> true; @@ -914,25 +785,21 @@ has_resp_body(#http_req{resp_body={Length, _}}) -> has_resp_body(#http_req{resp_body=RespBody}) -> iolist_size(RespBody) > 0. -%% @doc Remove a header previously set for the response. -spec delete_resp_header(binary(), Req) -> Req when Req::req(). delete_resp_header(Name, Req=#http_req{resp_headers=RespHeaders}) -> RespHeaders2 = lists:keydelete(Name, 1, RespHeaders), Req#http_req{resp_headers=RespHeaders2}. -%% @equiv reply(Status, [], [], Req) -spec reply(cowboy:http_status(), Req) -> {ok, Req} when Req::req(). reply(Status, Req=#http_req{resp_body=Body}) -> reply(Status, [], Body, Req). -%% @equiv reply(Status, Headers, [], Req) -spec reply(cowboy:http_status(), cowboy:http_headers(), Req) -> {ok, Req} when Req::req(). reply(Status, Headers, Req=#http_req{resp_body=Body}) -> reply(Status, Headers, Body, Req). -%% @doc Send a reply to the client. -spec reply(cowboy:http_status(), cowboy:http_headers(), iodata() | {non_neg_integer() | resp_body_fun()}, Req) -> {ok, Req} when Req::req(). @@ -1060,22 +927,16 @@ reply_no_compress(Status, Headers, Body, Req, Req), Req2. -%% @equiv chunked_reply(Status, [], Req) -spec chunked_reply(cowboy:http_status(), Req) -> {ok, Req} when Req::req(). chunked_reply(Status, Req) -> chunked_reply(Status, [], Req). -%% @doc Initiate the sending of a chunked reply to the client. -%% @see cowboy_req:chunk/2 -spec chunked_reply(cowboy:http_status(), cowboy:http_headers(), Req) -> {ok, Req} when Req::req(). chunked_reply(Status, Headers, Req) -> {_, Req2} = chunked_response(Status, Headers, Req), {ok, Req2}. -%% @doc Send a chunk of data. -%% -%% A chunked reply must have been initiated before calling this function. -spec chunk(iodata(), req()) -> ok | {error, atom()}. chunk(_Data, #http_req{method= <<"HEAD">>}) -> ok; @@ -1090,8 +951,7 @@ chunk(Data, #http_req{socket=Socket, transport=Transport, Transport:send(Socket, [integer_to_list(iolist_size(Data), 16), <<"\r\n">>, Data, <<"\r\n">>]). -%% @doc Finish the chunked reply. -%% @todo If ever made public, need to send nothing if HEAD. +%% If ever made public, need to send nothing if HEAD. -spec last_chunk(Req) -> Req when Req::req(). last_chunk(Req=#http_req{socket=Socket, transport=cowboy_spdy}) -> _ = cowboy_spdy:stream_close(Socket), @@ -1100,8 +960,6 @@ last_chunk(Req=#http_req{socket=Socket, transport=Transport}) -> _ = Transport:send(Socket, <<"0\r\n\r\n">>), Req#http_req{resp_state=done}. -%% @doc Send an upgrade reply. -%% @private -spec upgrade_reply(cowboy:http_status(), cowboy:http_headers(), Req) -> {ok, Req} when Req::req(). upgrade_reply(Status, Headers, Req=#http_req{transport=Transport, @@ -1112,10 +970,7 @@ upgrade_reply(Status, Headers, Req=#http_req{transport=Transport, ], <<>>, Req), {ok, Req2#http_req{resp_state=done, resp_headers=[], resp_body= <<>>}}. -%% @doc Send a reply if one hasn't been sent already. -%% %% Meant to be used internally for sending errors after crashes. -%% @private -spec maybe_reply(cowboy:http_status(), req()) -> ok. maybe_reply(Status, Req) -> receive @@ -1125,8 +980,6 @@ maybe_reply(Status, Req) -> ok end. -%% @doc Ensure the response has been sent fully. -%% @private -spec ensure_response(req(), cowboy:http_status()) -> ok. %% The response has already been fully sent to the client. ensure_response(#http_req{resp_state=done}, _) -> @@ -1148,12 +1001,10 @@ ensure_response(#http_req{}, _) -> %% Private setter/getter API. -%% @private -spec append_buffer(binary(), Req) -> Req when Req::req(). append_buffer(Suffix, Req=#http_req{buffer=Buffer}) -> Req#http_req{buffer= << Buffer/binary, Suffix/binary >>}. -%% @private -spec get(atom(), req()) -> any(); ([atom()], req()) -> any(). get(List, Req) when is_list(List) -> [g(Atom, Req) || Atom <- List]; @@ -1188,7 +1039,6 @@ g(socket, #http_req{socket=Ret}) -> Ret; g(transport, #http_req{transport=Ret}) -> Ret; g(version, #http_req{version=Ret}) -> Ret. -%% @private -spec set([{atom(), any()}], Req) -> Req when Req::req(). set([], Req) -> Req; set([{bindings, Val}|Tail], Req) -> set(Tail, Req#http_req{bindings=Val}); @@ -1218,7 +1068,6 @@ set([{socket, Val}|Tail], Req) -> set(Tail, Req#http_req{socket=Val}); set([{transport, Val}|Tail], Req) -> set(Tail, Req#http_req{transport=Val}); set([{version, Val}|Tail], Req) -> set(Tail, Req#http_req{version=Val}). -%% @private -spec set_bindings(cowboy_router:tokens(), cowboy_router:tokens(), cowboy_router:bindings(), Req) -> Req when Req::req(). set_bindings(HostInfo, PathInfo, Bindings, Req) -> @@ -1227,13 +1076,6 @@ set_bindings(HostInfo, PathInfo, Bindings, Req) -> %% Misc API. -%% @doc Compact the request data by removing all non-system information. -%% -%% This essentially removes the host and path info, query string, bindings, -%% headers and cookies. -%% -%% Use it when you really need to save up memory, for example when having -%% many concurrent long-running connections. -spec compact(Req) -> Req when Req::req(). compact(Req) -> Req#http_req{host_info=undefined, @@ -1241,13 +1083,10 @@ compact(Req) -> bindings=undefined, headers=[], p_headers=[], cookies=[]}. -%% @doc Prevent any further responses. -%% @private -spec lock(Req) -> Req when Req::req(). lock(Req) -> Req#http_req{resp_state=locked}. -%% @doc Convert the Req object to a list of key/values. -spec to_list(req()) -> [{atom(), any()}]. to_list(Req) -> lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))). @@ -1340,7 +1179,7 @@ response_connection([], Connection) -> response_connection([{Name, Value}|Tail], Connection) -> case Name of <<"connection">> -> - Tokens = parse_connection_before(Value, []), + Tokens = cow_http_hd:parse_connection(Value), connection_to_atom(Tokens); _ -> response_connection(Tail, Connection) @@ -1382,57 +1221,6 @@ atom_to_connection(keepalive) -> atom_to_connection(close) -> <<"close">>. -%% Optimized parsing functions for the Connection header. -parse_connection_before(<<>>, Acc) -> - lists:reverse(Acc); -parse_connection_before(<< C, Rest/bits >>, Acc) - when C =:= $,; C =:= $\s; C =:= $\t -> - parse_connection_before(Rest, Acc); -parse_connection_before(Buffer, Acc) -> - parse_connection(Buffer, Acc, <<>>). - -%% An evil block of code appeared! -parse_connection(<<>>, Acc, <<>>) -> - lists:reverse(Acc); -parse_connection(<<>>, Acc, Token) -> - lists:reverse([Token|Acc]); -parse_connection(<< C, Rest/bits >>, Acc, Token) - when C =:= $,; C =:= $\s; C =:= $\t -> - parse_connection_before(Rest, [Token|Acc]); -parse_connection(<< C, Rest/bits >>, Acc, Token) -> - case C of - $A -> parse_connection(Rest, Acc, << Token/binary, $a >>); - $B -> parse_connection(Rest, Acc, << Token/binary, $b >>); - $C -> parse_connection(Rest, Acc, << Token/binary, $c >>); - $D -> parse_connection(Rest, Acc, << Token/binary, $d >>); - $E -> parse_connection(Rest, Acc, << Token/binary, $e >>); - $F -> parse_connection(Rest, Acc, << Token/binary, $f >>); - $G -> parse_connection(Rest, Acc, << Token/binary, $g >>); - $H -> parse_connection(Rest, Acc, << Token/binary, $h >>); - $I -> parse_connection(Rest, Acc, << Token/binary, $i >>); - $J -> parse_connection(Rest, Acc, << Token/binary, $j >>); - $K -> parse_connection(Rest, Acc, << Token/binary, $k >>); - $L -> parse_connection(Rest, Acc, << Token/binary, $l >>); - $M -> parse_connection(Rest, Acc, << Token/binary, $m >>); - $N -> parse_connection(Rest, Acc, << Token/binary, $n >>); - $O -> parse_connection(Rest, Acc, << Token/binary, $o >>); - $P -> parse_connection(Rest, Acc, << Token/binary, $p >>); - $Q -> parse_connection(Rest, Acc, << Token/binary, $q >>); - $R -> parse_connection(Rest, Acc, << Token/binary, $r >>); - $S -> parse_connection(Rest, Acc, << Token/binary, $s >>); - $T -> parse_connection(Rest, Acc, << Token/binary, $t >>); - $U -> parse_connection(Rest, Acc, << Token/binary, $u >>); - $V -> parse_connection(Rest, Acc, << Token/binary, $v >>); - $W -> parse_connection(Rest, Acc, << Token/binary, $w >>); - $X -> parse_connection(Rest, Acc, << Token/binary, $x >>); - $Y -> parse_connection(Rest, Acc, << Token/binary, $y >>); - $Z -> parse_connection(Rest, Acc, << Token/binary, $z >>); - C -> parse_connection(Rest, Acc, << Token/binary, C >>) - end. - -%% @doc Walk through a tokens list and return whether -%% the connection is keepalive or closed. -%% %% We don't match on "keep-alive" since it is the default value. -spec connection_to_atom([binary()]) -> keepalive | close. connection_to_atom([]) -> @@ -1505,7 +1293,6 @@ status(B) when is_binary(B) -> B. %% Tests. -ifdef(TEST). - url_test() -> {undefined, _} = url(#http_req{transport=ranch_tcp, host= <<>>, port= undefined, @@ -1533,19 +1320,7 @@ url_test() -> path= <<"/path">>, qs= <<"dummy=2785">>, pid=self()}), ok. -parse_connection_test_() -> - %% {Binary, Result} - Tests = [ - {<<"close">>, [<<"close">>]}, - {<<"ClOsE">>, [<<"close">>]}, - {<<"Keep-Alive">>, [<<"keep-alive">>]}, - {<<"keep-alive, Upgrade">>, [<<"keep-alive">>, <<"upgrade">>]} - ], - [{B, fun() -> R = parse_connection_before(B, []) end} - || {B, R} <- Tests]. - connection_to_atom_test_() -> - %% {Tokens, Result} Tests = [ {[<<"close">>], close}, {[<<"keep-alive">>], keepalive}, @@ -1569,5 +1344,4 @@ merge_headers_test_() -> {<<"server">>,<<"Cowboy">>}]} ], [fun() -> Res = merge_headers(L,R) end || {L, R, Res} <- Tests]. - -endif. diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl index 30ed15d..63e4dd9 100644 --- a/src/cowboy_rest.erl +++ b/src/cowboy_rest.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,8 +12,6 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc REST protocol implementation. -%% %% Originally based on the Webmachine Diagram from Alan Dean and %% Justin Sheehy. -module(cowboy_rest). @@ -57,11 +55,6 @@ expires :: undefined | no_call | calendar:datetime() }). -%% @doc Upgrade a HTTP request to the REST protocol. -%% -%% You do not need to call this function manually. To upgrade to the REST -%% protocol, you simply need to return <em>{upgrade, protocol, {@module}}</em> -%% in your <em>cowboy_http_handler:init/3</em> handler function. -spec upgrade(Req, Env, module(), any()) -> {ok, Req, Env} | {error, 500, Req} when Req::cowboy_req:req(), Env::cowboy_middleware:env(). diff --git a/src/cowboy_router.erl b/src/cowboy_router.erl index 16af2d4..ef91c6d 100644 --- a/src/cowboy_router.erl +++ b/src/cowboy_router.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,7 +12,7 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc Routing middleware. +%% Routing middleware. %% %% Resolve the handler to be used for the request based on the %% routing information found in the <em>dispatch</em> environment value. @@ -51,8 +51,6 @@ -opaque dispatch_rules() :: [dispatch_rule()]. -export_type([dispatch_rules/0]). -%% @doc Compile a list of routes into the dispatch format used -%% by Cowboy's routing. -spec compile(routes()) -> dispatch_rules(). compile(Routes) -> compile(Routes, []). @@ -162,7 +160,6 @@ compile_brackets_split(<< $], Rest/binary >>, Acc, 0) -> compile_brackets_split(<< C, Rest/binary >>, Acc, N) -> compile_brackets_split(Rest, << Acc/binary, C >>, N). -%% @private -spec execute(Req, Env) -> {ok, Req, Env} | {error, 400 | 404, Req} when Req::cowboy_req:req(), Env::cowboy_middleware:env(). @@ -183,7 +180,7 @@ execute(Req, Env) -> %% Internal. -%% @doc Match hostname tokens and path tokens against dispatch rules. +%% Match hostname tokens and path tokens against dispatch rules. %% %% It is typically used for matching tokens for the hostname and path of %% the request against a global dispatch rule for your listener. @@ -300,7 +297,6 @@ check_constraint({_, int}, Value) -> check_constraint({_, function, Fun}, Value) -> Fun(Value). -%% @doc Split a hostname into a list of tokens. -spec split_host(binary()) -> tokens(). split_host(Host) -> split_host(Host, []). @@ -317,8 +313,6 @@ split_host(Host, Acc) -> split_host(Rest, [Segment|Acc]) end. -%% @doc Split a path into a list of path segments. -%% %% Following RFC2396, this function may return path segments containing any %% character, including <em>/</em> if, and only if, a <em>/</em> was escaped %% and part of a path segment. @@ -332,9 +326,9 @@ split_path(Path, Acc) -> try case binary:match(Path, <<"/">>) of nomatch when Path =:= <<>> -> - lists:reverse([cowboy_http:urldecode(S) || S <- Acc]); + lists:reverse([cow_qs:urldecode(S) || S <- Acc]); nomatch -> - lists:reverse([cowboy_http:urldecode(S) || S <- [Path|Acc]]); + lists:reverse([cow_qs:urldecode(S) || S <- [Path|Acc]]); {Pos, _} -> << Segment:Pos/binary, _:8, Rest/bits >> = Path, split_path(Rest, [Segment|Acc]) @@ -376,9 +370,7 @@ list_match(_List, _Match, _Binds) -> %% Tests. -ifdef(TEST). - compile_test_() -> - %% {Routes, Result} Tests = [ %% Match any host and path. {[{'_', [{'_', h, o}]}], @@ -390,14 +382,22 @@ compile_test_() -> {[<<"path">>, <<"to">>, <<"resource">>], [], hb, ob}]}]}, {[{'_', [{"/path/to/resource/", h, o}]}], [{'_', [], [{[<<"path">>, <<"to">>, <<"resource">>], [], h, o}]}]}, - {[{'_', [{"/путь/к/ресурсу/", h, o}]}], - [{'_', [], [{[<<"путь">>, <<"к">>, <<"ресурсу">>], [], h, o}]}]}, + % Cyrillic from a latin1 encoded file. + {[{'_', [{[47,208,191,209,131,209,130,209,140,47,208,186,47,209,128, + 208,181,209,129,209,131,209,128,209,129,209,131,47], h, o}]}], + [{'_', [], [{[<<208,191,209,131,209,130,209,140>>, <<208,186>>, + <<209,128,208,181,209,129,209,131,209,128,209,129,209,131>>], + [], h, o}]}]}, {[{"cowboy.example.org.", [{'_', h, o}]}], [{[<<"org">>, <<"example">>, <<"cowboy">>], [], [{'_', [], h, o}]}]}, {[{".cowboy.example.org", [{'_', h, o}]}], [{[<<"org">>, <<"example">>, <<"cowboy">>], [], [{'_', [], h, o}]}]}, - {[{"некий.сайт.рф.", [{'_', h, o}]}], - [{[<<"рф">>, <<"сайт">>, <<"некий">>], [], [{'_', [], h, o}]}]}, + % Cyrillic from a latin1 encoded file. + {[{[208,189,208,181,208,186,208,184,208,185,46,209,129,208,176, + 208,185,209,130,46,209,128,209,132,46], [{'_', h, o}]}], + [{[<<209,128,209,132>>, <<209,129,208,176,208,185,209,130>>, + <<208,189,208,181,208,186,208,184,208,185>>], + [], [{'_', [], h, o}]}]}, {[{":subdomain.example.org", [{"/hats/:name/prices", h, o}]}], [{[<<"org">>, <<"example">>, subdomain], [], [ {[<<"hats">>, name, <<"prices">>], [], h, o}]}]}, @@ -425,7 +425,6 @@ compile_test_() -> fun() -> Rs = compile(Rt) end} || {Rt, Rs} <- Tests]. split_host_test_() -> - %% {Host, Result} Tests = [ {<<"">>, []}, {<<"*">>, [<<"*">>]}, @@ -442,7 +441,6 @@ split_host_test_() -> [{H, fun() -> R = split_host(H) end} || {H, R} <- Tests]. split_path_test_() -> - %% {Path, Result, QueryString} Tests = [ {<<"/">>, []}, {<<"/extend//cowboy">>, [<<"extend">>, <<>>, <<"cowboy">>]}, @@ -473,7 +471,6 @@ match_test_() -> {'_', [], match_any, []} ]} ], - %% {Host, Path, Result} Tests = [ {<<"any">>, <<"/">>, {ok, match_any, [], []}}, {<<"www.any.ninenines.eu">>, <<"/users/42/mails">>, @@ -506,8 +503,9 @@ match_info_test_() -> {[<<"eu">>, <<"ninenines">>, '...'], [], [ {'_', [], match_any, []} ]}, - {[<<"рф">>, <<"сайт">>], [], [ - {[<<"путь">>, '...'], [], match_path, []} + % Cyrillic from a latin1 encoded file. + {[<<209,128,209,132>>, <<209,129,208,176,208,185,209,130>>], [], [ + {[<<208,191,209,131,209,130,209,140>>, '...'], [], match_path, []} ]} ], Tests = [ @@ -523,8 +521,10 @@ match_info_test_() -> {ok, match_path, [], [], undefined, [<<"path_info">>]}}, {<<"www.ninenines.eu">>, <<"/pathinfo/is/next/foo/bar">>, {ok, match_path, [], [], undefined, [<<"foo">>, <<"bar">>]}}, - {<<"сайт.рф">>, <<"/путь/домой">>, - {ok, match_path, [], [], undefined, [<<"домой">>]}} + % Cyrillic from a latin1 encoded file. + {<<209,129,208,176,208,185,209,130,46,209,128,209,132>>, + <<47,208,191,209,131,209,130,209,140,47,208,180,208,190,208,188,208,190,208,185>>, + {ok, match_path, [], [], undefined, [<<208,180,208,190,208,188,208,190,208,185>>]}} ], [{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() -> R = match(Dispatch, H, P) @@ -569,5 +569,4 @@ match_same_bindings_test() -> {error, notfound, path} = match(Dispatch3, <<"ninenines.eu">>, <<"/path/to">>), ok. - -endif. diff --git a/src/cowboy_spdy.erl b/src/cowboy_spdy.erl index dd7882c..ce75419 100644 --- a/src/cowboy_spdy.erl +++ b/src/cowboy_spdy.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2013-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,10 +12,6 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc SPDY protocol handler. -%% -%% Note that there is no need to monitor these processes when using Cowboy as -%% an application as it already supervises them under the listener supervisor. -module(cowboy_spdy). %% API. @@ -37,17 +33,22 @@ %% Internal transport functions. -export([name/0]). +-export([messages/0]). -export([recv/3]). -export([send/2]). -export([sendfile/2]). +-export([setopts/2]). + +-type streamid() :: non_neg_integer(). +-type socket() :: {pid(), streamid()}. -record(child, { - streamid :: non_neg_integer(), + streamid :: streamid(), pid :: pid(), input = nofin :: fin | nofin, in_buffer = <<>> :: binary(), - is_recv = false :: {true, {non_neg_integer(), pid()}, - pid(), non_neg_integer(), reference()} | false, + is_recv = false :: false | {active, socket(), pid()} + | {passive, socket(), pid(), non_neg_integer(), reference()}, output = nofin :: fin | nofin }). @@ -63,7 +64,7 @@ peer, zdef, zinf, - last_streamid = 0 :: non_neg_integer(), + last_streamid = 0 :: streamid(), children = [] :: [#child{}] }). @@ -75,7 +76,6 @@ %% API. -%% @doc Start a SPDY protocol process. -spec start_link(any(), inet:socket(), module(), any()) -> {ok, pid()}. start_link(Ref, Socket, Transport, Opts) -> proc_lib:start_link(?MODULE, init, @@ -83,15 +83,13 @@ start_link(Ref, Socket, Transport, Opts) -> %% Internal. -%% @doc Faster alternative to proplists:get_value/3. -%% @private +%% Faster alternative to proplists:get_value/3. get_value(Key, Opts, Default) -> case lists:keyfind(Key, 1, Opts) of {_, Value} -> Value; _ -> Default end. -%% @private -spec init(pid(), ranch:ref(), inet:socket(), module(), opts()) -> ok. init(Parent, Ref, Socket, Transport, Opts) -> process_flag(trap_exit, true), @@ -142,15 +140,15 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport, TRef = erlang:send_after(Timeout, self(), {recv_timeout, FromSocket}), loop(replace_child(Child#child{ - is_recv={true, FromSocket, FromPid, Length, TRef}}, + is_recv={passive, FromSocket, FromPid, Length, TRef}}, State)) end; {recv_timeout, {Pid, StreamID}} when Pid =:= self() -> - Child = #child{is_recv={true, FromSocket, FromPid, _, _}} + Child = #child{is_recv={passive, FromSocket, FromPid, _, _}} = get_child(StreamID, State), FromPid ! {recv, FromSocket, {error, timeout}}, - loop(replace_child(Child#child{is_recv=false}, State)); + loop(replace_child(Child#child{is_recv=passive}, State)); {reply, {Pid, StreamID}, Status, Headers} when Pid =:= self() -> Child = #child{output=nofin} = get_child(StreamID, State), @@ -182,6 +180,22 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport, Child = #child{output=nofin} = get_child(StreamID, State), data_from_file(State, StreamID, Filepath), loop(replace_child(Child#child{output=fin}, State)); + {active, FromSocket = {Pid, StreamID}, FromPid} when Pid =:= self() -> + Child = #child{in_buffer=InBuffer, is_recv=false} + = get_child(StreamID, State), + case InBuffer of + <<>> -> + loop(replace_child(Child#child{ + is_recv={active, FromSocket, FromPid}}, State)); + _ -> + FromPid ! {spdy, FromSocket, InBuffer}, + loop(replace_child(Child#child{in_buffer= <<>>}, State)) + end; + {passive, FromSocket = {Pid, StreamID}, FromPid} when Pid =:= self() -> + Child = #child{is_recv=IsRecv} = get_child(StreamID, State), + %% Make sure we aren't in the middle of a recv call. + case IsRecv of false -> ok; {active, FromSocket, FromPid} -> ok end, + loop(replace_child(Child#child{is_recv=false}, State)); {'EXIT', Parent, Reason} -> exit(Reason); {'EXIT', Pid, _} -> @@ -209,6 +223,7 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport, terminate(State) end. +-spec system_continue(_, _, #state{}) -> ok. system_continue(_, _, State) -> loop(State). @@ -216,6 +231,7 @@ system_continue(_, _, State) -> system_terminate(Reason, _, _, _) -> exit(Reason). +-spec system_code_change(Misc, _, _, _) -> {ok, Misc} when Misc::#state{}. system_code_change(Misc, _, _, _) -> {ok, Misc}. @@ -264,11 +280,14 @@ handle_frame(State, {data, StreamID, IsFin, Data}) -> Data2 = << Buffer/binary, Data/binary >>, IsFin2 = if IsFin -> fin; true -> nofin end, Child2 = case IsRecv of - {true, FromSocket, FromPid, 0, TRef} -> + {active, FromSocket, FromPid} -> + FromPid ! {spdy, FromSocket, Data}, + Child#child{input=IsFin2, is_recv=false}; + {passive, FromSocket, FromPid, 0, TRef} -> FromPid ! {recv, FromSocket, {ok, Data2}}, cancel_recv_timeout(StreamID, TRef), Child#child{input=IsFin2, in_buffer= <<>>, is_recv=false}; - {true, FromSocket, FromPid, Length, TRef} + {passive, FromSocket, FromPid, Length, TRef} when byte_size(Data2) >= Length -> << Data3:Length/binary, Rest/binary >> = Data2, FromPid ! {recv, FromSocket, {ok, Data3}}, @@ -358,6 +377,11 @@ delete_child(Pid, State=#state{children=Children}) -> %% Request process. +-spec request_init(socket(), {inet:ip_address(), inet:port_number()}, + cowboy:onrequest_fun(), cowboy:onresponse_fun(), + cowboy_middleware:env(), [module()], + binary(), binary(), binary(), binary(), [{binary(), binary()}]) + -> ok. request_init(FakeSocket, Peer, OnRequest, OnResponse, Env, Middlewares, Method, Host, Path, Version, Headers) -> {Host2, Port} = cow_http:parse_fullhost(Host), @@ -394,7 +418,6 @@ execute(Req, Env, [Middleware|Tail]) -> cowboy_req:maybe_reply(Status, Req2) end. -%% @private -spec resume(cowboy_middleware:env(), [module()], module(), module(), [any()]) -> ok. resume(Env, Tail, Module, Function, Args) -> @@ -412,6 +435,7 @@ resume(Env, Tail, Module, Function, Args) -> %% Reply functions used by cowboy_req. +-spec reply(socket(), binary(), cowboy:http_headers(), iodata()) -> ok. reply(Socket = {Pid, _}, Status, Headers, Body) -> _ = case iolist_size(Body) of 0 -> Pid ! {reply, Socket, Status, Headers}; @@ -419,23 +443,33 @@ reply(Socket = {Pid, _}, Status, Headers, Body) -> end, ok. +-spec stream_reply(socket(), binary(), cowboy:http_headers()) -> ok. stream_reply(Socket = {Pid, _}, Status, Headers) -> _ = Pid ! {stream_reply, Socket, Status, Headers}, ok. +-spec stream_data(socket(), iodata()) -> ok. stream_data(Socket = {Pid, _}, Data) -> _ = Pid ! {stream_data, Socket, Data}, ok. +-spec stream_close(socket()) -> ok. stream_close(Socket = {Pid, _}) -> _ = Pid ! {stream_close, Socket}, ok. %% Internal transport functions. +-spec name() -> spdy. name() -> spdy. +-spec messages() -> {spdy, spdy_closed, spdy_error}. +messages() -> + {spdy, spdy_closed, spdy_error}. + +-spec recv(socket(), non_neg_integer(), timeout()) + -> {ok, binary()} | {error, timeout}. recv(Socket = {Pid, _}, Length, Timeout) -> _ = Pid ! {recv, Socket, self(), Length, Timeout}, receive @@ -443,12 +477,22 @@ recv(Socket = {Pid, _}, Length, Timeout) -> Ret end. +-spec send(socket(), iodata()) -> ok. send(Socket, Data) -> stream_data(Socket, Data). %% We don't wait for the result of the actual sendfile call, %% therefore we can't know how much was actually sent. %% This isn't a problem as we don't use this value in Cowboy. +-spec sendfile(socket(), file:name_all()) -> {ok, undefined}. sendfile(Socket = {Pid, _}, Filepath) -> _ = Pid ! {sendfile, Socket, Filepath}, {ok, undefined}. + +-spec setopts(inet:socket(), list()) -> ok. +setopts(Socket = {Pid, _}, [{active, once}]) -> + _ = Pid ! {active, Socket, self()}, + ok; +setopts(Socket = {Pid, _}, [{active, false}]) -> + _ = Pid ! {passive, Socket, self()}, + ok. diff --git a/src/cowboy_static.erl b/src/cowboy_static.erl index 476d1b8..d10ee87 100644 --- a/src/cowboy_static.erl +++ b/src/cowboy_static.erl @@ -1,5 +1,5 @@ -%% Copyright (c) 2013, Loïc Hoguin <[email protected]> %% Copyright (c) 2011, Magnus Klaar <[email protected]> +%% Copyright (c) 2013-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -39,10 +39,11 @@ -type state() :: {binary(), {ok, #file_info{}} | {error, atom()}, extra()}. +-spec init(_, _, _) -> {upgrade, protocol, cowboy_rest}. init(_, _, _) -> {upgrade, protocol, cowboy_rest}. -%% @doc Resolve the file that will be sent and get its file information. +%% Resolve the file that will be sent and get its file information. %% If the handler is configured to manage a directory, check that the %% requested file is inside the configured directory. @@ -197,7 +198,7 @@ bad_path_win32_check_test_() -> end} || P <- Tests]. -endif. -%% @doc Reject requests that tried to access a file outside +%% Reject requests that tried to access a file outside %% the target directory. -spec malformed_request(Req, State) @@ -205,7 +206,7 @@ bad_path_win32_check_test_() -> malformed_request(Req, State) -> {State =:= error, Req, State}. -%% @doc Directories, files that can't be accessed at all and +%% Directories, files that can't be accessed at all and %% files with no read flag are forbidden. -spec forbidden(Req, State) @@ -221,7 +222,7 @@ forbidden(Req, State={_, {ok, #file_info{access=Access}}, _}) forbidden(Req, State) -> {false, Req, State}. -%% @doc Detect the mimetype of the file. +%% Detect the mimetype of the file. -spec content_types_provided(Req, State) -> {[{binary(), get_file}], Req, State} @@ -236,7 +237,7 @@ content_types_provided(Req, State={Path, _, Extra}) -> {[{Type, get_file}], Req, State} end. -%% @doc Assume the resource doesn't exist if it's not a regular file. +%% Assume the resource doesn't exist if it's not a regular file. -spec resource_exists(Req, State) -> {boolean(), Req, State} @@ -246,7 +247,7 @@ resource_exists(Req, State={_, {ok, #file_info{type=regular}}, _}) -> resource_exists(Req, State) -> {false, Req, State}. -%% @doc Generate an etag for the file. +%% Generate an etag for the file. -spec generate_etag(Req, State) -> {{strong | weak, binary()}, Req, State} @@ -266,7 +267,7 @@ generate_default_etag(Size, Mtime) -> {strong, list_to_binary(integer_to_list( erlang:phash2({Size, Mtime}, 16#ffffffff)))}. -%% @doc Return the time of last modification of the file. +%% Return the time of last modification of the file. -spec last_modified(Req, State) -> {calendar:datetime(), Req, State} @@ -274,7 +275,7 @@ generate_default_etag(Size, Mtime) -> last_modified(Req, State={_, {ok, #file_info{mtime=Modified}}, _}) -> {Modified, Req, State}. -%% @doc Stream the file. +%% Stream the file. %% @todo Export cowboy_req:resp_body_fun()? -spec get_file(Req, State) diff --git a/src/cowboy_sub_protocol.erl b/src/cowboy_sub_protocol.erl index 26ccd7e..713c3cd 100644 --- a/src/cowboy_sub_protocol.erl +++ b/src/cowboy_sub_protocol.erl @@ -1,4 +1,5 @@ %% Copyright (c) 2013, James Fish <[email protected]> +%% Copyright (c) 2013-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,21 +13,6 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc Behaviour for sub protocols. -%% -%% Only one function needs to be implemented, <em>upgrade/4</em>. -%% It receives the Req, the environment, the handler that the request has been -%% routed to and the handler's options. It acts exactly the same as a -%% middleware, so returns the same values a middleware's execute/2. -%% -%% Once the sub protocol has processed the request it should add the result -%% to the environment. This is done by adding the tuple {result, Value} to the -%% environment list. To continue handling requests on the current connection the -%% Value should be the atom ok. Any other value will prevent the processing of -%% subsequent requests. -%% -%% <em>upgrade/4</em> will be called when a handler's init/3 returns -%% {upgrade, protocol, Module}, where Module is the module of the sub protocol. -module(cowboy_sub_protocol). -callback upgrade(Req, Env, module(), any()) diff --git a/src/cowboy_sup.erl b/src/cowboy_sup.erl index 0e4e59a..cf48595 100644 --- a/src/cowboy_sup.erl +++ b/src/cowboy_sup.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,26 +12,18 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @private -module(cowboy_sup). -behaviour(supervisor). -%% API. -export([start_link/0]). - -%% supervisor. -export([init/1]). --define(SUPERVISOR, ?MODULE). - -%% API. - -spec start_link() -> {ok, pid()}. start_link() -> - supervisor:start_link({local, ?SUPERVISOR}, ?MODULE, []). - -%% supervisor. + supervisor:start_link({local, ?MODULE}, ?MODULE, []). +-spec init([]) + -> {ok, {{supervisor:strategy(), 10, 10}, [supervisor:child_spec()]}}. init([]) -> Procs = [{cowboy_clock, {cowboy_clock, start_link, []}, permanent, 5000, worker, [cowboy_clock]}], diff --git a/src/cowboy_websocket.erl b/src/cowboy_websocket.erl index 40046e1..98e25e6 100644 --- a/src/cowboy_websocket.erl +++ b/src/cowboy_websocket.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,8 +12,6 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc Websocket protocol implementation. -%% %% Cowboy supports versions 7 through 17 of the Websocket drafts. %% It also supports RFC6455, the proposed standard for Websocket. -module(cowboy_websocket). @@ -23,10 +21,7 @@ %% @todo Remove when we support only R16B+. -compile(nowarn_deprecated_function). -%% API. -export([upgrade/4]). - -%% Internal. -export([handler_loop/4]). -type close_code() :: 1000..4999. @@ -42,6 +37,8 @@ -type frag_state() :: undefined | {nofin, opcode(), binary()} | {fin, opcode(), binary()}. -type rsv() :: << _:3 >>. +-type terminate_reason() :: {normal | error | remote, atom()} + | {remote, close_code(), binary()}. -record(state, { env :: cowboy_middleware:env(), @@ -60,11 +57,6 @@ deflate_state :: undefined | port() }). -%% @doc Upgrade an HTTP request to the Websocket protocol. -%% -%% You do not need to call this function manually. To upgrade to the Websocket -%% protocol, you simply need to return <em>{upgrade, protocol, {@module}}</em> -%% in your <em>cowboy_http_handler:init/3</em> handler function. -spec upgrade(Req, Env, module(), any()) -> {ok, Req, Env} | {error, 400, Req} | {suspend, module(), atom(), [any()]} @@ -213,7 +205,6 @@ handler_loop_timeout(State=#state{timeout=Timeout, timeout_ref=PrevRef}) -> TRef = erlang:start_timer(Timeout, self(), ?MODULE), State#state{timeout_ref=TRef}. -%% @private -spec handler_loop(#state{}, Req, any(), binary()) -> {ok, Req, cowboy_middleware:env()} | {suspend, module(), atom(), [any()]} @@ -730,8 +721,7 @@ websocket_send_many([Frame|Tail], State) -> {Error, State2} -> {Error, State2} end. --spec websocket_close(#state{}, Req, any(), - {atom(), atom()} | {remote, close_code(), binary()}) +-spec websocket_close(#state{}, Req, any(), terminate_reason()) -> {ok, Req, cowboy_middleware:env()} when Req::cowboy_req:req(). websocket_close(State=#state{socket=Socket, transport=Transport}, @@ -752,7 +742,7 @@ websocket_close(State=#state{socket=Socket, transport=Transport}, end, handler_terminate(State, Req, HandlerState, Reason). --spec handler_terminate(#state{}, Req, any(), atom() | {atom(), atom()}) +-spec handler_terminate(#state{}, Req, any(), terminate_reason()) -> {ok, Req, cowboy_middleware:env()} when Req::cowboy_req:req(). handler_terminate(#state{env=Env, handler=Handler}, diff --git a/src/cowboy_websocket_handler.erl b/src/cowboy_websocket_handler.erl index 8bbbc08..177e5f6 100644 --- a/src/cowboy_websocket_handler.erl +++ b/src/cowboy_websocket_handler.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -12,40 +12,6 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @doc Handler for HTTP WebSocket requests. -%% -%% WebSocket handlers must implement five callbacks: <em>init/3</em>, -%% <em>websocket_init/3</em>, <em>websocket_handle/3</em>, -%% <em>websocket_info/3</em> and <em>websocket_terminate/3</em>. -%% These callbacks will only be called if the connection is upgraded -%% to WebSocket in the HTTP handler's <em>init/3</em> callback. -%% They are then called in that order, although <em>websocket_handle/3</em> -%% will be called for each packet received, and <em>websocket_info</em> -%% for each message received. -%% -%% <em>websocket_init/3</em> is meant for initialization. It receives -%% information about the transport and protocol used, along with the handler -%% options from the dispatch list. You can define a request-wide state here. -%% If you are going to want to compact the request, you should probably do it -%% here. -%% -%% <em>websocket_handle/3</em> receives the data from the socket. It can reply -%% something, do nothing or close the connection. -%% -%% <em>websocket_info/3</em> receives messages sent to the process. It has -%% the same reply format as <em>websocket_handle/3</em> described above. Note -%% that unlike in a <em>gen_server</em>, when <em>websocket_info/3</em> -%% replies something, it is always to the socket, not to the process that -%% originated the message. -%% -%% <em>websocket_terminate/3</em> is meant for cleaning up. It also receives -%% the request and the state previously defined, along with a reason for -%% termination. -%% -%% All of <em>websocket_init/3</em>, <em>websocket_handle/3</em> and -%% <em>websocket_info/3</em> can decide to hibernate the process by adding -%% an extra element to the returned tuple, containing the atom -%% <em>hibernate</em>. Doing so helps save memory and improve CPU usage. -module(cowboy_websocket_handler). -type opts() :: any(). diff --git a/test/autobahn_SUITE.erl b/test/autobahn_SUITE.erl deleted file mode 100644 index da56667..0000000 --- a/test/autobahn_SUITE.erl +++ /dev/null @@ -1,100 +0,0 @@ -%% Copyright (c) 2011, Magnus Klaar <[email protected]> -%% -%% 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(autobahn_SUITE). - -%% This CT suite reuses the websocket server test suite from the Autobahn -%% project. The Autobahn project is a websocket implementation for Python. -%% Given that we don't expect to find the packages and tools to properly -%% set up and run such a test on a system used primarily for Erlang devlopment -%% this test suite is not included in the default 'ct' target in the makefile. - --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. --export([run_tests/1]). %% autobahn. - -%% ct. - -all() -> - [{group, autobahn}]. - -groups() -> - BaseTests = [run_tests], - [{autobahn, [], BaseTests}]. - -init_per_suite(Config) -> - application:start(crypto), - application:start(cowlib), - application:start(ranch), - application:start(cowboy), - %% /tmp must be used as the parent directory for the virtualenv because - %% the directory names used in CT are so long that the interpreter path - %% in the scripts generated by virtualenv get so long that the system - %% refuses to execute them. - EnvPath = "/tmp/cowboy_autobahn_env", - os:putenv("AB_TESTS_ENV", EnvPath), - os:putenv("AB_TESTS_PRIV", ?config(priv_dir, Config)), - BinPath = filename:join(?config(data_dir, Config), "test.py"), - Stdout = os:cmd(BinPath ++ " setup"), - ct:log("~s~n", [Stdout]), - case string:str(Stdout, "AB-TESTS-SETUP-OK") of - 0 -> erlang:error(failed); - _ -> [{env_path, EnvPath},{bin_path,BinPath}|Config] - end. - -end_per_suite(_Config) -> - os:cmd("deactivate"), - application:stop(cowboy), - application:stop(ranch), - application:stop(cowlib), - application:stop(crypto), - ok. - -init_per_group(autobahn, Config) -> - Port = 33080, - cowboy:start_http(autobahn, 100, [{port, Port}], [ - {env, [{dispatch, init_dispatch()}]} - ]), - [{port, Port}|Config]. - -end_per_group(Listener, _Config) -> - cowboy:stop_listener(Listener), - ok. - -%% Dispatch configuration. - -init_dispatch() -> - cowboy_router:compile([{"localhost", [ - {"/echo", autobahn_echo, []}]}]). - -%% autobahn cases - -run_tests(Config) -> - PrivDir = ?config(priv_dir, Config), - IndexFile = filename:join([PrivDir, "reports", "servers", "index.html"]), - ct:log("<h2><a href=\"~s\">Full Test Results Report</a></h2>~n", [IndexFile]), - BinPath = ?config(bin_path, Config), - Stdout = os:cmd(BinPath ++ " test"), - ct:log("~s~n", [Stdout]), - case string:str(Stdout, "AB-TESTS-TEST-OK") of - 0 -> erlang:error(failed); - _ -> ok - end, - {ok, IndexHTML} = file:read_file(IndexFile), - case length(binary:matches(IndexHTML, <<"case_failed">>)) > 2 of - true -> erlang:error(failed); - false -> ok - end. diff --git a/test/autobahn_SUITE_data/autobahn_echo.erl b/test/autobahn_SUITE_data/autobahn_echo.erl deleted file mode 100644 index 07f7fd9..0000000 --- a/test/autobahn_SUITE_data/autobahn_echo.erl +++ /dev/null @@ -1,26 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(autobahn_echo). --behaviour(cowboy_websocket_handler). --export([init/3]). --export([websocket_init/3, websocket_handle/3, - websocket_info/3, websocket_terminate/3]). - -init(_Any, _Req, _Opts) -> - {upgrade, protocol, cowboy_websocket}. - -websocket_init(_TransportName, Req, _Opts) -> - {ok, Req, undefined}. - -websocket_handle({text, Data}, Req, State) -> - {reply, {text, Data}, Req, State}; -websocket_handle({binary, Data}, Req, State) -> - {reply, {binary, Data}, Req, State}; -websocket_handle(_Frame, Req, State) -> - {ok, Req, State}. - -websocket_info(_Info, Req, State) -> - {ok, Req, State}. - -websocket_terminate(_Reason, _Req, _State) -> - ok. diff --git a/test/autobahn_SUITE_data/test.py b/test/autobahn_SUITE_data/test.py deleted file mode 100755 index f142683..0000000 --- a/test/autobahn_SUITE_data/test.py +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env python -import os -import os.path -import sys -import subprocess - - -AB_TESTS_ENV = os.getenv("AB_TESTS_ENV") -AB_TESTS_PRIV = os.getenv("AB_TESTS_PRIV") - -VIRTUALENV_URL = 'https://raw.github.com/pypa/virtualenv/master/virtualenv.py' -VIRTUALENV_BIN = os.path.join(AB_TESTS_ENV, "virtualenv.py") -INSTALL_BIN = os.path.join(AB_TESTS_ENV, "bin", "easy_install") - - -def activate_env(env): - """ - See 'Using Virtualenv without bin/python' at http://www.virtualenv.org - """ - activate_this = os.path.join(env, 'bin', 'activate_this.py') - exec(compile(open(activate_this).read(), activate_this, 'exec'), - dict(__file__=activate_this)) - -def install_env(env): - """ - Install a new virtualenv at a path and also install the Autobahn package. - """ - os.makedirs(env) if not os.path.isdir(env) else None - subprocess.check_call(["curl", "-sS", VIRTUALENV_URL, "-o", VIRTUALENV_BIN]) - subprocess.check_call(["python", VIRTUALENV_BIN, env]) - activate_env(env) - subprocess.check_call([INSTALL_BIN, "http://pypi.python.org/packages/2.7/a/autobahntestsuite/autobahntestsuite-0.5.3-py2.7.egg"]) - -def client_config(): - """ - See comment on SUPPORTED_SPEC_VERSIONS in Autobahn/.../websocket.py - """ - base = { - 'options': {'failByDrop': False}, - 'enable-ssl': False, - 'servers': [{ - 'agent': 'Cowboy', - 'url': 'ws://localhost:33080/echo', - 'options': {'version': 18} # RFC6455 - }], - 'cases': ['*'], - 'exclude-cases': [] } - return base - -def run_test(env, config): - activate_env(env) - from twisted.python import log - from twisted.internet import reactor - from autobahntestsuite.fuzzing import FuzzingClientFactory - os.chdir(AB_TESTS_PRIV) - log.startLogging(sys.stdout) - fuzzer = FuzzingClientFactory(config) - return reactor.run() - - -def main(): - cmd = sys.argv[1] - if cmd == 'setup': - install_env(AB_TESTS_ENV) - print('AB-TESTS-SETUP-OK') - elif cmd == 'test': - run_test(AB_TESTS_ENV, client_config()) - print('AB-TESTS-TEST-OK') - else: - return 1 - -if __name__ == '__main__': - main() diff --git a/test/cowboy_ct_hook.erl b/test/cowboy_ct_hook.erl new file mode 100644 index 0000000..1586412 --- /dev/null +++ b/test/cowboy_ct_hook.erl @@ -0,0 +1,23 @@ +%% Copyright (c) 2014, Loïc Hoguin <[email protected]> +%% +%% 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(cowboy_ct_hook). + +-export([init/2]). + +init(_, _) -> + cowboy_test:start([cowboy, gun]), + cowboy_test:make_certs(), + error_logger:add_report_handler(cowboy_error_h), + {ok, undefined}. diff --git a/test/cowboy_error_h.erl b/test/cowboy_error_h.erl new file mode 100644 index 0000000..b4ae78f --- /dev/null +++ b/test/cowboy_error_h.erl @@ -0,0 +1,145 @@ +%% Copyright (c) 2014, Loïc Hoguin <[email protected]> +%% +%% 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(cowboy_error_h). +-behaviour(gen_event). + +%% Public interface. +-export([ignore/3]). + +%% gen_event. +-export([init/1]). +-export([handle_event/2]). +-export([handle_call/2]). +-export([handle_info/2]). +-export([terminate/2]). +-export([code_change/3]). + +%% Public interface. + +%% Ignore crashes from Pid occuring in M:F/A. +ignore(M, F, A) -> + gen_event:call(error_logger, ?MODULE, {expect, {self(), M, F, A}}). + +%% gen_event. + +init(_) -> + spawn(fun() -> error_logger:tty(false) end), + {ok, []}. + +%% Ignore supervisor and progress reports. +handle_event({info_report, _, {_, progress, _}}, State) -> + {ok, State}; +handle_event({info_report, _, {_, std_info, _}}, State) -> + {ok, State}; +handle_event({error_report, _, {_, supervisor_report, _}}, State) -> + {ok, State}; +%% Ignore gun retry failures. +handle_event({error_report, _, {_, crash_report, + [[{initial_call, {gun, init, _}}, _, _, + {error_info, {error, gone, _}}|_]|_]}}, + State) -> + {ok, State}; +%% Ignore emulator reports that are a duplicate of what Ranch gives us. +%% +%% The emulator always sends strings for errors, which makes it very +%% difficult to extract the information we need, hence the regexps. +handle_event(Event = {error, GL, {emulator, _, Msg}}, State) + when node(GL) =:= node() -> + Result = re:run(Msg, + "Error in process ([^\s]+).+? with exit value: " + ".+?{stacktrace,\\[{([^,]+),([^,]+),(.+)", + [{capture, all_but_first, list}]), + case Result of + nomatch -> + write_event(Event), + {ok, State}; + {match, [PidStr, MStr, FStr, Rest]} -> + A = case Rest of + "[]" ++ _ -> + 0; + "[" ++ Rest2 -> + count_args(Rest2, 1, 0); + _ -> + {match, [AStr]} = re:run(Rest, "([^,]+).+", + [{capture, all_but_first, list}]), + list_to_integer(AStr) + end, + Crash = {list_to_pid(PidStr), list_to_existing_atom(MStr), + list_to_existing_atom(FStr), A}, + case lists:member(Crash, State) of + true -> + {ok, lists:delete(Crash, State)}; + false -> + write_event(Event), + {ok, State} + end + end; +handle_event(Event = {error, GL, + {_, "Ranch listener" ++ _, [_, _, Pid, {[_, _, + {stacktrace, [{M, F, A, _}|_]}|_], _}]}}, + State) when node(GL) =:= node() -> + A2 = if is_list(A) -> length(A); true -> A end, + Crash = {Pid, M, F, A2}, + case lists:member(Crash, State) of + true -> + {ok, lists:delete(Crash, State)}; + false -> + write_event(Event), + {ok, State} + end; +handle_event(Event = {_, GL, _}, State) when node(GL) =:= node() -> + write_event(Event), + {ok, State}; +handle_event(_, State) -> + {ok, State}. + +handle_call({expect, Crash}, State) -> + {ok, ok, [Crash, Crash|State]}; +handle_call(_, State) -> + {ok, {error, bad_query}, State}. + +handle_info(_, State) -> + {ok, State}. + +terminate(_, _) -> + spawn(fun() -> error_logger:tty(true) end), + ok. + +code_change(_, State, _) -> + {ok, State}. + +%% Internal. + +write_event(Event) -> + error_logger_tty_h:write_event( + {erlang:universaltime(), Event}, + io). + +count_args("]" ++ _, N, 0) -> + N; +count_args("]" ++ Tail, N, Levels) -> + count_args(Tail, N, Levels - 1); +count_args("[" ++ Tail, N, Levels) -> + count_args(Tail, N, Levels + 1); +count_args("}" ++ Tail, N, Levels) -> + count_args(Tail, N, Levels - 1); +count_args("{" ++ Tail, N, Levels) -> + count_args(Tail, N, Levels + 1); +count_args("," ++ Tail, N, Levels = 0) -> + count_args(Tail, N + 1, Levels); +count_args("," ++ Tail, N, Levels) -> + count_args(Tail, N, Levels); +count_args([_|Tail], N, Levels) -> + count_args(Tail, N, Levels). diff --git a/test/cowboy_test.erl b/test/cowboy_test.erl new file mode 100644 index 0000000..f4a5706 --- /dev/null +++ b/test/cowboy_test.erl @@ -0,0 +1,200 @@ +%% Copyright (c) 2014, Loïc Hoguin <[email protected]> +%% +%% 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(cowboy_test). +-compile(export_all). + +%% Start and stop applications and their dependencies. + +start(Apps) -> + _ = [do_start(App) || App <- Apps], + ok. + +do_start(App) -> + case application:start(App) of + ok -> + ok; + {error, {not_started, Dep}} -> + do_start(Dep), + do_start(App) + end. + +%% SSL certificate creation and safekeeping. + +make_certs() -> + {_, Cert, Key} = ct_helper:make_certs(), + CertOpts = [{cert, Cert}, {key, Key}], + Pid = spawn(fun() -> receive after infinity -> ok end end), + ?MODULE = ets:new(?MODULE, [ordered_set, public, named_table, + {heir, Pid, undefined}]), + ets:insert(?MODULE, {cert_opts, CertOpts}), + ok. + +get_certs() -> + ets:lookup_element(?MODULE, cert_opts, 2). + +%% Quick configuration value retrieval. + +config(Key, Config) -> + {_, Value} = lists:keyfind(Key, 1, Config), + Value. + +%% Test case description. + +doc(String) -> + ct:comment(String), + ct:log(String). + +%% List of all test cases in the suite. + +all(Suite) -> + lists:usort([F || {F, 1} <- Suite:module_info(exports), + F =/= module_info, + F =/= test, %% This is leftover from the eunit parse_transform... + F =/= all, + F =/= groups, + string:substr(atom_to_list(F), 1, 5) =/= "init_", + string:substr(atom_to_list(F), 1, 4) =/= "end_", + string:substr(atom_to_list(F), 1, 3) =/= "do_" + ]). + +%% Listeners initialization. + +init_http(Ref, ProtoOpts, Config) -> + {ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [ + {max_keepalive, 50}, + {timeout, 500} + |ProtoOpts]), + Port = ranch:get_port(Ref), + [{type, tcp}, {port, Port}, {opts, []}|Config]. + +init_https(Ref, ProtoOpts, Config) -> + Opts = get_certs(), + {ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [ + {max_keepalive, 50}, + {timeout, 500} + |ProtoOpts]), + Port = ranch:get_port(Ref), + [{type, ssl}, {port, Port}, {opts, Opts}|Config]. + +init_spdy(Ref, ProtoOpts, Config) -> + Opts = get_certs(), + {ok, _} = cowboy:start_spdy(Ref, 100, Opts ++ [{port, 0}], + ProtoOpts), + Port = ranch:get_port(Ref), + [{type, ssl}, {port, Port}, {opts, Opts}|Config]. + +%% Common group of listeners used by most suites. + +common_all() -> + [ + {group, http}, + {group, https}, + {group, spdy}, + {group, http_compress}, + {group, https_compress}, + {group, spdy_compress} + ]. + +common_groups(Tests) -> + [ + {http, [parallel], Tests}, + {https, [parallel], Tests}, + {spdy, [parallel], Tests}, + {http_compress, [parallel], Tests}, + {https_compress, [parallel], Tests}, + {spdy_compress, [parallel], Tests} + ]. + +init_common_groups(Name = http, Config, Mod) -> + init_http(Name, [ + {env, [{dispatch, Mod:init_dispatch(Config)}]} + ], Config); +init_common_groups(Name = https, Config, Mod) -> + init_https(Name, [ + {env, [{dispatch, Mod:init_dispatch(Config)}]} + ], Config); +init_common_groups(Name = spdy, Config, Mod) -> + init_spdy(Name, [ + {env, [{dispatch, Mod:init_dispatch(Config)}]} + ], Config); +init_common_groups(Name = http_compress, Config, Mod) -> + init_http(Name, [ + {env, [{dispatch, Mod:init_dispatch(Config)}]}, + {compress, true} + ], Config); +init_common_groups(Name = https_compress, Config, Mod) -> + init_https(Name, [ + {env, [{dispatch, Mod:init_dispatch(Config)}]}, + {compress, true} + ], Config); +init_common_groups(Name = spdy_compress, Config, Mod) -> + init_spdy(Name, [ + {env, [{dispatch, Mod:init_dispatch(Config)}]}, + {compress, true} + ], Config). + +%% Support functions for testing using Gun. + +gun_open(Config) -> + gun_open(Config, []). + +gun_open(Config, Opts) -> + {ok, ConnPid} = gun:open("localhost", config(port, Config), + [{retry, 0}, {type, config(type, Config)}|Opts]), + ConnPid. + +gun_monitor_open(Config) -> + gun_monitor_open(Config, []). + +gun_monitor_open(Config, Opts) -> + ConnPid = gun_open(Config, Opts), + {ConnPid, monitor(process, ConnPid)}. + +gun_is_gone(ConnPid, MRef) -> + receive {'DOWN', MRef, process, ConnPid, gone} -> ok + after 500 -> error(timeout) end. + +%% Support functions for testing using a raw socket. + +raw_open(Config) -> + Transport = case config(type, Config) of + tcp -> gen_tcp; + ssl -> ssl + end, + {_, Opts} = lists:keyfind(opts, 1, Config), + {ok, Socket} = Transport:connect("localhost", config(port, Config), + [binary, {active, false}, {packet, raw}, + {reuseaddr, true}, {nodelay, true}|Opts]), + {raw_client, Socket, Transport}. + +raw_send({raw_client, Socket, Transport}, Data) -> + Transport:send(Socket, Data). + +raw_recv_head({raw_client, Socket, Transport}) -> + {ok, Data} = Transport:recv(Socket, 0, 5000), + raw_recv_head(Socket, Transport, Data). + +raw_recv_head(Socket, Transport, Buffer) -> + case binary:match(Buffer, <<"\r\n\r\n">>) of + nomatch -> + {ok, Data} = Transport:recv(Socket, 0, 5000), + raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>); + {_, _} -> + Buffer + end. + +raw_expect_recv({raw_client, Socket, Transport}, Expect) -> + {ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 5000), + ok. diff --git a/test/eunit_SUITE.erl b/test/eunit_SUITE.erl index a460890..1d817c6 100644 --- a/test/eunit_SUITE.erl +++ b/test/eunit_SUITE.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2013-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -13,16 +13,7 @@ %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -module(eunit_SUITE). - --include_lib("common_test/include/ct.hrl"). - -%% ct. --export([all/0]). - -%% Tests. --export([eunit/1]). - -%% ct. +-compile(export_all). all() -> [eunit]. diff --git a/test/handlers/long_polling_h.erl b/test/handlers/long_polling_h.erl new file mode 100644 index 0000000..21f1d4d --- /dev/null +++ b/test/handlers/long_polling_h.erl @@ -0,0 +1,27 @@ +%% This module implements a loop handler for long-polling. +%% It starts by sending itself a message after 200ms, +%% then sends another after that for a total of 3 messages. +%% When it receives the last message, it sends a 102 reply back. + +-module(long_polling_h). +-behaviour(cowboy_loop_handler). + +-export([init/3]). +-export([info/3]). +-export([terminate/3]). + +init(_, Req, _) -> + erlang:send_after(200, self(), timeout), + {loop, Req, 2, 5000, hibernate}. + +info(timeout, Req, 0) -> + {ok, Req2} = cowboy_req:reply(102, Req), + {ok, Req2, 0}; +info(timeout, Req, Count) -> + erlang:send_after(200, self(), timeout), + {loop, Req, Count - 1, hibernate}. + +terminate({normal, shutdown}, _, 0) -> + ok; +terminate({error, overflow}, _, _) -> + ok. diff --git a/test/handlers/loop_handler_body_h.erl b/test/handlers/loop_handler_body_h.erl new file mode 100644 index 0000000..db69b02 --- /dev/null +++ b/test/handlers/loop_handler_body_h.erl @@ -0,0 +1,24 @@ +%% This module implements a loop handler that reads +%% the request body after sending itself a message, +%% checks that its size is exactly 100000 bytes, +%% then sends a 200 reply back. + +-module(loop_handler_body_h). +-behaviour(cowboy_loop_handler). + +-export([init/3]). +-export([info/3]). +-export([terminate/3]). + +init(_, Req, _) -> + self() ! timeout, + {loop, Req, undefined, 5000, hibernate}. + +info(timeout, Req, State) -> + {ok, Body, Req2} = cowboy_req:body(Req), + 100000 = byte_size(Body), + {ok, Req3} = cowboy_req:reply(200, Req2), + {ok, Req3, State}. + +terminate({normal, shutdown}, _, _) -> + ok. diff --git a/test/handlers/loop_handler_timeout_h.erl b/test/handlers/loop_handler_timeout_h.erl new file mode 100644 index 0000000..1125046 --- /dev/null +++ b/test/handlers/loop_handler_timeout_h.erl @@ -0,0 +1,23 @@ +%% This module implements a loop handler that sends +%% itself a timeout that will intentionally arrive +%% too late, as it configures itself to only wait +%% 200ms before closing the connection in init/3. +%% This results in a 204 reply being sent back by Cowboy. + +-module(loop_handler_timeout_h). +-behaviour(cowboy_loop_handler). + +-export([init/3]). +-export([info/3]). +-export([terminate/3]). + +init(_, Req, _) -> + erlang:send_after(1000, self(), timeout), + {loop, Req, undefined, 200, hibernate}. + +info(timeout, Req, State) -> + {ok, Req2} = cowboy_req:reply(500, Req), + {ok, Req2, State}. + +terminate({normal, timeout}, _, _) -> + ok. diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl index 13e7b0b..4e4c058 100644 --- a/test/http_SUITE.erl +++ b/test/http_SUITE.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% Copyright (c) 2011, Anthony Ramine <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any @@ -14,90 +14,17 @@ %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -module(http_SUITE). - --include_lib("common_test/include/ct.hrl"). - -%% ct. --export([all/0]). --export([groups/0]). --export([init_per_suite/1]). --export([end_per_suite/1]). --export([init_per_group/2]). --export([end_per_group/2]). - -%% Callbacks. --export([etag_gen/3]). --export([mimetypes_text_html/1]). - -%% Tests. --export([check_raw_status/1]). --export([check_status/1]). --export([chunked_response/1]). --export([echo_body/1]). --export([echo_body_max_length/1]). --export([echo_body_qs/1]). --export([echo_body_qs_max_length/1]). --export([error_chain_handle_after_reply/1]). --export([error_chain_handle_before_reply/1]). --export([error_handle_after_reply/1]). --export([error_init_after_reply/1]). --export([error_init_reply_handle_error/1]). --export([headers_dupe/1]). --export([http10_chunkless/1]). --export([http10_hostless/1]). --export([keepalive_max/1]). --export([keepalive_nl/1]). --export([keepalive_stream_loop/1]). --export([multipart/1]). --export([nc_rand/1]). --export([nc_zero/1]). --export([onrequest/1]). --export([onrequest_reply/1]). --export([onresponse_capitalize/1]). --export([onresponse_crash/1]). --export([onresponse_reply/1]). --export([parse_host/1]). --export([pipeline/1]). --export([pipeline_long_polling/1]). --export([rest_bad_accept/1]). --export([rest_bad_content_type/1]). --export([rest_expires/1]). --export([rest_keepalive/1]). --export([rest_keepalive_post/1]). --export([rest_missing_get_callbacks/1]). --export([rest_missing_put_callbacks/1]). --export([rest_nodelete/1]). --export([rest_options_default/1]). --export([rest_param_all/1]). --export([rest_patch/1]). --export([rest_post_charset/1]). --export([rest_postonly/1]). --export([rest_resource_etags/1]). --export([rest_resource_etags_if_none_match/1]). --export([set_env_dispatch/1]). --export([set_resp_body/1]). --export([set_resp_header/1]). --export([set_resp_overwrite/1]). --export([slowloris/1]). --export([slowloris2/1]). --export([static_attribute_etag/1]). --export([static_function_etag/1]). --export([static_mimetypes_function/1]). --export([static_specify_file/1]). --export([static_specify_file_catchall/1]). --export([static_test_file/1]). --export([static_test_file_css/1]). --export([stream_body_set_resp/1]). --export([stream_body_set_resp_close/1]). --export([stream_body_set_resp_chunked/1]). --export([stream_body_set_resp_chunked10/1]). --export([streamed_response/1]). --export([te_chunked/1]). --export([te_chunked_chopped/1]). --export([te_chunked_delayed/1]). --export([te_chunked_split_body/1]). --export([te_chunked_split_crlf/1]). --export([te_identity/1]). +-compile(export_all). + +-import(cowboy_test, [config/2]). +-import(cowboy_test, [gun_open/1]). +-import(cowboy_test, [gun_monitor_open/1]). +-import(cowboy_test, [gun_monitor_open/2]). +-import(cowboy_test, [gun_is_gone/2]). +-import(cowboy_test, [raw_open/1]). +-import(cowboy_test, [raw_send/2]). +-import(cowboy_test, [raw_recv_head/1]). +-import(cowboy_test, [raw_expect_recv/2]). %% ct. @@ -115,68 +42,10 @@ all() -> ]. groups() -> - Tests = [ - check_raw_status, - check_status, - chunked_response, - echo_body, - echo_body_max_length, - echo_body_qs, - echo_body_qs_max_length, - error_chain_handle_after_reply, - error_chain_handle_before_reply, - error_handle_after_reply, - error_init_after_reply, - error_init_reply_handle_error, - headers_dupe, - http10_chunkless, - http10_hostless, - keepalive_max, - keepalive_nl, - keepalive_stream_loop, - multipart, - nc_rand, - nc_zero, - pipeline, - pipeline_long_polling, - rest_bad_accept, - rest_bad_content_type, - rest_expires, - rest_keepalive, - rest_keepalive_post, - rest_missing_get_callbacks, - rest_missing_put_callbacks, - rest_nodelete, - rest_options_default, - rest_param_all, - rest_patch, - rest_post_charset, - rest_postonly, - rest_resource_etags, - rest_resource_etags_if_none_match, - set_resp_body, - set_resp_header, - set_resp_overwrite, - slowloris, - slowloris2, - static_attribute_etag, - static_function_etag, - static_mimetypes_function, - static_specify_file, - static_specify_file_catchall, - static_test_file, - static_test_file_css, - stream_body_set_resp, - stream_body_set_resp_close, - stream_body_set_resp_chunked, - stream_body_set_resp_chunked10, - streamed_response, - te_chunked, - te_chunked_chopped, - te_chunked_delayed, - te_chunked_split_body, - te_chunked_split_crlf, - te_identity + Tests = cowboy_test:all(?MODULE) -- [ + onrequest, onrequest_reply, onrequest_hook, + onresponse_crash, onresponse_reply, onresponse_capitalize, + parse_host, set_env_dispatch ], [ {http, [parallel], Tests}, @@ -203,152 +72,83 @@ groups() -> ]. init_per_suite(Config) -> - application:start(crypto), - application:start(cowlib), - application:start(ranch), - application:start(cowboy), - Dir = ?config(priv_dir, Config) ++ "/static", + Dir = config(priv_dir, Config) ++ "/static", ct_helper:create_static_dir(Dir), [{static_dir, Dir}|Config]. end_per_suite(Config) -> - Dir = ?config(static_dir, Config), - ct_helper:delete_static_dir(Dir), - application:stop(cowboy), - application:stop(ranch), - application:stop(cowlib), - application:stop(crypto), - ok. - -init_per_group(http, Config) -> - Transport = ranch_tcp, - {ok, _} = cowboy:start_http(http, 100, [{port, 0}], [ - {env, [{dispatch, init_dispatch(Config)}]}, - {max_keepalive, 50}, - {timeout, 500} - ]), - Port = ranch:get_port(http), - {ok, Client} = cowboy_client:init([]), - [{scheme, <<"http">>}, {port, Port}, {opts, []}, - {transport, Transport}, {client, Client}|Config]; -init_per_group(https, Config) -> - Transport = ranch_ssl, - {_, Cert, Key} = ct_helper:make_certs(), - Opts = [{cert, Cert}, {key, Key}], - application:start(asn1), - application:start(public_key), - application:start(ssl), - {ok, _} = cowboy:start_https(https, 100, Opts ++ [{port, 0}], [ - {env, [{dispatch, init_dispatch(Config)}]}, - {max_keepalive, 50}, - {timeout, 500} - ]), - Port = ranch:get_port(https), - {ok, Client} = cowboy_client:init(Opts), - [{scheme, <<"https">>}, {port, Port}, {opts, Opts}, - {transport, Transport}, {client, Client}|Config]; -init_per_group(http_compress, Config) -> - Transport = ranch_tcp, - {ok, _} = cowboy:start_http(http_compress, 100, [{port, 0}], [ - {compress, true}, + ct_helper:delete_static_dir(config(static_dir, Config)). + +init_per_group(Name = http, Config) -> + cowboy_test:init_http(Name, [ + {env, [{dispatch, init_dispatch(Config)}]} + ], Config); +init_per_group(Name = https, Config) -> + cowboy_test:init_https(Name, [ + {env, [{dispatch, init_dispatch(Config)}]} + ], Config); +init_per_group(Name = http_compress, Config) -> + cowboy_test:init_http(Name, [ {env, [{dispatch, init_dispatch(Config)}]}, - {max_keepalive, 50}, - {timeout, 500} - ]), - Port = ranch:get_port(http_compress), - {ok, Client} = cowboy_client:init([]), - [{scheme, <<"http">>}, {port, Port}, {opts, []}, - {transport, Transport}, {client, Client}|Config]; -init_per_group(https_compress, Config) -> - Transport = ranch_ssl, - {_, Cert, Key} = ct_helper:make_certs(), - Opts = [{cert, Cert}, {key, Key}], - application:start(asn1), - application:start(public_key), - application:start(ssl), - {ok, _} = cowboy:start_https(https_compress, 100, Opts ++ [{port, 0}], [ - {compress, true}, + {compress, true} + ], Config); +init_per_group(Name = https_compress, Config) -> + cowboy_test:init_https(Name, [ {env, [{dispatch, init_dispatch(Config)}]}, - {max_keepalive, 50}, - {timeout, 500} - ]), - Port = ranch:get_port(https_compress), - {ok, Client} = cowboy_client:init(Opts), - [{scheme, <<"https">>}, {port, Port}, {opts, Opts}, - {transport, Transport}, {client, Client}|Config]; + {compress, true} + ], Config); +%% Most, if not all of these, should be in separate test suites. init_per_group(onrequest, Config) -> - Transport = ranch_tcp, {ok, _} = cowboy:start_http(onrequest, 100, [{port, 0}], [ {env, [{dispatch, init_dispatch(Config)}]}, {max_keepalive, 50}, - {onrequest, fun onrequest_hook/1}, + {onrequest, fun do_onrequest_hook/1}, {timeout, 500} ]), Port = ranch:get_port(onrequest), - {ok, Client} = cowboy_client:init([]), - [{scheme, <<"http">>}, {port, Port}, {opts, []}, - {transport, Transport}, {client, Client}|Config]; + [{type, tcp}, {port, Port}, {opts, []}|Config]; init_per_group(onresponse, Config) -> - Transport = ranch_tcp, {ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [ {env, [{dispatch, init_dispatch(Config)}]}, {max_keepalive, 50}, - {onresponse, fun onresponse_hook/4}, + {onresponse, fun do_onresponse_hook/4}, {timeout, 500} ]), Port = ranch:get_port(onresponse), - {ok, Client} = cowboy_client:init([]), - [{scheme, <<"http">>}, {port, Port}, {opts, []}, - {transport, Transport}, {client, Client}|Config]; + [{type, tcp}, {port, Port}, {opts, []}|Config]; init_per_group(onresponse_capitalize, Config) -> - Transport = ranch_tcp, {ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [ {env, [{dispatch, init_dispatch(Config)}]}, {max_keepalive, 50}, - {onresponse, fun onresponse_capitalize_hook/4}, + {onresponse, fun do_onresponse_capitalize_hook/4}, {timeout, 500} ]), Port = ranch:get_port(onresponse_capitalize), - {ok, Client} = cowboy_client:init([]), - [{scheme, <<"http">>}, {port, Port}, {opts, []}, - {transport, Transport}, {client, Client}|Config]; + [{type, tcp}, {port, Port}, {opts, []}|Config]; init_per_group(parse_host, Config) -> - Transport = ranch_tcp, Dispatch = cowboy_router:compile([ {'_', [ {"/req_attr", http_req_attr, []} ]} ]), - {ok, _} = cowboy:start_http(http, 100, [{port, 0}], [ + {ok, _} = cowboy:start_http(parse_host, 100, [{port, 0}], [ {env, [{dispatch, Dispatch}]}, {max_keepalive, 50}, {timeout, 500} ]), - Port = ranch:get_port(http), - {ok, Client} = cowboy_client:init([]), - [{scheme, <<"http">>}, {port, Port}, {opts, []}, - {transport, Transport}, {client, Client}|Config]; + Port = ranch:get_port(parse_host), + [{type, tcp}, {port, Port}, {opts, []}|Config]; init_per_group(set_env, Config) -> - Transport = ranch_tcp, {ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [ {env, [{dispatch, []}]}, {max_keepalive, 50}, {timeout, 500} ]), Port = ranch:get_port(set_env), - {ok, Client} = cowboy_client:init([]), - [{scheme, <<"http">>}, {port, Port}, {opts, []}, - {transport, Transport}, {client, Client}|Config]. - -end_per_group(Name, _) when Name =:= https; Name =:= https_compress -> - cowboy:stop_listener(Name), - application:stop(ssl), - application:stop(public_key), - application:stop(asn1), - ok; + [{type, tcp}, {port, Port}, {opts, []}|Config]. + end_per_group(Name, _) -> - cowboy:stop_listener(Name), - ok. + ok = cowboy:stop_listener(Name). %% Dispatch configuration. @@ -358,7 +158,6 @@ init_dispatch(Config) -> {"/chunked_response", http_chunked, []}, {"/streamed_response", http_streamed, []}, {"/init_shutdown", http_init_shutdown, []}, - {"/long_polling", http_long_polling, []}, {"/headers/dupe", http_handler, [{headers, [{<<"connection">>, <<"close">>}]}]}, {"/set_resp/header", http_set_resp, @@ -378,19 +177,20 @@ init_dispatch(Config) -> {reply, set_resp_chunked}, {body, [<<"stream_body">>, <<"_set_resp_chunked">>]}]}, {"/static/[...]", cowboy_static, - {dir, ?config(static_dir, Config)}}, + {dir, config(static_dir, Config)}}, {"/static_mimetypes_function/[...]", cowboy_static, - {dir, ?config(static_dir, Config), - [{mimetypes, ?MODULE, mimetypes_text_html}]}}, + {dir, config(static_dir, Config), + [{mimetypes, ?MODULE, do_mimetypes_text_html}]}}, {"/handler_errors", http_errors, []}, {"/static_attribute_etag/[...]", cowboy_static, - {dir, ?config(static_dir, Config)}}, + {dir, config(static_dir, Config)}}, {"/static_function_etag/[...]", cowboy_static, - {dir, ?config(static_dir, Config), - [{etag, ?MODULE, etag_gen}]}}, + {dir, config(static_dir, Config), + [{etag, ?MODULE, do_etag_gen}]}}, {"/static_specify_file/[...]", cowboy_static, - {file, ?config(static_dir, Config) ++ "/style.css"}}, + {file, config(static_dir, Config) ++ "/style.css"}}, {"/multipart", http_multipart, []}, + {"/multipart/large", http_multipart_stream, []}, {"/echo/body", http_echo_body, []}, {"/echo/body_qs", http_body_qs, []}, {"/param_all", rest_param_all, []}, @@ -408,59 +208,36 @@ init_dispatch(Config) -> {"/resetags", rest_resource_etags, []}, {"/rest_expires", rest_expires, []}, {"/rest_empty_resource", rest_empty_resource, []}, - {"/loop_recv", http_loop_recv, []}, {"/loop_stream_recv", http_loop_stream_recv, []}, - {"/loop_timeout", http_loop_timeout, []}, {"/", http_handler, []} ]} ]). -etag_gen(_, _, _) -> +%% Callbacks. + +do_etag_gen(_, _, _) -> {strong, <<"etag">>}. -mimetypes_text_html(_) -> +do_mimetypes_text_html(_) -> <<"text/html">>. %% Convenience functions. -quick_raw(Data, Config) -> - Client = ?config(client, Config), - Transport = ?config(transport, Config), - {ok, Client2} = cowboy_client:connect( - Transport, "localhost", ?config(port, Config), Client), - {ok, Client3} = cowboy_client:raw_request(Data, Client2), - case cowboy_client:response(Client3) of - {ok, Status, _, _} -> Status; - {error, _} -> closed +do_raw(Data, Config) -> + Client = raw_open(Config), + ok = raw_send(Client, Data), + case catch raw_recv_head(Client) of + {'EXIT', _} -> closed; + Resp -> element(2, cow_http:parse_status_line(Resp)) end. -build_url(Path, Config) -> - {scheme, Scheme} = lists:keyfind(scheme, 1, Config), - {port, Port} = lists:keyfind(port, 1, Config), - PortBin = list_to_binary(integer_to_list(Port)), - PathBin = list_to_binary(Path), - << Scheme/binary, "://localhost:", PortBin/binary, PathBin/binary >>. - -quick_get(URL, Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url(URL, Config), Client), - {ok, Status, _, _} = cowboy_client:response(Client2), +do_get(Path, Config) -> + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, Path), + {response, _, Status, _} = gun:await(ConnPid, Ref), + gun:close(ConnPid), Status. -body_to_chunks(_, <<>>, Acc) -> - lists:reverse([<<"0\r\n\r\n">>|Acc]); -body_to_chunks(ChunkSize, Body, Acc) -> - BodySize = byte_size(Body), - ChunkSize2 = case BodySize < ChunkSize of - true -> BodySize; - false -> ChunkSize - end, - << Chunk:ChunkSize2/binary, Rest/binary >> = Body, - ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)), - body_to_chunks(ChunkSize, Rest, - [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]). - %% Tests. check_raw_status(Config) -> @@ -486,14 +263,10 @@ The document has moved <A HREF=\"http://www.google.co.il/\">here</A>. </BODY></HTML>", Tests = [ - {102, <<"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n" - "Content-Length: 5000\r\n\r\n", 0:5000/unit:8 >>}, {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n" "Set-Cookie: ", HugeCookie, "\r\n\r\n"]}, {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"}, {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"}, - {200, <<"POST /loop_recv HTTP/1.1\r\nHost: localhost\r\n" - "Content-Length: 100000\r\n\r\n", 0:100000/unit:8 >>}, {400, "\n"}, {400, "Garbage\r\n\r\n"}, {400, "\r\n\r\n\r\n\r\n\r\n\r\n"}, @@ -507,8 +280,6 @@ The document has moved {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"}, {414, Huge}, {400, "GET / HTTP/1.1\r\n" ++ Huge}, - {500, <<"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n" - "Content-Length: 100000\r\n\r\n", 0:100000/unit:8 >>}, {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"}, {closed, ""}, {closed, "\r\n"}, @@ -516,17 +287,15 @@ The document has moved {closed, "GET / HTTP/1.1"} ], _ = [{Status, Packet} = begin - Ret = quick_raw(Packet, Config), + Ret = do_raw(Packet, Config), {Ret, Packet} end || {Status, Packet} <- Tests], ok. check_status(Config) -> Tests = [ - {102, "/long_polling"}, {200, "/"}, {200, "/simple"}, - {204, "/loop_timeout"}, {400, "/static/%2f"}, {400, "/static/%2e"}, {400, "/static/%2e%2e"}, @@ -540,237 +309,197 @@ check_status(Config) -> {666, "/init_shutdown"} ], _ = [{Status, URL} = begin - Ret = quick_get(URL, Config), + Ret = do_get(URL, Config), {Ret, URL} end || {Status, URL} <- Tests]. chunked_response(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/chunked_response", Config), Client), - {ok, 200, Headers, Client3} = cowboy_client:response(Client2), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/chunked_response"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), true = lists:keymember(<<"transfer-encoding">>, 1, Headers), - {ok, Transport, Socket} = cowboy_client:transport(Client3), - {ok, <<"11\r\nchunked_handler\r\n\r\nB\r\nworks fine!\r\n0\r\n\r\n">>} - = Transport:recv(Socket, 44, 1000), - {error, closed} = cowboy_client:response(Client3). + {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref), + ok. %% Check if sending requests whose size is around the MTU breaks something. echo_body(Config) -> - Client = ?config(client, Config), MTU = ct_helper:get_loopback_mtu(), _ = [begin Body = list_to_binary(lists:duplicate(Size, $a)), - {ok, Client2} = cowboy_client:request(<<"POST">>, - build_url("/echo/body", Config), - [{<<"connection">>, <<"close">>}], - Body, Client), - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {ok, Body, _} = cowboy_client:response_body(Client3) + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", [], Body), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref) end || Size <- lists:seq(MTU - 500, MTU)], ok. %% Check if sending request whose size is bigger than 1000000 bytes causes 413 echo_body_max_length(Config) -> - Client = ?config(client, Config), - Body = <<$a:8000008>>, - {ok, Client2} = cowboy_client:request(<<"POST">>, - build_url("/echo/body", Config), - [{<<"connection">>, <<"close">>}], - Body, Client), - {ok, 413, _, _} = cowboy_client:response(Client2). + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", [], << 0:8000008 >>), + {response, nofin, 413, _} = gun:await(ConnPid, Ref), + ok. % check if body_qs echo's back results echo_body_qs(Config) -> - Client = ?config(client, Config), - Body = <<"echo=67890">>, - {ok, Client2} = cowboy_client:request(<<"POST">>, - build_url("/echo/body_qs", Config), - [{<<"connection">>, <<"close">>}], - Body, Client), - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {ok, <<"67890">>, _} = cowboy_client:response_body(Client3). - -%% Check if sending request whose size is bigger 16000 bytes causes 413 + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body_qs", [], <<"echo=67890">>), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, <<"67890">>} = gun:await_body(ConnPid, Ref), + ok. + echo_body_qs_max_length(Config) -> - Client = ?config(client, Config), - DefaultMaxBodyQsLength = 16000, - % subtract "echo=" minus 1 byte from max to hit the limit - Bits = (DefaultMaxBodyQsLength - 4) * 8, - AppendedBody = <<$a:Bits>>, - Body = <<"echo=", AppendedBody/binary>>, - {ok, Client2} = cowboy_client:request(<<"POST">>, - build_url("/echo/body_qs", Config), - [{<<"connection">>, <<"close">>}], - Body, Client), - {ok, 413, _, _} = cowboy_client:response(Client2). + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body_qs", [], << "echo=", 0:15996/unit:8 >>), + {response, nofin, 413, _} = gun:await(ConnPid, Ref), + ok. error_chain_handle_after_reply(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/", Config), Client), - {ok, Client3} = cowboy_client:request(<<"GET">>, - build_url("/handler_errors?case=handle_after_reply", Config), Client2), - {ok, 200, _, Client4} = cowboy_client:response(Client3), - {ok, 200, _, Client5} = cowboy_client:response(Client4), - {error, closed} = cowboy_client:response(Client5). + {ConnPid, MRef} = gun_monitor_open(Config), + Ref1 = gun:get(ConnPid, "/"), + Ref2 = gun:get(ConnPid, "/handler_errors?case=handle_after_reply"), + {response, nofin, 200, _} = gun:await(ConnPid, Ref1, MRef), + {response, nofin, 200, _} = gun:await(ConnPid, Ref2, MRef), + gun_is_gone(ConnPid, MRef). error_chain_handle_before_reply(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/", Config), Client), - {ok, Client3} = cowboy_client:request(<<"GET">>, - build_url("/handler_errors?case=handle_before_reply", Config), Client2), - {ok, 200, _, Client4} = cowboy_client:response(Client3), - {ok, 500, _, Client5} = cowboy_client:response(Client4), - {error, closed} = cowboy_client:response(Client5). + {ConnPid, MRef} = gun_monitor_open(Config), + Ref1 = gun:get(ConnPid, "/"), + Ref2 = gun:get(ConnPid, "/handler_errors?case=handle_before_reply"), + {response, nofin, 200, _} = gun:await(ConnPid, Ref1, MRef), + {response, fin, 500, _} = gun:await(ConnPid, Ref2, MRef), + gun_is_gone(ConnPid, MRef). error_handle_after_reply(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/handler_errors?case=handle_after_reply", Config), Client), - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {error, closed} = cowboy_client:response(Client3). + {ConnPid, MRef} = gun_monitor_open(Config), + Ref = gun:get(ConnPid, "/handler_errors?case=handle_after_reply"), + {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef), + gun_is_gone(ConnPid, MRef). error_init_after_reply(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/handler_errors?case=init_after_reply", Config), Client), - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {error, closed} = cowboy_client:response(Client3). + {ConnPid, MRef} = gun_monitor_open(Config), + Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"), + {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef), + gun_is_gone(ConnPid, MRef). error_init_reply_handle_error(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/handler_errors?case=init_reply_handle_error", Config), Client), - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {error, closed} = cowboy_client:response(Client3). + {ConnPid, MRef} = gun_monitor_open(Config), + Ref = gun:get(ConnPid, "/handler_errors?case=init_reply_handle_error"), + {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef), + gun_is_gone(ConnPid, MRef). headers_dupe(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/headers/dupe", Config), Client), - {ok, 200, Headers, Client3} = cowboy_client:response(Client2), - {<<"connection">>, <<"close">>} - = lists:keyfind(<<"connection">>, 1, Headers), - Connections = [H || H = {Name, _} <- Headers, Name =:= <<"connection">>], - 1 = length(Connections), - {error, closed} = cowboy_client:response(Client3). + {ConnPid, MRef} = gun_monitor_open(Config), + Ref = gun:get(ConnPid, "/headers/dupe"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef), + %% Ensure that only one connection header was received. + [<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>], + gun_is_gone(ConnPid, MRef). http10_chunkless(Config) -> - Client = ?config(client, Config), - Transport = ?config(transport, Config), - {ok, Client2} = cowboy_client:connect( - Transport, "localhost", ?config(port, Config), Client), - Data = "GET /chunked_response HTTP/1.0\r\nHost: localhost\r\n\r\n", - {ok, Client3} = cowboy_client:raw_request(Data, Client2), - {ok, 200, Headers, Client4} = cowboy_client:response(Client3), + {ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]), + Ref = gun:get(ConnPid, "/chunked_response"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef), false = lists:keyfind(<<"transfer-encoding">>, 1, Headers), - %% Hack: we just try to get 28 bytes and compare. - {ok, Transport, Socket} = cowboy_client:transport(Client4), - Buffer = element(7, Client4), - Buffer2 = case Transport:recv(Socket, 28 - byte_size(Buffer), 1000) of - {ok, Recv} -> << Buffer/binary, Recv/binary >>; - _ -> Buffer - end, - <<"chunked_handler\r\nworks fine!">> = Buffer2. + {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref, MRef), + gun_is_gone(ConnPid, MRef). http10_hostless(Config) -> - Port10 = ?config(port, Config) + 10, - Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)), - ranch:start_listener(Name, 5, - ?config(transport, Config), ?config(opts, Config) ++ [{port, Port10}], + Name = http10_hostless, + Port10 = config(port, Config) + 10, + Transport = case config(type, Config) of + tcp -> ranch_tcp; + ssl -> ranch_ssl + end, + ranch:start_listener(Name, 5, Transport, + config(opts, Config) ++ [{port, Port10}], cowboy_protocol, [ {env, [{dispatch, cowboy_router:compile([ {'_', [{"/http1.0/hostless", http_handler, []}]}])}]}, {max_keepalive, 50}, {timeout, 500}] ), - 200 = quick_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n", + 200 = do_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n", [{port, Port10}|Config]), - cowboy:stop_listener(http10). + cowboy:stop_listener(http10_hostless). keepalive_max(Config) -> - Client = ?config(client, Config), - URL = build_url("/", Config), - ok = keepalive_max_loop(Client, URL, 50). - -keepalive_max_loop(Client, _, 0) -> - {error, closed} = cowboy_client:response(Client), - ok; -keepalive_max_loop(Client, URL, N) -> - Headers = [{<<"connection">>, <<"keep-alive">>}], - {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client), - {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2), - Expected = case N of - 1 -> <<"close">>; - N -> <<"keep-alive">> - end, - {<<"connection">>, Expected} - = lists:keyfind(<<"connection">>, 1, RespHeaders), - keepalive_max_loop(Client3, URL, N - 1). + {ConnPid, MRef} = gun_monitor_open(Config), + Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]) + || _ <- lists:seq(1, 49)], + CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]), + _ = [begin + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef), + {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers) + end || Ref <- Refs], + {response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef, MRef), + {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers), + gun_is_gone(ConnPid, MRef). keepalive_nl(Config) -> - Client = ?config(client, Config), - URL = build_url("/", Config), - ok = keepalive_nl_loop(Client, URL, 10). - -keepalive_nl_loop(Client, _, 0) -> - {error, closed} = cowboy_client:response(Client), - ok; -keepalive_nl_loop(Client, URL, N) -> - Headers = [{<<"connection">>, <<"keep-alive">>}], - {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client), - {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2), - {<<"connection">>, <<"keep-alive">>} - = lists:keyfind(<<"connection">>, 1, RespHeaders), - {ok, Transport, Socket} = cowboy_client:transport(Client2), - ok = Transport:send(Socket, <<"\r\n">>), %% empty line - keepalive_nl_loop(Client3, URL, N - 1). + ConnPid = gun_open(Config), + Refs = [begin + Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]), + gun:dbg_send_raw(ConnPid, <<"\r\n">>), + Ref + end || _ <- lists:seq(1, 10)], + _ = [begin + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers) + end || Ref <- Refs], + ok. keepalive_stream_loop(Config) -> - Client = ?config(client, Config), - Transport = ?config(transport, Config), - {ok, Client2} = cowboy_client:connect( - Transport, "localhost", ?config(port, Config), Client), - keepalive_stream_loop(Client2, 10). - -keepalive_stream_loop(Client, 0) -> - {error, closed} = cowboy_client:response(Client), - ok; -keepalive_stream_loop(Client, N) -> - {ok, _} = cowboy_client:raw_request("POST /loop_stream_recv HTTP/1.1\r\n" - "Host: localhost\r\n" - "Connection: keepalive\r\n" - "Transfer-Encoding: chunked\r\n\r\n", Client), - _ = [{ok, _} = cowboy_client:raw_request(<<"4\r\n",Id:32,"\r\n">>, Client) || - Id <- lists:seq(1, 250)], - {ok, _} = cowboy_client:raw_request(<<"0\r\n\r\n">>, Client), - {ok, 200, _, _} = cowboy_client:response(Client), - keepalive_stream_loop(Client, N-1). + ConnPid = gun_open(Config), + Refs = [begin + Ref = gun:post(ConnPid, "/loop_stream_recv", + [{<<"transfer-encoding">>, <<"chunked">>}]), + _ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>) + || ID <- lists:seq(1, 250)], + gun:data(ConnPid, Ref, fin, <<>>), + Ref + end || _ <- lists:seq(1, 10)], + _ = [begin + {response, fin, 200, _} = gun:await(ConnPid, Ref) + end || Ref <- Refs], + ok. multipart(Config) -> - Client = ?config(client, Config), + ConnPid = gun_open(Config), Body = << "This is a preamble." "\r\n--OHai\r\nX-Name:answer\r\n\r\n42" "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n" - "\r\n--OHai--" - "This is an epiloque." + "\r\n--OHai--\r\n" + "This is an epilogue." >>, - {ok, Client2} = cowboy_client:request(<<"POST">>, - build_url("/multipart", Config), + Ref = gun:post(ConnPid, "/multipart", [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}], - Body, Client), - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {ok, RespBody, _} = cowboy_client:response_body(Client3), + Body), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, RespBody} = gun:await_body(ConnPid, Ref), Parts = binary_to_term(RespBody), Parts = [ {[{<<"x-name">>, <<"answer">>}], <<"42">>}, {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>} - ]. + ], + ok. + +multipart_large(Config) -> + ConnPid = gun_open(Config), + Boundary = "----------", + Big = << 0:9000000/unit:8 >>, + Bigger = << 0:9999999/unit:8 >>, + Body = ["--", Boundary, "\r\ncontent-length: 9000000\r\n\r\n", Big, "\r\n", + "--", Boundary, "\r\ncontent-length: 9999999\r\n\r\n", Bigger, "\r\n", + "--", Boundary, "--\r\n"], + Ref = gun:post(ConnPid, "/multipart/large", + [{<<"content-type">>, ["multipart/x-large; boundary=", Boundary]}], + Body), + {response, fin, 200, _} = gun:await(ConnPid, Ref), + ok. -nc_reqs(Config, Input) -> +do_nc(Config, Input) -> Cat = os:find_executable("cat"), Nc = os:find_executable("nc"), case {Cat, Nc} of @@ -780,37 +509,36 @@ nc_reqs(Config, Input) -> {skip, {notfound, nc}}; _Good -> %% Throw garbage at the server then check if it's still up. - {port, Port} = lists:keyfind(port, 1, Config), - StrPort = integer_to_list(Port), + StrPort = integer_to_list(config(port, Config)), [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort) || _ <- lists:seq(1, 100)], - 200 = quick_get("/", Config) + 200 = do_get("/", Config) end. nc_rand(Config) -> - nc_reqs(Config, "/dev/urandom"). + do_nc(Config, "/dev/urandom"). nc_zero(Config) -> - nc_reqs(Config, "/dev/zero"). + do_nc(Config, "/dev/zero"). onrequest(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/", Config), Client), - {ok, 200, Headers, Client3} = cowboy_client:response(Client2), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), {<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers), - {ok, <<"http_handler">>, _} = cowboy_client:response_body(Client3). + {ok, <<"http_handler">>} = gun:await_body(ConnPid, Ref), + ok. onrequest_reply(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/?reply=1", Config), Client), - {ok, 200, Headers, Client3} = cowboy_client:response(Client2), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/?reply=1"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), {<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers), - {ok, <<"replied!">>, _} = cowboy_client:response_body(Client3). + {ok, <<"replied!">>} = gun:await_body(ConnPid, Ref), + ok. %% Hook for the above onrequest tests. -onrequest_hook(Req) -> +do_onrequest_hook(Req) -> case cowboy_req:qs_val(<<"reply">>, Req) of {undefined, Req2} -> cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req2); @@ -821,229 +549,172 @@ onrequest_hook(Req) -> end. onresponse_capitalize(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/", Config), Client), - {ok, Transport, Socket} = cowboy_client:transport(Client2), - {ok, Data} = Transport:recv(Socket, 0, 1000), - false = nomatch =:= binary:match(Data, <<"Content-Length">>). + Client = raw_open(Config), + ok = raw_send(Client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"), + Data = raw_recv_head(Client), + false = nomatch =:= binary:match(Data, <<"Content-Length">>), + ok. %% Hook for the above onresponse_capitalize test. -onresponse_capitalize_hook(Status, Headers, Body, Req) -> +do_onresponse_capitalize_hook(Status, Headers, Body, Req) -> Headers2 = [{cowboy_bstr:capitalize_token(N), V} || {N, V} <- Headers], {ok, Req2} = cowboy_req:reply(Status, Headers2, Body, Req), Req2. onresponse_crash(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/handler_errors?case=init_before_reply", Config), Client), - {ok, 777, Headers, _} = cowboy_client:response(Client2), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/handler_errors?case=init_before_reply"), + {response, fin, 777, Headers} = gun:await(ConnPid, Ref), {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers). onresponse_reply(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/", Config), Client), - {ok, 777, Headers, Client3} = cowboy_client:response(Client2), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/"), + {response, nofin, 777, Headers} = gun:await(ConnPid, Ref), {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers), - %% Make sure we don't get the body initially sent. - {error, closed} = cowboy_client:response_body(Client3). + ok. %% Hook for the above onresponse tests. -onresponse_hook(_, Headers, _, Req) -> +do_onresponse_hook(_, Headers, _, Req) -> {ok, Req2} = cowboy_req:reply( <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req), Req2. parse_host(Config) -> + ConnPid = gun_open(Config), Tests = [ - {<<"example.org\n8080">>, <<"example.org:8080">>}, - {<<"example.org\n80">>, <<"example.org">>}, - {<<"192.0.2.1\n8080">>, <<"192.0.2.1:8080">>}, - {<<"192.0.2.1\n80">>, <<"192.0.2.1">>}, - {<<"[2001:db8::1]\n8080">>, <<"[2001:db8::1]:8080">>}, - {<<"[2001:db8::1]\n80">>, <<"[2001:db8::1]">>}, - {<<"[::ffff:192.0.2.1]\n8080">>, <<"[::ffff:192.0.2.1]:8080">>}, - {<<"[::ffff:192.0.2.1]\n80">>, <<"[::ffff:192.0.2.1]">>} + {<<"example.org:8080">>, <<"example.org\n8080">>}, + {<<"example.org">>, <<"example.org\n80">>}, + {<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>}, + {<<"192.0.2.1">>, <<"192.0.2.1\n80">>}, + {<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>}, + {<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>}, + {<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>}, + {<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>} ], [begin - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/req_attr?attr=host_and_port", Config), - [{<<"host">>, Host}], - Client), - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {ok, Value, Client4} = cowboy_client:response_body(Client3), - {error, closed} = cowboy_client:response(Client4), - Value - end || {Value, Host} <- Tests]. + Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port", + [{<<"host">>, Host}]), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref) + end || {Host, Body} <- Tests], + ok. pipeline(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/", Config), Client), - {ok, Client3} = cowboy_client:request(<<"GET">>, - build_url("/", Config), Client2), - {ok, Client4} = cowboy_client:request(<<"GET">>, - build_url("/", Config), Client3), - {ok, Client5} = cowboy_client:request(<<"GET">>, - build_url("/", Config), Client4), - {ok, Client6} = cowboy_client:request(<<"GET">>, - build_url("/", Config), [{<<"connection">>, <<"close">>}], Client5), - {ok, 200, _, Client7} = cowboy_client:response(Client6), - {ok, 200, _, Client8} = cowboy_client:response(Client7), - {ok, 200, _, Client9} = cowboy_client:response(Client8), - {ok, 200, _, Client10} = cowboy_client:response(Client9), - {ok, 200, _, Client11} = cowboy_client:response(Client10), - {error, closed} = cowboy_client:response(Client11). - -pipeline_long_polling(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/long_polling", Config), Client), - {ok, Client3} = cowboy_client:request(<<"GET">>, - build_url("/long_polling", Config), Client2), - {ok, 102, _, Client4} = cowboy_client:response(Client3), - {ok, 102, _, Client5} = cowboy_client:response(Client4), - {error, closed} = cowboy_client:response(Client5). + ConnPid = gun_open(Config), + Refs = [gun:get(ConnPid, "/") || _ <- lists:seq(1, 5)], + _ = [{response, nofin, 200, _} = gun:await(ConnPid, Ref) || Ref <- Refs], + ok. rest_param_all(Config) -> - Client = ?config(client, Config), - URL = build_url("/param_all", Config), - % Accept without param - {ok, Client2} = cowboy_client:request(<<"GET">>, URL, - [{<<"accept">>, <<"text/plain">>}], Client), - Client3 = check_response(Client2, <<"[]">>), - % Accept with param - {ok, Client4} = cowboy_client:request(<<"GET">>, URL, - [{<<"accept">>, <<"text/plain;level=1">>}], Client3), - Client5 = check_response(Client4, <<"level=1">>), - % Accept with param and quality - {ok, Client6} = cowboy_client:request(<<"GET">>, URL, - [{<<"accept">>, - <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}], - Client5), - Client7 = check_response(Client6, <<"level=1">>), - {ok, Client8} = cowboy_client:request(<<"GET">>, URL, - [{<<"accept">>, - <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}], - Client7), - Client9 = check_response(Client8, <<"level=2">>), - % Without Accept - {ok, Client10} = cowboy_client:request(<<"GET">>, URL, [], Client9), - Client11 = check_response(Client10, <<"'*'">>), - % Content-Type without param - {ok, Client12} = cowboy_client:request(<<"PUT">>, URL, - [{<<"content-type">>, <<"text/plain">>}], Client11), - {ok, 204, _, Client13} = cowboy_client:response(Client12), - % Content-Type with param - {ok, Client14} = cowboy_client:request(<<"PUT">>, URL, - [{<<"content-type">>, <<"text/plain; charset=utf-8">>}], Client13), - {ok, 204, _, _} = cowboy_client:response(Client14). - -check_response(Client, Body) -> - {ok, 200, _, Client2} = cowboy_client:response(Client), - {ok, Body, Client3} = cowboy_client:response_body(Client2), - Client3. + ConnPid = gun_open(Config), + %% Accept without param. + Ref1 = gun:get(ConnPid, "/param_all", + [{<<"accept">>, <<"text/plain">>}]), + {response, nofin, 200, _} = gun:await(ConnPid, Ref1), + {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1), + %% Accept with param. + Ref2 = gun:get(ConnPid, "/param_all", + [{<<"accept">>, <<"text/plain;level=1">>}]), + {response, nofin, 200, _} = gun:await(ConnPid, Ref2), + {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2), + %% Accept with param and quality. + Ref3 = gun:get(ConnPid, "/param_all", + [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]), + {response, nofin, 200, _} = gun:await(ConnPid, Ref3), + {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3), + Ref4 = gun:get(ConnPid, "/param_all", + [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]), + {response, nofin, 200, _} = gun:await(ConnPid, Ref4), + {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4), + %% Without Accept. + Ref5 = gun:get(ConnPid, "/param_all"), + {response, nofin, 200, _} = gun:await(ConnPid, Ref5), + {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5), + %% Content-Type without param. + Ref6 = gun:put(ConnPid, "/param_all", + [{<<"content-type">>, <<"text/plain">>}]), + {response, fin, 204, _} = gun:await(ConnPid, Ref6), + %% Content-Type with param. + Ref7 = gun:put(ConnPid, "/param_all", + [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]), + {response, fin, 204, _} = gun:await(ConnPid, Ref7), + ok. rest_bad_accept(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/bad_accept", Config), - [{<<"accept">>, <<"1">>}], - Client), - {ok, 400, _, _} = cowboy_client:response(Client2). + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/bad_accept", + [{<<"accept">>, <<"1">>}]), + {response, fin, 400, _} = gun:await(ConnPid, Ref), + ok. rest_bad_content_type(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"PATCH">>, - build_url("/bad_content_type", Config), - [{<<"content-type">>, <<"text/plain, text/html">>}], - <<"Whatever">>, Client), - {ok, 415, _, _} = cowboy_client:response(Client2). + ConnPid = gun_open(Config), + Ref = gun:patch(ConnPid, "/bad_content_type", + [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>), + {response, fin, 415, _} = gun:await(ConnPid, Ref), + ok. rest_expires(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/rest_expires", Config), Client), - {ok, 200, RespHeaders, _} = cowboy_client:response(Client2), - {_, Expires} = lists:keyfind(<<"expires">>, 1, RespHeaders), - {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, RespHeaders), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/rest_expires"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers), + {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers), Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>, ok. rest_keepalive(Config) -> - Client = ?config(client, Config), - URL = build_url("/simple", Config), - ok = rest_keepalive_loop(Client, URL, 10). - -rest_keepalive_loop(_, _, 0) -> - ok; -rest_keepalive_loop(Client, URL, N) -> - Headers = [{<<"connection">>, <<"keep-alive">>}], - {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client), - {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2), - {<<"connection">>, <<"keep-alive">>} - = lists:keyfind(<<"connection">>, 1, RespHeaders), - rest_keepalive_loop(Client3, URL, N - 1). + ConnPid = gun_open(Config), + Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)], + _ = [begin + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers) + end || Ref <- Refs], + ok. rest_keepalive_post(Config) -> - Client = ?config(client, Config), - ok = rest_keepalive_post_loop(Config, Client, forbidden_post, 10). - -rest_keepalive_post_loop(_, _, _, 0) -> - ok; -rest_keepalive_post_loop(Config, Client, simple_post, N) -> - Headers = [ - {<<"connection">>, <<"keep-alive">>}, - {<<"content-type">>, <<"text/plain">>} - ], - {ok, Client2} = cowboy_client:request(<<"POST">>, - build_url("/simple_post", Config), Headers, "12345", Client), - {ok, 303, RespHeaders, Client3} = cowboy_client:response(Client2), - {<<"connection">>, <<"keep-alive">>} - = lists:keyfind(<<"connection">>, 1, RespHeaders), - rest_keepalive_post_loop(Config, Client3, forbidden_post, N - 1); -rest_keepalive_post_loop(Config, Client, forbidden_post, N) -> - Headers = [ - {<<"connection">>, <<"keep-alive">>}, - {<<"content-type">>, <<"text/plain">>} - ], - {ok, Client2} = cowboy_client:request(<<"POST">>, - build_url("/forbidden_post", Config), Headers, "12345", Client), - {ok, 403, RespHeaders, Client3} = cowboy_client:response(Client2), - {<<"connection">>, <<"keep-alive">>} - = lists:keyfind(<<"connection">>, 1, RespHeaders), - rest_keepalive_post_loop(Config, Client3, simple_post, N - 1). + ConnPid = gun_open(Config), + Refs = [{ + gun:post(ConnPid, "/forbidden_post", + [{<<"content-type">>, <<"text/plain">>}]), + gun:post(ConnPid, "/simple_post", + [{<<"content-type">>, <<"text/plain">>}]) + } || _ <- lists:seq(1, 5)], + _ = [begin + {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1), + {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers1), + {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2), + {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers2) + end || {Ref1, Ref2} <- Refs], + ok. rest_missing_get_callbacks(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/missing_get_callbacks", Config), Client), - {ok, 500, _, _} = cowboy_client:response(Client2). + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/missing_get_callbacks"), + {response, fin, 500, _} = gun:await(ConnPid, Ref), + ok. rest_missing_put_callbacks(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"PUT">>, - build_url("/missing_put_callbacks", Config), - [{<<"content-type">>, <<"application/json">>}], - <<"{}">>, Client), - {ok, 500, _, _} = cowboy_client:response(Client2). + ConnPid = gun_open(Config), + Ref = gun:put(ConnPid, "/missing_put_callbacks", + [{<<"content-type">>, <<"application/json">>}], <<"{}">>), + {response, fin, 500, _} = gun:await(ConnPid, Ref), + ok. rest_nodelete(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"DELETE">>, - build_url("/nodelete", Config), Client), - {ok, 500, _, _} = cowboy_client:response(Client2). + ConnPid = gun_open(Config), + Ref = gun:delete(ConnPid, "/nodelete"), + {response, fin, 500, _} = gun:await(ConnPid, Ref), + ok. rest_options_default(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"OPTIONS">>, - build_url("/rest_empty_resource", Config), Client), - {ok, 200, Headers, _} = cowboy_client:response(Client2), - {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers). + ConnPid = gun_open(Config), + Ref = gun:options(ConnPid, "/rest_empty_resource"), + {response, fin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers), + ok. rest_patch(Config) -> Tests = [ @@ -1052,40 +723,34 @@ rest_patch(Config) -> {400, [{<<"content-type">>, <<"text/plain">>}], <<"halt">>}, {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>} ], - Client = ?config(client, Config), + ConnPid = gun_open(Config), _ = [begin - {ok, Client2} = cowboy_client:request(<<"PATCH">>, - build_url("/patch", Config), Headers, Body, Client), - {ok, Status, _, _} = cowboy_client:response(Client2), - ok - end || {Status, Headers, Body} <- Tests]. + Ref = gun:patch(ConnPid, "/patch", Headers, Body), + {response, fin, Status, _} = gun:await(ConnPid, Ref) + end || {Status, Headers, Body} <- Tests], + ok. rest_post_charset(Config) -> - Client = ?config(client, Config), - Headers = [ - {<<"content-type">>, <<"text/plain;charset=UTF-8">>} - ], - {ok, Client2} = cowboy_client:request(<<"POST">>, - build_url("/post_charset", Config), Headers, "12345", Client), - {ok, 204, _, _} = cowboy_client:response(Client2). + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/post_charset", + [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"), + {response, fin, 204, _} = gun:await(ConnPid, Ref), + ok. rest_postonly(Config) -> - Client = ?config(client, Config), - Headers = [ - {<<"content-type">>, <<"text/plain">>} - ], - {ok, Client2} = cowboy_client:request(<<"POST">>, - build_url("/postonly", Config), Headers, "12345", Client), - {ok, 204, _, _} = cowboy_client:response(Client2). + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/postonly", + [{<<"content-type">>, <<"text/plain">>}], "12345"), + {response, fin, 204, _} = gun:await(ConnPid, Ref), + ok. rest_resource_get_etag(Config, Type) -> rest_resource_get_etag(Config, Type, []). rest_resource_get_etag(Config, Type, Headers) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/resetags?type=" ++ Type, Config), Headers, Client), - {ok, Status, RespHeaders, _} = cowboy_client:response(Client2), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers), + {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref), case lists:keyfind(<<"etag">>, 1, RespHeaders) of false -> {Status, false}; {<<"etag">>, ETag} -> {Status, ETag} @@ -1119,48 +784,44 @@ rest_resource_etags_if_none_match(Config) -> end || {Status, ETag, Type} <- Tests]. set_env_dispatch(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/", Config), Client), - {ok, 400, _, _} = cowboy_client:response(Client2), + ConnPid1 = gun_open(Config), + Ref1 = gun:get(ConnPid1, "/"), + {response, fin, 400, _} = gun:await(ConnPid1, Ref1), ok = cowboy:set_env(set_env, dispatch, cowboy_router:compile([{'_', [{"/", http_handler, []}]}])), - {ok, Client3} = cowboy_client:request(<<"GET">>, - build_url("/", Config), Client), - {ok, 200, _, _} = cowboy_client:response(Client3). + ConnPid2 = gun_open(Config), + Ref2 = gun:get(ConnPid2, "/"), + {response, nofin, 200, _} = gun:await(ConnPid2, Ref2), + ok. set_resp_body(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/set_resp/body", Config), Client), - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {ok, <<"A flameless dance does not equal a cycle">>, _} - = cowboy_client:response_body(Client3). + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/set_resp/body"), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, <<"A flameless dance does not equal a cycle">>} + = gun:await_body(ConnPid, Ref), + ok. set_resp_header(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/set_resp/header", Config), Client), - {ok, 200, Headers, _} = cowboy_client:response(Client2), - {<<"vary">>, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers), - {<<"set-cookie">>, _} = lists:keyfind(<<"set-cookie">>, 1, Headers). + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/set_resp/header"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers), + {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers), + ok. set_resp_overwrite(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/set_resp/overwrite", Config), Client), - {ok, 200, Headers, _} = cowboy_client:response(Client2), - {<<"server">>, <<"DesireDrive/1.0">>} - = lists:keyfind(<<"server">>, 1, Headers). + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/set_resp/overwrite"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers), + ok. slowloris(Config) -> - Client = ?config(client, Config), - Transport = ?config(transport, Config), - {ok, Client2} = cowboy_client:connect( - Transport, "localhost", ?config(port, Config), Client), + Client = raw_open(Config), try [begin - {ok, _} = cowboy_client:raw_request([C], Client2), + ok = raw_send(Client, [C]), receive after 25 -> ok end end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n" "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n" @@ -1171,249 +832,214 @@ slowloris(Config) -> end. slowloris2(Config) -> - Client = ?config(client, Config), - Transport = ?config(transport, Config), - {ok, Client2} = cowboy_client:connect( - Transport, "localhost", ?config(port, Config), Client), - {ok, _} = cowboy_client:raw_request("GET / HTTP/1.1\r\n", Client2), + Client = raw_open(Config), + ok = raw_send(Client, "GET / HTTP/1.1\r\n"), receive after 300 -> ok end, - {ok, _} = cowboy_client:raw_request("Host: localhost\r\n", Client2), + ok = raw_send(Client, "Host: localhost\r\n"), receive after 300 -> ok end, - {ok, 408, _, _} = cowboy_client:response(Client2). + Data = raw_recv_head(Client), + {_, 408, _, _} = cow_http:parse_status_line(Data), + ok. static_attribute_etag(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/static_attribute_etag/index.html", Config), Client), - {ok, Client3} = cowboy_client:request(<<"GET">>, - build_url("/static_attribute_etag/index.html", Config), Client2), - {ok, 200, Headers1, Client4} = cowboy_client:response(Client3), - {ok, 200, Headers2, _} = cowboy_client:response(Client4), - {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1), - {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2), - false = ETag1 =:= undefined, - ETag1 = ETag2. + ConnPid = gun_open(Config), + Ref1 = gun:get(ConnPid, "/static_attribute_etag/index.html"), + Ref2 = gun:get(ConnPid, "/static_attribute_etag/index.html"), + {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1), + {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2), + {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1), + {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2), + true = ETag =/= undefined, + ok. static_function_etag(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/static_function_etag/index.html", Config), Client), - {ok, Client3} = cowboy_client:request(<<"GET">>, - build_url("/static_function_etag/index.html", Config), Client2), - {ok, 200, Headers1, Client4} = cowboy_client:response(Client3), - {ok, 200, Headers2, _} = cowboy_client:response(Client4), - {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1), - {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2), - false = ETag1 =:= undefined, - ETag1 = ETag2. + ConnPid = gun_open(Config), + Ref1 = gun:get(ConnPid, "/static_function_etag/index.html"), + Ref2 = gun:get(ConnPid, "/static_function_etag/index.html"), + {response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1), + {response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2), + {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1), + {_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2), + true = ETag =/= undefined, + ok. static_mimetypes_function(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/static_mimetypes_function/index.html", Config), Client), - {ok, 200, Headers, _} = cowboy_client:response(Client2), - {<<"content-type">>, <<"text/html">>} - = lists:keyfind(<<"content-type">>, 1, Headers). + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/static_mimetypes_function/index.html"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers), + ok. static_specify_file(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/static_specify_file", Config), Client), - {ok, 200, Headers, Client3} = cowboy_client:response(Client2), - {<<"content-type">>, <<"text/css">>} - = lists:keyfind(<<"content-type">>, 1, Headers), - {ok, <<"body{color:red}\n">>, _} = cowboy_client:response_body(Client3). + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/static_specify_file"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers), + {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref), + ok. static_specify_file_catchall(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/static_specify_file/none", Config), Client), - {ok, 200, Headers, Client3} = cowboy_client:response(Client2), - {<<"content-type">>, <<"text/css">>} - = lists:keyfind(<<"content-type">>, 1, Headers), - {ok, <<"body{color:red}\n">>, _} = cowboy_client:response_body(Client3). + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/static_specify_file/none"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers), + {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref), + ok. static_test_file(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/static/unknown", Config), Client), - {ok, 200, Headers, _} = cowboy_client:response(Client2), - {<<"content-type">>, <<"application/octet-stream">>} - = lists:keyfind(<<"content-type">>, 1, Headers). + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/static/unknown"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers), + ok. static_test_file_css(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/static/style.css", Config), Client), - {ok, 200, Headers, _} = cowboy_client:response(Client2), - {<<"content-type">>, <<"text/css">>} - = lists:keyfind(<<"content-type">>, 1, Headers). + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/static/style.css"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers), + ok. stream_body_set_resp(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/stream_body/set_resp", Config), Client), - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {ok, <<"stream_body_set_resp">>, _} - = cowboy_client:response_body(Client3). + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/stream_body/set_resp"), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, <<"stream_body_set_resp">>} = gun:await_body(ConnPid, Ref), + ok. stream_body_set_resp_close(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/stream_body/set_resp_close", Config), Client), - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {ok, Transport, Socket} = cowboy_client:transport(Client3), - case element(7, Client3) of - <<"stream_body_set_resp_close">> -> - ok; - Buffer -> - {ok, Rest} = Transport:recv(Socket, 26 - byte_size(Buffer), 1000), - <<"stream_body_set_resp_close">> = << Buffer/binary, Rest/binary >>, - ok - end, - {error, closed} = Transport:recv(Socket, 0, 1000). + {ConnPid, MRef} = gun_monitor_open(Config), + Ref = gun:get(ConnPid, "/stream_body/set_resp_close"), + {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef), + {ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref, MRef), + gun_is_gone(ConnPid, MRef). stream_body_set_resp_chunked(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/stream_body/set_resp_chunked", Config), Client), - {ok, 200, Headers, Client3} = cowboy_client:response(Client2), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), {_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers), - {ok, Transport, Socket} = cowboy_client:transport(Client3), - case element(7, Client3) of - <<"B\r\nstream_body\r\n11\r\n_set_resp_chunked\r\n0\r\n\r\n">> -> - ok; - Buffer -> - {ok, Rest} = Transport:recv(Socket, 44 - byte_size(Buffer), 1000), - <<"B\r\nstream_body\r\n11\r\n_set_resp_chunked\r\n0\r\n\r\n">> - = <<Buffer/binary, Rest/binary>>, - ok - end. + {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref), + ok. stream_body_set_resp_chunked10(Config) -> - Client = ?config(client, Config), - Transport = ?config(transport, Config), - {ok, Client2} = cowboy_client:connect( - Transport, "localhost", ?config(port, Config), Client), - Data = ["GET /stream_body/set_resp_chunked HTTP/1.0\r\n", - "Host: localhost\r\n\r\n"], - {ok, Client3} = cowboy_client:raw_request(Data, Client2), - {ok, 200, Headers, Client4} = cowboy_client:response(Client3), - false = lists:keymember(<<"transfer-encoding">>, 1, Headers), - {ok, Transport, Socket} = cowboy_client:transport(Client4), - case element(7, Client4) of - <<"stream_body_set_resp_chunked">> -> - ok; - Buffer -> - {ok, Rest} = Transport:recv(Socket, 28 - byte_size(Buffer), 1000), - <<"stream_body_set_resp_chunked">> - = <<Buffer/binary, Rest/binary>>, - ok - end, - {error, closed} = Transport:recv(Socket, 0, 1000). + {ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]), + Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef), + false = lists:keyfind(<<"transfer-encoding">>, 1, Headers), + {ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref, MRef), + gun_is_gone(ConnPid, MRef). +%% Undocumented hack: force chunked response to be streamed as HTTP/1.1. streamed_response(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/streamed_response", Config), Client), - {ok, 200, Headers, Client3} = cowboy_client:response(Client2), + Client = raw_open(Config), + ok = raw_send(Client, "GET /streamed_response HTTP/1.1\r\nHost: localhost\r\n\r\n"), + Data = raw_recv_head(Client), + {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data), + {Headers, Rest2} = cow_http:parse_headers(Rest), false = lists:keymember(<<"transfer-encoding">>, 1, Headers), - {ok, Transport, Socket} = cowboy_client:transport(Client3), - {ok, <<"streamed_handler\r\nworks fine!">>} - = Transport:recv(Socket, 29, 1000), - {error, closed} = cowboy_client:response(Client3). + Rest2Size = byte_size(Rest2), + ok = case <<"streamed_handler\r\nworks fine!">> of + Rest2 -> ok; + << Rest2:Rest2Size/binary, Expect/bits >> -> raw_expect_recv(Client, Expect) + end. te_chunked(Config) -> - Client = ?config(client, Config), Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - Chunks = body_to_chunks(50, Body, []), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/echo/body", Config), - [{<<"transfer-encoding">>, <<"chunked">>}], - Chunks, Client), - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {ok, Body, _} = cowboy_client:response_body(Client3). + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", + [{<<"transfer-encoding">>, <<"chunked">>}], Body), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref), + ok. + +do_body_to_chunks(_, <<>>, Acc) -> + lists:reverse([<<"0\r\n\r\n">>|Acc]); +do_body_to_chunks(ChunkSize, Body, Acc) -> + BodySize = byte_size(Body), + ChunkSize2 = case BodySize < ChunkSize of + true -> BodySize; + false -> ChunkSize + end, + << Chunk:ChunkSize2/binary, Rest/binary >> = Body, + ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)), + do_body_to_chunks(ChunkSize, Rest, + [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]). te_chunked_chopped(Config) -> - Client = ?config(client, Config), Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - Body2 = iolist_to_binary(body_to_chunks(50, Body, [])), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/echo/body", Config), - [{<<"transfer-encoding">>, <<"chunked">>}], Client), - {ok, Transport, Socket} = cowboy_client:transport(Client2), + Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", + [{<<"transfer-encoding">>, <<"chunked">>}]), _ = [begin - ok = Transport:send(Socket, << C >>), - ok = timer:sleep(10) + ok = gun:dbg_send_raw(ConnPid, << C >>), + receive after 10 -> ok end end || << C >> <= Body2], - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {ok, Body, _} = cowboy_client:response_body(Client3). + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref), + ok. te_chunked_delayed(Config) -> - Client = ?config(client, Config), Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - Chunks = body_to_chunks(50, Body, []), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/echo/body", Config), - [{<<"transfer-encoding">>, <<"chunked">>}], Client), - {ok, Transport, Socket} = cowboy_client:transport(Client2), + Chunks = do_body_to_chunks(50, Body, []), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", + [{<<"transfer-encoding">>, <<"chunked">>}]), _ = [begin - ok = Transport:send(Socket, Chunk), - ok = timer:sleep(10) + ok = gun:dbg_send_raw(ConnPid, Chunk), + receive after 10 -> ok end end || Chunk <- Chunks], - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {ok, Body, _} = cowboy_client:response_body(Client3). + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref), + ok. te_chunked_split_body(Config) -> - Client = ?config(client, Config), Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - Chunks = body_to_chunks(50, Body, []), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/echo/body", Config), - [{<<"transfer-encoding">>, <<"chunked">>}], Client), - {ok, Transport, Socket} = cowboy_client:transport(Client2), + Chunks = do_body_to_chunks(50, Body, []), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", + [{<<"transfer-encoding">>, <<"chunked">>}]), _ = [begin case Chunk of - %% Final chunk. <<"0\r\n\r\n">> -> - ok = Transport:send(Socket, Chunk); + ok = gun:dbg_send_raw(ConnPid, Chunk); _ -> - %% Chunk of form <<"9\r\nChunkBody\r\n">>. [Size, ChunkBody, <<>>] = binary:split(Chunk, [<<"\r\n">>], [global]), PartASize = random:uniform(byte_size(ChunkBody)), <<PartA:PartASize/binary, PartB/binary>> = ChunkBody, - ok = Transport:send(Socket, [Size, <<"\r\n">>, PartA]), - ok = timer:sleep(10), - ok = Transport:send(Socket, [PartB, <<"\r\n">>]) + ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]), + receive after 10 -> ok end, + ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>]) end end || Chunk <- Chunks], - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {ok, Body, _} = cowboy_client:response_body(Client3). + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref), + ok. te_chunked_split_crlf(Config) -> - Client = ?config(client, Config), Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - Chunks = body_to_chunks(50, Body, []), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/echo/body", Config), - [{<<"transfer-encoding">>, <<"chunked">>}], Client), - {ok, Transport, Socket} = cowboy_client:transport(Client2), + Chunks = do_body_to_chunks(50, Body, []), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", + [{<<"transfer-encoding">>, <<"chunked">>}]), _ = [begin - %% <<"\r\n">> is last 2 bytes of Chunk split before or after <<"\r">>. + %% Split in the newline just before the end of the chunk. Len = byte_size(Chunk) - (random:uniform(2) - 1), - <<Chunk2:Len/binary, End/binary>> = Chunk, - ok = Transport:send(Socket, Chunk2), - ok = timer:sleep(10), - ok = Transport:send(Socket, End) + << Chunk2:Len/binary, End/binary >> = Chunk, + ok = gun:dbg_send_raw(ConnPid, Chunk2), + receive after 10 -> ok end, + ok = gun:dbg_send_raw(ConnPid, End) end || Chunk <- Chunks], - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {ok, Body, _} = cowboy_client:response_body(Client3). + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref), + ok. te_identity(Config) -> - Client = ?config(client, Config), Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/echo/body", Config), [], Body, Client), - {ok, 200, _, Client3} = cowboy_client:response(Client2), - {ok, Body, _} = cowboy_client:response_body(Client3). + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", [], Body), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref), + ok. diff --git a/test/http_SUITE_data/http_errors.erl b/test/http_SUITE_data/http_errors.erl index 8831362..35ac3bd 100644 --- a/test/http_SUITE_data/http_errors.erl +++ b/test/http_SUITE_data/http_errors.erl @@ -9,30 +9,28 @@ init({_Transport, http}, Req, _Opts) -> case_init(Case, Req1). case_init(<<"init_before_reply">> = Case, _Req) -> + cowboy_error_h:ignore(?MODULE, case_init, 2), erlang:error(Case); - case_init(<<"init_after_reply">> = Case, Req) -> + cowboy_error_h:ignore(?MODULE, case_init, 2), {ok, _Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req), erlang:error(Case); - case_init(<<"init_reply_handle_error">> = Case, Req) -> {ok, Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req), {ok, Req1, Case}; - case_init(<<"handle_before_reply">> = Case, Req) -> {ok, Req, Case}; - case_init(<<"handle_after_reply">> = Case, Req) -> {ok, Req, Case}. - handle(_Req, <<"init_reply_handle_error">> = Case) -> + cowboy_error_h:ignore(?MODULE, handle, 2), erlang:error(Case); - handle(_Req, <<"handle_before_reply">> = Case) -> + cowboy_error_h:ignore(?MODULE, handle, 2), erlang:error(Case); - handle(Req, <<"handle_after_reply">> = Case) -> + cowboy_error_h:ignore(?MODULE, handle, 2), {ok, _Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req), erlang:error(Case). diff --git a/test/http_SUITE_data/http_long_polling.erl b/test/http_SUITE_data/http_long_polling.erl deleted file mode 100644 index ad4e66e..0000000 --- a/test/http_SUITE_data/http_long_polling.erl +++ /dev/null @@ -1,24 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(http_long_polling). --behaviour(cowboy_http_handler). --export([init/3, handle/2, info/3, terminate/3]). - -init({_Transport, http}, Req, _Opts) -> - erlang:send_after(500, self(), timeout), - {loop, Req, 5, 5000, hibernate}. - -handle(_Req, _State) -> - exit(badarg). - -info(timeout, Req, 0) -> - {ok, Req2} = cowboy_req:reply(102, Req), - {ok, Req2, 0}; -info(timeout, Req, State) -> - erlang:send_after(500, self(), timeout), - {loop, Req, State - 1, hibernate}. - -terminate({normal, shutdown}, _, _) -> - ok; -terminate({error, overflow}, _, _) -> - ok. diff --git a/test/http_SUITE_data/http_loop_recv.erl b/test/http_SUITE_data/http_loop_recv.erl deleted file mode 100644 index d0577f0..0000000 --- a/test/http_SUITE_data/http_loop_recv.erl +++ /dev/null @@ -1,18 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(http_loop_recv). --behaviour(cowboy_loop_handler). --export([init/3, info/3, terminate/3]). - -init({_, http}, Req, _) -> - self() ! recv_timeout, - {loop, Req, undefined, 500, hibernate}. - -info(recv_timeout, Req, State) -> - {ok, Body, Req1} = cowboy_req:body(Req), - 100000 = byte_size(Body), - {ok, Req2} = cowboy_req:reply(200, Req1), - {ok, Req2, State}. - -terminate({normal, shutdown}, _, _) -> - ok. diff --git a/test/http_SUITE_data/http_loop_stream_recv.erl b/test/http_SUITE_data/http_loop_stream_recv.erl index 87113c6..9f7646a 100644 --- a/test/http_SUITE_data/http_loop_stream_recv.erl +++ b/test/http_SUITE_data/http_loop_stream_recv.erl @@ -8,34 +8,27 @@ init({_, http}, Req, _) -> receive after 100 -> ok end, self() ! stream, - {loop, Req, 1, 100}. + {loop, Req, undefined, 100}. -info(stream, Req, Id) -> - case stream_next(Req) of - {ok, Id, Req2} -> - info(stream, Req2, Id+1); +info(stream, Req, undefined) -> + stream(Req, 1, <<>>). + +stream(Req, ID, Acc) -> + case cowboy_req:stream_body(Req) of + {ok, Data, Req2} -> + parse_id(Req2, ID, << Acc/binary, Data/binary >>); {done, Req2} -> {ok, Req3} = cowboy_req:reply(200, Req2), - {ok, Req3, Id} + {ok, Req3, undefined} + end. + +parse_id(Req, ID, Data) -> + case Data of + << ID:32, Rest/bits >> -> + parse_id(Req, ID + 1, Rest); + _ -> + stream(Req, ID, Data) end. terminate({normal, shutdown}, _, _) -> ok. - -stream_next(Req) -> - stream_next(<<>>, Req). - -stream_next(Buffer, Req) -> - case cowboy_req:stream_body(Req) of - {ok, Packet, Req2} -> - case <<Buffer/binary, Packet/binary>> of - <<Id:32>> -> - {ok, Id, Req2}; - Buffer2 when byte_size(Buffer2) < 4 -> - stream_next(Buffer2, Req2); - _InvalidBuffer -> - {error, invalid_chunk} - end; - Other -> - Other - end. diff --git a/test/http_SUITE_data/http_loop_timeout.erl b/test/http_SUITE_data/http_loop_timeout.erl deleted file mode 100644 index dd3472c..0000000 --- a/test/http_SUITE_data/http_loop_timeout.erl +++ /dev/null @@ -1,16 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(http_loop_timeout). --behaviour(cowboy_loop_handler). --export([init/3, info/3, terminate/3]). - -init({_, http}, Req, _) -> - erlang:send_after(1000, self(), error_timeout), - {loop, Req, undefined, 500, hibernate}. - -info(error_timeout, Req, State) -> - {ok, Req2} = cowboy_req:reply(500, Req), - {ok, Req2, State}. - -terminate({normal, timeout}, _, _) -> - ok. diff --git a/test/http_SUITE_data/http_multipart.erl b/test/http_SUITE_data/http_multipart.erl index c94739f..79bfeb8 100644 --- a/test/http_SUITE_data/http_multipart.erl +++ b/test/http_SUITE_data/http_multipart.erl @@ -8,22 +8,18 @@ init({_Transport, http}, Req, []) -> {ok, Req, {}}. handle(Req, State) -> - {Result, Req2} = acc_multipart(Req), + {Result, Req2} = acc_multipart(Req, []), {ok, Req3} = cowboy_req:reply(200, [], term_to_binary(Result), Req2), {ok, Req3, State}. terminate(_, _, _) -> ok. -acc_multipart(Req) -> - acc_multipart(cowboy_req:multipart_data(Req), []). - -acc_multipart({headers, Headers, Req}, Acc) -> - acc_multipart(cowboy_req:multipart_data(Req), [{Headers, []}|Acc]); -acc_multipart({body, Data, Req}, [{Headers, BodyAcc}|Acc]) -> - acc_multipart(cowboy_req:multipart_data(Req), [{Headers, [Data|BodyAcc]}|Acc]); -acc_multipart({end_of_part, Req}, [{Headers, BodyAcc}|Acc]) -> - acc_multipart(cowboy_req:multipart_data(Req), - [{Headers, list_to_binary(lists:reverse(BodyAcc))}|Acc]); -acc_multipart({eof, Req}, Acc) -> - {lists:reverse(Acc), Req}. +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. diff --git a/test/http_SUITE_data/http_multipart_stream.erl b/test/http_SUITE_data/http_multipart_stream.erl new file mode 100644 index 0000000..926d150 --- /dev/null +++ b/test/http_SUITE_data/http_multipart_stream.erl @@ -0,0 +1,34 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_multipart_stream). +-behaviour(cowboy_http_handler). +-export([init/3, handle/2, terminate/3]). + +init(_, Req, []) -> + {ok, Req, undefined}. + +handle(Req, State) -> + Req2 = multipart(Req), + {ok, Req3} = cowboy_req:reply(200, Req2), + {ok, Req3, State}. + +terminate(_, _, _) -> + ok. + +multipart(Req) -> + case cowboy_req:part(Req) of + {ok, [{<<"content-length">>, BinLength}], Req2} -> + Length = list_to_integer(binary_to_list(BinLength)), + {Length, Req3} = stream_body(Req2, 0), + multipart(Req3); + {done, Req2} -> + Req2 + end. + +stream_body(Req, N) -> + case cowboy_req:part_body(Req) of + {ok, Data, Req2} -> + {N + byte_size(Data), Req2}; + {more, Data, Req2} -> + stream_body(Req2, N + byte_size(Data)) + end. diff --git a/test/http_SUITE_data/rest_missing_callbacks.erl b/test/http_SUITE_data/rest_missing_callbacks.erl index 171c856..94bfbbd 100644 --- a/test/http_SUITE_data/rest_missing_callbacks.erl +++ b/test/http_SUITE_data/rest_missing_callbacks.erl @@ -11,11 +11,13 @@ allowed_methods(Req, State) -> {[<<"GET">>, <<"PUT">>], Req, State}. content_types_accepted(Req, State) -> + cowboy_error_h:ignore(cowboy_rest, process_content_type, 3), {[ {<<"application/json">>, put_application_json} ], Req, State}. content_types_provided(Req, State) -> + cowboy_error_h:ignore(cowboy_rest, set_resp_body, 2), {[ {<<"text/plain">>, get_text_plain} ], Req, State}. diff --git a/test/http_SUITE_data/rest_resource_etags.erl b/test/http_SUITE_data/rest_resource_etags.erl index 43f1e05..2652f57 100644 --- a/test/http_SUITE_data/rest_resource_etags.erl +++ b/test/http_SUITE_data/rest_resource_etags.erl @@ -18,8 +18,10 @@ generate_etag(Req, State) -> {<<"\"etag-header-value\"">>, Req2, State}; %% Invalid return values from generate_etag/2. {<<"binary-strong-unquoted">>, Req2} -> + cowboy_error_h:ignore(cowboy_http, quoted_string, 2), {<<"etag-header-value">>, Req2, State}; {<<"binary-weak-unquoted">>, Req2} -> + cowboy_error_h:ignore(cowboy_http, quoted_string, 2), {<<"W/etag-header-value">>, Req2, State} end. diff --git a/test/loop_handler_SUITE.erl b/test/loop_handler_SUITE.erl new file mode 100644 index 0000000..5f69490 --- /dev/null +++ b/test/loop_handler_SUITE.erl @@ -0,0 +1,87 @@ +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> +%% +%% 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(loop_handler_SUITE). +-compile(export_all). + +-import(cowboy_test, [config/2]). +-import(cowboy_test, [doc/1]). +-import(cowboy_test, [gun_open/1]). + +%% ct. + +all() -> + cowboy_test:common_all(). + +groups() -> + cowboy_test:common_groups(cowboy_test:all(?MODULE)). + +init_per_group(Name, Config) -> + cowboy_test:init_common_groups(Name, Config, ?MODULE). + +end_per_group(Name, _) -> + cowboy:stop_listener(Name). + +%% Dispatch configuration. + +init_dispatch(_) -> + cowboy_router:compile([{'_', [ + {"/long_polling", long_polling_h, []}, + {"/loop_body", loop_handler_body_h, []}, + {"/loop_timeout", loop_handler_timeout_h, []} + ]}]). + +%% Tests. + +long_polling(Config) -> + doc("Simple long-polling."), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/long_polling"), + {response, fin, 102, _} = gun:await(ConnPid, Ref), + ok. + +long_polling_body(Config) -> + doc("Long-polling with a body that falls within the configurable limits."), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/long_polling", [], << 0:5000/unit:8 >>), + {response, fin, 102, _} = gun:await(ConnPid, Ref), + ok. + +long_polling_body_too_large(Config) -> + doc("Long-polling with a body that exceeds the configurable limits."), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/long_polling", [], << 0:100000/unit:8 >>), + {response, fin, 500, _} = gun:await(ConnPid, Ref), + ok. + +long_polling_pipeline(Config) -> + doc("Pipeline of long-polling calls."), + ConnPid = gun_open(Config), + Refs = [gun:get(ConnPid, "/long_polling") || _ <- lists:seq(1, 2)], + _ = [{response, fin, 102, _} = gun:await(ConnPid, Ref) || Ref <- Refs], + ok. + +loop_body(Config) -> + doc("Check that a loop handler can read the request body in info/3."), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/loop_body", [], << 0:100000/unit:8 >>), + {response, fin, 200, _} = gun:await(ConnPid, Ref), + ok. + +loop_timeout(Config) -> + doc("Ensure that the loop handler timeout results in a 204 response."), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/loop_timeout"), + {response, fin, 204, _} = gun:await(ConnPid, Ref), + ok. diff --git a/test/spdy_SUITE.erl b/test/spdy_SUITE.erl index c73c4f7..2e9c7e4 100644 --- a/test/spdy_SUITE.erl +++ b/test/spdy_SUITE.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2013-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -13,21 +13,10 @@ %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -module(spdy_SUITE). +-compile(export_all). --include_lib("common_test/include/ct.hrl"). - -%% ct. --export([all/0]). --export([groups/0]). --export([init_per_suite/1]). --export([end_per_suite/1]). --export([init_per_group/2]). --export([end_per_group/2]). - -%% Tests. --export([check_status/1]). --export([echo_body/1]). --export([echo_body_multi/1]). +-import(cowboy_test, [config/2]). +-import(cowboy_test, [gun_monitor_open/1]). %% ct. @@ -35,55 +24,28 @@ all() -> [{group, spdy}]. groups() -> - [{spdy, [], [ - check_status, - echo_body, - echo_body_multi - ]}]. + [{spdy, [], cowboy_test:all(?MODULE)}]. init_per_suite(Config) -> case proplists:get_value(ssl_app, ssl:versions()) of Version when Version < "5.2.1" -> {skip, "No NPN support in SSL application."}; _ -> - application:start(crypto), - application:start(cowlib), - application:start(ranch), - application:start(cowboy), - application:start(asn1), - application:start(public_key), - application:start(ssl), - application:start(gun), - Dir = ?config(priv_dir, Config) ++ "/static", + Dir = config(priv_dir, Config) ++ "/static", ct_helper:create_static_dir(Dir), [{static_dir, Dir}|Config] end. end_per_suite(Config) -> - Dir = ?config(static_dir, Config), - ct_helper:delete_static_dir(Dir), - application:stop(gun), - application:stop(ssl), - application:stop(public_key), - application:stop(asn1), - application:stop(cowboy), - application:stop(ranch), - application:stop(cowlib), - application:stop(crypto), - ok. + ct_helper:delete_static_dir(config(static_dir, Config)). init_per_group(Name, Config) -> - {_, Cert, Key} = ct_helper:make_certs(), - Opts = [{cert, Cert}, {key, Key}], - {ok, _} = cowboy:start_spdy(Name, 100, Opts ++ [{port, 0}], [ + cowboy_test:init_spdy(Name, [ {env, [{dispatch, init_dispatch(Config)}]} - ]), - Port = ranch:get_port(Name), - [{port, Port}|Config]. + ], Config). end_per_group(Name, _) -> - cowboy:stop_listener(Name), - ok. + cowboy:stop_listener(Name). %% Dispatch configuration. @@ -91,7 +53,7 @@ init_dispatch(Config) -> cowboy_router:compile([ {"localhost", [ {"/static/[...]", cowboy_static, - {dir, ?config(static_dir, Config)}}, + {dir, config(static_dir, Config)}}, {"/echo/body", http_echo_body, []}, {"/chunked", http_chunked, []}, {"/", http_handler, []} @@ -100,24 +62,14 @@ init_dispatch(Config) -> %% Convenience functions. -quick_get(Pid, Host, Path) -> - MRef = monitor(process, Pid), - StreamRef = gun:get(Pid, Path, [{":host", Host}]), - receive - {'DOWN', MRef, _, _, Reason} -> - error(Reason); - {gun_response, Pid, StreamRef, IsFin, - << Status:3/binary, _/bits >>, Headers} -> - {IsFin, list_to_integer(binary_to_list(Status)), Headers} - after 1000 -> - error(timeout) - end. +do_get(ConnPid, MRef, Host, Path) -> + StreamRef = gun:get(ConnPid, Path, [{":host", Host}]), + {response, IsFin, Status, _} = gun:await(ConnPid, StreamRef, MRef), + {IsFin, Status}. %% Tests. check_status(Config) -> - {_, Port} = lists:keyfind(port, 1, Config), - {ok, Pid} = gun:open("localhost", Port), Tests = [ {200, nofin, "localhost", "/"}, {200, nofin, "localhost", "/chunked"}, @@ -126,64 +78,33 @@ check_status(Config) -> {400, fin, "localhost", "bad-path"}, {404, fin, "localhost", "/this/path/does/not/exist"} ], + {ConnPid, MRef} = gun_monitor_open(Config), _ = [{Status, Fin, Host, Path} = begin - {IsFin, Ret, _} = quick_get(Pid, Host, Path), + {IsFin, Ret} = do_get(ConnPid, MRef, Host, Path), {Ret, IsFin, Host, Path} end || {Status, Fin, Host, Path} <- Tests], - gun:close(Pid). + gun:close(ConnPid). echo_body(Config) -> - {_, Port} = lists:keyfind(port, 1, Config), - {ok, Pid} = gun:open("localhost", Port), - MRef = monitor(process, Pid), + {ConnPid, MRef} = gun_monitor_open(Config), Body = << 0:800000 >>, - StreamRef = gun:post(Pid, "/echo/body", [ - {<<"content-length">>, integer_to_list(byte_size(Body))}, + StreamRef = gun:post(ConnPid, "/echo/body", [ {<<"content-type">>, "application/octet-stream"} ], Body), - receive - {'DOWN', MRef, _, _, Reason} -> - error(Reason); - {gun_response, Pid, StreamRef, nofin, << "200", _/bits >>, _} -> - ok - after 1000 -> - error(response_timeout) - end, - receive - {'DOWN', MRef, _, _, Reason2} -> - error({gun_data, Reason2}); - {gun_data, Pid, StreamRef, fin, Body} -> - ok - after 1000 -> - error(data_timeout) - end, - gun:close(Pid). + {response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef), + {ok, Body} = gun:await_body(ConnPid, StreamRef, MRef), + gun:close(ConnPid). echo_body_multi(Config) -> - {_, Port} = lists:keyfind(port, 1, Config), - {ok, Pid} = gun:open("localhost", Port), - MRef = monitor(process, Pid), + {ConnPid, MRef} = gun_monitor_open(Config), BodyChunk = << 0:80000 >>, - StreamRef = gun:post(Pid, "/echo/body", [ + StreamRef = gun:post(ConnPid, "/echo/body", [ + %% @todo I'm still unhappy with this. It shouldn't be required... {<<"content-length">>, integer_to_list(byte_size(BodyChunk) * 10)}, {<<"content-type">>, "application/octet-stream"} ]), - _ = [gun:data(Pid, StreamRef, nofin, BodyChunk) || _ <- lists:seq(1, 9)], - gun:data(Pid, StreamRef, fin, BodyChunk), - receive - {'DOWN', MRef, _, _, Reason} -> - error(Reason); - {gun_response, Pid, StreamRef, nofin, << "200", _/bits >>, _} -> - ok - after 1000 -> - error(response_timeout) - end, - receive - {'DOWN', MRef, _, _, Reason2} -> - error({gun_data, Reason2}); - {gun_data, Pid, StreamRef, fin, << 0:800000 >>} -> - ok - after 1000 -> - error(data_timeout) - end, - gun:close(Pid). + _ = [gun:data(ConnPid, StreamRef, nofin, BodyChunk) || _ <- lists:seq(1, 9)], + gun:data(ConnPid, StreamRef, fin, BodyChunk), + {response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef), + {ok, << 0:800000 >>} = gun:await_body(ConnPid, StreamRef, MRef), + gun:close(ConnPid). diff --git a/test/ws_SUITE.erl b/test/ws_SUITE.erl index 0a8ca43..77c82f6 100644 --- a/test/ws_SUITE.erl +++ b/test/ws_SUITE.erl @@ -1,4 +1,4 @@ -%% Copyright (c) 2011-2013, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -13,94 +13,53 @@ %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -module(ws_SUITE). +-compile(export_all). --include_lib("common_test/include/ct.hrl"). - -%% ct. --export([all/0]). --export([groups/0]). --export([init_per_suite/1]). --export([end_per_suite/1]). --export([init_per_group/2]). --export([end_per_group/2]). - -%% Tests. --export([ws0/1]). --export([ws8/1]). --export([ws8_init_shutdown/1]). --export([ws8_single_bytes/1]). --export([ws13/1]). --export([ws_deflate/1]). --export([ws_deflate_chunks/1]). --export([ws_deflate_fragments/1]). --export([ws_send_close/1]). --export([ws_send_close_payload/1]). --export([ws_send_many/1]). --export([ws_text_fragments/1]). --export([ws_timeout_hibernate/1]). --export([ws_timeout_cancel/1]). --export([ws_timeout_reset/1]). --export([ws_upgrade_with_opts/1]). +-import(cowboy_test, [config/2]). %% ct. all() -> - [{group, ws}]. + [{group, autobahn}, {group, ws}]. groups() -> - BaseTests = [ - ws0, - ws8, - ws8_init_shutdown, - ws8_single_bytes, - ws13, - ws_deflate, - ws_deflate_chunks, - ws_deflate_fragments, - ws_send_close, - ws_send_close_payload, - ws_send_many, - ws_text_fragments, - ws_timeout_hibernate, - ws_timeout_cancel, - ws_timeout_reset, - ws_upgrade_with_opts - ], - [{ws, [parallel], BaseTests}]. + BaseTests = cowboy_test:all(?MODULE) -- [autobahn_fuzzingclient], + [{autobahn, [], [autobahn_fuzzingclient]}, {ws, [parallel], BaseTests}]. init_per_suite(Config) -> - application:start(crypto), - application:start(cowlib), - application:start(ranch), - application:start(cowboy), Config. -end_per_suite(_Config) -> - application:stop(cowboy), - application:stop(ranch), - application:stop(cowlib), - application:stop(crypto), - ok. - -init_per_group(ws, Config) -> - cowboy:start_http(ws, 100, [{port, 0}], [ +init_per_group(Name = autobahn, Config) -> + %% Some systems have it named pip2. + Out = os:cmd("pip show autobahntestsuite ; pip2 show autobahntestsuite"), + case string:str(Out, "autobahntestsuite") of + 0 -> + ct:print("Skipping the autobahn group because the " + "Autobahn Test Suite is not installed.~nTo install it, " + "please follow the instructions on this page:~n~n " + "http://autobahn.ws/testsuite/installation.html"), + {skip, "Autobahn Test Suite not installed."}; + _ -> + {ok, _} = cowboy:start_http(Name, 100, [{port, 33080}], [ + {env, [{dispatch, init_dispatch()}]}]), + Config + end; +init_per_group(Name = ws, Config) -> + cowboy_test:init_http(Name, [ {env, [{dispatch, init_dispatch()}]}, {compress, true} - ]), - Port = ranch:get_port(ws), - [{port, Port}|Config]. + ], Config). end_per_group(Listener, _Config) -> - cowboy:stop_listener(Listener), - ok. + cowboy:stop_listener(Listener). %% Dispatch configuration. init_dispatch() -> cowboy_router:compile([ {"localhost", [ - {"/ws_echo_timer", ws_echo_timer, []}, {"/ws_echo", ws_echo, []}, + {"/ws_echo_timer", ws_echo_timer, []}, {"/ws_init_shutdown", ws_init_shutdown, []}, {"/ws_send_many", ws_send_many, [ {sequence, [ @@ -127,7 +86,21 @@ init_dispatch() -> ]} ]). -%% ws and wss. +%% Tests. + +autobahn_fuzzingclient(Config) -> + Out = os:cmd("cd " ++ config(priv_dir, Config) + ++ " && wstest -m fuzzingclient -s " + ++ config(data_dir, Config) ++ "client.json"), + Report = config(priv_dir, Config) ++ "reports/servers/index.html", + ct:log("<h2><a href=\"~s\">Full report</a></h2>~n", [Report]), + ct:print("Autobahn Test Suite report: file://~s~n", [Report]), + ct:log("~s~n", [Out]), + {ok, HTML} = file:read_file(Report), + case length(binary:matches(HTML, <<"case_failed">>)) > 2 of + true -> error(failed); + false -> ok + end. %% We do not support hixie76 anymore. ws0(Config) -> @@ -163,7 +136,7 @@ ws8(Config) -> {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( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -223,7 +196,7 @@ ws8_single_bytes(Config) -> {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( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -283,7 +256,7 @@ ws13(Config) -> {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( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -335,7 +308,7 @@ ws_deflate(Config) -> {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( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -346,7 +319,7 @@ ws_deflate(Config) -> Mask = 16#11223344, Hello = << 242, 72, 205, 201, 201, 7, 0 >>, - MaskedHello = websocket_mask(Hello, Mask, <<>>), + MaskedHello = do_mask(Hello, Mask, <<>>), % send compressed text frame containing the Hello string ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, @@ -377,7 +350,7 @@ ws_deflate_chunks(Config) -> {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( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -388,7 +361,7 @@ ws_deflate_chunks(Config) -> Mask = 16#11223344, Hello = << 242, 72, 205, 201, 201, 7, 0 >>, - MaskedHello = websocket_mask(Hello, Mask, <<>>), + MaskedHello = do_mask(Hello, Mask, <<>>), % send compressed text frame containing the Hello string ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, @@ -422,7 +395,7 @@ ws_deflate_fragments(Config) -> {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( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -437,9 +410,9 @@ ws_deflate_fragments(Config) -> % send compressed text frame containing the Hello string % as 2 separate fragments ok = gen_tcp:send(Socket, << 0:1, 1:1, 0:2, 1:4, 1:1, 4:7, Mask:32, - (websocket_mask(binary:part(Hello, 0, 4), Mask, <<>>))/binary >>), + (do_mask(binary:part(Hello, 0, 4), Mask, <<>>))/binary >>), ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 0:4, 1:1, 3:7, Mask:32, - (websocket_mask(binary:part(Hello, 4, 3), Mask, <<>>))/binary >>), + (do_mask(binary:part(Hello, 4, 3), Mask, <<>>))/binary >>), % receive compressed text frame containing the Hello string {ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, Hello/binary >>} = gen_tcp:recv(Socket, 0, 6000), @@ -465,7 +438,7 @@ ws_send_close(Config) -> {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( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -494,7 +467,7 @@ ws_send_close_payload(Config) -> {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( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -523,7 +496,7 @@ ws_send_many(Config) -> {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( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -555,7 +528,7 @@ ws_text_fragments(Config) -> {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( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -609,7 +582,7 @@ ws_timeout_hibernate(Config) -> {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( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -636,7 +609,7 @@ ws_timeout_cancel(Config) -> {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( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -663,7 +636,7 @@ ws_timeout_reset(Config) -> {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( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -696,7 +669,7 @@ ws_upgrade_with_opts(Config) -> {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( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -711,27 +684,27 @@ ws_upgrade_with_opts(Config) -> %% Internal. -websocket_headers({ok, http_eoh, Rest}, Acc) -> +do_decode_headers({ok, http_eoh, Rest}, Acc) -> [Acc, Rest]; -websocket_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) -> +do_decode_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, []), + do_decode_headers(erlang:decode_packet(httph, Rest, []), [{F(Key), Value}|Acc]). -websocket_mask(<<>>, _, Unmasked) -> - Unmasked; -websocket_mask(<< O:32, Rest/bits >>, MaskKey, Acc) -> +do_mask(<<>>, _, Acc) -> + Acc; +do_mask(<< O:32, Rest/bits >>, MaskKey, Acc) -> T = O bxor MaskKey, - websocket_mask(Rest, MaskKey, << Acc/binary, T:32 >>); -websocket_mask(<< O:24 >>, MaskKey, Acc) -> + do_mask(Rest, MaskKey, << Acc/binary, T:32 >>); +do_mask(<< O:24 >>, MaskKey, Acc) -> << MaskKey2:24, _:8 >> = << MaskKey:32 >>, T = O bxor MaskKey2, << Acc/binary, T:24 >>; -websocket_mask(<< O:16 >>, MaskKey, Acc) -> +do_mask(<< O:16 >>, MaskKey, Acc) -> << MaskKey2:16, _:16 >> = << MaskKey:32 >>, T = O bxor MaskKey2, << Acc/binary, T:16 >>; -websocket_mask(<< O:8 >>, MaskKey, Acc) -> +do_mask(<< O:8 >>, MaskKey, Acc) -> << MaskKey2:8, _:24 >> = << MaskKey:32 >>, T = O bxor MaskKey2, << Acc/binary, T:8 >>. diff --git a/test/ws_SUITE_data/client.json b/test/ws_SUITE_data/client.json new file mode 100644 index 0000000..7899503 --- /dev/null +++ b/test/ws_SUITE_data/client.json @@ -0,0 +1,14 @@ +{ + "options": {"failByDrop": false}, + "enable-ssl": false, + + "servers": [{ + "agent": "Cowboy", + "url": "ws://localhost:33080/ws_echo", + "options": {"version": 18} + }], + + "cases": ["*"], + "exclude-cases": [], + "exclude-agent-cases": {} +} |