0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 04:30:25 +00:00

Use spaces in snippets in the guide

This commit is contained in:
Loïc Hoguin 2016-09-14 18:51:11 +02:00
parent a231216b07
commit 31cabe0fb9
10 changed files with 104 additions and 104 deletions

View file

@ -74,11 +74,11 @@ to an integer:
[source,erlang] [source,erlang]
---- ----
fun (Value0) when is_binary(Value0) -> fun (Value0) when is_binary(Value0) ->
try binary_to_integer(Value0) of try binary_to_integer(Value0) of
Value -> {true, Value} Value -> {true, Value}
catch _:_ -> catch _:_ ->
false false
end. end.
---- ----
Constraint functions should only crash because the programmer Constraint functions should only crash because the programmer

View file

@ -50,7 +50,7 @@ They can also be set for a duration in seconds:
---- ----
SessionID = generate_session_id(), SessionID = generate_session_id(),
Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID,
#{max_age => 3600}, Req0). #{max_age => 3600}, Req0).
---- ----
To delete cookies, set `max_age` to 0: To delete cookies, set `max_age` to 0:
@ -59,7 +59,7 @@ To delete cookies, set `max_age` to 0:
---- ----
SessionID = generate_session_id(), SessionID = generate_session_id(),
Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID,
#{max_age => 0}, Req0). #{max_age => 0}, Req0).
---- ----
To restrict cookies to a specific domain and path, the options To restrict cookies to a specific domain and path, the options
@ -68,7 +68,7 @@ of the same name can be used:
[source,erlang] [source,erlang]
---- ----
Req = cowboy_req:set_resp_cookie(<<"inaccount">>, <<"1">>, Req = cowboy_req:set_resp_cookie(<<"inaccount">>, <<"1">>,
#{domain => "my.example.org", path => "/account"}, Req0). #{domain => "my.example.org", path => "/account"}, Req0).
---- ----
Cookies will be sent with requests to this domain and all Cookies will be sent with requests to this domain and all
@ -82,7 +82,7 @@ available over HTTPS):
---- ----
SessionID = generate_session_id(), SessionID = generate_session_id(),
Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID,
#{secure => true}, Req0). #{secure => true}, Req0).
---- ----
To prevent client-side scripts from accessing a cookie: To prevent client-side scripts from accessing a cookie:
@ -91,7 +91,7 @@ To prevent client-side scripts from accessing a cookie:
---- ----
SessionID = generate_session_id(), SessionID = generate_session_id(),
Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID,
#{http_only => true}, Req0). #{http_only => true}, Req0).
---- ----
Cookies may also be set client-side, for example using Cookies may also be set client-side, for example using

View file

@ -91,14 +91,14 @@ code to the `start/2` function to make it look like this:
[source,erlang] [source,erlang]
---- ----
start(_Type, _Args) -> start(_Type, _Args) ->
Dispatch = cowboy_router:compile([ Dispatch = cowboy_router:compile([
{'_', [{"/", hello_handler, []}]} {'_', [{"/", hello_handler, []}]}
]), ]),
{ok, _} = cowboy:start_clear(my_http_listener, 100, {ok, _} = cowboy:start_clear(my_http_listener, 100,
[{port, 8080}], [{port, 8080}],
#{env => #{dispatch => Dispatch}} #{env => #{dispatch => Dispatch}}
), ),
hello_erlang_sup:start_link(). hello_erlang_sup:start_link().
---- ----
Routes are explained in details in the xref:routing[Routing] Routes are explained in details in the xref:routing[Routing]
@ -127,11 +127,11 @@ the `init/2` function like this to send a reply.
[source,erlang] [source,erlang]
---- ----
init(Req0, State) -> init(Req0, State) ->
Req = cowboy_req:reply(200, Req = cowboy_req:reply(200,
#{<<"content-type">> => <<"text/plain">>}, #{<<"content-type">> => <<"text/plain">>},
<<"Hello Erlang!">>, <<"Hello Erlang!">>,
Req0), Req0),
{ok, Req, State}. {ok, Req, State}.
---- ----
What the above code does is send a `200 OK` reply, with the What the above code does is send a `200 OK` reply, with the

View file

@ -65,7 +65,7 @@ following snippet switches to a Websocket handler:
[source,erlang] [source,erlang]
---- ----
init(Req, State) -> init(Req, State) ->
{cowboy_websocket, Req, State}. {cowboy_websocket, Req, State}.
---- ----
You can also switch to your own custom handler type: You can also switch to your own custom handler type:
@ -73,7 +73,7 @@ You can also switch to your own custom handler type:
[source,erlang] [source,erlang]
---- ----
init(Req, State) -> init(Req, State) ->
{my_handler_type, Req, State}. {my_handler_type, Req, State}.
---- ----
How to implement a custom handler type is described in the How to implement a custom handler type is described in the

View file

@ -28,14 +28,14 @@ on port 8080:
[source,erlang] [source,erlang]
---- ----
start(_Type, _Args) -> start(_Type, _Args) ->
Dispatch = cowboy_router:compile([ Dispatch = cowboy_router:compile([
{'_', [{"/", hello_handler, []}]} {'_', [{"/", hello_handler, []}]}
]), ]),
{ok, _} = cowboy:start_clear(my_http_listener, 100, {ok, _} = cowboy:start_clear(my_http_listener, 100,
[{port, 8080}], [{port, 8080}],
#{env => #{dispatch => Dispatch}} #{env => #{dispatch => Dispatch}}
), ),
hello_erlang_sup:start_link(). hello_erlang_sup:start_link().
---- ----
The xref:getting_started[Getting Started] chapter uses a The xref:getting_started[Getting Started] chapter uses a
@ -72,18 +72,18 @@ used directly to setup a custom listener.
[source,erlang] [source,erlang]
---- ----
start(_Type, _Args) -> start(_Type, _Args) ->
Dispatch = cowboy_router:compile([ Dispatch = cowboy_router:compile([
{'_', [{"/", hello_handler, []}]} {'_', [{"/", hello_handler, []}]}
]), ]),
{ok, _} = cowboy:start_tls(my_http_listener, 100, {ok, _} = cowboy:start_tls(my_http_listener, 100,
[ [
{port, 8443}, {port, 8443},
{certfile, "/path/to/certfile"}, {certfile, "/path/to/certfile"},
{keyfile, "/path/to/keyfile"} {keyfile, "/path/to/keyfile"}
], ],
#{env => #{dispatch => Dispatch}} #{env => #{dispatch => Dispatch}}
), ),
hello_erlang_sup:start_link(). hello_erlang_sup:start_link().
---- ----
Clients connecting to Cowboy on the secure listener are Clients connecting to Cowboy on the secure listener are

View file

@ -32,15 +32,15 @@ otherwise.
[source,erlang] [source,erlang]
---- ----
init(Req0=#{method := <<"GET">>}, State) -> init(Req0=#{method := <<"GET">>}, State) ->
Req = cowboy_req:reply(200, #{ Req = cowboy_req:reply(200, #{
<<"content-type">> => <<"text/plain">> <<"content-type">> => <<"text/plain">>
}, <<"Hello world!">>, Req0), }, <<"Hello world!">>, Req0),
{ok, Req, State}; {ok, Req, State};
init(Req0, State) -> init(Req0, State) ->
Req = cowboy_req:reply(405, #{ Req = cowboy_req:reply(405, #{
<<"allow">> => <<"GET">> <<"allow">> => <<"GET">>
}, Req0), }, Req0),
{ok, Req, State}. {ok, Req, State}.
---- ----
Any other field is internal and should not be accessed. Any other field is internal and should not be accessed.
@ -135,11 +135,11 @@ of the effective request URI can all be retrieved directly:
[source,erlang] [source,erlang]
---- ----
#{ #{
scheme := Scheme, scheme := Scheme,
host := Host, host := Host,
port := Port, port := Port,
path := Path, path := Path,
qs := Qs qs := Qs
} = Req. } = Req.
---- ----
@ -348,7 +348,7 @@ directly:
[source,erlang] [source,erlang]
---- ----
ParsedVal = cowboy_req:parse_header(<<"content-type">>, Req, ParsedVal = cowboy_req:parse_header(<<"content-type">>, Req,
{<<"text">>, <<"plain">>, []}). {<<"text">>, <<"plain">>, []}).
---- ----
=== Peer === Peer

