ninenines/cowboy — pull request #754 by fishcakez (2014-10-10 11:37:42+00:00)
Title: Add basic support for system messages

This changes performs the first step of #750, a simplified version with much improved testing. All system messages are handled by `cowboy_protocol` or `cowboy_middleware`. Middlewares, sub_protocols (and handlers) can not change how system messages are handled.

To have `cowboy_middleware` handle a system message a middleware (or sub_protocol) returns:
```erlang
{system, From, Msg, Module, Req, State}
```
Once the system message(s) have been handled control is passed back to the middleware via a `cowboy_sys` callback. Either to continue processing as before:
```erlang
sys_continue(Req, #state{env=Env}) ->
{ok, Req, Env}.
```
Or to terminate with reason `Reason`:
```erlang
sys_terminate(Reason, _Req, _State) ->
exit(Reason).
```

For more information please see the commit message or the additions to the guide.

Help is required to test handling of system messages when a websocket handler receives the message in the `websocket_payload_loop` receive clause.