123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- <?xml version="1.0" encoding="utf-8" ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <!-- This file is generated by Nim. -->
- <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>std/asynchttpserver</title>
- <!-- Google fonts -->
- <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
- <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
- <!-- Favicon -->
- <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
- <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
- <!-- CSS -->
- <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
- <!-- JS -->
- <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
- </head>
- <body>
- <div class="document" id="documentId">
- <div class="container">
- <h1 class="title">std/asynchttpserver</h1>
- <div class="row">
- <div class="three columns">
- <div class="theme-select-wrapper">
- <label for="theme-select">Theme: </label>
- <select id="theme-select" onchange="setTheme(this.value)">
- <option value="auto">🌗 Match OS</option>
- <option value="dark">🌑 Dark</option>
- <option value="light">🌕 Light</option>
- </select>
- </div>
- <div id="global-links">
- <ul class="simple-boot">
- <li><a href="manual.html">Manual</a></li>
- <li><a href="lib.html">Standard library</a></li>
- <li> <a id="indexLink" href="theindex.html">Index</a></li>
- <li><a href="compiler/theindex.html">Compiler docs</a></li>
- <li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
- <li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
- </ul>
- </div>
- <div id="searchInputDiv">
- Search: <input type="search" id="searchInput"
- oninput="search()" />
- </div>
-
- <ul class="simple simple-toc" id="toc-list">
- <li>
- <a class="reference reference-toplevel" href="#6" id="56">Imports</a>
- </li>
- <li>
- <details open>
- <summary><a class="reference reference-toplevel" href="#7" id="57">Types</a></summary>
- <ul class="simple simple-toc-section">
- <li><a class="reference" href="#AsyncHttpServer" title="AsyncHttpServer = ref object
- ## The maximum content-length that will be read for the body.">AsyncHttpServer</a></li>
- <li><a class="reference" href="#Request" title="Request = object
- client*: AsyncSocket
- reqMethod*: HttpMethod
- headers*: HttpHeaders
- protocol*: tuple[orig: string, major, minor: int]
- url*: Uri
- hostname*: string ## The hostname of the client that made the request.
- body*: string">Request</a></li>
- </ul>
- </details>
- </li>
- <li>
- <details open>
- <summary><a class="reference reference-toplevel" href="#10" id="60">Consts</a></summary>
- <ul class="simple simple-toc-section">
- <li><a class="reference" href="#nimMaxDescriptorsFallback" title="nimMaxDescriptorsFallback {.intdefine.} = 16000">nimMaxDescriptorsFallback</a></li>
- </ul>
- </details>
- </li>
- <li>
- <details open>
- <summary><a class="reference reference-toplevel" href="#12" id="62">Procs</a></summary>
- <ul class="simple simple-toc-section">
- <ul class="simple nested-toc-section">acceptRequest
- <li><a class="reference" href="#acceptRequest%2CAsyncHttpServer%2Cproc%28Request%29" title="acceptRequest(server: AsyncHttpServer; callback: proc (request: Request): Future[
- void] {.closure, gcsafe.}): owned(Future[void])">acceptRequest(server: AsyncHttpServer; callback: proc (request: Request): Future[
- void] {.closure, gcsafe.}): owned(Future[void])</a></li>
- </ul>
- <ul class="simple nested-toc-section">close
- <li><a class="reference" href="#close%2CAsyncHttpServer" title="close(server: AsyncHttpServer)">close(server: AsyncHttpServer)</a></li>
- </ul>
- <ul class="simple nested-toc-section">getPort
- <li><a class="reference" href="#getPort%2CAsyncHttpServer" title="getPort(self: AsyncHttpServer): Port">getPort(self: AsyncHttpServer): Port</a></li>
- </ul>
- <ul class="simple nested-toc-section">listen
- <li><a class="reference" href="#listen%2CAsyncHttpServer%2CPort%2Cstring" title="listen(server: AsyncHttpServer; port: Port; address = ""; domain = AF_INET)">listen(server: AsyncHttpServer; port: Port; address = ""; domain = AF_INET)</a></li>
- </ul>
- <ul class="simple nested-toc-section">newAsyncHttpServer
- <li><a class="reference" href="#newAsyncHttpServer%2Cint" title="newAsyncHttpServer(reuseAddr = true; reusePort = false; maxBody = 8388608): AsyncHttpServer">newAsyncHttpServer(reuseAddr = true; reusePort = false; maxBody = 8388608): AsyncHttpServer</a></li>
- </ul>
- <ul class="simple nested-toc-section">respond
- <li><a class="reference" href="#respond%2CRequest%2CHttpCode%2Cstring%2CHttpHeaders" title="respond(req: Request; code: HttpCode; content: string;
- headers: HttpHeaders = nil): Future[void]">respond(req: Request; code: HttpCode; content: string;
- headers: HttpHeaders = nil): Future[void]</a></li>
- </ul>
- <ul class="simple nested-toc-section">sendHeaders
- <li><a class="reference" href="#sendHeaders%2CRequest%2CHttpHeaders" title="sendHeaders(req: Request; headers: HttpHeaders): Future[void]">sendHeaders(req: Request; headers: HttpHeaders): Future[void]</a></li>
- </ul>
- <ul class="simple nested-toc-section">serve
- <li><a class="reference" href="#serve%2CAsyncHttpServer%2CPort%2Cproc%28Request%29%2Cstring%2Cint" title="serve(server: AsyncHttpServer; port: Port;
- callback: proc (request: Request): Future[void] {.closure, gcsafe.};
- address = ""; assumedDescriptorsPerRequest = -1; domain = AF_INET): owned(
- Future[void])">serve(server: AsyncHttpServer; port: Port;
- callback: proc (request: Request): Future[void] {.closure, gcsafe.};
- address = ""; assumedDescriptorsPerRequest = -1; domain = AF_INET): owned(
- Future[void])</a></li>
- </ul>
- <ul class="simple nested-toc-section">shouldAcceptRequest
- <li><a class="reference" href="#shouldAcceptRequest%2CAsyncHttpServer%2Cint" title="shouldAcceptRequest(server: AsyncHttpServer; assumedDescriptorsPerRequest = 5): bool">shouldAcceptRequest(server: AsyncHttpServer; assumedDescriptorsPerRequest = 5): bool</a></li>
- </ul>
- </ul>
- </details>
- </li>
- </ul>
- </div>
- <div class="nine columns" id="content">
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/asynchttpserver.nim#L1" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/asynchttpserver.nim#L1" class="link-seesrc" target="_blank" >Edit</a>
- <div id="tocRoot"></div>
-
- <p class="module-desc"><p>This module implements a high performance asynchronous HTTP server.</p>
- <p>This HTTP server has not been designed to be used in production, but for testing applications locally. Because of this, when deploying your application in production you should use a reverse proxy (for example nginx) instead of allowing users to connect directly to this server.</p>
- <p><strong class="examples_text">Example: cmd: -r:off</strong></p>
- <pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">asynchttpserver</span>
- <span class="Comment"># This example will create an HTTP server on an automatically chosen port.</span>
- <span class="Comment"># It will respond to all requests with a `200 OK` response code and "Hello World"</span>
- <span class="Comment"># as the response body.</span>
- <span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">asyncdispatch</span>
- <span class="Keyword">proc</span> <span class="Identifier">main</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">async</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span>
- <span class="Keyword">var</span> <span class="Identifier">server</span> <span class="Operator">=</span> <span class="Identifier">newAsyncHttpServer</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
- <span class="Keyword">proc</span> <span class="Identifier">cb</span><span class="Punctuation">(</span><span class="Identifier">req</span><span class="Punctuation">:</span> <span class="Identifier">Request</span><span class="Punctuation">)</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">async</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span>
- <span class="Identifier">echo</span> <span class="Punctuation">(</span><span class="Identifier">req</span><span class="Operator">.</span><span class="Identifier">reqMethod</span><span class="Punctuation">,</span> <span class="Identifier">req</span><span class="Operator">.</span><span class="Identifier">url</span><span class="Punctuation">,</span> <span class="Identifier">req</span><span class="Operator">.</span><span class="Identifier">headers</span><span class="Punctuation">)</span>
- <span class="Keyword">let</span> <span class="Identifier">headers</span> <span class="Operator">=</span> <span class="Punctuation">{</span><span class="StringLit">"Content-type"</span><span class="Punctuation">:</span> <span class="StringLit">"text/plain; charset=utf-8"</span><span class="Punctuation">}</span>
- <span class="Identifier">await</span> <span class="Identifier">req</span><span class="Operator">.</span><span class="Identifier">respond</span><span class="Punctuation">(</span><span class="Identifier">Http200</span><span class="Punctuation">,</span> <span class="StringLit">"Hello World"</span><span class="Punctuation">,</span> <span class="Identifier">headers</span><span class="Operator">.</span><span class="Identifier">newHttpHeaders</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
- <span class="Identifier">server</span><span class="Operator">.</span><span class="Identifier">listen</span><span class="Punctuation">(</span><span class="Identifier">Port</span><span class="Punctuation">(</span><span class="DecNumber">0</span><span class="Punctuation">)</span><span class="Punctuation">)</span> <span class="Comment"># or Port(8080) to hardcode the standard HTTP port.</span>
- <span class="Keyword">let</span> <span class="Identifier">port</span> <span class="Operator">=</span> <span class="Identifier">server</span><span class="Operator">.</span><span class="Identifier">getPort</span>
- <span class="Identifier">echo</span> <span class="StringLit">"test this with: curl localhost:"</span> <span class="Operator">&</span> <span class="Operator">$</span><span class="Identifier">port</span><span class="Operator">.</span><span class="Identifier">uint16</span> <span class="Operator">&</span> <span class="StringLit">"/"</span>
- <span class="Keyword">while</span> <span class="Identifier">true</span><span class="Punctuation">:</span>
- <span class="Keyword">if</span> <span class="Identifier">server</span><span class="Operator">.</span><span class="Identifier">shouldAcceptRequest</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
- <span class="Identifier">await</span> <span class="Identifier">server</span><span class="Operator">.</span><span class="Identifier">acceptRequest</span><span class="Punctuation">(</span><span class="Identifier">cb</span><span class="Punctuation">)</span>
- <span class="Keyword">else</span><span class="Punctuation">:</span>
- <span class="Comment"># too many concurrent connections, `maxFDs` exceeded</span>
- <span class="Comment"># wait 500ms for FDs to be closed</span>
- <span class="Identifier">await</span> <span class="Identifier">sleepAsync</span><span class="Punctuation">(</span><span class="DecNumber">500</span><span class="Punctuation">)</span>
- <span class="Identifier">waitFor</span> <span class="Identifier">main</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></p>
- <div class="section" id="6">
- <h1><a class="toc-backref" href="#6">Imports</a></h1>
- <dl class="item">
- <a class="reference external" href="asyncnet.html">asyncnet</a>, <a class="reference external" href="asyncdispatch.html">asyncdispatch</a>, <a class="reference external" href="parseutils.html">parseutils</a>, <a class="reference external" href="uri.html">uri</a>, <a class="reference external" href="strutils.html">strutils</a>, <a class="reference external" href="httpcore.html">httpcore</a>, <a class="reference external" href="nativesockets.html">nativesockets</a>, <a class="reference external" href="since.html">since</a>, <a class="reference external" href="assertions.html">assertions</a>
- </dl>
- </div>
- <div class="section" id="7">
- <h1><a class="toc-backref" href="#7">Types</a></h1>
- <dl class="item">
- <div id="AsyncHttpServer">
- <dt><pre><a href="asynchttpserver.html#AsyncHttpServer"><span class="Identifier">AsyncHttpServer</span></a> <span class="Other">=</span> <span class="Keyword">ref</span> <span class="Keyword">object</span>
- <span class="Comment">## The maximum content-length that will be read for the body.</span></pre></dt>
- <dd>
-
-
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/asynchttpserver.nim#L70" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/asynchttpserver.nim#L70" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- <div id="Request">
- <dt><pre><a href="asynchttpserver.html#Request"><span class="Identifier">Request</span></a> <span class="Other">=</span> <span class="Keyword">object</span>
- <span class="Identifier">client</span><span class="Operator">*</span><span class="Other">:</span> <a href="asyncnet.html#AsyncSocket"><span class="Identifier">AsyncSocket</span></a>
- <span class="Identifier">reqMethod</span><span class="Operator">*</span><span class="Other">:</span> <a href="httpcore.html#HttpMethod"><span class="Identifier">HttpMethod</span></a>
- <span class="Identifier">headers</span><span class="Operator">*</span><span class="Other">:</span> <a href="httpcore.html#HttpHeaders"><span class="Identifier">HttpHeaders</span></a>
- <span class="Identifier">protocol</span><span class="Operator">*</span><span class="Other">:</span> <span class="Keyword">tuple</span><span class="Other">[</span><span class="Identifier">orig</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a><span class="Other">,</span> <span class="Identifier">major</span><span class="Other">,</span> <span class="Identifier">minor</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">]</span>
- <span class="Identifier">url</span><span class="Operator">*</span><span class="Other">:</span> <a href="uri.html#Uri"><span class="Identifier">Uri</span></a>
- <span class="Identifier">hostname</span><span class="Operator">*</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a> <span class="Comment">## The hostname of the client that made the request.</span>
- <span class="Identifier">body</span><span class="Operator">*</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a></pre></dt>
- <dd>
-
-
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/asynchttpserver.nim#L61" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/asynchttpserver.nim#L61" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </dl>
- </div>
- <div class="section" id="10">
- <h1><a class="toc-backref" href="#10">Consts</a></h1>
- <dl class="item">
- <div id="nimMaxDescriptorsFallback">
- <dt><pre><a href="asynchttpserver.html#nimMaxDescriptorsFallback"><span class="Identifier">nimMaxDescriptorsFallback</span></a> {.<span class="Identifier">intdefine</span>.} <span class="Other">=</span> <span class="DecNumber">16000</span></pre></dt>
- <dd>
-
- fallback value for
- when <tt class="docutils literal"><span class="pre"><span class="Identifier">maxDescriptors</span></span></tt> is not available. This can be set on the command line during compilation via <tt class="docutils literal"><span class="pre"><span class="Operator">-</span><span class="Identifier">d</span><span class="Punctuation">:</span><span class="Identifier">nimMaxDescriptorsFallback</span><span class="Operator">=</span><span class="Identifier">N</span></span></tt>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/asynchttpserver.nim#L377" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/asynchttpserver.nim#L377" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </dl>
- </div>
- <div class="section" id="12">
- <h1><a class="toc-backref" href="#12">Procs</a></h1>
- <dl class="item">
- <div id="acceptRequest-procs-all">
- <div id="acceptRequest,AsyncHttpServer,proc(Request)">
- <dt><pre><span class="Keyword">proc</span> <a href="#acceptRequest%2CAsyncHttpServer%2Cproc%28Request%29"><span class="Identifier">acceptRequest</span></a><span class="Other">(</span><span class="Identifier">server</span><span class="Other">:</span> <a href="asynchttpserver.html#AsyncHttpServer"><span class="Identifier">AsyncHttpServer</span></a><span class="Other">;</span> <span class="Identifier">callback</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">request</span><span class="Other">:</span> <a href="asynchttpserver.html#Request"><span class="Identifier">Request</span></a><span class="Other">)</span><span class="Other">:</span> <a href="asyncfutures.html#Future"><span class="Identifier">Future</span></a><span class="Other">[</span>
- <a href="system.html#void"><span class="Identifier">void</span></a><span class="Other">]</span> {.<span class="Identifier">closure</span><span class="Other">,</span> <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">gcsafe</span></span>.}<span class="Other">)</span><span class="Other">:</span> <a href="system.html#owned"><span class="Identifier">owned</span></a><span class="Other">(</span><a href="asyncfutures.html#Future"><span class="Identifier">Future</span></a><span class="Other">[</span><a href="system.html#void"><span class="Identifier">void</span></a><span class="Other">]</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">stackTrace</span><span class="Other">:</span> <span class="DecNumber">false</span><span class="Other">,</span>
- <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Exception</span><span class="Other">,</span> <span class="Identifier">ValueError</span><span class="Other">,</span> <span class="Identifier">OSError</span><span class="Other">,</span> <span class="Identifier">SslError</span><span class="Other">,</span> <span class="Identifier">LibraryError</span><span class="Other">,</span> <span class="Identifier">KeyError</span><span class="Other">]</span><span class="Other">,</span>
- <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- Accepts a single request. Write an explicit loop around this proc so that errors can be handled properly.
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/asynchttpserver.nim#L406" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/asynchttpserver.nim#L406" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="close-procs-all">
- <div id="close,AsyncHttpServer">
- <dt><pre><span class="Keyword">proc</span> <a href="#close%2CAsyncHttpServer"><span class="Identifier">close</span></a><span class="Other">(</span><span class="Identifier">server</span><span class="Other">:</span> <a href="asynchttpserver.html#AsyncHttpServer"><span class="Identifier">AsyncHttpServer</span></a><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">LibraryError</span><span class="Other">,</span> <span class="Identifier">Exception</span><span class="Other">,</span> <span class="Identifier">SslError</span><span class="Other">]</span><span class="Other">,</span>
- <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- Terminates the async http server instance.
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/asynchttpserver.nim#L439" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/asynchttpserver.nim#L439" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="getPort-procs-all">
- <div id="getPort,AsyncHttpServer">
- <dt><pre><span class="Keyword">proc</span> <a href="#getPort%2CAsyncHttpServer"><span class="Identifier">getPort</span></a><span class="Other">(</span><span class="Identifier">self</span><span class="Other">:</span> <a href="asynchttpserver.html#AsyncHttpServer"><span class="Identifier">AsyncHttpServer</span></a><span class="Other">)</span><span class="Other">:</span> <a href="nativesockets.html#Port"><span class="Identifier">Port</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">OSError</span><span class="Other">,</span> <span class="Identifier">Exception</span><span class="Other">]</span><span class="Other">,</span>
- <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Returns the port <tt class="docutils literal"><span class="pre"><span class="Identifier">self</span></span></tt> was bound to.</p>
- <p>Useful for identifying what port <tt class="docutils literal"><span class="pre"><span class="Identifier">self</span></span></tt> is bound to, if it was chosen automatically, for example via <tt class="docutils literal"><span class="pre"><span class="Identifier">listen</span><span class="Punctuation">(</span><span class="Identifier">Port</span><span class="Punctuation">(</span><span class="DecNumber">0</span><span class="Punctuation">)</span><span class="Punctuation">)</span></span></tt>.</p>
- <p><strong class="examples_text">Example:</strong></p>
- <pre class="listing"><span class="Keyword">from</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">nativesockets</span> <span class="Keyword">import</span> <span class="Identifier">Port</span>
- <span class="Keyword">let</span> <span class="Identifier">server</span> <span class="Operator">=</span> <span class="Identifier">newAsyncHttpServer</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
- <span class="Identifier">server</span><span class="Operator">.</span><span class="Identifier">listen</span><span class="Punctuation">(</span><span class="Identifier">Port</span><span class="Punctuation">(</span><span class="DecNumber">0</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
- <span class="Identifier">assert</span> <span class="Identifier">server</span><span class="Operator">.</span><span class="Identifier">getPort</span><span class="Operator">.</span><span class="Identifier">uint16</span> <span class="Operator">></span> <span class="DecNumber">0</span>
- <span class="Identifier">server</span><span class="Operator">.</span><span class="Identifier">close</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/asynchttpserver.nim#L77" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/asynchttpserver.nim#L77" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="listen-procs-all">
- <div id="listen,AsyncHttpServer,Port,string">
- <dt><pre><span class="Keyword">proc</span> <a href="#listen%2CAsyncHttpServer%2CPort%2Cstring"><span class="Identifier">listen</span></a><span class="Other">(</span><span class="Identifier">server</span><span class="Other">:</span> <a href="asynchttpserver.html#AsyncHttpServer"><span class="Identifier">AsyncHttpServer</span></a><span class="Other">;</span> <span class="Identifier">port</span><span class="Other">:</span> <a href="nativesockets.html#Port"><span class="Identifier">Port</span></a><span class="Other">;</span> <span class="Identifier">address</span> <span class="Other">=</span> <span class="StringLit">""</span><span class="Other">;</span> <span class="Identifier">domain</span> <span class="Other">=</span> <span class="Identifier">AF_INET</span><span class="Other">)</span> {.
- <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">OSError</span><span class="Other">,</span> <span class="Identifier">ValueError</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">WriteIOEffect</span><span class="Other">,</span> <span class="Identifier">ReadIOEffect</span><span class="Other">]</span><span class="Other">,</span>
- <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- Listen to the given port and address.
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/asynchttpserver.nim#L382" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/asynchttpserver.nim#L382" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="newAsyncHttpServer-procs-all">
- <div id="newAsyncHttpServer,int">
- <dt><pre><span class="Keyword">proc</span> <a href="#newAsyncHttpServer%2Cint"><span class="Identifier">newAsyncHttpServer</span></a><span class="Other">(</span><span class="Identifier">reuseAddr</span> <span class="Other">=</span> <span class="Identifier">true</span><span class="Other">;</span> <span class="Identifier">reusePort</span> <span class="Other">=</span> <span class="Identifier">false</span><span class="Other">;</span> <span class="Identifier">maxBody</span> <span class="Other">=</span> <span class="DecNumber">8388608</span><span class="Other">)</span><span class="Other">:</span> <a href="asynchttpserver.html#AsyncHttpServer"><span class="Identifier">AsyncHttpServer</span></a> {.
- <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- Creates a new <tt class="docutils literal"><span class="pre"><span class="Identifier">AsyncHttpServer</span></span></tt> instance.
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/asynchttpserver.nim#L90" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/asynchttpserver.nim#L90" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="respond-procs-all">
- <div id="respond,Request,HttpCode,string,HttpHeaders">
- <dt><pre><span class="Keyword">proc</span> <a href="#respond%2CRequest%2CHttpCode%2Cstring%2CHttpHeaders"><span class="Identifier">respond</span></a><span class="Other">(</span><span class="Identifier">req</span><span class="Other">:</span> <a href="asynchttpserver.html#Request"><span class="Identifier">Request</span></a><span class="Other">;</span> <span class="Identifier">code</span><span class="Other">:</span> <a href="httpcore.html#HttpCode"><span class="Identifier">HttpCode</span></a><span class="Other">;</span> <span class="Identifier">content</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a><span class="Other">;</span>
- <span class="Identifier">headers</span><span class="Other">:</span> <a href="httpcore.html#HttpHeaders"><span class="Identifier">HttpHeaders</span></a> <span class="Other">=</span> <span class="Keyword">nil</span><span class="Other">)</span><span class="Other">:</span> <a href="asyncfutures.html#Future"><span class="Identifier">Future</span></a><span class="Other">[</span><a href="system.html#void"><span class="Identifier">void</span></a><span class="Other">]</span> {.
- <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Exception</span><span class="Other">,</span> <span class="Identifier">SslError</span><span class="Other">,</span> <span class="Identifier">ValueError</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Responds to the request with the specified <tt class="docutils literal"><span class="pre"><span class="Identifier">HttpCode</span></span></tt>, headers and content.</p>
- <p>This procedure will <strong>not</strong> close the client socket.</p>
- <p>Example:</p>
- <p><pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">json</span>
- <span class="Keyword">proc</span> <span class="Identifier">handler</span><span class="Punctuation">(</span><span class="Identifier">req</span><span class="Punctuation">:</span> <span class="Identifier">Request</span><span class="Punctuation">)</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">async</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span>
- <span class="Keyword">if</span> <span class="Identifier">req</span><span class="Operator">.</span><span class="Identifier">url</span><span class="Operator">.</span><span class="Identifier">path</span> <span class="Operator">==</span> <span class="StringLit">"/hello-world"</span><span class="Punctuation">:</span>
- <span class="Keyword">let</span> <span class="Identifier">msg</span> <span class="Operator">=</span> <span class="Operator">%*</span> <span class="Punctuation">{</span><span class="StringLit">"message"</span><span class="Punctuation">:</span> <span class="StringLit">"Hello World"</span><span class="Punctuation">}</span>
- <span class="Keyword">let</span> <span class="Identifier">headers</span> <span class="Operator">=</span> <span class="Identifier">newHttpHeaders</span><span class="Punctuation">(</span><span class="Punctuation">[</span><span class="Punctuation">(</span><span class="StringLit">"Content-Type"</span><span class="Punctuation">,</span><span class="StringLit">"application/json"</span><span class="Punctuation">)</span><span class="Punctuation">]</span><span class="Punctuation">)</span>
- <span class="Identifier">await</span> <span class="Identifier">req</span><span class="Operator">.</span><span class="Identifier">respond</span><span class="Punctuation">(</span><span class="Identifier">Http200</span><span class="Punctuation">,</span> <span class="Operator">$</span><span class="Identifier">msg</span><span class="Punctuation">,</span> <span class="Identifier">headers</span><span class="Punctuation">)</span>
- <span class="Keyword">else</span><span class="Punctuation">:</span>
- <span class="Identifier">await</span> <span class="Identifier">req</span><span class="Operator">.</span><span class="Identifier">respond</span><span class="Punctuation">(</span><span class="Identifier">Http404</span><span class="Punctuation">,</span> <span class="StringLit">"Not Found"</span><span class="Punctuation">)</span></pre></p>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/asynchttpserver.nim#L105" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/asynchttpserver.nim#L105" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="sendHeaders-procs-all">
- <div id="sendHeaders,Request,HttpHeaders">
- <dt><pre><span class="Keyword">proc</span> <a href="#sendHeaders%2CRequest%2CHttpHeaders"><span class="Identifier">sendHeaders</span></a><span class="Other">(</span><span class="Identifier">req</span><span class="Other">:</span> <a href="asynchttpserver.html#Request"><span class="Identifier">Request</span></a><span class="Other">;</span> <span class="Identifier">headers</span><span class="Other">:</span> <a href="httpcore.html#HttpHeaders"><span class="Identifier">HttpHeaders</span></a><span class="Other">)</span><span class="Other">:</span> <a href="asyncfutures.html#Future"><span class="Identifier">Future</span></a><span class="Other">[</span><a href="system.html#void"><span class="Identifier">void</span></a><span class="Other">]</span> {.
- <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Exception</span><span class="Other">,</span> <span class="Identifier">SslError</span><span class="Other">,</span> <span class="Identifier">ValueError</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- Sends the specified headers to the requesting client.
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/asynchttpserver.nim#L99" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/asynchttpserver.nim#L99" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="serve-procs-all">
- <div id="serve,AsyncHttpServer,Port,proc(Request),string,int">
- <dt><pre><span class="Keyword">proc</span> <a href="#serve%2CAsyncHttpServer%2CPort%2Cproc%28Request%29%2Cstring%2Cint"><span class="Identifier">serve</span></a><span class="Other">(</span><span class="Identifier">server</span><span class="Other">:</span> <a href="asynchttpserver.html#AsyncHttpServer"><span class="Identifier">AsyncHttpServer</span></a><span class="Other">;</span> <span class="Identifier">port</span><span class="Other">:</span> <a href="nativesockets.html#Port"><span class="Identifier">Port</span></a><span class="Other">;</span>
- <span class="Identifier">callback</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">request</span><span class="Other">:</span> <a href="asynchttpserver.html#Request"><span class="Identifier">Request</span></a><span class="Other">)</span><span class="Other">:</span> <a href="asyncfutures.html#Future"><span class="Identifier">Future</span></a><span class="Other">[</span><a href="system.html#void"><span class="Identifier">void</span></a><span class="Other">]</span> {.<span class="Identifier">closure</span><span class="Other">,</span> <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">gcsafe</span></span>.}<span class="Other">;</span>
- <span class="Identifier">address</span> <span class="Other">=</span> <span class="StringLit">""</span><span class="Other">;</span> <span class="Identifier">assumedDescriptorsPerRequest</span> <span class="Other">=</span> <span class="DecNumber">-1</span><span class="Other">;</span> <span class="Identifier">domain</span> <span class="Other">=</span> <span class="Identifier">AF_INET</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#owned"><span class="Identifier">owned</span></a><span class="Other">(</span>
- <a href="asyncfutures.html#Future"><span class="Identifier">Future</span></a><span class="Other">[</span><a href="system.html#void"><span class="Identifier">void</span></a><span class="Other">]</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">stackTrace</span><span class="Other">:</span> <span class="DecNumber">false</span><span class="Other">,</span> <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Exception</span><span class="Other">,</span> <span class="Identifier">OSError</span><span class="Other">,</span> <span class="Identifier">ValueError</span><span class="Other">,</span>
- <span class="Identifier">SslError</span><span class="Other">,</span> <span class="Identifier">LibraryError</span><span class="Other">,</span> <span class="Identifier">KeyError</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">,</span> <span class="Identifier">WriteIOEffect</span><span class="Other">,</span>
- <span class="Identifier">ReadIOEffect</span><span class="Other">,</span> <span class="Identifier">TimeEffect</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- <p>Starts the process of listening for incoming HTTP connections on the specified address and port.</p>
- <p>When a request is made by a client the specified callback will be called.</p>
- <p>If <tt class="docutils literal"><span class="pre"><span class="Identifier">assumedDescriptorsPerRequest</span></span></tt> is 0 or greater the server cares about the process's maximum file descriptor limit. It then ensures that the process still has the resources for <tt class="docutils literal"><span class="pre"><span class="Identifier">assumedDescriptorsPerRequest</span></span></tt> file descriptors before accepting a connection.</p>
- <p>You should prefer to call <tt class="docutils literal"><span class="pre"><span class="Identifier">acceptRequest</span></span></tt> instead with a custom server loop so that you're in control over the error handling and logging.</p>
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/asynchttpserver.nim#L416" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/asynchttpserver.nim#L416" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- <div id="shouldAcceptRequest-procs-all">
- <div id="shouldAcceptRequest,AsyncHttpServer,int">
- <dt><pre><span class="Keyword">proc</span> <a href="#shouldAcceptRequest%2CAsyncHttpServer%2Cint"><span class="Identifier">shouldAcceptRequest</span></a><span class="Other">(</span><span class="Identifier">server</span><span class="Other">:</span> <a href="asynchttpserver.html#AsyncHttpServer"><span class="Identifier">AsyncHttpServer</span></a><span class="Other">;</span>
- <span class="Identifier">assumedDescriptorsPerRequest</span> <span class="Other">=</span> <span class="DecNumber">5</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a> {.<span class="Identifier">inline</span><span class="Other">,</span>
- <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
- <dd>
-
- Returns true if the process's current number of opened file descriptors is still within the maximum limit and so it's reasonable to accept yet another request.
- <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/asynchttpserver.nim#L397" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/asynchttpserver.nim#L397" class="link-seesrc" target="_blank" >Edit</a>
- </dd>
- </div>
- </div>
- </dl>
- </div>
- </div>
- </div>
- <div class="twelve-columns footer">
- <span class="nim-sprite"></span>
- <br>
- <small style="color: var(--hint);">Made with Nim. Generated: 2025-04-03 14:18:24 UTC</small>
- </div>
- </div>
- </div>
- <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
-
- </body>
- </html>
|