diff options
Diffstat (limited to '_build/static/docs/en/cowboy/1.0/guide/resp/index.html')
-rw-r--r-- | _build/static/docs/en/cowboy/1.0/guide/resp/index.html | 327 |
1 files changed, 327 insertions, 0 deletions
diff --git a/_build/static/docs/en/cowboy/1.0/guide/resp/index.html b/_build/static/docs/en/cowboy/1.0/guide/resp/index.html new file mode 100644 index 00000000..d61be41b --- /dev/null +++ b/_build/static/docs/en/cowboy/1.0/guide/resp/index.html @@ -0,0 +1,327 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <title>Nine Nines Support: Cowboy User Guide</title> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <!-- Change them or set them up as you like --> + <meta name="description" content=""> + <meta name="author" content="(Soft10) Pol Cámara"> + + <!-- Stylesheets --> + <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700,400italic' rel='stylesheet' type='text/css'> + <link href="/css/bootstrap.min.css" rel="stylesheet"> + <link href="/css/99s.css" rel="stylesheet"> +<!-- <link href="js/google-code-prettify/prettify.css" rel="stylesheet"> --> + <link href="/css/sh99s.css" rel="stylesheet"/> + + <!-- Enables html5 support on older browsers, other js is placed at the end of the page to speed up loading --> + <!--[if lt IE 9]> + <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> + <![endif]--> + + <link rel="shortcut icon" href="/img/ico/favicon.ico"> + <link rel="apple-touch-icon-precomposed" sizes="114x114" href="/img/ico/apple-touch-icon-114.png"> + <link rel="apple-touch-icon-precomposed" sizes="72x72" href="/img/ico/apple-touch-icon-72.png"> + <link rel="apple-touch-icon-precomposed" href="/img/ico/apple-touch-icon-57.png"> + <link rel="alternate" href="/feeds/atom.xml" type="application/atom+xml" title="Nine Nines Atom Feed"> + </head> + + <body class="big_text docs"> + <header id="page-head"> + <div id="topbar" class="container"> + <div class="row"> + <div class="span2"> + <h1 id="logo"><a href="/" title="99s">99s</a></h1> + </div> + <div class="span10"> + <!-- Top navigation and social icons--> + <div id="side-header"> + <nav> + <ul> + <li><a title="Erlang training" href="/training">Training</a></li> + <li><a title="Technical publications" href="/articles">Articles</a></li> + <li><a title="Our talks" href="/talks">Talks</a></li> + <li class="active"><a title="Our services" href="/support">Pricing & Sponsoring</a></li> + <li><a title="Community support" href="http://lists.ninenines.eu">Mailing Lists</a></li> + <li><a title="Contact us" href="mailto:[email protected]">Contact</a></li> + </ul> + </nav> + <ul id="social"> + <li> + <a href="https://github.com/ninenines" title="Check our Github repositories"><img src="/img/ico_github.png" data-hover="/img/ico_github_alt.png" alt="Github"></a> + </li> + <li class="dropdown" id="twitter-links"> + <a href="#twitter-links" class="dropdown-toggle" data-toggle="dropdown" title="Follow us on Twitter"> + <img src="/img/ico_twitter.png" data-hover="/img/ico_twitter_alt.png" alt="Twitter"> + </a> + <ul class="dropdown-menu"> + <li><a title="Visit Loïc Hoguin's Twitter Account" href="http://twitter.com/lhoguin">@lhoguin</a></li> + <!-- <li class="divider"></li> + <li><a title="Visit our official Twitter account" href="#">@99s</a></li> --> + </ul> + </li> + <!-- <li> + <a href="/css/" title="Add us on Linkedin"><img src="/img/ico_linkedin.png" data-hover="img/ico_linkedin_alt.png" alt="Linkedin"></a> + </li> --> + </ul> + </div> + </div> + </div> + </div> + + + </header> + + +<div id="contents" class="two_col"> +<div class="container"> +<div class="row"> +<div id="docs" class="span9 maincol"> + +<h1 class="lined-header"><span>Sending a response</span></h1> + +<p>The Req object also allows you to send a response.</p> + +<p>You can only send one response. Any other attempt will trigger a crash. The response may be sent in one go or with its body streamed by chunks of arbitrary size.</p> + +<p>You can also set headers or the response body in advance and Cowboy will use them when you finally do reply.</p> + +<h2 id="reply">Reply</h2> + +<p>You can send a reply with no particular headers or body. Cowboy will make sure to send the mandatory headers with the response.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +{ok, Req2} = cowboy_req:reply(200, Req). +]]></script> + +<p>You can define headers to be sent with the response. Note that header names must be lowercase. Again, Cowboy will make sure to send the mandatory headers with the response.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +{ok, Req2} = cowboy_req:reply(303, [ + {<<"location">>, <<"http://ninenines.eu">>} +], Req). +]]></script> + +<p>You can override headers that Cowboy would send otherwise. Any header set by the user will be used over the ones set by Cowboy. For example, you can advertise yourself as a different server.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +{ok, Req2} = cowboy_req:reply(200, [ + {<<"server">>, <<"yaws">>} +], Req). +]]></script> + +<p>We also saw earlier how to force close the connection by overriding the connection header.</p> + +<p>Finally, you can also send a body with the response. Cowboy will automatically set the content-length header if you do. We recommend that you set the content-type header so the client may know how to read the body.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +{ok, Req2} = cowboy_req:reply(200, [ + {<<"content-type">>, <<"text/plain">>} +], "Hello world!", Req). +]]></script> + +<p>Here is the same example but sending HTML this time.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +{ok, Req2} = cowboy_req:reply(200, [ + {<<"content-type">>, <<"text/html">>} +], "<html><head>Hello world!</head><body><p>Hats off!</p></body></html>", Req). +]]></script> + +<p>Note that the reply is sent immediately.</p> + +<h2 id="chunked_reply">Chunked reply</h2> + +<p>You can also stream the response body. First, you need to initiate the reply by sending the response status code. Then you can send the body in chunks of arbitrary size.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +{ok, Req2} = cowboy_req:chunked_reply(200, Req), +ok = cowboy_req:chunk("Hello...", Req2), +ok = cowboy_req:chunk("chunked...", Req2), +ok = cowboy_req:chunk("world!!", Req2). +]]></script> + +<p>You should make sure to match on <code>ok</code> as an error may be returned.</p> + +<p>While it is possible to send a chunked response without a content-type header, it is still recommended. You can set this header or any other just like for normal replies.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +{ok, Req2} = cowboy_req:chunked_reply(200, [ + {<<"content-type">>, <<"text/html">>} +], Req), +ok = cowboy_req:chunk("<html><head>Hello world!</head>", Req2), +ok = cowboy_req:chunk("<body><p>Hats off!</p></body></html>", Req2). +]]></script> + +<p>Note that the reply and each chunk following it are sent immediately.</p> + +<h2 id="preset_response_headers">Preset response headers</h2> + +<p>You can define response headers in advance. They will be merged into the headers given in the reply call. Headers in the reply call override preset response headers which override the default Cowboy headers.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +Req2 = cowboy_req:set_resp_header(<<"allow">>, "GET", Req). +]]></script> + +<p>You can check if a response header has already been set. This will only check the response headers that you set, and not the ones Cowboy will add when actually sending the reply.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +cowboy_req:has_resp_header(<<"allow">>, Req). +]]></script> + +<p>It will return <code>true</code> if the header is defined, and <code>false</code> otherwise.</p> + +<p>Finally, you can also delete a preset response header if needed. If you do, it will not be sent.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +Req2 = cowboy_req:delete_resp_header(<<"allow">>, Req). +]]></script> + +<h2 id="preset_response_body">Preset response body</h2> + +<p>You can set the response body in advance. Note that this body will be ignored if you then choose to send a chunked reply, or if you send a reply with an explicit body.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +Req2 = cowboy_req:set_resp_body("Hello world!", Req). +]]></script> + +<p>You can also set a fun that will be called when it is time to send the body. There are three different ways of doing that.</p> + +<p>If you know the length of the body that needs to be sent, you should specify it, as it will help clients determine the remaining download time and allow them to inform the user.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +F = fun (Socket, Transport) -> + Transport:send(Socket, "Hello world!") +end, +Req2 = cowboy_req:set_resp_body_fun(12, F, Req). +]]></script> + +<p>If you do not know the length of the body, you should use a chunked response body fun instead.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +F = fun (SendChunk) -> + Body = lists:duplicate(random:uniform(1024, $a)), + SendChunk(Body) +end, +Req2 = cowboy_req:set_resp_body_fun(chunked, F, Req). +]]></script> + +<p>Finally, you can also send data on the socket directly, without knowing the length in advance. Cowboy may be forced to close the connection at the end of the response though depending on the protocol capabilities.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +F = fun (Socket, Transport) -> + Body = lists:duplicate(random:uniform(1024, $a)), + Transport:send(Socket, Body) +end, +Req2 = cowboy_req:set_resp_body_fun(F, Req). +]]></script> + +<h2 id="sending_files">Sending files</h2> + +<p>You can send files directly from disk without having to read them. Cowboy will use the <code>sendfile</code> syscall when possible, which means that the file is sent to the socket directly from the kernel, which is a lot more performant than doing it from userland.</p> + +<p>Again, it is recommended to set the size of the file if it can be known in advance.</p> + +<script type="syntaxhighlighter" class="brush: erlang"><![CDATA[ +F = fun (Socket, Transport) -> + Transport:sendfile(Socket, "priv/styles.css") +end, +Req2 = cowboy_req:set_resp_body_fun(FileSize, F, Req). +]]></script> + +<p>Please see the Ranch guide for more information about sending files.</p> + + +<!-- a.code --> +</div> + +<div class="span3 sidecol"> +<div class="input-append"> +<form id="form-search" class="form-search" action="#"> + <input id="input-search" type="text" placeholder="Function search" autocomplete="off" autofocus class="input-medium search-query span2"> + <button type="submit" class="btn btn-success">Go</button> +</form> +</div> + +<h3 id="docs-nav">Navigation</h3> + +<h3>See also</h3><ul><li><a href="/docs/en/cowboy/1.0/manual/">Function Reference</a></li><li><a href="/docs/en/cowboy/1.0/index.html">README</a></li></ul> + +<h3>Version select</h3> +<ul> + + <li><a href="/docs/en/cowboy/1.0/guide/"><strong>1.0</strong></a></li> + + <li><a href="/docs/en/cowboy/HEAD/guide/"><strong>HEAD</strong></a></li> + +</ul> + +</div> +</div> +</div> +</div> + + + <footer> + <div class="container"> + <div class="row"> + <div class="span6"> + <p id="scroll-top"><a href="#">↑ Scroll to top</a></p> + <nav> + <ul> + <li><a href="mailto:[email protected]" title="Contact us">Contact us</a></li><li><a href="https://github.com/ninenines/ninenines.github.io" title="Github repository">Contribute to this site</a></li> + </ul> + </nav> + </div> + <div class="span6 credits"> + <p><img src="/img/footer_logo.png"></p> + <p>Copyright © Nine Nines 2012-2014</p> + </div> + </div> + </div> + </footer> + + <!-- Javascript --> + <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> + <script src="/js/bootstrap-carousel.js"></script> + <script src="/js/bootstrap-dropdown.js"></script> + <script src="/js/custom.js"></script> + + +<script type="text/javascript" src="/js/shCore.js"></script> +<script type="text/javascript" src="/js/shlang/shBrushBash.js"></script> +<script type="text/javascript" src="/js/shlang/shBrushErlang.js"></script> +<script type="text/javascript" src="/js/shlang/shBrushJScript.js"></script> +<script type="text/javascript" src="/js/shlang/shBrushPlain.js"></script> +<script type="text/javascript">SyntaxHighlighter.all();</script> + +<script type="text/javascript" src="/js/fuse.min.js"></script> +<script type="text/javascript"> +$(document).ready(function(){ + var f; + + $.getJSON("/docs/db.json", function(data){ + f = new Fuse(data, {keys: ["n"], threshold: 0.3}); + $("<ul id=\"search-results\">").insertAfter("#form-search"); + }); + + $("#input-search").keyup(function(e){if(f){if (e.which != 13 ){ + var results = f.search($(this).val()); + if (results.length == 0){ + $("#form-search").attr("action", "#"); + }else{ + $("#form-search").attr("action", results[0].l); + } + + $("#search-results").empty(); + for (var i = 0; i < 10 && i < results.length; i++){ + $("<li><a href=\"" + results[i].l + "\">" + results[i].n + "</a></li>") + .appendTo("#search-results"); + } + }}}); +}); +</script> + + </body> +</html> |