View file

@ -71,7 +71,7 @@ only up to 1MB for up to 5 seconds:
[source,erlang] [source,erlang]
---- ----
{ok, Data, Req} = cowboy_req:read_body(Req0, {ok, Data, Req} = cowboy_req:read_body(Req0,
#{length => 1000000, period => 5000}). #{length => 1000000, period => 5000}).
---- ----
You may also disable the length limit: You may also disable the length limit:
@ -126,5 +126,5 @@ read for up to 64KB and up to 5 seconds. They can be modified:
[source,erlang] [source,erlang]
---- ----
{ok, KeyValues, Req} = cowboy_req:read_urlencoded_body(Req0, {ok, KeyValues, Req} = cowboy_req:read_urlencoded_body(Req0,
#{length => 4096, period => 3000}). #{length => 4096, period => 3000}).
---- ----

View file

@ -71,7 +71,7 @@ Body = <<"Hats off!">>,
Req = cowboy_req:reply(200, #{ Req = cowboy_req:reply(200, #{
<<"content-type">> => <<"text/html">> <<"content-type">> => <<"text/html">>
}, ["<html><head><title>", Title, "</title></head>", }, ["<html><head><title>", Title, "</title></head>",
"<body><p>", Body, "</p></body></html>"], Req0). "<body><p>", Body, "</p></body></html>"], Req0).
---- ----
This method of building responses is more efficient than This method of building responses is more efficient than
@ -247,7 +247,7 @@ To send a file while replying:
[source,erlang] [source,erlang]
---- ----
Req = cowboy_req:reply(200, #{ Req = cowboy_req:reply(200, #{
<<"content-type">> => "image/png" <<"content-type">> => "image/png"
}, {sendfile, 0, 12345, "path/to/logo.png"}, Req0). }, {sendfile, 0, 12345, "path/to/logo.png"}, Req0).
---- ----
@ -289,13 +289,13 @@ in the response:
[source,erlang] [source,erlang]
---- ----
cowboy_req:push("/static/style.css", #{ cowboy_req:push("/static/style.css", #{
<<"accept">> => <<"text/css">> <<"accept">> => <<"text/css">>
}, Req0), }, Req0),
Req = cowboy_req:reply(200, #{ Req = cowboy_req:reply(200, #{
<<"content-type">> => <<"text/html">> <<"content-type">> => <<"text/html">>
}, ["<html><head><title>My web page</title>", }, ["<html><head><title>My web page</title>",
"<link rel='stylesheet' type='text/css' href='/static/style.css'>", "<link rel='stylesheet' type='text/css' href='/static/style.css'>",
"<body><p>Welcome to Erlang!</p></body></html>"], Req0). "<body><p>Welcome to Erlang!</p></body></html>"], Req0).
---- ----
To override the method, scheme, host, port or query string, To override the method, scheme, host, port or query string,
@ -305,7 +305,7 @@ uses a different host name:
[source,erlang] [source,erlang]
---- ----
cowboy_req:push("/static/style.css", #{ cowboy_req:push("/static/style.css", #{
<<"accept">> => <<"text/css">> <<"accept">> => <<"text/css">>
}, #{host => <<"cdn.example.org">>}, Req), }, #{host => <<"cdn.example.org">>}, Req),
---- ----

