diff options
Diffstat (limited to 'ref/api/http.html')
| -rw-r--r-- | ref/api/http.html | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/ref/api/http.html b/ref/api/http.html index 325f793b..45f469f4 100644 --- a/ref/api/http.html +++ b/ref/api/http.html @@ -732,12 +732,19 @@ request interleaving.</p> <p>This function returns immediately, with no return value. Completion of the operation is signaled via the <em>aio</em>, and the final result may be obtained via <a href="../api/aio.html#result-of-operation"><code>nng_aio_result</code></a>.</p> +<h3 id="socket-addresses"><a class="header" href="#socket-addresses">Socket Addresses</a></h3> +<pre><code class="language-c">nng_err nng_http_local_address(nng_http *conn, nng_sockaddr *addr); +nng_err nng_http_remote_address(nng_http *conn, nng_sockaddr *addr); +</code></pre> +<p>The <a name="a040"></a><code>nng_http_local_address</code> and <a name="a041"></a><code>nng_http_remote_address</code> functions +can be used to determine the local and remote addresses for an HTTP connection. +This can only be done while the connection is alive.</p> <h3 id="response-body"><a class="header" href="#response-body">Response Body</a></h3> <h2 id="server-api"><a class="header" href="#server-api">Server API</a></h2> <h3 id="handlers"><a class="header" href="#handlers">Handlers</a></h3> <pre><code class="language-c">typedef struct nng_http_handler nng_http_handler; </code></pre> -<p>An <a name="a040"></a><code>nng_http_handler</code> encapsulates a function used used to handle +<p>An <a name="a042"></a><code>nng_http_handler</code> encapsulates a function used used to handle incoming requests on an HTTP server, routed based on method and URI, and the parameters used with that function.</p> <p>Every handler has a Request-URI to which it refers, which is determined by the <em>path</em> argument. @@ -764,7 +771,7 @@ rather than just a single element. nng_err nng_http_handler_alloc(nng_http_handler **hp, const char *path, nng_http_handler_func cb); </code></pre> -<p>The <a name="a041"></a><code>nng_http_handler_alloc</code> function allocates a generic handler +<p>The <a name="a043"></a><code>nng_http_handler_alloc</code> function allocates a generic handler which will be used to process requests coming into an HTTP server. On success, a pointer to the handler is stored at the located pointed to by <em>hp</em>.</p> <p>The handler function is specified by <em>cb</em>. @@ -798,7 +805,7 @@ sent, if possible, and the connection will be closed.</p> <pre><code class="language-c">nng_err nng_http_handler_alloc_directory(nng_http_handler **hp, const char *path, const char *dirname); nng_err nng_http_handler_alloc_file(nng_http_handler **hp, const char *path, const char *filename); </code></pre> -<p>The <a name="a042"></a><code>nng_http_handler_alloc_directory</code> and <a name="a043"></a><code>nng_http_handler_alloc_file</code> +<p>The <a name="a044"></a><code>nng_http_handler_alloc_directory</code> and <a name="a045"></a><code>nng_http_handler_alloc_file</code> create handlers pre-configured to act as static content servers for either a full directory at <em>dirname</em>, or the single file at <em>filename</em>. These support the “GET” and “HEAD” methods, and the directory variant will dynamically generate <code>index.html</code> content based on @@ -809,7 +816,7 @@ determined, the content type is set to “application/octet-stream”.</p> <pre><code class="language-c">nng_err nng_http_handler_alloc_static(nng_http_handler **hp, const char *path, const void *data, size_t size, const char *content_type); </code></pre> -<p>The <a name="a044"></a><code>nng_http_handler_alloc_static</code> function creates a handler that +<p>The <a name="a046"></a><code>nng_http_handler_alloc_static</code> function creates a handler that serves the content located in <em>data</em> (consisting of <em>size</em> bytes) at the URI <em>path</em>. The <em>content_type</em> determines the “Content-Type” header. If <code>NULL</code> is specified then a value of <code>application/octet-stream</code> is assumed.</p> @@ -817,7 +824,7 @@ then a value of <code>application/octet-stream</code> is assumed.</p> <pre><code class="language-c">nng_err nng_http_handler_alloc_redirect(nng_http_handler **hp, const char *path, nng_http_status status, const char *location); </code></pre> -<p>The <a name="a045"></a><code>nng_http_handler_alloc_redirect</code> function creates a handler with +<p>The <a name="a047"></a><code>nng_http_handler_alloc_redirect</code> function creates a handler with a function that simply directions from the URI at <em>path</em> to the given <em>location</em>.</p> <p>The HTTP reply it creates will be with <a href="../api/http.html#http-status">status code</a> <em>status</em>, which should be a 3XX code such as 301, and a <code>Location:</code> header will contain the URL @@ -847,7 +854,7 @@ created with <code>POST</code> should use <a href="../api/http.html#NNG_HTTP_STA <h3 id="collecting-request-body"><a class="header" href="#collecting-request-body">Collecting Request Body</a></h3> <pre><code class="language-c">void nng_http_handler_collect_body(nng_http_handler *handler, bool want, size_t maxsz); </code></pre> -<p>The <a name="a046"></a><code>nng_http_handler_collect_body</code> function requests that HTTP server +<p>The <a name="a048"></a><code>nng_http_handler_collect_body</code> function requests that HTTP server framework collect any request body for the request and attach it to the connection before calling the callback for the <em>handler</em>.</p> <p>Subsequently the data can be retrieved by the handler from the request with the @@ -891,7 +898,7 @@ create few limitations.</p> <pre><code class="language-c">void nng_http_handler_set_data(nng_http_handler *handler, void *data, void (*dtor)(void *)); </code></pre> -<p>The <a name="a047"></a><code>nng_http_handler_set_data</code> function is used to set the +<p>The <a name="a049"></a><code>nng_http_handler_set_data</code> function is used to set the <em>data</em> argument that will be passed to the callback.</p> <p>Additionally, when the handler is deallocated, if <em>dtor</em> is not <code>NULL</code>, then it will be called with <em>data</em> as its argument. @@ -899,7 +906,7 @@ The intended use of this function is deallocate any resources associated with <e <h3 id="setting-the-method"><a class="header" href="#setting-the-method">Setting the Method</a></h3> <pre><code class="language-c">void nng_http_handler_set_method(nng_http_handler *handler, const char *method); </code></pre> -<p>The <a name="a048"></a><code>nng_http_handler_set_method</code> function sets the <em>method</em> that the +<p>The <a name="a050"></a><code>nng_http_handler_set_method</code> function sets the <em>method</em> that the <em>handler</em> will be called for, such as “GET” or “POST”. (By default the “GET” method is handled.)</p> <p>If <em>method</em> is <code>NULL</code> the handler will be executed for all methods. @@ -917,7 +924,7 @@ sent for such requests.</p> <h3 id="filtering-by-host"><a class="header" href="#filtering-by-host">Filtering by Host</a></h3> <pre><code class="language-c">void nng_http_handler_set_host(nng_http_handler *handler, const char *host); </code></pre> -<p>The <a name="a049"></a><code>nng_http_handler_set_host</code> function is used to limit the scope of the +<p>The <a name="a051"></a><code>nng_http_handler_set_host</code> function is used to limit the scope of the <em>handler</em> so that it will only be called when the specified <em>host</em> matches the value of the <code>Host:</code> HTTP header.</p> <p>This can be used to create servers with different content for different virtual hosts.</p> @@ -932,10 +939,15 @@ exactly the value of the <code>Host</code> header sent by the client. <p>The port number may be ignored; at present the HTTP server framework does not support a single server listening on different ports concurrently.</p> </div> +<h3 id="detecting-addresses"><a class="header" href="#detecting-addresses">Detecting Addresses</a></h3> +<p>The <a href="../api/http.html#socket-addresses"><code>nng_http_local_address</code></a> and <a href="../api/http.html#socket-addresses"><code>nng_http_remote_address</code></a> functions +can be used to determine the local and remote addresses for an HTTP connection +on the server side (in a handler) just like the can be for HTTP clients +This can be useful to provide different handling behaviors based on network identity.</p> <h3 id="handling-an-entire-tree"><a class="header" href="#handling-an-entire-tree">Handling an Entire Tree</a></h3> <pre><code class="language-c">void nng_http_handler_set_tree(nng_http_handler *handler); </code></pre> -<p>The <a name="a050"></a><code>nng_http_handler_set_tree</code> function causes the <em>handler</em> to be matched if the request URI sent +<p>The <a name="a052"></a><code>nng_http_handler_set_tree</code> function causes the <em>handler</em> to be matched if the request URI sent by the client is a logical child of the path for <em>handler</em>, and no more specific <em>handler</em> has been registered.</p> <p>This is useful in cases when the handler would like to examine the entire path @@ -955,7 +967,7 @@ It can also provide a logical fallback instead of relying on a 404 error code.</ </code></pre> <p>Normally the server will send any attached response, but there are circumstances where a response must be sent manually, such as when <a href="../api/http.html#hijacking-connections">hijacking</a> a connection.</p> -<p>In such a case, <a name="a051"></a><code>nng_http_write_response</code> can be called, which will send the response and any attached data, asynchronously +<p>In such a case, <a name="a053"></a><code>nng_http_write_response</code> can be called, which will send the response and any attached data, asynchronously using the <a href="../api/aio.html#asynchronous-io-handle"><code>nng_aio</code></a> <em>aio</em>.</p> <p>By default, for <code>HTTP/1.1</code> connections, the connection is kept open, and will be reused to receive new requests. For <code>HTTP/1.0</code>, or if the client has requested |
