0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 20:30:23 +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]
----
fun (Value0) when is_binary(Value0) ->
try binary_to_integer(Value0) of
Value -> {true, Value}
catch _:_ ->
false
end.
try binary_to_integer(Value0) of
Value -> {true, Value}
catch _:_ ->
false
end.
----
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(),
Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID,
#{max_age => 3600}, Req0).
#{max_age => 3600}, Req0).
----
To delete cookies, set `max_age` to 0:
@ -59,7 +59,7 @@ To delete cookies, set `max_age` to 0:
----
SessionID = generate_session_id(),
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
@ -68,7 +68,7 @@ of the same name can be used:
[source,erlang]
----
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
@ -82,7 +82,7 @@ available over HTTPS):
----
SessionID = generate_session_id(),
Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID,
#{secure => true}, Req0).
#{secure => true}, Req0).
----
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(),
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

View file

@ -91,14 +91,14 @@ code to the `start/2` function to make it look like this:
[source,erlang]
----
start(_Type, _Args) ->
Dispatch = cowboy_router:compile([
{'_', [{"/", hello_handler, []}]}
]),
{ok, _} = cowboy:start_clear(my_http_listener, 100,
[{port, 8080}],
#{env => #{dispatch => Dispatch}}
),
hello_erlang_sup:start_link().
Dispatch = cowboy_router:compile([
{'_', [{"/", hello_handler, []}]}
]),
{ok, _} = cowboy:start_clear(my_http_listener, 100,
[{port, 8080}],
#{env => #{dispatch => Dispatch}}
),
hello_erlang_sup:start_link().
----
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]
----
init(Req0, State) ->
Req = cowboy_req:reply(200,
#{<<"content-type">> => <<"text/plain">>},
<<"Hello Erlang!">>,
Req0),
{ok, Req, State}.
Req = cowboy_req:reply(200,
#{<<"content-type">> => <<"text/plain">>},
<<"Hello Erlang!">>,
Req0),
{ok, Req, State}.
----
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]
----
init(Req, State) ->
{cowboy_websocket, Req, State}.
{cowboy_websocket, Req, State}.
----
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]
----
init(Req, State) ->
{my_handler_type, Req, State}.
{my_handler_type, Req, State}.
----
How to implement a custom handler type is described in the

View file

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

View file

@ -32,15 +32,15 @@ otherwise.
[source,erlang]
----
init(Req0=#{method := <<"GET">>}, State) ->
Req = cowboy_req:reply(200, #{
<<"content-type">> => <<"text/plain">>
}, <<"Hello world!">>, Req0),
{ok, Req, State};
Req = cowboy_req:reply(200, #{
<<"content-type">> => <<"text/plain">>
}, <<"Hello world!">>, Req0),
{ok, Req, State};
init(Req0, State) ->
Req = cowboy_req:reply(405, #{
<<"allow">> => <<"GET">>
}, Req0),
{ok, Req, State}.
Req = cowboy_req:reply(405, #{
<<"allow">> => <<"GET">>
}, Req0),
{ok, Req, State}.
----
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]
----
#{
scheme := Scheme,
host := Host,
port := Port,
path := Path,
qs := Qs
scheme := Scheme,
host := Host,
port := Port,
path := Path,
qs := Qs
} = Req.
----
@ -348,7 +348,7 @@ directly:
[source,erlang]
----
ParsedVal = cowboy_req:parse_header(<<"content-type">>, Req,
{<<"text">>, <<"plain">>, []}).
{<<"text">>, <<"plain">>, []}).
----
=== Peer

View file

@ -71,7 +71,7 @@ only up to 1MB for up to 5 seconds:
[source,erlang]
----
{ok, Data, Req} = cowboy_req:read_body(Req0,
#{length => 1000000, period => 5000}).
#{length => 1000000, period => 5000}).
----
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]
----
{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, #{
<<"content-type">> => <<"text/html">>
}, ["<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
@ -247,7 +247,7 @@ To send a file while replying:
[source,erlang]
----
Req = cowboy_req:reply(200, #{
<<"content-type">> => "image/png"
<<"content-type">> => "image/png"
}, {sendfile, 0, 12345, "path/to/logo.png"}, Req0).
----
@ -289,13 +289,13 @@ in the response:
[source,erlang]
----
cowboy_req:push("/static/style.css", #{
<<"accept">> => <<"text/css">>
<<"accept">> => <<"text/css">>
}, Req0),
Req = cowboy_req:reply(200, #{
<<"content-type">> => <<"text/html">>
<<"content-type">> => <<"text/html">>
}, ["<html><head><title>My web page</title>",
"<link rel='stylesheet' type='text/css' href='/static/style.css'>",
"<body><p>Welcome to Erlang!</p></body></html>"], Req0).
"<link rel='stylesheet' type='text/css' href='/static/style.css'>",
"<body><p>Welcome to Erlang!</p></body></html>"], Req0).
----
To override the method, scheme, host, port or query string,
@ -305,7 +305,7 @@ uses a different host name:
[source,erlang]
----
cowboy_req:push("/static/style.css", #{
<<"accept">> => <<"text/css">>
<<"accept">> => <<"text/css">>
}, #{host => <<"cdn.example.org">>}, Req),
----

View file

@ -17,7 +17,7 @@ is handled by the sub protocol.
[source,erlang]
----
init(Req, State) ->
{cowboy_websocket, Req, State}.
{cowboy_websocket, Req, State}.
----
The return value may also have a `Timeout` value and/or the
@ -34,7 +34,7 @@ hibernation:
[source,erlang]
----
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
@ -59,7 +59,7 @@ timeout and hibernate values.
[source,erlang]
----
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

View file

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