View file

@ -17,7 +17,7 @@ is handled by the sub protocol.
[source,erlang] [source,erlang]
---- ----
init(Req, State) -> init(Req, State) ->
{cowboy_websocket, Req, State}. {cowboy_websocket, Req, State}.
---- ----
The return value may also have a `Timeout` value and/or the The return value may also have a `Timeout` value and/or the
@ -34,7 +34,7 @@ hibernation:
[source,erlang] [source,erlang]
---- ----
init(Req, State) -> init(Req, State) ->
{my_protocol, Req, State, 5000, hibernate}. {my_protocol, Req, State, 5000, hibernate}.
---- ----
If a sub protocol does not make use of these options, it should If a sub protocol does not make use of these options, it should
@ -59,7 +59,7 @@ timeout and hibernate values.
[source,erlang] [source,erlang]
---- ----
upgrade(Req, Env, Handler, HandlerOpts, Timeout, Hibernate) -> upgrade(Req, Env, Handler, HandlerOpts, Timeout, Hibernate) ->
%% Sub protocol code here. %% Sub protocol code here.
---- ----
This callback is expected to behave like a middleware and to This callback is expected to behave like a middleware and to

View file

@ -21,7 +21,7 @@ To establish a Websocket connection, you must switch to the
[source,erlang] [source,erlang]
---- ----
init(Req, State) -> init(Req, State) ->
{cowboy_websocket, Req, State}. {cowboy_websocket, Req, State}.
---- ----
Cowboy will perform the Websocket handshake immediately. Note Cowboy will perform the Websocket handshake immediately. Note
@ -58,19 +58,19 @@ be:
[source,erlang] [source,erlang]
---- ----
init(Req, State) -> init(Req, State) ->
case cowboy_req:parse_header(<<"sec-websocket-protocol">>, Req) of case cowboy_req:parse_header(<<"sec-websocket-protocol">>, Req) of
undefined -> undefined ->
{ok, Req, State}; {ok, Req, State};
Subprotocols -> Subprotocols ->
case lists:keymember(<<"mqtt">>, 1, Subprotocols) of case lists:keymember(<<"mqtt">>, 1, Subprotocols) of
true -> true ->
Req2 = cowboy_req:set_resp_header(<<"sec-websocket-protocol">>, Req2 = cowboy_req:set_resp_header(<<"sec-websocket-protocol">>,
<<"mqtt">>, Req), <<"mqtt">>, Req),
{ok, Req2, State}; {ok, Req2, State};
false -> false ->
{stop, Req, State} {stop, Req, State}
end end
end. end.
---- ----
=== Post-upgrade initialization === Post-upgrade initialization
@ -93,8 +93,8 @@ The optional `websocket_init/1` can be used instead:
[source,erlang] [source,erlang]
---- ----
websocket_init(State) -> websocket_init(State) ->
erlang:start_timer(1000, self(), <<"Hello!">>), erlang:start_timer(1000, self(), <<"Hello!">>),
{ok, State}. {ok, State}.
---- ----
All Websocket callbacks share the same return values. This All Websocket callbacks share the same return values. This
@ -104,7 +104,7 @@ the upgrade:
[source,erlang] [source,erlang]
---- ----
websocket_init(State) -> websocket_init(State) ->
{reply, {text, <<"Hello!">>}, State}. {reply, {text, <<"Hello!">>}, State}.
---- ----
=== Receiving frames === Receiving frames
@ -121,9 +121,9 @@ ignores all others:
[source,erlang] [source,erlang]
---- ----
websocket_handle(Frame = {text, _}, State) -> websocket_handle(Frame = {text, _}, State) ->
{reply, Frame, State}; {reply, Frame, State};
websocket_handle(_Frame, State) -> websocket_handle(_Frame, State) ->
{ok, State}. {ok, State}.
---- ----
Note that ping and pong frames require no action from the Note that ping and pong frames require no action from the
@ -144,9 +144,9 @@ and ignores all others:
[source,erlang] [source,erlang]
---- ----
websocket_info({log, Text}, State) -> websocket_info({log, Text}, State) ->
{reply, {text, Text}, State}; {reply, {text, Text}, State};
websocket_info(_Info, State) -> websocket_info(_Info, State) ->
{ok, State}. {ok, State}.
---- ----
=== Sending frames === Sending frames
@ -161,7 +161,7 @@ To send nothing, just return an ok tuple:
[source,erlang] [source,erlang]
---- ----
websocket_info(_Info, State) -> websocket_info(_Info, State) ->
{ok, State}. {ok, State}.
---- ----
To send one frame, return a reply tuple with the frame to send: To send one frame, return a reply tuple with the frame to send:
@ -169,7 +169,7 @@ To send one frame, return a reply tuple with the frame to send:
[source,erlang] [source,erlang]
---- ----
websocket_info(_Info, State) -> websocket_info(_Info, State) ->
{reply, {text, <<"Hello!">>}, State}. {reply, {text, <<"Hello!">>}, State}.
---- ----
You can send frames of any type: text, binary, ping, pong You can send frames of any type: text, binary, ping, pong
@ -181,11 +181,11 @@ list of frames to send:
[source,erlang] [source,erlang]
---- ----
websocket_info(_Info, State) -> websocket_info(_Info, State) ->
{reply, [ {reply, [
{text, "Hello"}, {text, "Hello"},
{text, <<"world!">>}, {text, <<"world!">>},
{binary, <<0:8000>>} {binary, <<0:8000>>}
], State}. ], State}.
---- ----
They are sent in the given order. They are sent in the given order.
@ -215,7 +215,7 @@ close connections idle for more than 60 seconds:
[source,erlang] [source,erlang]
---- ----
init(Req, State) -> init(Req, State) ->
{cowboy_websocket, Req, State, 60000}. {cowboy_websocket, Req, State, 60000}.
---- ----
This value cannot be changed once it is set. It defaults to This value cannot be changed once it is set. It defaults to
@ -233,13 +233,13 @@ Simply add an `hibernate` field to the ok or reply tuples:
[source,erlang] [source,erlang]
---- ----
websocket_init(State) -> websocket_init(State) ->
{ok, State, hibernate}. {ok, State, hibernate}.
websocket_handle(_Frame, State) -> websocket_handle(_Frame, State) ->
{ok, State, hibernate}. {ok, State, hibernate}.
websocket_info(_Info, State) -> websocket_info(_Info, State) ->
{reply, {text, <<"Hello!">>}, State, hibernate}. {reply, {text, <<"Hello!">>}, State, hibernate}.
---- ----
It is highly recommended to write your handlers with It is highly recommended to write your handlers with
@ -258,7 +258,7 @@ To tell Cowboy to close the connection, use a stop tuple:
[source,erlang] [source,erlang]
---- ----
websocket_info(_Info, State) -> websocket_info(_Info, State) ->
{stop, State}. {stop, State}.
---- ----
Sending a `close` frame will immediately initiate the closing Sending a `close` frame will immediately initiate the closing