From 6d74a90b72c80edbd58d8b2b29105e749bdfc28e Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Fri, 17 Jan 2025 05:49:14 -0800 Subject: [PATCH 1/2] http: handler API clean ups --- docs/man/nng_http_handler_free.3http.adoc | 47 ------- docs/ref/api/http.md | 157 ++++++++++++---------- docs/ref/xref.md | 5 + include/nng/http.h | 13 +- src/supplemental/http/http_api.h | 72 +++++----- src/supplemental/http/http_public.c | 22 +-- src/supplemental/http/http_server.c | 127 +++++++++-------- src/supplemental/http/http_server_test.c | 48 +++---- src/supplemental/websocket/websocket.c | 18 +-- 9 files changed, 234 insertions(+), 275 deletions(-) delete mode 100644 docs/man/nng_http_handler_free.3http.adoc diff --git a/docs/man/nng_http_handler_free.3http.adoc b/docs/man/nng_http_handler_free.3http.adoc deleted file mode 100644 index 0e1a5b1c1..000000000 --- a/docs/man/nng_http_handler_free.3http.adoc +++ /dev/null @@ -1,47 +0,0 @@ -= nng_http_handler_free(3http) -// -// Copyright 2018 Staysail Systems, Inc. -// Copyright 2018 Capitar IT Group BV -// -// This document is supplied under the terms of the MIT License, a -// copy of which should be located in the distribution where this -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - -== NAME - -nng_http_handler_free - free HTTP server handler - -== SYNOPSIS - -[source, c] ----- -#include -#include - -void nng_http_handler_free(nng_http_handler *h); ----- - -== DESCRIPTION - -The `nng_http_handler_free()` function frees an allocated HTTP server handler. - -IMPORTANT: It is an error to free a handler that is registered with a server. -Any handlers that are registered with servers are automatically freed -when the server itself is deallocated. - -== RETURN VALUES - -None. - -== ERRORS - -None. - -== SEE ALSO - -[.text-left] -xref:nng_http_handler_alloc.3http.adoc[nng_http_handler_alloc(3http)], -xref:nng_http_server_add_handler.3http.adoc[nng_http_server_add_handler(3http)], -xref:nng.7.adoc[nng(7)] diff --git a/docs/ref/api/http.md b/docs/ref/api/http.md index 8cb1945e4..bec22f1be 100644 --- a/docs/ref/api/http.md +++ b/docs/ref/api/http.md @@ -121,68 +121,68 @@ then a built in reason based on the _status_ will be used instead. Status codes are defined by the IETF. Here are defininitions that NNG provides for convenience: -| Name | Code | Reason Text | Notes | -| ------------------------------------------------------------------------------------------------ | ---- | ------------------------------- | ----------------------------------------------------- | -| `NNG_HTTP_STATUS_CONTINUE` | 100 | Continue | Partial transfer, client may send body. | -| `NNG_HTTP_STATUS_SWITCHING` | 101 | Switching Protocols | Used when upgrading or hijacking a connection. | -| `NNG_HTTP_STATUS_PROCESSING` | 102 | Processing | -| `NNG_HTTP_STATUS_OK` | 200 | OK | Successful result. | -| `NNG_HTTP_STATUS_CREATED` | 201 | Created | Resource created successfully. | -| `NNG_HTTP_STATUS_ACCEPTED` | 202 | Created | Request accepted for future processing. | -| `NNG_HTTP_STATUS_NOT_AUTHORITATIVE` | 203 | Not Authoritative | Request successful, but modified by proxy. | -| `NNG_HTTP_STATUS_NO_CONTENT` | 204 | No Content | Request successful, no content returned. | -| `NNG_HTTP_STATUS_RESET_CONTENT` | 205 | Reset Content | Request successful, client should reload content. | -| `NNG_HTTP_STATUS_PARTIAL_CONTENT` | 206 | Partial Content | Response to a range request. | -| `NNG_HTTP_STATUS_MULTI_STATUS` | 207 | Multi-Status | Used with WebDAV. | -| `NNG_HTTP_STATUS_ALREADY_REPORTED` | 208 | Already Reported | Used with WebDAV. | -| `NNG_HTTP_STATUS_IM_USED` | 226 | IM Used | Used with delta encodings, rarely supported. | -| `NNG_HTTP_STATUS_MULTIPLE_CHOICES` | 300 | Multiple Choices | Multiple responses possible, client should choose. | -| `NNG_HTTP_STATUS_MOVED_PERMANENTLY` | 301 | Moved Permanently | Permanent redirection, may be saved by client. | -| `NNG_HTTP_STATUS_FOUND` | 302 | Found | Temporary redirection, client may switch to GET. | -| `NNG_HTTP_STATUS_SEE_OTHER` | 303 | See Other | Redirect, perhaps after a success POST or PUT. | -| `NNG_HTTP_STATUS_NOT_MODIFIED` | 304 | Not Modified | Resource not modified, client may use cached version. | -| `NNG_HTTP_STATUS_USE_PROXY` | 305 | Use Proxy | -| `NNG_HTTP_STATUS_TEMPORARY_REDIRECT` | 307 | Temporary Redirect | Temporary redirect, preserves method. | -| `NNG_HTTP_STATUS_PERMANENT_REDIRECT` | 308 | Permanent Redirect | Permanent redirect. | -| `NNG_HTTP_STATUS_BAD_REQUEST` | 400 | Bad Request | Generic problem with the request. | -| `NNG_HTTP_STATUS_UNAUTHORIZED` | 401 | Unauthorized | Indicates a problem with authentication. | -| `NNG_HTTP_STATUS_PAYMENT_REQUIRED` | 402 | Payment Required | -| `NNG_HTTP_STATUS_FORBIDDEN` | 403 | Forbidden | No permission to access resource. | -| `NNG_HTTP_STATUS_NOT_FOUND` | 404 | Not Found | Resource does not exist. | -| `NNG_HTTP_STATUS_METHOD_NOT_ALLOWED` | 405 | Method Not Allowed | Resource does not support the method. | -| `NNG_HTTP_STATUS_METHOD_NOT_ACCEPTABLE` | 406 | Not Acceptable | Could not satisfy accept requirements. | -| `NNG_HTTP_STATUS_PROXY_AUTH_REQUIRED` | 407 | Proxy Authentication Required | Proxy requires authentication. | -| `NNG_HTTP_STATUS_REQUEST_TIMEOUT` | 408 | Request Timeout | Timed out waiting for request. | -| `NNG_HTTP_STATUS_CONFLICT` | 409 | Conflict | Conflicting request. | -| `NNG_HTTP_STATUS_GONE` | 410 | Gone | Resource no longer exists. | -| `NNG_HTTP_STATUS_LENGTH_REQUIRED` | 411 | Length Required | Missing Content-Length. | -| `NNG_HTTP_STATUS_PRECONDITION_FAILED` | 412 | Precondition Failed | | -| `NNG_HTTP_STATUS_CONTENT_TOO_LARGE` | 413 | Content Too Large | | -| `NNG_HTTP_STATUS_URI_TOO_LONG` | 414 | URI Too Long | | -| `NNG_HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE` | 415 | Unsupported Media Type | -| `NNG_HTTP_STATUS_RANGE_NOT_SATISFIABLE` | 416 | Range Not Satisfiable | -| `NNG_HTTP_STATUS_EXPECTATION_FAILED` | 417 | Expectation Failed | -| `NNG_HTTP_STATUS_TEAPOT` | 418 | I Am A Teapot | RFC 2324. | -| `NNG_HTTP_STATUS_UNPROCESSABLE_ENTITY` | 422 | Unprocessable Entity | -| `NNG_HTTP_STATUS_LOCKED` | 423 | Locked | -| `NNG_HTTP_STATUS_FAILED_DEPENDENCY` | 424 | Failed Dependency | -| `NNG_HTTP_STATUS_TOO_EARLY` | 425 | Too Early | -| `NNG_HTTP_STATUS_UPGRADE_REQUIRED` | 426 | Upgrade Required | -| `NNG_HTTP_STATUS_PRECONDITION_REQUIRED` | 428 | Precondition Required | | -| `NNG_HTTP_STATUS_TOO_MANY_REQUESTS` | 429 | Too Many Requests | | -| `NNG_HTTP_STATUS_HEADERS_TOO_LARGE` | 431 | Headers Too Large | | -| `NNG_HTTP_STATUS_UNAVAIL_LEGAL_REASONS` | 451 | Unavailabe For Legal Reasons | | -| `NNG_HTTP_STATUS_INTERNAL_SERVER_ERROR` | 500 | Internal Server Error | -| `NNG_HTTP_STATUS_NOT_IMPLEMENTED` | 501 | Not Implemented | Server does not implement method. | -| `NNG_HTTP_STATUS_BAD_GATEWAY` | 502 | Bad Gateway | -| `NNG_HTTP_STATUS_SERVICE_UNAVAILALE` | 503 | Service Unavailable | -| `NNG_HTTP_STATUS_GATEWAY_TIMEOUT` | 504 | Gateway TImeout | -| `NNG_HTTP_STATUS_HTTP_VERSION_NOT_SUPP` | 505 | HTTP Version Not Supported | -| `NNG_HTTP_STATUS_VARIANT_ALSO_NEGOTIATES` | 506 | Variant Also Negotiates | -| `NNG_HTTP_STATUS_INSUFFICIENT_STORAGE` | 507 | Variant Also Negotiates | -| `NNG_HTTP_STATUS_LOOP_DETECTED` | 508 | Loop Detected | -| `NNG_HTTP_STATUS_NOT_EXTENDED` | 510 | Not Extended | -| `NNG_HTTP_STATUS_NETWORK_AUTH_REQUIRED` | 511 | Network Authentication Required | +| Name | Code | Reason Text | Notes | +| ----------------------------------------------------------------------------------------------- | ---- | ------------------------------- | ----------------------------------------------------- | +| `NNG_HTTP_STATUS_CONTINUE` | 100 | Continue | Partial transfer, client may send body. | +| `NNG_HTTP_STATUS_SWITCHING` | 101 | Switching Protocols | Used when upgrading or hijacking a connection. | +| `NNG_HTTP_STATUS_PROCESSING` | 102 | Processing | +| `NNG_HTTP_STATUS_OK` | 200 | OK | Successful result. | +| `NNG_HTTP_STATUS_CREATED` | 201 | Created | Resource created successfully. | +| `NNG_HTTP_STATUS_ACCEPTED` | 202 | Created | Request accepted for future processing. | +| `NNG_HTTP_STATUS_NOT_AUTHORITATIVE` | 203 | Not Authoritative | Request successful, but modified by proxy. | +| `NNG_HTTP_STATUS_NO_CONTENT` | 204 | No Content | Request successful, no content returned. | +| `NNG_HTTP_STATUS_RESET_CONTENT` | 205 | Reset Content | Request successful, client should reload content. | +| `NNG_HTTP_STATUS_PARTIAL_CONTENT` | 206 | Partial Content | Response to a range request. | +| `NNG_HTTP_STATUS_MULTI_STATUS` | 207 | Multi-Status | Used with WebDAV. | +| `NNG_HTTP_STATUS_ALREADY_REPORTED` | 208 | Already Reported | Used with WebDAV. | +| `NNG_HTTP_STATUS_IM_USED` | 226 | IM Used | Used with delta encodings, rarely supported. | +| `NNG_HTTP_STATUS_MULTIPLE_CHOICES` | 300 | Multiple Choices | Multiple responses possible, client should choose. | +| `NNG_HTTP_STATUS_MOVED_PERMANENTLY` | 301 | Moved Permanently | Permanent redirection, may be saved by client. | +| `NNG_HTTP_STATUS_FOUND` | 302 | Found | Temporary redirection, client may switch to GET. | +| `NNG_HTTP_STATUS_SEE_OTHER` | 303 | See Other | Redirect, perhaps after a success POST or PUT. | +| `NNG_HTTP_STATUS_NOT_MODIFIED` | 304 | Not Modified | Resource not modified, client may use cached version. | +| `NNG_HTTP_STATUS_USE_PROXY` | 305 | Use Proxy | +| `NNG_HTTP_STATUS_TEMPORARY_REDIRECT` | 307 | Temporary Redirect | Temporary redirect, preserves method. | +| `NNG_HTTP_STATUS_PERMANENT_REDIRECT` | 308 | Permanent Redirect | Permanent redirect. | +| `NNG_HTTP_STATUS_BAD_REQUEST` | 400 | Bad Request | Generic problem with the request. | +| `NNG_HTTP_STATUS_UNAUTHORIZED` | 401 | Unauthorized | Indicates a problem with authentication. | +| `NNG_HTTP_STATUS_PAYMENT_REQUIRED` | 402 | Payment Required | +| `NNG_HTTP_STATUS_FORBIDDEN` | 403 | Forbidden | No permission to access resource. | +| `NNG_HTTP_STATUS_NOT_FOUND` | 404 | Not Found | Resource does not exist. | +| `NNG_HTTP_STATUS_METHOD_NOT_ALLOWED` | 405 | Method Not Allowed | Resource does not support the method. | +| `NNG_HTTP_STATUS_METHOD_NOT_ACCEPTABLE` | 406 | Not Acceptable | Could not satisfy accept requirements. | +| `NNG_HTTP_STATUS_PROXY_AUTH_REQUIRED` | 407 | Proxy Authentication Required | Proxy requires authentication. | +| `NNG_HTTP_STATUS_REQUEST_TIMEOUT` | 408 | Request Timeout | Timed out waiting for request. | +| `NNG_HTTP_STATUS_CONFLICT` | 409 | Conflict | Conflicting request. | +| `NNG_HTTP_STATUS_GONE` | 410 | Gone | Resource no longer exists. | +| `NNG_HTTP_STATUS_LENGTH_REQUIRED` | 411 | Length Required | Missing Content-Length. | +| `NNG_HTTP_STATUS_PRECONDITION_FAILED` | 412 | Precondition Failed | | +| `NNG_HTTP_STATUS_CONTENT_TOO_LARGE` | 413 | Content Too Large | | +| `NNG_HTTP_STATUS_URI_TOO_LONG` | 414 | URI Too Long | | +| `NNG_HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE` | 415 | Unsupported Media Type | +| `NNG_HTTP_STATUS_RANGE_NOT_SATISFIABLE` | 416 | Range Not Satisfiable | +| `NNG_HTTP_STATUS_EXPECTATION_FAILED` | 417 | Expectation Failed | +| `NNG_HTTP_STATUS_TEAPOT` | 418 | I Am A Teapot | RFC 2324. | +| `NNG_HTTP_STATUS_UNPROCESSABLE_ENTITY` | 422 | Unprocessable Entity | +| `NNG_HTTP_STATUS_LOCKED` | 423 | Locked | +| `NNG_HTTP_STATUS_FAILED_DEPENDENCY` | 424 | Failed Dependency | +| `NNG_HTTP_STATUS_TOO_EARLY` | 425 | Too Early | +| `NNG_HTTP_STATUS_UPGRADE_REQUIRED` | 426 | Upgrade Required | +| `NNG_HTTP_STATUS_PRECONDITION_REQUIRED` | 428 | Precondition Required | | +| `NNG_HTTP_STATUS_TOO_MANY_REQUESTS` | 429 | Too Many Requests | | +| `NNG_HTTP_STATUS_HEADERS_TOO_LARGE` | 431 | Headers Too Large | | +| `NNG_HTTP_STATUS_UNAVAIL_LEGAL_REASONS` | 451 | Unavailabe For Legal Reasons | | +| `NNG_HTTP_STATUS_INTERNAL_SERVER_ERROR` | 500 | Internal Server Error | +| `NNG_HTTP_STATUS_NOT_IMPLEMENTED` | 501 | Not Implemented | Server does not implement method. | +| `NNG_HTTP_STATUS_BAD_GATEWAY` | 502 | Bad Gateway | +| `NNG_HTTP_STATUS_SERVICE_UNAVAILALE` | 503 | Service Unavailable | +| `NNG_HTTP_STATUS_GATEWAY_TIMEOUT` | 504 | Gateway TImeout | +| `NNG_HTTP_STATUS_HTTP_VERSION_NOT_SUPP` | 505 | HTTP Version Not Supported | +| `NNG_HTTP_STATUS_VARIANT_ALSO_NEGOTIATES` | 506 | Variant Also Negotiates | +| `NNG_HTTP_STATUS_INSUFFICIENT_STORAGE` | 507 | Variant Also Negotiates | +| `NNG_HTTP_STATUS_LOOP_DETECTED` | 508 | Loop Detected | +| `NNG_HTTP_STATUS_NOT_EXTENDED` | 510 | Not Extended | +| `NNG_HTTP_STATUS_NETWORK_AUTH_REQUIRED` | 511 | Network Authentication Required | ### Retrieving Headers @@ -578,6 +578,7 @@ typedef void (*nng_http_hander_func)(nng_http_conn *conn, void *arg, nng_aio *ai nng_err nng_http_handler_alloc(nng_http_handler **hp, const char *path, nng_http_handler_func cb); ``` + The {{i:`nng_http_handler_alloc`}} 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 _hp_. @@ -615,26 +616,27 @@ The _aio_ may be scheduled for deferred completion using the [`nng_aio_start`]. ### Serving Directories and Files ```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); +nng_err nng_http_handler_directory(nng_http_handler **hp, const char *path, const char *dirname); +nng_err nng_http_handler_file(nng_http_handler **hp, const char *path, const char *filename, const char *ctype); ``` -The {{i:`nng_http_handler_alloc_directory`}} and {{i:`nng_http_handler_alloc_file`}} +The {{i:`nng_http_handler_directory`}} and {{i:`nng_http_handler_file`}} create handlers pre-configured to act as static content servers for either a full directory at _dirname_, or the single file at _filename_. These support the "GET" and "HEAD" methods, and the directory variant will dynamically generate `index.html` content based on the directory contents. These will also set the "Content-Type" if the file extension -matches one of the built-in values already known. If the no suitable MIME type can be -determined, the content type is set to "application/octet-stream". +matches one of the built-in values already known and _ctype_ is `NULL`. +If _ctype_ is not `NULL`, then the "Content-Type" is set to its value. +Otherwise, if no suitable MIME type can be determined, the content type is set to "application/octet-stream". ### Static Handler ```c -nng_err nng_http_handler_alloc_static(nng_http_handler **hp, const char *path, +nng_err nng_http_handler_static(nng_http_handler **hp, const char *path, const void *data, size_t size, const char *content_type); ``` -The {{i:`nng_http_handler_alloc_static`}} function creates a handler that +The {{i:`nng_http_handler_static`}} function creates a handler that serves the content located in _data_ (consisting of _size_ bytes) at the URI _path_. The _content_type_ determines the "Content-Type" header. If `NULL` is specified then a value of `application/octet-stream` is assumed. @@ -642,11 +644,11 @@ then a value of `application/octet-stream` is assumed. ### Redirect Handler ```c -nng_err nng_http_handler_alloc_redirect(nng_http_handler **hp, const char *path, +nng_err nng_http_handler_redirect(nng_http_handler **hp, const char *path, nng_http_status status, const char *location); ``` -The {{i:`nng_http_handler_alloc_redirect`}} function creates a handler with +The {{i:`nng_http_handler_redirect`}} function creates a handler with a function that simply directions from the URI at _path_ to the given _location_. The HTTP reply it creates will be with [status code][`nng_http_status`] _status_, @@ -779,6 +781,19 @@ the path to pass additional parameters. > service address (path) supports dynamically generated children. > It can also provide a logical fallback instead of relying on a 404 error code. +### Destroying a Handler + +```c +void nng_http_handler_free(nng_http_handler *handler); +``` + +The {{i:`nng_http_handler_free`}} function can be called to destroy a handler. + +> [!TIP] +> This function should not normally be needed, unless a the handler cannot be +> added to a server. Once the handler is taken over by the server, it is owned +> by the server and the server will dispose of it when it is removed or the server is destroyed. + ### Sending the Response Explicitly ```c diff --git a/docs/ref/xref.md b/docs/ref/xref.md index e3b575853..32523c775 100644 --- a/docs/ref/xref.md +++ b/docs/ref/xref.md @@ -294,6 +294,11 @@ [`nng_http_read_all`]: /api/http.md#direct-read-and-write [`nng_http_write`]: /api/http.md#direct-read-and-write [`nng_http_write_all`]: /api/http.md#direct-read-and-write +[`nng_http_handler_alloc`]: /api/http.md#nng_http_handler_alloc +[`nng_http_handler_static`]: /api/http.md#nng_http_handler_static +[`nng_http_handler_redirect`]: /api/http.md#nng_http_handler_redirect +[`nng_http_handler_file`]: /api/http.md#nng_http_handler_file +[`nng_http_handler_directory`]: /api/http.md#nng_http_handler_directory diff --git a/include/nng/http.h b/include/nng/http.h index 1ff541ac7..e4c578a62 100644 --- a/include/nng/http.h +++ b/include/nng/http.h @@ -242,20 +242,21 @@ NNG_DECL void nng_http_handler_free(nng_http_handler *); // nng_http_handler_alloc_file creates a "file" based handler, that // serves up static content from the given file path. The content-type -// supplied is determined from the file name using a simple built-in map. -NNG_DECL nng_err nng_http_handler_alloc_file( - nng_http_handler **, const char *, const char *); +// is taken from the 3rd argument, unless that is NULL, in which case +// it is determined from the file name using a simple built-in map. +NNG_DECL nng_err nng_http_handler_file( + nng_http_handler **, const char *, const char *, const char *); // nng_http_handler_alloc_static creates a static-content handler. // The last argument is the content-type, which may be NULL (in which case // "application/octet-stream" is assumed.) -NNG_DECL nng_err nng_http_handler_alloc_static( +NNG_DECL nng_err nng_http_handler_static( nng_http_handler **, const char *, const void *, size_t, const char *); // nng_http_handler_alloc_redirect creates an HTTP redirect handler. // The status is given, along with the new URL. If the status is 0, // then 301 will be used instead. -NNG_DECL nng_err nng_http_handler_alloc_redirect( +NNG_DECL nng_err nng_http_handler_redirect( nng_http_handler **, const char *, nng_http_status, const char *); // nng_http_handler_alloc_file creates a "directory" based handler, that @@ -264,7 +265,7 @@ NNG_DECL nng_err nng_http_handler_alloc_redirect( // directory content, otherwise a suitable error page is returned (the server // does not generate index pages automatically.) The content-type for // files is determined from the file name using a simple built-in map. -NNG_DECL nng_err nng_http_handler_alloc_directory( +NNG_DECL nng_err nng_http_handler_directory( nng_http_handler **, const char *, const char *); // nng_http_handler_set_method sets the method that the handler will be diff --git a/src/supplemental/http/http_api.h b/src/supplemental/http/http_api.h index 61c6e45f4..c2fd5eec2 100644 --- a/src/supplemental/http/http_api.h +++ b/src/supplemental/http/http_api.h @@ -22,14 +22,13 @@ #include -typedef struct nng_http_req nni_http_req; -typedef struct nng_http_res nni_http_res; -typedef struct nng_http_conn nni_http_conn; -typedef struct nng_http_handler nni_http_handler; -typedef struct nng_http_server nni_http_server; -typedef struct nng_http_client nni_http_client; -typedef struct nng_http_chunk nni_http_chunk; -typedef struct nng_http_chunks nni_http_chunks; +typedef struct nng_http_req nni_http_req; +typedef struct nng_http_res nni_http_res; +typedef struct nng_http_conn nni_http_conn; +typedef struct nng_http_server nni_http_server; +typedef struct nng_http_client nni_http_client; +typedef struct nng_http_chunk nni_http_chunk; +typedef struct nng_http_chunks nni_http_chunks; // These functions are private to the internal framework, and really should // not be used elsewhere. @@ -155,14 +154,14 @@ extern void nni_http_server_fini(nni_http_server *); // is already registered (i.e. a handler with the same value for Host, // Method, and URL.) extern nng_err nni_http_server_add_handler( - nni_http_server *, nni_http_handler *); + nni_http_server *, nng_http_handler *); // nni_http_del_handler removes the given handler. The caller is // responsible for finalizing it afterwards. If the handler was not found // (not registered), NNG_ENOENT is returned. In this case it is unsafe // to make assumptions about the validity of the handler. extern nng_err nni_http_server_del_handler( - nni_http_server *, nni_http_handler *); + nni_http_server *, nng_http_handler *); // nni_http_server_set_tls adds a TLS configuration to the server, // and enables the use of it. This returns NNG_EBUSY if the server is @@ -220,56 +219,51 @@ extern nng_err nni_http_server_error(nni_http_server *, nng_http *); // further processing.) extern nng_err nni_http_hijack(nni_http_conn *); -// nni_http_handler_init creates a server handler object, for the supplied +// nni_http_handler_alloc creates a server handler object, for the supplied // URI (path only) with the callback. // // Note that methods which modify a handler cannot be called while the handler // is registered with the server, and that a handler can only be registered // once per server. -extern nng_err nni_http_handler_init( - nni_http_handler **, const char *, nng_http_handler_func); +extern nng_err nni_http_handler_alloc( + nng_http_handler **, const char *, nng_http_handler_func); -// nni_http_handler_init_file creates a handler with a function to serve +// nni_http_handler_creates a handler with a function to serve // up a file named in the last argument. -extern nng_err nni_http_handler_init_file( - nni_http_handler **, const char *, const char *); +extern nng_err nni_http_handler_file( + nng_http_handler **, const char *, const char *, const char *); -// nni_http_handler_init_file_ctype is like nni_http_handler_init_file, but -// provides for setting the Content-Type explicitly (last argument). -extern nng_err nni_http_handler_init_file_ctype( - nni_http_handler **, const char *, const char *, const char *); - -// nni_http_handler_init_directory arranges to serve up an entire +// nni_http_handler_directory serves up an entire // directory tree. The content types are determined from the built-in // content type list. Actual directories are required to contain a // file called index.html or index.htm. We do not generate directory // listings for security reasons. -extern nng_err nni_http_handler_init_directory( - nni_http_handler **, const char *, const char *); +extern nng_err nni_http_handler_directory( + nng_http_handler **, const char *, const char *); -// nni_http_handler_init_static creates a handler that serves up static content -// supplied, with the Content-Type supplied in the final argument. -extern nng_err nni_http_handler_init_static( - nni_http_handler **, const char *, const void *, size_t, const char *); +// nni_http_handler_static creates a handler that serves up static +// content supplied, with the Content-Type supplied in the final argument. +extern nng_err nni_http_handler_static( + nng_http_handler **, const char *, const void *, size_t, const char *); -// nni_http_handler_init_redirect creates a handler that redirects the request. -extern nng_err nni_http_handler_init_redirect( - nni_http_handler **, const char *, nng_http_status, const char *); +// nni_http_handler_redirect creates a handler that redirects the request. +extern nng_err nni_http_handler_redirect( + nng_http_handler **, const char *, nng_http_status, const char *); -// nni_http_handler_fini destroys a handler. This should only be done before +// nni_http_handler_free destroys a handler. This should only be done before // the handler is added, or after it is deleted. The server automatically // calls this for any handlers still registered with it if it is destroyed. -extern void nni_http_handler_fini(nni_http_handler *); +extern void nni_http_handler_free(nng_http_handler *); // nni_http_handler_collect_body informs the server that it should collect // the entitty data associated with the client request, and sets the maximum // size to accept. -extern void nni_http_handler_collect_body(nni_http_handler *, bool, size_t); +extern void nni_http_handler_collect_body(nng_http_handler *, bool, size_t); // nni_http_handler_set_tree marks the handler as servicing the entire // tree (e.g. a directory), rather than just a leaf node. The handler // will probably need to inspect the URL of the request. -extern void nni_http_handler_set_tree(nni_http_handler *); +extern void nni_http_handler_set_tree(nng_http_handler *); // nni_http_handler_set_host limits the handler to only being called for // the given Host: field. This can be used to set up multiple virtual @@ -280,7 +274,7 @@ extern void nni_http_handler_set_tree(nni_http_handler *); // on port number as we assume that clients MUST have gotten that part right // as we do not support virtual hosting on multiple separate ports; the // server only listens on a single port. -extern void nni_http_handler_set_host(nni_http_handler *, const char *); +extern void nni_http_handler_set_host(nng_http_handler *, const char *); // nni_http_handler_set_method limits the handler to only being called // for the given HTTP method. By default a handler is called for GET @@ -290,16 +284,16 @@ extern void nni_http_handler_set_host(nni_http_handler *, const char *); // is obligated to inspect the method. (Note: the passed method must be // in upper case and should come from a statically allocated string; the // server does not make its own copy.) -extern void nni_http_handler_set_method(nni_http_handler *, const char *); +extern void nni_http_handler_set_method(nng_http_handler *, const char *); // nni_http_handler_set_data sets an opaque data element on the handler, // which will be available to the handler function as argument. // The callback is an optional destructor, and will be called with the // data as its argument, when the handler is being destroyed. -extern void nni_http_handler_set_data(nni_http_handler *, void *, nni_cb); +extern void nni_http_handler_set_data(nng_http_handler *, void *, nni_cb); // nni_http_handler_get_uri returns the URI set on the handler. -extern const char *nni_http_handler_get_uri(nni_http_handler *); +extern const char *nni_http_handler_get_uri(nng_http_handler *); // Client stuff. diff --git a/src/supplemental/http/http_public.c b/src/supplemental/http/http_public.c index 84477cc13..054080c09 100644 --- a/src/supplemental/http/http_public.c +++ b/src/supplemental/http/http_public.c @@ -299,7 +299,7 @@ nng_http_handler_alloc( nng_http_handler **hp, const char *uri, nng_http_handler_func cb) { #ifdef NNG_SUPP_HTTP - return (nni_http_handler_init(hp, uri, cb)); + return (nni_http_handler_alloc(hp, uri, cb)); #else NNI_ARG_UNUSED(hp); NNI_ARG_UNUSED(uri); @@ -312,18 +312,18 @@ void nng_http_handler_free(nng_http_handler *h) { #ifdef NNG_SUPP_HTTP - nni_http_handler_fini(h); + nni_http_handler_free(h); #else NNI_ARG_UNUSED(h); #endif } nng_err -nng_http_handler_alloc_file( - nng_http_handler **hp, const char *uri, const char *path) +nng_http_handler_file(nng_http_handler **hp, const char *uri, const char *path, + const char *ctype) { #ifdef NNG_SUPP_HTTP - return (nni_http_handler_init_file(hp, uri, path)); + return (nni_http_handler_file(hp, uri, path, ctype)); #else NNI_ARG_UNUSED(hp); NNI_ARG_UNUSED(uri); @@ -333,11 +333,11 @@ nng_http_handler_alloc_file( } nng_err -nng_http_handler_alloc_directory( +nng_http_handler_directory( nng_http_handler **hp, const char *uri, const char *path) { #ifdef NNG_SUPP_HTTP - return (nni_http_handler_init_directory(hp, uri, path)); + return (nni_http_handler_directory(hp, uri, path)); #else NNI_ARG_UNUSED(hp); NNI_ARG_UNUSED(uri); @@ -347,11 +347,11 @@ nng_http_handler_alloc_directory( } nng_err -nng_http_handler_alloc_redirect(nng_http_handler **hp, const char *uri, +nng_http_handler_redirect(nng_http_handler **hp, const char *uri, nng_http_status status, const char *where) { #ifdef NNG_SUPP_HTTP - return (nni_http_handler_init_redirect(hp, uri, status, where)); + return (nni_http_handler_redirect(hp, uri, status, where)); #else NNI_ARG_UNUSED(hp); NNI_ARG_UNUSED(uri); @@ -362,11 +362,11 @@ nng_http_handler_alloc_redirect(nng_http_handler **hp, const char *uri, } nng_err -nng_http_handler_alloc_static(nng_http_handler **hp, const char *uri, +nng_http_handler_static(nng_http_handler **hp, const char *uri, const void *data, size_t size, const char *ctype) { #ifdef NNG_SUPP_HTTP - return (nni_http_handler_init_static(hp, uri, data, size, ctype)); + return (nni_http_handler_static(hp, uri, data, size, ctype)); #else NNI_ARG_UNUSED(hp); NNI_ARG_UNUSED(uri); diff --git a/src/supplemental/http/http_server.c b/src/supplemental/http/http_server.c index 53da78c29..725e824ff 100644 --- a/src/supplemental/http/http_server.c +++ b/src/supplemental/http/http_server.c @@ -36,7 +36,7 @@ struct nng_http_handler { nng_sockaddr host_addr; bool host_ip; bool tree; - nni_atomic_int ref; + nni_refcnt ref; nni_atomic_bool busy; size_t maxbody; bool getbody; @@ -50,8 +50,8 @@ typedef struct http_sconn { nni_list_node node; nni_http_conn *conn; nni_http_server *server; - nni_http_handler *handler; // set if we deferred to read body - nni_http_handler *release; // set if we dispatched handler + nng_http_handler *handler; // set if we deferred to read body + nng_http_handler *release; // set if we dispatched handler bool close; bool finished; size_t unconsumed_body; @@ -104,17 +104,34 @@ static nni_reap_list http_server_reap_list = { .rl_func = (nni_cb) http_server_fini, }; +static void +http_handler_fini(void *arg) +{ + nng_http_handler *h = arg; + if (h->dtor != NULL) { + h->dtor(h->data); + } + NNI_FREE_STRUCT(h); +} + +void +nni_http_handler_free(nng_http_handler *h) +{ + if (h) { + nni_refcnt_rele(&h->ref); + } +} + nng_err -nni_http_handler_init( - nni_http_handler **hp, const char *uri, nng_http_handler_func cb) +nni_http_handler_alloc( + nng_http_handler **hp, const char *uri, nng_http_handler_func cb) { - nni_http_handler *h; + nng_http_handler *h; if ((h = NNI_ALLOC_STRUCT(h)) == NULL) { return (NNG_ENOMEM); } - nni_atomic_init(&h->ref); - nni_atomic_inc(&h->ref); + nni_refcnt_init(&h->ref, 1, h, http_handler_fini); // Default for HTTP is /. But remap it to "" for ease of matching. if ((uri == NULL) || (strlen(uri) == 0) || (strcmp(uri, "/") == 0)) { @@ -134,29 +151,15 @@ nni_http_handler_init( return (NNG_OK); } -// nni_http_handler_fini just drops the reference count, only destroying -// the handler if the reference drops to zero. void -nni_http_handler_fini(nni_http_handler *h) -{ - if (nni_atomic_dec_nv(&h->ref) != 0) { - return; - } - if (h->dtor != NULL) { - h->dtor(h->data); - } - NNI_FREE_STRUCT(h); -} - -void -nni_http_handler_collect_body(nni_http_handler *h, bool want, size_t maxbody) +nni_http_handler_collect_body(nng_http_handler *h, bool want, size_t maxbody) { h->getbody = want; h->maxbody = maxbody; } void -nni_http_handler_set_data(nni_http_handler *h, void *data, nni_cb dtor) +nni_http_handler_set_data(nng_http_handler *h, void *data, nni_cb dtor) { NNI_ASSERT(!nni_atomic_get_bool(&h->busy)); h->data = data; @@ -164,7 +167,7 @@ nni_http_handler_set_data(nni_http_handler *h, void *data, nni_cb dtor) } const char * -nni_http_handler_get_uri(nni_http_handler *h) +nni_http_handler_get_uri(nng_http_handler *h) { if (strlen(h->uri) == 0) { return ("/"); @@ -173,14 +176,14 @@ nni_http_handler_get_uri(nni_http_handler *h) } void -nni_http_handler_set_tree(nni_http_handler *h) +nni_http_handler_set_tree(nng_http_handler *h) { NNI_ASSERT(!nni_atomic_get_bool(&h->busy)); h->tree = true; } void -nni_http_handler_set_host(nni_http_handler *h, const char *host) +nni_http_handler_set_host(nng_http_handler *h, const char *host) { NNI_ASSERT(!nni_atomic_get_bool(&h->busy)); @@ -214,7 +217,7 @@ nni_http_handler_set_host(nni_http_handler *h, const char *host) } void -nni_http_handler_set_method(nni_http_handler *h, const char *method) +nni_http_handler_set_method(nng_http_handler *h, const char *method) { NNI_ASSERT(!nni_atomic_get_bool(&h->busy)); if (method == NULL) { @@ -360,7 +363,7 @@ nni_http_hijack(nni_http_conn *conn) } static bool -http_handler_host_match(nni_http_handler *h, const char *host) +http_handler_host_match(nng_http_handler *h, const char *host) { nng_sockaddr sa; size_t len; @@ -422,8 +425,8 @@ http_sconn_rxdone(void *arg) nni_http_server *s = sc->server; nni_aio *aio = &sc->rxaio; int rv; - nni_http_handler *h = NULL; - nni_http_handler *head = NULL; + nng_http_handler *h = NULL; + nng_http_handler *head = NULL; const char *val; nni_http_req *req = nni_http_conn_req(sc->conn); const char *uri; @@ -604,7 +607,6 @@ http_sconn_rxdone(void *arg) // Set a reference -- this because the callback may be running // asynchronously even after it gets removed from the server. - nni_atomic_inc(&h->ref); nni_aio_reset(&sc->cbaio); @@ -615,6 +617,7 @@ http_sconn_rxdone(void *arg) nni_http_set_version(sc->conn, NNG_HTTP_VERSION_1_1); nni_http_set_status(sc->conn, 0, NULL); + nni_refcnt_hold(&h->ref); h->cb(sc->conn, h->data, &sc->cbaio); } @@ -623,7 +626,7 @@ http_sconn_cbdone(void *arg) { http_sconn *sc = arg; nni_aio *aio = &sc->cbaio; - nni_http_handler *h; + nng_http_handler *h; nni_http_server *s = sc->server; // Get the handler. It may be set regardless of success or @@ -631,7 +634,7 @@ http_sconn_cbdone(void *arg) // done with the handler for now. if ((h = sc->release) != NULL) { sc->release = NULL; - nni_http_handler_fini(h); + nni_refcnt_rele(&h->ref); } if (nni_aio_result(aio) != 0) { @@ -751,7 +754,7 @@ http_server_acccb(void *arg) static void http_server_fini(nni_http_server *s) { - nni_http_handler *h; + nng_http_handler *h; http_error *epage; nni_aio_stop(&s->accaio); @@ -762,7 +765,7 @@ http_server_fini(nni_http_server *s) nng_stream_listener_free(s->listener); while ((h = nni_list_first(&s->handlers)) != NULL) { nni_list_remove(&s->handlers, h); - nni_http_handler_fini(h); + nni_refcnt_rele(&h->ref); } nni_mtx_unlock(&s->mtx); nni_mtx_lock(&s->errors_mtx); @@ -800,7 +803,7 @@ http_server_init(nni_http_server **serverp, const nng_url *url) } nni_mtx_init(&s->mtx); nni_mtx_init(&s->errors_mtx); - NNI_LIST_INIT(&s->handlers, nni_http_handler, node); + NNI_LIST_INIT(&s->handlers, nng_http_handler, node); NNI_LIST_INIT(&s->conns, http_sconn, node); nni_mtx_init(&s->errors_mtx); @@ -1009,9 +1012,9 @@ nni_http_server_error(nni_http_server *s, nng_http *conn) } nng_err -nni_http_server_add_handler(nni_http_server *s, nni_http_handler *h) +nni_http_server_add_handler(nni_http_server *s, nng_http_handler *h) { - nni_http_handler *h2; + nng_http_handler *h2; // Must have a legal method (and not one that is HEAD), path, // and handler. (The reason HEAD is verboten is that we supply @@ -1066,23 +1069,23 @@ nni_http_server_add_handler(nni_http_server *s, nni_http_handler *h) } nng_err -nni_http_server_del_handler(nni_http_server *s, nni_http_handler *h) +nni_http_server_del_handler(nni_http_server *s, nng_http_handler *h) { - nng_err rv = NNG_ENOENT; - nni_http_handler *srch; + nng_http_handler *srch; nni_mtx_lock(&s->mtx); NNI_LIST_FOREACH (&s->handlers, srch) { if (srch == h) { // NB: We are giving the caller our reference // on the handler. nni_list_remove(&s->handlers, h); - rv = NNG_OK; - break; + nni_mtx_unlock(&s->mtx); + nni_refcnt_rele(&h->ref); + return (NNG_OK); } } nni_mtx_unlock(&s->mtx); - return (rv); + return (NNG_ENOENT); } // Very limited MIME type map. Used only if the handler does not @@ -1216,10 +1219,10 @@ http_file_free(void *arg) } nng_err -nni_http_handler_init_file_ctype(nni_http_handler **hpp, const char *uri, +nni_http_handler_file(nng_http_handler **hpp, const char *uri, const char *path, const char *ctype) { - nni_http_handler *h; + nng_http_handler *h; http_file *hf; nng_err rv; @@ -1241,7 +1244,7 @@ nni_http_handler_init_file_ctype(nni_http_handler **hpp, const char *uri, return (NNG_ENOMEM); } - if ((rv = nni_http_handler_init(&h, uri, http_handle_file)) != 0) { + if ((rv = nni_http_handler_alloc(&h, uri, http_handle_file)) != 0) { http_file_free(hf); return (rv); } @@ -1255,13 +1258,6 @@ nni_http_handler_init_file_ctype(nni_http_handler **hpp, const char *uri, return (NNG_OK); } -nng_err -nni_http_handler_init_file( - nni_http_handler **hpp, const char *uri, const char *path) -{ - return (nni_http_handler_init_file_ctype(hpp, uri, path, NULL)); -} - static void http_handle_dir(nng_http *conn, void *arg, nng_aio *aio) { @@ -1388,11 +1384,11 @@ http_handle_dir(nng_http *conn, void *arg, nng_aio *aio) } nng_err -nni_http_handler_init_directory( - nni_http_handler **hpp, const char *uri, const char *path) +nni_http_handler_directory( + nng_http_handler **hpp, const char *uri, const char *path) { http_file *hf; - nni_http_handler *h; + nng_http_handler *h; nng_err rv; if ((hf = NNI_ALLOC_STRUCT(hf)) == NULL) { @@ -1404,7 +1400,7 @@ nni_http_handler_init_directory( return (NNG_ENOMEM); } - if ((rv = nni_http_handler_init(&h, uri, http_handle_dir)) != 0) { + if ((rv = nni_http_handler_alloc(&h, uri, http_handle_dir)) != 0) { http_file_free(hf); return (rv); } @@ -1480,10 +1476,10 @@ http_redirect_free(void *arg) } nng_err -nni_http_handler_init_redirect(nni_http_handler **hpp, const char *uri, +nni_http_handler_redirect(nng_http_handler **hpp, const char *uri, nng_http_status status, const char *where) { - nni_http_handler *h; + nng_http_handler *h; nng_err rv; http_redirect *hr; @@ -1500,7 +1496,8 @@ nni_http_handler_init_redirect(nni_http_handler **hpp, const char *uri, } hr->code = status; - if ((rv = nni_http_handler_init(&h, uri, http_handle_redirect)) != 0) { + if ((rv = nni_http_handler_alloc(&h, uri, http_handle_redirect)) != + 0) { http_redirect_free(hr); return (rv); } @@ -1555,10 +1552,10 @@ http_static_free(void *arg) } nng_err -nni_http_handler_init_static(nni_http_handler **hpp, const char *uri, +nni_http_handler_static(nng_http_handler **hpp, const char *uri, const void *data, size_t size, const char *ctype) { - nni_http_handler *h; + nng_http_handler *h; nng_err rv; http_static *hs; @@ -1573,7 +1570,7 @@ nni_http_handler_init_static(nni_http_handler **hpp, const char *uri, hs->size = size; memcpy(hs->data, data, size); - if ((rv = nni_http_handler_init(&h, uri, http_handle_static)) != 0) { + if ((rv = nni_http_handler_alloc(&h, uri, http_handle_static)) != 0) { http_static_free(hs); return (rv); } diff --git a/src/supplemental/http/http_server_test.c b/src/supplemental/http/http_server_test.c index e091cd89f..b42a15d65 100644 --- a/src/supplemental/http/http_server_test.c +++ b/src/supplemental/http/http_server_test.c @@ -203,7 +203,7 @@ test_server_basic(void) nng_iov iov; nng_http_handler *h; - NUTS_PASS(nng_http_handler_alloc_static( + NUTS_PASS(nng_http_handler_static( &h, "/home.html", doc1, strlen(doc1), "text/html")); server_setup(&st, h); @@ -245,7 +245,7 @@ test_server_canonify(void) nng_iov iov; nng_http_handler *h; - NUTS_PASS(nng_http_handler_alloc_static( + NUTS_PASS(nng_http_handler_static( &h, "/home/index.html", doc1, strlen(doc1), "text/html")); server_setup(&st, h); @@ -287,7 +287,7 @@ test_server_head(void) size_t size; nng_http_handler *h; - NUTS_PASS(nng_http_handler_alloc_static( + NUTS_PASS(nng_http_handler_static( &h, "/home.html", doc1, strlen(doc1), "text/html")); server_setup(&st, h); @@ -354,7 +354,7 @@ test_server_no_authoritive_form(void) struct server_test st; nng_http_handler *h; - NUTS_PASS(nng_http_handler_alloc_static( + NUTS_PASS(nng_http_handler_static( &h, "/home.html", doc1, strlen(doc1), "text/html")); server_setup(&st, h); @@ -381,7 +381,7 @@ test_server_bad_canonify(void) struct server_test st; nng_http_handler *h; - NUTS_PASS(nng_http_handler_alloc_static( + NUTS_PASS(nng_http_handler_static( &h, "/home.html", doc1, strlen(doc1), "text/html")); server_setup(&st, h); @@ -451,7 +451,7 @@ test_server_method_too_long(void) { nng_http_handler *h; - NUTS_PASS(nng_http_handler_alloc_static( + NUTS_PASS(nng_http_handler_static( &h, "/home.html", doc1, strlen(doc1), "text/html")); nng_http_handler_set_method(h, @@ -467,7 +467,7 @@ test_server_wrong_method(void) struct server_test st; nng_http_handler *h; - NUTS_PASS(nng_http_handler_alloc_static( + NUTS_PASS(nng_http_handler_static( &h, "/home.html", doc1, strlen(doc1), "text/html")); server_setup(&st, h); @@ -495,7 +495,7 @@ test_server_uri_too_long(void) nng_http_handler *h; char buf[32768]; - NUTS_PASS(nng_http_handler_alloc_static( + NUTS_PASS(nng_http_handler_static( &h, "/home.html", doc1, strlen(doc1), "text/html")); server_setup(&st, h); @@ -526,7 +526,7 @@ test_server_header_too_long(void) nng_http_handler *h; char buf[32768]; - NUTS_PASS(nng_http_handler_alloc_static( + NUTS_PASS(nng_http_handler_static( &h, "/home.html", doc1, strlen(doc1), "text/html")); server_setup(&st, h); @@ -557,7 +557,7 @@ test_server_invalid_utf8(void) struct server_test st; nng_http_handler *h; - NUTS_PASS(nng_http_handler_alloc_static( + NUTS_PASS(nng_http_handler_static( &h, "/home.html", doc1, strlen(doc1), "text/html")); server_setup(&st, h); @@ -625,7 +625,7 @@ test_server_get_redirect(void) struct server_test st; // We'll use a 303 (SEE OTHER) to ensure codes carry thru - NUTS_PASS(nng_http_handler_alloc_redirect( + NUTS_PASS(nng_http_handler_redirect( &h, "/here", NNG_HTTP_STATUS_SEE_OTHER, "http://127.0.0.1/there")); server_setup(&st, h); @@ -651,7 +651,7 @@ test_server_tree_redirect(void) struct server_test st; // We'll use a permanent redirect to ensure codes carry thru - NUTS_PASS(nng_http_handler_alloc_redirect(&h, "/here", + NUTS_PASS(nng_http_handler_redirect(&h, "/here", NNG_HTTP_STATUS_PERMANENT_REDIRECT, "http://127.0.0.1/there")); nng_http_handler_set_tree(h); server_setup(&st, h); @@ -679,7 +679,7 @@ test_server_post_redirect(void) struct server_test st; nng_http_handler *h; - NUTS_PASS(nng_http_handler_alloc_redirect( + NUTS_PASS(nng_http_handler_redirect( &h, "/here", 301, "http://127.0.0.1/there")); server_setup(&st, h); @@ -772,20 +772,20 @@ test_server_multiple_trees(void) NUTS_PASS(nni_file_put(file1, doc1, strlen(doc1))); NUTS_PASS(nni_file_put(file2, doc2, strlen(doc2))); - NUTS_PASS(nng_http_handler_alloc_directory(&h, "/", workdir)); + NUTS_PASS(nng_http_handler_directory(&h, "/", workdir)); nng_http_handler_set_tree(h); server_setup(&st, h); - NUTS_PASS(nng_http_handler_alloc_directory(&h, "/", workdir)); + NUTS_PASS(nng_http_handler_directory(&h, "/", workdir)); nng_http_handler_set_tree(h); NUTS_FAIL(nng_http_server_add_handler(st.s, h), NNG_EADDRINUSE); nng_http_handler_free(h); - NUTS_PASS(nng_http_handler_alloc_directory(&h, "/subdir", workdir2)); + NUTS_PASS(nng_http_handler_directory(&h, "/subdir", workdir2)); nng_http_handler_set_tree(h); NUTS_PASS(nng_http_server_add_handler(st.s, h)); - NUTS_PASS(nng_http_handler_alloc_directory(&h, "/subdir", workdir2)); + NUTS_PASS(nng_http_handler_directory(&h, "/subdir", workdir2)); nng_http_handler_set_tree(h); NUTS_FAIL(nng_http_server_add_handler(st.s, h), NNG_EADDRINUSE); nng_http_handler_free(h); @@ -892,7 +892,7 @@ test_serve_directory(void) struct serve_directory sd; setup_directory(&sd); - NUTS_PASS(nng_http_handler_alloc_directory(&h, "/", sd.workdir)); + NUTS_PASS(nng_http_handler_directory(&h, "/", sd.workdir)); server_setup(&st, h); NUTS_PASS(nng_http_set_uri(st.conn, "/subdir1/index.html", NULL)); @@ -921,7 +921,7 @@ test_serve_directory_index(void) struct serve_directory sd; setup_directory(&sd); - NUTS_PASS(nng_http_handler_alloc_directory(&h, "/", sd.workdir)); + NUTS_PASS(nng_http_handler_directory(&h, "/", sd.workdir)); server_setup(&st, h); NUTS_CASE("Directory 1: index.html"); @@ -963,7 +963,7 @@ test_serve_plain_text(void) struct serve_directory sd; setup_directory(&sd); - NUTS_PASS(nng_http_handler_alloc_directory(&h, "/", sd.workdir)); + NUTS_PASS(nng_http_handler_directory(&h, "/", sd.workdir)); server_setup(&st, h); NUTS_PASS(nng_http_set_uri(st.conn, "/file.txt", NULL)); @@ -992,7 +992,7 @@ test_serve_file_parameters(void) struct serve_directory sd; setup_directory(&sd); - NUTS_PASS(nng_http_handler_alloc_directory(&h, "/", sd.workdir)); + NUTS_PASS(nng_http_handler_directory(&h, "/", sd.workdir)); server_setup(&st, h); NUTS_PASS(nng_http_set_uri(st.conn, "/file.txt?param=1234", NULL)); @@ -1033,7 +1033,7 @@ test_serve_missing_index(void) struct serve_directory sd; setup_directory(&sd); - NUTS_PASS(nng_http_handler_alloc_directory(&h, "/", sd.workdir)); + NUTS_PASS(nng_http_handler_directory(&h, "/", sd.workdir)); server_setup(&st, h); NUTS_PASS(nng_http_set_uri(st.conn, "/index.html", NULL)); @@ -1059,7 +1059,7 @@ test_serve_index_not_post(void) struct serve_directory sd; setup_directory(&sd); - NUTS_PASS(nng_http_handler_alloc_directory(&h, "/", sd.workdir)); + NUTS_PASS(nng_http_handler_directory(&h, "/", sd.workdir)); server_setup(&st, h); NUTS_PASS(nng_http_set_uri(st.conn, "/subdir2/index.html", NULL)); @@ -1086,7 +1086,7 @@ test_serve_subdir_index(void) struct serve_directory sd; setup_directory(&sd); - NUTS_PASS(nng_http_handler_alloc_directory(&h, "/docs", sd.workdir)); + NUTS_PASS(nng_http_handler_directory(&h, "/docs", sd.workdir)); server_setup(&st, h); NUTS_PASS(nng_http_set_uri(st.conn, "/docs/subdir1/", NULL)); diff --git a/src/supplemental/websocket/websocket.c b/src/supplemental/websocket/websocket.c index 86edf1235..08b3a0710 100644 --- a/src/supplemental/websocket/websocket.c +++ b/src/supplemental/websocket/websocket.c @@ -102,7 +102,7 @@ struct nni_ws_listener { bool isstream; bool send_text; bool recv_text; - nni_http_handler *handler; + nng_http_handler *handler; nni_ws_listen_hook hookfn; void *hookarg; nni_list headers; // response headers @@ -1403,9 +1403,8 @@ ws_init(nni_ws **wsp) static void ws_listener_stop(void *arg) { - nni_ws_listener *l = arg; - nni_http_handler *h; - nni_http_server *s; + nni_ws_listener *l = arg; + nni_http_server *s; ws_listener_close(l); @@ -1413,15 +1412,10 @@ ws_listener_stop(void *arg) while (!nni_list_empty(&l->reply)) { nni_cv_wait(&l->cv); } - h = l->handler; - s = l->server; - l->handler = NULL; - l->server = NULL; + s = l->server; + l->server = NULL; nni_mtx_unlock(&l->mtx); - if (h != NULL) { - nni_http_handler_fini(h); - } if (s != NULL) { nni_http_server_fini(s); } @@ -2026,7 +2020,7 @@ nni_ws_listener_alloc(nng_stream_listener **wslp, const nng_url *url) if (strlen(host) == 0) { host = NULL; } - rv = nni_http_handler_init(&l->handler, url->u_path, ws_handler); + rv = nng_http_handler_alloc(&l->handler, url->u_path, ws_handler); if (rv != 0) { ws_listener_free(l); return (rv); From d203647145c7c3e5f0f4ae8288d2c4796f9e23d5 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Fri, 17 Jan 2025 09:24:36 -0800 Subject: [PATCH 2/2] http: start of clean up of public vs. private functions We have a lot of "private" wrappers around public functions, which doesn't really help at all, and just add needless extra stack frames and extra cruft in the linker tables. We should eliminate the trivially thin wrappers where possible, and this is a start. --- src/supplemental/http/CMakeLists.txt | 16 +- src/supplemental/http/http_api.h | 181 ++------- src/supplemental/http/http_client.c | 50 +-- src/supplemental/http/http_conn.c | 132 +++---- src/supplemental/http/http_msg.c | 22 +- src/supplemental/http/http_public.c | 514 ------------------------- src/supplemental/http/http_server.c | 90 ++--- src/supplemental/http/http_stubs.c | 431 +++++++++++++++++++++ src/supplemental/websocket/websocket.c | 28 +- 9 files changed, 622 insertions(+), 842 deletions(-) create mode 100644 src/supplemental/http/http_stubs.c diff --git a/src/supplemental/http/CMakeLists.txt b/src/supplemental/http/CMakeLists.txt index 31a544a9f..9f85631b3 100644 --- a/src/supplemental/http/CMakeLists.txt +++ b/src/supplemental/http/CMakeLists.txt @@ -11,13 +11,9 @@ option (NNG_ENABLE_HTTP "Enable HTTP API" ON) if (NNG_ENABLE_HTTP) set(NNG_SUPP_HTTP ON) -endif() -mark_as_advanced(NNG_ENABLE_HTTP) - -nng_sources(http_public.c http_api.h) - -nng_defines_if(NNG_SUPP_HTTP NNG_SUPP_HTTP) -nng_sources_if(NNG_SUPP_HTTP + nng_defines(NNG_SUPP_HTTP) + nng_sources( + http_api.h http_client.c http_chunk.c http_conn.c @@ -25,4 +21,8 @@ nng_sources_if(NNG_SUPP_HTTP http_public.c http_schemes.c http_server.c) -nng_test_if(NNG_SUPP_HTTP http_server_test) + nng_test(http_server_test) +else() + nng_sources(http_stubs.c) +endif() +mark_as_advanced(NNG_ENABLE_HTTP) diff --git a/src/supplemental/http/http_api.h b/src/supplemental/http/http_api.h index c2fd5eec2..425828edc 100644 --- a/src/supplemental/http/http_api.h +++ b/src/supplemental/http/http_api.h @@ -24,9 +24,7 @@ typedef struct nng_http_req nni_http_req; typedef struct nng_http_res nni_http_res; -typedef struct nng_http_conn nni_http_conn; typedef struct nng_http_server nni_http_server; -typedef struct nng_http_client nni_http_client; typedef struct nng_http_chunk nni_http_chunk; typedef struct nng_http_chunks nni_http_chunks; @@ -66,15 +64,14 @@ extern void *nni_http_chunk_data(nni_http_chunk *); extern nng_err nni_http_chunks_parse( nni_http_chunks *, void *, size_t, size_t *); -extern void nni_http_read_chunks( - nni_http_conn *, nni_http_chunks *, nni_aio *); +extern void nni_http_read_chunks(nng_http *, nni_http_chunks *, nni_aio *); -extern nni_http_req *nni_http_conn_req(nni_http_conn *); -extern nni_http_res *nni_http_conn_res(nni_http_conn *); +extern nni_http_req *nni_http_conn_req(nng_http *); +extern nni_http_res *nni_http_conn_res(nng_http *); // Private to the server. (Used to support session hijacking.) -extern void nni_http_conn_set_ctx(nni_http_conn *, void *); -extern void *nni_http_conn_get_ctx(nni_http_conn *); +extern void nni_http_conn_set_ctx(nng_http *, void *); +extern void *nni_http_conn_get_ctx(nng_http *); // An HTTP connection is a connection over which messages are exchanged. // Generally, clients send request messages, and then read responses. @@ -82,8 +79,8 @@ extern void *nni_http_conn_get_ctx(nni_http_conn *); // require a 1:1 mapping between request and response here -- the application // is responsible for dealing with that. // -// We only support HTTP/1.1, though using the nni_http_conn_read and -// nni_http_conn_write low level methods, it is possible to write an upgrader +// We only support HTTP/1.1, though using the nni_http_read and +// nni_http_write low level methods, it is possible to write an upgrader // (such as websocket!) that might support e.g. HTTP/2 or reading data that // follows a legacy HTTP/1.0 message. // @@ -96,7 +93,7 @@ extern void *nni_http_conn_get_ctx(nni_http_conn *); extern nng_err nni_http_init(nng_http **, nng_stream *, bool); extern void nni_http_conn_close(nng_http *); -extern void nni_http_conn_fini(nni_http_conn *); +extern void nni_http_conn_fini(nng_http *); extern int nni_http_conn_getopt( nng_http *, const char *, void *, size_t *, nni_type); @@ -105,30 +102,21 @@ extern int nni_http_conn_getopt( // Note that the iovs of the aio's are clobbered by these methods -- callers // must not use them for any other purpose. -extern void nni_http_write_req(nni_http_conn *, nni_aio *); -extern void nni_http_read_res(nni_http_conn *, nni_aio *); -extern void nni_http_read_req(nni_http_conn *, nni_aio *); -extern void nni_http_write_res(nni_http_conn *, nni_aio *); -extern void nni_http_read_discard(nni_http_conn *, size_t, nni_aio *); +extern void nni_http_write_req(nng_http *, nni_aio *); +extern void nni_http_read_res(nng_http *, nni_aio *); +extern void nni_http_read_req(nng_http *, nni_aio *); +extern void nni_http_write_res(nng_http *, nni_aio *); +extern void nni_http_read_discard(nng_http *, size_t, nni_aio *); extern nng_err nni_http_req_alloc_data(nni_http_req *, size_t); extern nng_err nni_http_res_alloc_data(nni_http_res *, size_t); extern bool nni_http_is_error(nng_http *); -extern void nni_http_read(nni_http_conn *, nni_aio *); -extern void nni_http_read_full(nni_http_conn *, nni_aio *); -extern void nni_http_write(nni_http_conn *, nni_aio *); -extern void nni_http_write_full(nni_http_conn *, nni_aio *); - -extern nng_err nni_http_add_header(nng_http *, const char *, const char *); -extern nng_err nni_http_set_header(nng_http *, const char *, const char *); -extern void nni_http_del_header(nng_http *, const char *); -extern const char *nni_http_get_header(nng_http *, const char *); - -extern void nni_http_get_body(nng_http *, void **, size_t *); -extern void nni_http_set_body(nng_http *, void *, size_t); -extern nng_err nni_http_copy_body(nng_http *, const void *, size_t); +extern void nni_http_read(nng_http *, nni_aio *); +extern void nni_http_read_full(nng_http *, nni_aio *); +extern void nni_http_write(nng_http *, nni_aio *); +extern void nni_http_write_full(nng_http *, nni_aio *); // prune body clears the outgoing body (0 bytes), but leaves content-length // intact if present for the benefit of HEAD. @@ -207,153 +195,32 @@ extern nng_err nni_http_server_set_error_page( // of the res. The res must have the status set first. extern nng_err nni_http_server_error(nni_http_server *, nng_http *); -// nni_http_hijack is intended to be called by a handler that wishes to -// take over the processing of the HTTP session -- usually to change protocols -// (such as in the case of websocket). The caller is responsible for obtaining -// and disposal of the associated nni_http session. Also, this completely -// disassociates the http session from the server, so the server may be -// stopped or destroyed without affecting the hijacked session. Note also -// that the hijacker will need to issue any HTTP reply itself. Finally, -// when a session is hijacked, the caller is also responsible for disposing -// of the request structure. (Some hijackers may keep the request for -// further processing.) -extern nng_err nni_http_hijack(nni_http_conn *); - -// nni_http_handler_alloc creates a server handler object, for the supplied -// URI (path only) with the callback. -// -// Note that methods which modify a handler cannot be called while the handler -// is registered with the server, and that a handler can only be registered -// once per server. -extern nng_err nni_http_handler_alloc( - nng_http_handler **, const char *, nng_http_handler_func); - -// nni_http_handler_creates a handler with a function to serve -// up a file named in the last argument. -extern nng_err nni_http_handler_file( - nng_http_handler **, const char *, const char *, const char *); - -// nni_http_handler_directory serves up an entire -// directory tree. The content types are determined from the built-in -// content type list. Actual directories are required to contain a -// file called index.html or index.htm. We do not generate directory -// listings for security reasons. -extern nng_err nni_http_handler_directory( - nng_http_handler **, const char *, const char *); - -// nni_http_handler_static creates a handler that serves up static -// content supplied, with the Content-Type supplied in the final argument. -extern nng_err nni_http_handler_static( - nng_http_handler **, const char *, const void *, size_t, const char *); - -// nni_http_handler_redirect creates a handler that redirects the request. -extern nng_err nni_http_handler_redirect( - nng_http_handler **, const char *, nng_http_status, const char *); - -// nni_http_handler_free destroys a handler. This should only be done before -// the handler is added, or after it is deleted. The server automatically -// calls this for any handlers still registered with it if it is destroyed. -extern void nni_http_handler_free(nng_http_handler *); - -// nni_http_handler_collect_body informs the server that it should collect -// the entitty data associated with the client request, and sets the maximum -// size to accept. -extern void nni_http_handler_collect_body(nng_http_handler *, bool, size_t); - -// nni_http_handler_set_tree marks the handler as servicing the entire -// tree (e.g. a directory), rather than just a leaf node. The handler -// will probably need to inspect the URL of the request. -extern void nni_http_handler_set_tree(nng_http_handler *); - -// nni_http_handler_set_host limits the handler to only being called for -// the given Host: field. This can be used to set up multiple virtual -// hosts. Note that host names must match exactly. If NULL or an empty -// string is specified, then the client's Host: field is ignored. (The -// supplied value for the Host is copied by this function.) When supplying -// a hostname, do not include a value for the port number; we do not match -// on port number as we assume that clients MUST have gotten that part right -// as we do not support virtual hosting on multiple separate ports; the -// server only listens on a single port. -extern void nni_http_handler_set_host(nng_http_handler *, const char *); - -// nni_http_handler_set_method limits the handler to only being called -// for the given HTTP method. By default a handler is called for GET -// methods only (and HEAD, which is handled internally.) Handlers can -// be specified for any valid HTTP method. A handler may set the value -// NULL here, to be called for any HTTP method. In such a case, the handler -// is obligated to inspect the method. (Note: the passed method must be -// in upper case and should come from a statically allocated string; the -// server does not make its own copy.) -extern void nni_http_handler_set_method(nng_http_handler *, const char *); - -// nni_http_handler_set_data sets an opaque data element on the handler, -// which will be available to the handler function as argument. -// The callback is an optional destructor, and will be called with the -// data as its argument, when the handler is being destroyed. -extern void nni_http_handler_set_data(nng_http_handler *, void *, nni_cb); - -// nni_http_handler_get_uri returns the URI set on the handler. -extern const char *nni_http_handler_get_uri(nng_http_handler *); - // Client stuff. -extern nng_err nni_http_client_init(nni_http_client **, const nng_url *); -extern void nni_http_client_fini(nni_http_client *); - // nni_http_client_set_tls sets the TLS configuration. This wipes out // the entire TLS configuration on the client, so the caller must have // configured it reasonably. This API is not recommended unless the // caller needs complete control over the TLS configuration. extern nng_err nni_http_client_set_tls( - nni_http_client *, struct nng_tls_config *); + nng_http_client *, struct nng_tls_config *); // nni_http_client_get_tls obtains the TLS configuration if one is present, // or returns NNG_EINVAL. The supplied TLS configuration object may // be invalidated by any future calls to nni_http_client_set_tls. extern nng_err nni_http_client_get_tls( - nni_http_client *, struct nng_tls_config **); + nng_http_client *, struct nng_tls_config **); extern int nni_http_client_set( - nni_http_client *, const char *, const void *buf, size_t, nni_type); + nng_http_client *, const char *, const void *buf, size_t, nni_type); extern int nni_http_client_get( - nni_http_client *, const char *, void *, size_t *, nni_type); - -extern void nni_http_client_connect(nni_http_client *, nni_aio *); - -// nni_http_transact_conn is used to perform a round-trip exchange (i.e. a -// single HTTP transaction). It will not automatically close the connection, -// unless some kind of significant error occurs. The caller should dispose -// of the connection if the aio does not complete successfully. -// Note that this will fail with NNG_ENOTSUP if the server attempts to reply -// with a chunked transfer encoding. The request and response used are the -// ones associated with the connection. -extern void nni_http_transact_conn(nni_http_conn *, nni_aio *); - -// nni_http_transact is used to execute a single transaction to a server. -// The connection is opened, and will be closed when the transaction is -// complete. Note that this will fail with NNG_ENOTSUP if the server attempts -// to reply with a chunked transfer encoding. -extern void nni_http_transact( - nni_http_client *, nni_http_req *, nni_http_res *, nni_aio *); + nng_http_client *, const char *, void *, size_t *, nni_type); // nni_http_stream_scheme returns the underlying stream scheme for a given // upper layer scheme. extern const char *nni_http_stream_scheme(const char *); // Private method used for the server. -extern bool nni_http_res_sent(nni_http_conn *conn); - -extern const char *nni_http_get_version(nng_http *conn); -extern int nni_http_set_version(nng_http *conn, const char *vers); - -extern void nni_http_set_method(nng_http *conn, const char *method); -extern const char *nni_http_get_method(nng_http *conn); - -extern void nni_http_set_status( - nng_http *conn, nng_http_status status, const char *reason); - -extern nng_http_status nni_http_get_status(nng_http *); -extern const char *nni_http_get_reason(nng_http *); +extern bool nni_http_res_sent(nng_http *conn); // nni_http_set_error flags an error using the built in HTML page. // unless body is not NULL. To pass no content, pass an empty string for body. @@ -366,10 +233,6 @@ extern nng_err nni_http_set_error(nng_http *conn, nng_http_status status, extern nng_err nni_http_set_redirect(nng_http *conn, nng_http_status status, const char *reason, const char *dest); -extern nng_err nni_http_set_uri( - nng_http *conn, const char *uri, const char *query); -extern const char *nni_http_get_uri(nng_http *conn); - extern void nni_http_set_host(nng_http *conn, const char *); extern void nni_http_set_content_type(nng_http *conn, const char *); extern void nni_http_conn_reset(nng_http *conn); diff --git a/src/supplemental/http/http_client.c b/src/supplemental/http/http_client.c index 1f5d8fbd3..aee464584 100644 --- a/src/supplemental/http/http_client.c +++ b/src/supplemental/http/http_client.c @@ -31,7 +31,7 @@ struct nng_http_client { }; static void -http_dial_start(nni_http_client *c) +http_dial_start(nng_http_client *c) { if (nni_list_empty(&c->aios)) { return; @@ -42,11 +42,11 @@ http_dial_start(nni_http_client *c) static void http_dial_cb(void *arg) { - nni_http_client *c = arg; + nng_http_client *c = arg; nni_aio *aio; nng_err rv; nng_stream *stream; - nni_http_conn *conn; + nng_http *conn; nni_mtx_lock(&c->mtx); rv = nni_aio_result(&c->aio); @@ -90,7 +90,7 @@ http_dial_cb(void *arg) } void -nni_http_client_fini(nni_http_client *c) +nng_http_client_free(nng_http_client *c) { nni_aio_stop(&c->aio); nng_stream_dialer_stop(c->dialer); @@ -101,10 +101,10 @@ nni_http_client_fini(nni_http_client *c) } nng_err -nni_http_client_init(nni_http_client **cp, const nng_url *url) +nng_http_client_alloc(nng_http_client **cp, const nng_url *url) { nng_err rv; - nni_http_client *c; + nng_http_client *c; nng_url my_url; const char *scheme; @@ -139,7 +139,7 @@ nni_http_client_init(nni_http_client **cp, const nng_url *url) url->u_port); } if ((rv = nng_stream_dialer_alloc_url(&c->dialer, &my_url)) != 0) { - nni_http_client_fini(c); + nng_http_client_free(c); return (rv); } @@ -148,19 +148,19 @@ nni_http_client_init(nni_http_client **cp, const nng_url *url) } nng_err -nni_http_client_set_tls(nni_http_client *c, nng_tls_config *tls) +nng_http_client_set_tls(nng_http_client *c, nng_tls_config *tls) { return (nng_stream_dialer_set_tls(c->dialer, tls)); } nng_err -nni_http_client_get_tls(nni_http_client *c, nng_tls_config **tlsp) +nng_http_client_get_tls(nng_http_client *c, nng_tls_config **tlsp) { return (nng_stream_dialer_get_tls(c->dialer, tlsp)); } int -nni_http_client_set(nni_http_client *c, const char *name, const void *buf, +nni_http_client_set(nng_http_client *c, const char *name, const void *buf, size_t sz, nni_type t) { // We have no local options, but we just pass them straight through. @@ -169,7 +169,7 @@ nni_http_client_set(nni_http_client *c, const char *name, const void *buf, int nni_http_client_get( - nni_http_client *c, const char *name, void *buf, size_t *szp, nni_type t) + nng_http_client *c, const char *name, void *buf, size_t *szp, nni_type t) { return (nni_stream_dialer_get(c->dialer, name, buf, szp, t)); } @@ -177,7 +177,7 @@ nni_http_client_get( static void http_dial_cancel(nni_aio *aio, void *arg, int rv) { - nni_http_client *c = arg; + nng_http_client *c = arg; nni_mtx_lock(&c->mtx); if (nni_aio_list_active(aio)) { nni_aio_list_remove(aio); @@ -190,7 +190,7 @@ http_dial_cancel(nni_aio *aio, void *arg, int rv) } void -nni_http_client_connect(nni_http_client *c, nni_aio *aio) +nng_http_client_connect(nng_http_client *c, nni_aio *aio) { nni_aio_reset(aio); nni_mtx_lock(&c->mtx); @@ -215,8 +215,8 @@ typedef enum http_txn_state { typedef struct http_txn { nni_aio aio; // lower level aio nni_list aios; // upper level aio(s) -- maximum one - nni_http_client *client; - nni_http_conn *conn; + nng_http_client *client; + nng_http *conn; nni_http_res *res; nni_http_chunks *chunks; http_txn_state state; @@ -293,8 +293,8 @@ http_txn_cb(void *arg) // Detect chunked encoding. You poor bastard. (Only if not // HEAD.) - if ((strcmp(nni_http_get_method(txn->conn), "HEAD") != 0) && - ((str = nni_http_get_header( + if ((strcmp(nng_http_get_method(txn->conn), "HEAD") != 0) && + ((str = nng_http_get_header( txn->conn, "Transfer-Encoding")) != NULL) && (strstr(str, "chunked") != NULL)) { @@ -309,8 +309,8 @@ http_txn_cb(void *arg) return; } - if ((strcmp(nni_http_get_method(txn->conn), "HEAD") == 0) || - ((str = nni_http_get_header( + if ((strcmp(nng_http_get_method(txn->conn), "HEAD") == 0) || + ((str = nng_http_get_header( txn->conn, "Content-Length")) == NULL) || ((len = (uint64_t) strtoull(str, &end, 10)) == 0) || (end == NULL) || (*end != '\0')) { @@ -326,8 +326,8 @@ http_txn_cb(void *arg) NNG_OK) { goto error; } - nni_http_get_body(txn->conn, &iov.iov_buf, &iov.iov_len); - nni_aio_set_iov(&txn->aio, 1, &iov); + nng_http_get_body(txn->conn, &iov.iov_buf, &iov.iov_len); + nng_aio_set_iov(&txn->aio, 1, &iov); txn->state = HTTP_RECVING_BODY; nni_http_read_full(txn->conn, &txn->aio); nni_mtx_unlock(&http_txn_lk); @@ -347,7 +347,7 @@ http_txn_cb(void *arg) if ((rv = nni_http_res_alloc_data(txn->res, sz)) != 0) { goto error; } - nni_http_get_body(txn->conn, (void **) &dst, &sz); + nng_http_get_body(txn->conn, (void **) &dst, &sz); while ((chunk = nni_http_chunks_iter(txn->chunks, chunk)) != NULL) { memcpy(dst, nni_http_chunk_data(chunk), @@ -378,13 +378,13 @@ http_txn_cancel(nni_aio *aio, void *arg, int rv) nni_mtx_unlock(&http_txn_lk); } -// nni_http_transact_conn sends a request to an HTTP server, and reads the +// nng_http_transact sends a request to an HTTP server, and reads the // response. It also attempts to read any associated data. Note that // at present it can only read data that comes in normally, as support // for Chunked Transfer Encoding is missing. Note that cancelling the aio // is generally fatal to the connection. void -nni_http_transact_conn(nni_http_conn *conn, nni_aio *aio) +nng_http_transact(nng_http *conn, nni_aio *aio) { http_txn *txn; @@ -401,7 +401,7 @@ nni_http_transact_conn(nni_http_conn *conn, nni_aio *aio) txn->state = HTTP_SENDING; nni_http_res_reset(txn->res); - nni_http_set_status(txn->conn, 0, NULL); + nng_http_set_status(txn->conn, 0, NULL); nni_mtx_lock(&http_txn_lk); if (!nni_aio_start(aio, http_txn_cancel, txn)) { diff --git a/src/supplemental/http/http_conn.c b/src/supplemental/http/http_conn.c index 009618528..ec8adb0c7 100644 --- a/src/supplemental/http/http_conn.c +++ b/src/supplemental/http/http_conn.c @@ -102,19 +102,19 @@ nni_http_conn_res(nng_http *conn) } void -nni_http_conn_set_ctx(nni_http_conn *conn, void *ctx) +nni_http_conn_set_ctx(nng_http *conn, void *ctx) { conn->ctx = ctx; } void * -nni_http_conn_get_ctx(nni_http_conn *conn) +nni_http_conn_get_ctx(nng_http *conn) { return (conn->ctx); } static void -http_close(nni_http_conn *conn) +http_close(nng_http *conn) { // Call with lock held. nni_aio *aio; @@ -152,7 +152,7 @@ http_close(nni_http_conn *conn) } void -nni_http_conn_close(nni_http_conn *conn) +nni_http_conn_close(nng_http *conn) { nni_mtx_lock(&conn->mtx); http_close(conn); @@ -163,7 +163,7 @@ nni_http_conn_close(nni_http_conn *conn) // beginning, so that the next read can go at the end. This avoids the problem // of dealing with a read that might wrap. static void -http_buf_pull_up(nni_http_conn *conn) +http_buf_pull_up(nng_http *conn) { if (conn->rd_get != 0) { memmove(conn->buf, conn->buf + conn->rd_get, @@ -175,7 +175,7 @@ http_buf_pull_up(nni_http_conn *conn) // http_rd_buf attempts to satisfy the read from data in the buffer. static nng_err -http_rd_buf(nni_http_conn *conn, nni_aio *aio) +http_rd_buf(nng_http *conn, nni_aio *aio) { size_t cnt = conn->rd_put - conn->rd_get; size_t n; @@ -324,7 +324,7 @@ http_rd_buf(nni_http_conn *conn, nni_aio *aio) } static void -http_rd_start(nni_http_conn *conn) +http_rd_start(nng_http *conn) { for (;;) { nni_aio *aio; @@ -363,13 +363,13 @@ http_rd_start(nni_http_conn *conn) static void http_rd_cb(void *arg) { - nni_http_conn *conn = arg; - nni_aio *aio = &conn->rd_aio; - nni_aio *uaio; - size_t cnt; - nng_err rv; - unsigned niov; - nni_iov *iov; + nng_http *conn = arg; + nni_aio *aio = &conn->rd_aio; + nni_aio *uaio; + size_t cnt; + nng_err rv; + unsigned niov; + nni_iov *iov; nni_mtx_lock(&conn->mtx); @@ -436,7 +436,7 @@ http_rd_cb(void *arg) static void http_rd_cancel(nni_aio *aio, void *arg, int rv) { - nni_http_conn *conn = arg; + nng_http *conn = arg; nni_mtx_lock(&conn->mtx); if (aio == conn->rd_uaio) { @@ -451,7 +451,7 @@ http_rd_cancel(nni_aio *aio, void *arg, int rv) } static void -http_rd_submit(nni_http_conn *conn, nni_aio *aio, enum read_flavor flavor) +http_rd_submit(nng_http *conn, nni_aio *aio, enum read_flavor flavor) { nni_aio_reset(aio); if (conn->closed) { @@ -469,7 +469,7 @@ http_rd_submit(nni_http_conn *conn, nni_aio *aio, enum read_flavor flavor) } static void -http_wr_start(nni_http_conn *conn) +http_wr_start(nng_http *conn) { nni_aio *aio; nni_iov *iov; @@ -492,11 +492,11 @@ http_wr_start(nni_http_conn *conn) static void http_wr_cb(void *arg) { - nni_http_conn *conn = arg; - nni_aio *aio = &conn->wr_aio; - nni_aio *uaio; - nng_err rv; - size_t n; + nng_http *conn = arg; + nni_aio *aio = &conn->wr_aio; + nni_aio *uaio; + nng_err rv; + size_t n; nni_mtx_lock(&conn->mtx); @@ -551,7 +551,7 @@ http_wr_cb(void *arg) static void http_wr_cancel(nni_aio *aio, void *arg, int rv) { - nni_http_conn *conn = arg; + nng_http *conn = arg; nni_mtx_lock(&conn->mtx); if (aio == conn->wr_uaio) { @@ -566,7 +566,7 @@ http_wr_cancel(nni_aio *aio, void *arg, int rv) } static void -http_wr_submit(nni_http_conn *conn, nni_aio *aio, enum write_flavor flavor) +http_wr_submit(nng_http *conn, nni_aio *aio, enum write_flavor flavor) { nni_aio_reset(aio); if (conn->closed) { @@ -597,12 +597,12 @@ nni_http_conn_reset(nng_http *conn) nni_strfree(conn->uri); } conn->uri = NULL; - nni_http_set_version(conn, NNG_HTTP_VERSION_1_1); - nni_http_set_status(conn, 0, NULL); + nng_http_set_version(conn, NNG_HTTP_VERSION_1_1); + nng_http_set_status(conn, 0, NULL); } void -nni_http_read_req(nni_http_conn *conn, nni_aio *aio) +nni_http_read_req(nng_http *conn, nni_aio *aio) { // clear the sent flag (used for the server) conn->res_sent = false; @@ -613,7 +613,7 @@ nni_http_read_req(nni_http_conn *conn, nni_aio *aio) } void -nni_http_read_res(nni_http_conn *conn, nni_aio *aio) +nni_http_read_res(nng_http *conn, nni_aio *aio) { nni_mtx_lock(&conn->mtx); http_rd_submit(conn, aio, HTTP_RD_RES); @@ -621,7 +621,7 @@ nni_http_read_res(nni_http_conn *conn, nni_aio *aio) } void -nni_http_read_chunks(nni_http_conn *conn, nni_http_chunks *cl, nni_aio *aio) +nni_http_read_chunks(nng_http *conn, nni_http_chunks *cl, nni_aio *aio) { nni_aio_set_prov_data(aio, cl); @@ -632,7 +632,7 @@ nni_http_read_chunks(nni_http_conn *conn, nni_http_chunks *cl, nni_aio *aio) } void -nni_http_read_full(nni_http_conn *conn, nni_aio *aio) +nni_http_read_full(nng_http *conn, nni_aio *aio) { nni_aio_set_prov_data(aio, NULL); @@ -652,7 +652,7 @@ nni_http_read_discard(nng_http *conn, size_t discard, nng_aio *aio) } void -nni_http_read(nni_http_conn *conn, nni_aio *aio) +nni_http_read(nng_http *conn, nni_aio *aio) { nni_aio_set_prov_data(aio, NULL); @@ -692,13 +692,13 @@ http_snprintf(nng_http *conn, char *buf, size_t sz) if (conn->client) { len = snprintf(buf, sz, "%s %s %s\r\n", - nni_http_get_method(conn), nni_http_get_uri(conn), - nni_http_get_version(conn)); + nng_http_get_method(conn), nng_http_get_uri(conn), + nng_http_get_version(conn)); hdrs = &conn->req.data.hdrs; } else { len = snprintf(buf, sz, "%s %d %s\r\n", - nni_http_get_version(conn), nni_http_get_status(conn), - nni_http_get_reason(conn)); + nng_http_get_version(conn), nng_http_get_status(conn), + nng_http_get_reason(conn)); hdrs = &conn->res.data.hdrs; } @@ -819,7 +819,7 @@ nni_http_write_res(nng_http *conn, nni_aio *aio) } void -nni_http_write(nni_http_conn *conn, nni_aio *aio) +nni_http_write(nng_http *conn, nni_aio *aio) { nni_mtx_lock(&conn->mtx); http_wr_submit(conn, aio, HTTP_WR_RAW); @@ -827,7 +827,7 @@ nni_http_write(nni_http_conn *conn, nni_aio *aio) } void -nni_http_write_full(nni_http_conn *conn, nni_aio *aio) +nni_http_write_full(nng_http *conn, nni_aio *aio) { nni_mtx_lock(&conn->mtx); http_wr_submit(conn, aio, HTTP_WR_FULL); @@ -835,13 +835,13 @@ nni_http_write_full(nni_http_conn *conn, nni_aio *aio) } const char * -nni_http_get_version(nng_http *conn) +nng_http_get_version(nng_http *conn) { return (conn->vers); } -int -nni_http_set_version(nng_http *conn, const char *vers) +nng_err +nng_http_set_version(nng_http *conn, const char *vers) { static const char *http_versions[] = { // for efficiency, we order in most likely first @@ -864,7 +864,7 @@ nni_http_set_version(nng_http *conn, const char *vers) } void -nni_http_set_method(nng_http *conn, const char *method) +nng_http_set_method(nng_http *conn, const char *method) { if (method == NULL) { method = "GET"; @@ -875,13 +875,13 @@ nni_http_set_method(nng_http *conn, const char *method) } const char * -nni_http_get_method(nng_http *conn) +nng_http_get_method(nng_http *conn) { return (conn->meth); } nng_http_status -nni_http_get_status(nng_http *conn) +nng_http_get_status(nng_http *conn) { return (conn->code ? conn->code : NNG_HTTP_STATUS_OK); } @@ -987,13 +987,13 @@ nni_http_reason(nng_http_status code) } const char * -nni_http_get_reason(nng_http *conn) +nng_http_get_reason(nng_http *conn) { return (conn->rsn ? conn->rsn : nni_http_reason(conn->code)); } void -nni_http_set_status(nng_http *conn, nng_http_status status, const char *reason) +nng_http_set_status(nng_http *conn, nng_http_status status, const char *reason) { conn->code = status; char *dup = NULL; @@ -1043,8 +1043,8 @@ http_conn_set_error(nng_http *conn, nng_http_status status, const char *reason, conn->iserr = true; - nni_http_set_status(conn, status, reason); - reason = nni_http_get_reason(conn); + nng_http_set_status(conn, status, reason); + reason = nng_http_get_reason(conn); if (body == NULL) { snprintf(content, sizeof(content), prefix, status, reason, @@ -1074,7 +1074,7 @@ http_conn_set_error(nng_http *conn, nng_http_status status, const char *reason, if (strlen(body) > 0) { nni_http_set_content_type(conn, "text/html; charset=UTF-8"); // if the follow fails, live with it (ENOMEM, so no body). - (void) nni_http_copy_body(conn, body, strlen(body)); + (void) nng_http_copy_body(conn, body, strlen(body)); } return (0); } @@ -1103,7 +1103,7 @@ nni_http_set_redirect(nng_http *conn, nng_http_status status, } else if ((loc = nni_strdup(redirect)) == NULL) { return (NNG_ENOMEM); } - (void) nni_http_del_header(conn, "Location"); + (void) nng_http_del_header(conn, "Location"); nni_list_node_remove(&conn->location.node); nni_http_free_header(&conn->location); conn->location.name = "Location"; @@ -1151,13 +1151,13 @@ nni_http_set_content_type(nng_http *conn, const char *ctype) } const char * -nni_http_get_uri(nng_http *conn) +nng_http_get_uri(nng_http *conn) { return ((conn->uri && conn->uri[0]) ? conn->uri : "/"); } nng_err -nni_http_set_uri(nng_http *conn, const char *uri, const char *query) +nng_http_set_uri(nng_http *conn, const char *uri, const char *query) { size_t needed; const char *fmt; @@ -1298,7 +1298,7 @@ http_set_known_header(nng_http *conn, const char *key, const char *val) } nng_err -nni_http_add_header(nng_http *conn, const char *key, const char *val) +nng_http_add_header(nng_http *conn, const char *key, const char *val) { if (http_set_known_header(conn, key, val)) { return (NNG_OK); @@ -1318,7 +1318,7 @@ nni_http_set_static_header( headers = &conn->res.data.hdrs; } - nni_http_del_header(conn, key); + nng_http_del_header(conn, key); nni_list_node_remove(&h->node); h->alloc_header = false; h->static_name = true; @@ -1329,7 +1329,7 @@ nni_http_set_static_header( } nng_err -nni_http_set_header(nng_http *conn, const char *key, const char *val) +nng_http_set_header(nng_http *conn, const char *key, const char *val) { if (http_set_known_header(conn, key, val)) { return (0); @@ -1351,7 +1351,7 @@ http_del_header_one(nni_list *hdrs, const char *key) } void -nni_http_del_header(nng_http *conn, const char *key) +nng_http_del_header(nng_http *conn, const char *key) { nni_list *hdrs = conn->client ? &conn->req.data.hdrs : &conn->res.data.hdrs; @@ -1373,7 +1373,7 @@ http_get_header(const nni_list *hdrs, const char *key) } const char * -nni_http_get_header(nng_http *conn, const char *key) +nng_http_get_header(nng_http *conn, const char *key) { if (conn->client) { return (http_get_header(&conn->res.data.hdrs, key)); @@ -1383,7 +1383,7 @@ nni_http_get_header(nng_http *conn, const char *key) } void -nni_http_get_body(nng_http *conn, void **datap, size_t *sizep) +nng_http_get_body(nng_http *conn, void **datap, size_t *sizep) { if (conn->client) { *datap = conn->res.data.data; @@ -1430,7 +1430,7 @@ http_copy_data(nni_http_entity *entity, const void *data, size_t size) } void -nni_http_set_body(nng_http *conn, void *data, size_t size) +nng_http_set_body(nng_http *conn, void *data, size_t size) { if (conn->client) { http_set_data(&conn->req.data, data, size); @@ -1453,7 +1453,7 @@ nni_http_prune_body(nng_http *conn) } nng_err -nni_http_copy_body(nng_http *conn, const void *data, size_t size) +nng_http_copy_body(nng_http *conn, const void *data, size_t size) { nng_err rv; if (conn->client) { @@ -1469,7 +1469,7 @@ nni_http_copy_body(nng_http *conn, const void *data, size_t size) int nni_http_conn_getopt( - nni_http_conn *conn, const char *name, void *buf, size_t *szp, nni_type t) + nng_http *conn, const char *name, void *buf, size_t *szp, nni_type t) { int rv; nni_mtx_lock(&conn->mtx); @@ -1483,7 +1483,7 @@ nni_http_conn_getopt( } void -nni_http_conn_fini(nni_http_conn *conn) +nni_http_conn_fini(nng_http *conn) { nni_aio_stop(&conn->wr_aio); nni_aio_stop(&conn->rd_aio); @@ -1505,9 +1505,9 @@ nni_http_conn_fini(nni_http_conn *conn) } static nng_err -http_init(nni_http_conn **connp, nng_stream *data, bool client) +http_init(nng_http **connp, nng_stream *data, bool client) { - nni_http_conn *conn; + nng_http *conn; if ((conn = NNI_ALLOC_STRUCT(conn)) == NULL) { return (NNG_ENOMEM); @@ -1518,8 +1518,8 @@ http_init(nni_http_conn **connp, nng_stream *data, bool client) nni_aio_list_init(&conn->wrq); nni_http_req_init(&conn->req); nni_http_res_init(&conn->res); - nni_http_set_version(conn, NNG_HTTP_VERSION_1_1); - nni_http_set_method(conn, "GET"); + nng_http_set_version(conn, NNG_HTTP_VERSION_1_1); + nng_http_set_method(conn, "GET"); if ((conn->buf = nni_alloc(HTTP_BUFSIZE)) == NULL) { nni_http_conn_fini(conn); @@ -1549,7 +1549,7 @@ nni_http_init(nng_http **connp, nng_stream *stream, bool client) // private to the HTTP framework, used on the server bool -nni_http_res_sent(nni_http_conn *conn) +nni_http_res_sent(nng_http *conn) { return (conn->res_sent); } diff --git a/src/supplemental/http/http_msg.c b/src/supplemental/http/http_msg.c index 09470e317..17fb000ae 100644 --- a/src/supplemental/http/http_msg.c +++ b/src/supplemental/http/http_msg.c @@ -142,7 +142,7 @@ http_parse_header(nng_http *conn, void *line) end--; } - return (nni_http_add_header(conn, key, val)); + return (nng_http_add_header(conn, key, val)); } void @@ -207,39 +207,39 @@ http_req_parse_line(nng_http *conn, void *line) char *uri; char *version; - if (nni_http_get_status(conn) >= NNG_HTTP_STATUS_BAD_REQUEST) { + if (nng_http_get_status(conn) >= NNG_HTTP_STATUS_BAD_REQUEST) { // we've already failed it, nothing else for us to do return (NNG_OK); } method = line; if ((uri = strchr(method, ' ')) == NULL) { - nni_http_set_status(conn, NNG_HTTP_STATUS_BAD_REQUEST, NULL); + nng_http_set_status(conn, NNG_HTTP_STATUS_BAD_REQUEST, NULL); return (NNG_OK); } *uri = '\0'; uri++; if ((version = strchr(uri, ' ')) == NULL) { - nni_http_set_status(conn, NNG_HTTP_STATUS_BAD_REQUEST, NULL); + nng_http_set_status(conn, NNG_HTTP_STATUS_BAD_REQUEST, NULL); return (NNG_OK); } *version = '\0'; version++; if (nni_url_canonify_uri(uri) != 0) { - nni_http_set_status(conn, NNG_HTTP_STATUS_BAD_REQUEST, NULL); + nng_http_set_status(conn, NNG_HTTP_STATUS_BAD_REQUEST, NULL); return (NNG_OK); } - if (nni_http_set_version(conn, version)) { - nni_http_set_status( + if (nng_http_set_version(conn, version)) { + nng_http_set_status( conn, NNG_HTTP_STATUS_HTTP_VERSION_NOT_SUPP, NULL); return (NNG_OK); } - nni_http_set_method(conn, method); + nng_http_set_method(conn, method); // this one only can fail due to ENOMEM - return (nni_http_set_uri(conn, uri, NULL)); + return (nng_http_set_uri(conn, uri, NULL)); } static nng_err @@ -268,9 +268,9 @@ http_res_parse_line(nng_http *conn, uint8_t *line) return (NNG_EPROTO); } - nni_http_set_status(conn, (uint16_t) status, reason); + nng_http_set_status(conn, (uint16_t) status, reason); - return (nni_http_set_version(conn, version)); + return (nng_http_set_version(conn, version)); } // nni_http_req_parse parses a request (but not any attached entity data). diff --git a/src/supplemental/http/http_public.c b/src/supplemental/http/http_public.c index 054080c09..4331831af 100644 --- a/src/supplemental/http/http_public.c +++ b/src/supplemental/http/http_public.c @@ -15,642 +15,128 @@ // Symbols in this file are "public" versions of the HTTP API. // These are suitable for exposure to applications. -const char * -nng_http_get_header(nng_http *conn, const char *key) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_get_header(conn, key)); -#else - NNI_ARG_UNUSED(conn); - NNI_ARG_UNUSED(key); - return (NULL); -#endif -} - -nng_err -nng_http_set_header(nng_http *conn, const char *key, const char *val) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_set_header(conn, key, val)); -#else - NNI_ARG_UNUSED(conn); - NNI_ARG_UNUSED(key); - NNI_ARG_UNUSED(val); - return (NNG_ENOTSUP); -#endif -} - -nng_err -nng_http_add_header(nng_http *conn, const char *key, const char *val) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_add_header(conn, key, val)); -#else - NNI_ARG_UNUSED(conn); - NNI_ARG_UNUSED(key); - NNI_ARG_UNUSED(val); - return (NNG_ENOTSUP); -#endif -} - -void -nng_http_del_header(nng_http *conn, const char *key) -{ -#ifdef NNG_SUPP_HTTP - nni_http_del_header(conn, key); -#else - NNI_ARG_UNUSED(conn); - NNI_ARG_UNUSED(key); -#endif -} - -void -nng_http_set_body(nng_http *conn, void *data, size_t sz) -{ -#ifdef NNG_SUPP_HTTP - nni_http_set_body(conn, data, sz); -#else - NNI_ARG_UNUSED(conn); - NNI_ARG_UNUSED(data); - NNI_ARG_UNUSED(sz); - return (NNG_ENOTSUP); -#endif -} - -nng_err -nng_http_copy_body(nng_http *conn, const void *data, size_t len) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_copy_body(conn, data, len)); -#else - NNI_ARG_UNUSED(conn); - NNI_ARG_UNUSED(data); - NNI_ARG_UNUSED(len); - return (NNG_ENOTSUP); -#endif -} - -void -nng_http_get_body(nng_http *conn, void **datap, size_t *lenp) -{ -#ifdef NNG_SUPP_HTTP - nni_http_get_body(conn, datap, lenp); -#else - NNI_ARG_UNUSED(conn); - NNI_ARG_UNUSED(datap); - NNI_ARG_UNUSED(lenp); -#endif -} - -const char * -nng_http_get_uri(nng_http *conn) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_get_uri(conn)); -#else - NNI_ARG_UNUSED(req); - return (NULL); -#endif -} - -nng_err -nng_http_set_uri(nng_http *conn, const char *uri, const char *query) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_set_uri(conn, uri, query)); -#else - NNI_ARG_UNUSED(conn); - NNI_ARG_UNUSED(uri); - NNI_ARG_UNUSED(query); - return (NNG_ENOTSUP); -#endif -} - -const char * -nng_http_get_version(nng_http *conn) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_get_version(conn)); -#else - NNI_ARG_UNUSED(res); - return (NULL); -#endif -} - -void -nng_http_set_status(nng_http *conn, nng_http_status status, const char *reason) -{ -#ifdef NNG_SUPP_HTTP - nni_http_set_status(conn, status, reason); -#else - NNI_ARG_UNUSED(res); - NNI_ARG_UNUSED(status); - NNI_ARG_UNUSED(reason); -#endif -} - -nng_http_status -nng_http_get_status(nng_http *conn) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_get_status(conn)); -#else - NNI_ARG_UNUSED(res); - NNI_ARG_UNUSED(status); - return (0); -#endif -} - -const char * -nng_http_get_reason(nng_http *conn) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_get_reason(conn)); -#else - NNI_ARG_UNUSED(res); - NNI_ARG_UNUSED(status); - return (0); -#endif -} - -nng_err -nng_http_set_version(nng_http *conn, const char *version) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_set_version(conn, version)); -#else - return (NNG_ENOTSUP); -#endif -} - -void -nng_http_set_method(nng_http *conn, const char *method) -{ -#ifdef NNG_SUPP_HTTP - nni_http_set_method(conn, method); -#else - NNI_ARG_UNUSED(conn); - NNI_ARG_UNUSED(method); -#endif -} - -const char * -nng_http_get_method(nng_http *conn) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_get_method(conn)); -#else - NNI_ARG_UNUSED(conn); - return (NULL); -#endif -} - void nng_http_close(nng_http *conn) { -#ifdef NNG_SUPP_HTTP // API version of this closes *and* frees the structure. nni_http_conn_fini(conn); -#else - NNI_ARG_UNUSED(conn); -#endif } void nng_http_read(nng_http *conn, nng_aio *aio) { -#ifdef NNG_SUPP_HTTP nni_http_read(conn, aio); -#else - NNI_ARG_UNUSED(conn); - nni_aio_finish_error(aio, NNG_ENOTSUP); -#endif } void nng_http_read_all(nng_http *conn, nng_aio *aio) { -#ifdef NNG_SUPP_HTTP nni_http_read_full(conn, aio); -#else - NNI_ARG_UNUSED(conn); - nni_aio_finish_error(aio, NNG_ENOTSUP); -#endif } void nng_http_write(nng_http *conn, nng_aio *aio) { -#ifdef NNG_SUPP_HTTP nni_http_write(conn, aio); -#else - NNI_ARG_UNUSED(conn); - nni_aio_finish_error(aio, NNG_ENOTSUP); -#endif } void nng_http_write_all(nng_http *conn, nng_aio *aio) { -#ifdef NNG_SUPP_HTTP nni_http_write_full(conn, aio); -#else - NNI_ARG_UNUSED(conn); - NNI_ARG_UNUSED(aio); -#endif } void nng_http_write_request(nng_http *conn, nng_aio *aio) { -#ifdef NNG_SUPP_HTTP nni_http_write_req(conn, aio); -#else - NNI_ARG_UNUSED(conn); - nni_aio_finish_error(aio, NNG_ENOTSUP); -#endif } void nng_http_write_response(nng_http *conn, nng_aio *aio) { -#ifdef NNG_SUPP_HTTP nni_http_write_res(conn, aio); -#else - NNI_ARG_UNUSED(conn); - nni_aio_finish_error(aio, NNG_ENOTSUP); -#endif } void nng_http_read_response(nng_http *conn, nng_aio *aio) { -#ifdef NNG_SUPP_HTTP nni_http_read_res(conn, aio); -#else - NNI_ARG_UNUSED(conn); - NNI_ARG_UNUSED(res); - nni_aio_finish_error(aio, NNG_ENOTSUP); -#endif -} - -nng_err -nng_http_handler_alloc( - nng_http_handler **hp, const char *uri, nng_http_handler_func cb) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_handler_alloc(hp, uri, cb)); -#else - NNI_ARG_UNUSED(hp); - NNI_ARG_UNUSED(uri); - NNI_ARG_UNUSED(cb); - return (NNG_ENOTSUP); -#endif -} - -void -nng_http_handler_free(nng_http_handler *h) -{ -#ifdef NNG_SUPP_HTTP - nni_http_handler_free(h); -#else - NNI_ARG_UNUSED(h); -#endif -} - -nng_err -nng_http_handler_file(nng_http_handler **hp, const char *uri, const char *path, - const char *ctype) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_handler_file(hp, uri, path, ctype)); -#else - NNI_ARG_UNUSED(hp); - NNI_ARG_UNUSED(uri); - NNI_ARG_UNUSED(path); - return (NNG_ENOTSUP); -#endif -} - -nng_err -nng_http_handler_directory( - nng_http_handler **hp, const char *uri, const char *path) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_handler_directory(hp, uri, path)); -#else - NNI_ARG_UNUSED(hp); - NNI_ARG_UNUSED(uri); - NNI_ARG_UNUSED(path); - return (NNG_ENOTSUP); -#endif -} - -nng_err -nng_http_handler_redirect(nng_http_handler **hp, const char *uri, - nng_http_status status, const char *where) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_handler_redirect(hp, uri, status, where)); -#else - NNI_ARG_UNUSED(hp); - NNI_ARG_UNUSED(uri); - NNI_ARG_UNUSED(status); - NNI_ARG_UNUSED(where); - return (NNG_ENOTSUP); -#endif -} - -nng_err -nng_http_handler_static(nng_http_handler **hp, const char *uri, - const void *data, size_t size, const char *ctype) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_handler_static(hp, uri, data, size, ctype)); -#else - NNI_ARG_UNUSED(hp); - NNI_ARG_UNUSED(uri); - NNI_ARG_UNUSED(data); - NNI_ARG_UNUSED(size); - NNI_ARG_UNUSED(ctype); - return (NNG_ENOTSUP); -#endif -} - -void -nng_http_handler_set_method(nng_http_handler *h, const char *meth) -{ -#ifdef NNG_SUPP_HTTP - nni_http_handler_set_method(h, meth); -#else - NNI_ARG_UNUSED(h); - NNI_ARG_UNUSED(meth); -#endif -} - -void -nng_http_handler_collect_body(nng_http_handler *h, bool want, size_t len) -{ -#ifdef NNG_SUPP_HTTP - nni_http_handler_collect_body(h, want, len); -#else - NNI_ARG_UNUSED(h); - NNI_ARG_UNUSED(want); - NNI_ARG_UNUSED(len); -#endif -} - -void -nng_http_handler_set_host(nng_http_handler *h, const char *host) -{ -#ifdef NNG_SUPP_HTTP - nni_http_handler_set_host(h, host); -#else - NNI_ARG_UNUSED(h); - NNI_ARG_UNUSED(host); -#endif -} - -void -nng_http_handler_set_tree(nng_http_handler *h) -{ -#ifdef NNG_SUPP_HTTP - nni_http_handler_set_tree(h); -#else - NNI_ARG_UNUSED(h); -#endif -} - -void -nng_http_handler_set_data(nng_http_handler *h, void *dat, void (*dtor)(void *)) -{ -#ifdef NNG_SUPP_HTTP - nni_http_handler_set_data(h, dat, dtor); -#else - NNI_ARG_UNUSED(h); - NNI_ARG_UNUSED(dat); - NNI_ARG_UNUSED(dtor); -#endif } nng_err nng_http_server_hold(nng_http_server **srvp, const nng_url *url) { -#ifdef NNG_SUPP_HTTP return (nni_http_server_init(srvp, url)); -#else - NNI_ARG_UNUSED(srvp); - NNI_ARG_UNUSED(url); - return (NNG_ENOTSUP); -#endif } void nng_http_server_release(nng_http_server *srv) { -#ifdef NNG_SUPP_HTTP nni_http_server_fini(srv); -#else - NNI_ARG_UNUSED(srv); -#endif } nng_err nng_http_server_start(nng_http_server *srv) { -#ifdef NNG_SUPP_HTTP return (nni_http_server_start(srv)); -#else - NNI_ARG_UNUSED(srv); - return (NNG_ENOTSUP); -#endif } void nng_http_server_stop(nng_http_server *srv) { -#ifdef NNG_SUPP_HTTP nni_http_server_stop(srv); -#else - NNI_ARG_UNUSED(srv); -#endif } nng_err nng_http_server_add_handler(nng_http_server *srv, nng_http_handler *h) { -#ifdef NNG_SUPP_HTTP return (nni_http_server_add_handler(srv, h)); -#else - NNI_ARG_UNUSED(srv); - NNI_ARG_UNUSED(h); - return (NNG_ENOTSUP); -#endif } nng_err nng_http_server_del_handler(nng_http_server *srv, nng_http_handler *h) { -#ifdef NNG_SUPP_HTTP return (nni_http_server_del_handler(srv, h)); -#else - NNI_ARG_UNUSED(srv); - NNI_ARG_UNUSED(h); - return (NNG_ENOTSUP); -#endif } nng_err nng_http_server_set_error_page( nng_http_server *srv, nng_http_status code, const char *body) { -#ifdef NNG_SUPP_HTTP return (nni_http_server_set_error_page(srv, code, body)); -#else - NNI_ARG_UNUSED(srv); - NNI_ARG_UNUSED(code); - NNI_ARG_UNUSED(body); - return (NNG_ENOTSUP); -#endif } nng_err nng_http_server_set_tls(nng_http_server *srv, nng_tls_config *cfg) { -#if defined(NNG_SUPP_HTTP) && defined(NNG_SUPP_TLS) return (nni_http_server_set_tls(srv, cfg)); -#else - NNI_ARG_UNUSED(srv); - NNI_ARG_UNUSED(cfg); - return (NNG_ENOTSUP); -#endif } nng_err nng_http_server_get_tls(nng_http_server *srv, nng_tls_config **cfg) { -#if defined(NNG_SUPP_HTTP) && defined(NNG_SUPP_TLS) return (nni_http_server_get_tls(srv, cfg)); -#else - NNI_ARG_UNUSED(srv); - NNI_ARG_UNUSED(cfg); - return (NNG_ENOTSUP); -#endif } nng_err nng_http_server_get_addr(nng_http_server *srv, nng_sockaddr *addr) { -#ifdef NNG_SUPP_HTTP size_t size = sizeof(nng_sockaddr); if (srv == NULL || addr == NULL) return NNG_EINVAL; return (nni_http_server_get( srv, NNG_OPT_LOCADDR, addr, &size, NNI_TYPE_SOCKADDR)); -#else - NNI_ARG_UNUSED(srv); - NNI_ARG_UNUSED(addr); - return (NNG_ENOTSUP); -#endif } nng_err nng_http_server_error(nng_http_server *srv, nng_http *conn) { -#ifdef NNG_SUPP_HTTP return (nni_http_server_error(srv, conn)); -#else - NNI_ARG_UNUSED(srv); - NNI_ARG_UNUSED(res); - return (NNG_ENOTSUP); -#endif -} - -nng_err -nng_http_hijack(nng_http *conn) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_hijack(conn)); -#else - NNI_ARG_UNUSED(conn); - return (NNG_ENOTSUP); -#endif -} - -nng_err -nng_http_client_alloc(nng_http_client **clip, const nng_url *url) -{ -#ifdef NNG_SUPP_HTTP - return (nni_http_client_init(clip, url)); -#else - NNI_ARG_UNUSED(clip); - NNI_ARG_UNUSED(url); - return (NNG_ENOTSUP); -#endif -} - -void -nng_http_client_free(nng_http_client *cli) -{ -#ifdef NNG_SUPP_HTTP - nni_http_client_fini(cli); -#else - NNI_ARG_UNUSED(cli); -#endif -} - -nng_err -nng_http_client_set_tls(nng_http_client *cli, nng_tls_config *cfg) -{ -#if defined(NNG_SUPP_HTTP) && defined(NNG_SUPP_TLS) - return (nni_http_client_set_tls(cli, cfg)); -#else - NNI_ARG_UNUSED(cli); - NNI_ARG_UNUSED(cfg); - return (NNG_ENOTSUP); -#endif -} - -nng_err -nng_http_client_get_tls(nng_http_client *cli, nng_tls_config **cfgp) -{ -#if defined(NNG_SUPP_HTTP) && defined(NNG_SUPP_TLS) - return (nni_http_client_get_tls(cli, cfgp)); -#else - NNI_ARG_UNUSED(cli); - NNI_ARG_UNUSED(cfgp); - return (NNG_ENOTSUP); -#endif -} - -void -nng_http_client_connect(nng_http_client *cli, nng_aio *aio) -{ -#ifdef NNG_SUPP_HTTP - nni_http_client_connect(cli, aio); -#else - NNI_ARG_UNUSED(cli); - nni_aio_finish_error(aio, NNG_ENOTSUP); -#endif -} - -void -nng_http_transact(nng_http *conn, nng_aio *aio) -{ -#ifdef NNG_SUPP_HTTP - nni_http_transact_conn(conn, aio); -#else - NNI_ARG_UNUSED(conn); - nni_aio_finish_error(aio, NNG_ENOTSUP); -#endif } void nng_http_reset(nng_http *conn) { -#ifdef NNG_SUPP_HTTP nni_http_conn_reset(conn); -#else - NNI_ARG_UNUSED(req); -#endif } diff --git a/src/supplemental/http/http_server.c b/src/supplemental/http/http_server.c index 725e824ff..717f9a55d 100644 --- a/src/supplemental/http/http_server.c +++ b/src/supplemental/http/http_server.c @@ -48,8 +48,8 @@ struct nng_http_handler { typedef struct http_sconn { nni_list_node node; - nni_http_conn *conn; - nni_http_server *server; + nng_http *conn; + nng_http_server *server; nng_http_handler *handler; // set if we deferred to read body nng_http_handler *release; // set if we dispatched handler bool close; @@ -115,7 +115,7 @@ http_handler_fini(void *arg) } void -nni_http_handler_free(nng_http_handler *h) +nng_http_handler_free(nng_http_handler *h) { if (h) { nni_refcnt_rele(&h->ref); @@ -123,7 +123,7 @@ nni_http_handler_free(nng_http_handler *h) } nng_err -nni_http_handler_alloc( +nng_http_handler_alloc( nng_http_handler **hp, const char *uri, nng_http_handler_func cb) { nng_http_handler *h; @@ -152,14 +152,14 @@ nni_http_handler_alloc( } void -nni_http_handler_collect_body(nng_http_handler *h, bool want, size_t maxbody) +nng_http_handler_collect_body(nng_http_handler *h, bool want, size_t maxbody) { h->getbody = want; h->maxbody = maxbody; } void -nni_http_handler_set_data(nng_http_handler *h, void *data, nni_cb dtor) +nng_http_handler_set_data(nng_http_handler *h, void *data, nni_cb dtor) { NNI_ASSERT(!nni_atomic_get_bool(&h->busy)); h->data = data; @@ -167,7 +167,7 @@ nni_http_handler_set_data(nng_http_handler *h, void *data, nni_cb dtor) } const char * -nni_http_handler_get_uri(nng_http_handler *h) +nng_http_handler_get_uri(nng_http_handler *h) { if (strlen(h->uri) == 0) { return ("/"); @@ -176,14 +176,14 @@ nni_http_handler_get_uri(nng_http_handler *h) } void -nni_http_handler_set_tree(nng_http_handler *h) +nng_http_handler_set_tree(nng_http_handler *h) { NNI_ASSERT(!nni_atomic_get_bool(&h->busy)); h->tree = true; } void -nni_http_handler_set_host(nng_http_handler *h, const char *host) +nng_http_handler_set_host(nng_http_handler *h, const char *host) { NNI_ASSERT(!nni_atomic_get_bool(&h->busy)); @@ -217,7 +217,7 @@ nni_http_handler_set_host(nng_http_handler *h, const char *host) } void -nni_http_handler_set_method(nng_http_handler *h, const char *method) +nng_http_handler_set_method(nng_http_handler *h, const char *method) { NNI_ASSERT(!nni_atomic_get_bool(&h->busy)); if (method == NULL) { @@ -266,7 +266,7 @@ http_sc_reap(void *arg) static void http_sconn_close(http_sconn *sc) { - nni_http_conn *conn; + nng_http *conn; if (nni_atomic_flag_test_and_set(&sc->closed)) { return; @@ -346,7 +346,7 @@ http_sconn_error(http_sconn *sc, nng_http_status err) } nng_err -nni_http_hijack(nni_http_conn *conn) +nng_http_hijack(nng_http *conn) { http_sconn *sc; @@ -488,7 +488,7 @@ http_sconn_rxdone(void *arg) // If the connection was 1.0, or a connection: close was // requested, then mark this close on our end. - if ((val = nni_http_get_header(sc->conn, "Connection")) != NULL) { + if ((val = nng_http_get_header(sc->conn, "Connection")) != NULL) { // HTTP 1.1 says these have to be case insensitive if (nni_strcasestr(val, "close") != NULL) { // In theory this could falsely match some other weird @@ -501,7 +501,7 @@ http_sconn_rxdone(void *arg) } sc->unconsumed_body = 0; - if ((cls = nni_http_get_header(sc->conn, "Content-Length")) != NULL) { + if ((cls = nng_http_get_header(sc->conn, "Content-Length")) != NULL) { char *end; sc->unconsumed_body = strtoull(cls, &end, 10); if ((end == NULL) && (*end != '\0')) { @@ -511,7 +511,7 @@ http_sconn_rxdone(void *arg) } } - host = nni_http_get_header(sc->conn, "Host"); + host = nng_http_get_header(sc->conn, "Host"); if ((host == NULL) && (needhost)) { // Per RFC 2616 14.23 we have to send 400 status here. http_sconn_error(sc, NNG_HTTP_STATUS_BAD_REQUEST); @@ -548,7 +548,7 @@ http_sconn_rxdone(void *arg) break; } // So, what about the method? - val = nni_http_get_method(sc->conn); + val = nng_http_get_method(sc->conn); if (strcmp(val, h->method) == 0) { break; } @@ -614,8 +614,8 @@ http_sconn_rxdone(void *arg) // make sure the response is freshly initialized nni_http_res_reset(nni_http_conn_res(sc->conn)); - nni_http_set_version(sc->conn, NNG_HTTP_VERSION_1_1); - nni_http_set_status(sc->conn, 0, NULL); + nng_http_set_version(sc->conn, NNG_HTTP_VERSION_1_1); + nng_http_set_status(sc->conn, 0, NULL); nni_refcnt_hold(&h->ref); h->cb(sc->conn, h->data, &sc->cbaio); @@ -656,14 +656,14 @@ http_sconn_cbdone(void *arg) const char *val; const char *method; nng_http_status status; - val = nni_http_get_header(sc->conn, "Connection"); - status = nni_http_get_status(sc->conn); - method = nni_http_get_method(sc->conn); + val = nng_http_get_header(sc->conn, "Connection"); + status = nng_http_get_status(sc->conn); + method = nng_http_get_method(sc->conn); if ((val != NULL) && (strstr(val, "close") != NULL)) { sc->close = true; } if (sc->close) { - nni_http_set_header(sc->conn, "Connection", "close"); + nng_http_set_header(sc->conn, "Connection", "close"); } if ((strcmp(method, "HEAD") == 0) && status >= 200 && status <= 299) { @@ -996,7 +996,7 @@ nni_http_server_error(nni_http_server *s, nng_http *conn) { http_error *epage; char *body = NULL; - nng_http_status code = nni_http_get_status(conn); + nng_http_status code = nng_http_get_status(conn); nng_err rv; nni_mtx_lock(&s->errors_mtx); @@ -1193,8 +1193,8 @@ http_handle_file(nng_http *conn, void *arg, nni_aio *aio) nni_aio_finish(aio, NNG_OK, 0); return; } - if (((rv = nni_http_set_header(conn, "Content-Type", ctype)) != 0) || - ((rv = nni_http_copy_body(conn, data, size)) != 0)) { + if (((rv = nng_http_set_header(conn, "Content-Type", ctype)) != 0) || + ((rv = nng_http_copy_body(conn, data, size)) != 0)) { nni_free(data, size); nni_aio_finish_error(aio, rv); return; @@ -1219,7 +1219,7 @@ http_file_free(void *arg) } nng_err -nni_http_handler_file(nng_http_handler **hpp, const char *uri, +nng_http_handler_file(nng_http_handler **hpp, const char *uri, const char *path, const char *ctype) { nng_http_handler *h; @@ -1244,15 +1244,15 @@ nni_http_handler_file(nng_http_handler **hpp, const char *uri, return (NNG_ENOMEM); } - if ((rv = nni_http_handler_alloc(&h, uri, http_handle_file)) != 0) { + if ((rv = nng_http_handler_alloc(&h, uri, http_handle_file)) != 0) { http_file_free(hf); return (rv); } - nni_http_handler_set_data(h, hf, http_file_free); + nng_http_handler_set_data(h, hf, http_file_free); // We don't permit a body for getting a file. - nni_http_handler_collect_body(h, true, 0); + nng_http_handler_collect_body(h, true, 0); *hpp = h; return (NNG_OK); @@ -1267,7 +1267,7 @@ http_handle_dir(nng_http *conn, void *arg, nng_aio *aio) http_file *hf = arg; const char *path = hf->path; const char *base = hf->base; - const char *uri = nni_http_get_uri(conn); + const char *uri = nng_http_get_uri(conn); const char *ctype; char *dst; size_t len; @@ -1384,7 +1384,7 @@ http_handle_dir(nng_http *conn, void *arg, nng_aio *aio) } nng_err -nni_http_handler_directory( +nng_http_handler_directory( nng_http_handler **hpp, const char *uri, const char *path) { http_file *hf; @@ -1400,7 +1400,7 @@ nni_http_handler_directory( return (NNG_ENOMEM); } - if ((rv = nni_http_handler_alloc(&h, uri, http_handle_dir)) != 0) { + if ((rv = nng_http_handler_alloc(&h, uri, http_handle_dir)) != 0) { http_file_free(hf); return (rv); } @@ -1429,7 +1429,7 @@ http_handle_redirect(nng_http *conn, void *data, nng_aio *aio) const char *uri; base = hr->from; // base uri - uri = nni_http_get_uri(conn); + uri = nng_http_get_uri(conn); // If we are doing a full tree, then include the entire suffix. if (strncmp(uri, base, strlen(base)) == 0) { @@ -1447,7 +1447,7 @@ http_handle_redirect(nng_http *conn, void *data, nng_aio *aio) // keeps us from having to consume the entity body, we can just // discard it. if (((rv = nni_http_set_redirect(conn, hr->code, NULL, loc)) != 0) || - ((rv = nni_http_set_header(conn, "Connection", "close")) != 0)) { + ((rv = nng_http_set_header(conn, "Connection", "close")) != 0)) { if (loc != hr->where) { nni_strfree(loc); } @@ -1476,7 +1476,7 @@ http_redirect_free(void *arg) } nng_err -nni_http_handler_redirect(nng_http_handler **hpp, const char *uri, +nng_http_handler_redirect(nng_http_handler **hpp, const char *uri, nng_http_status status, const char *where) { nng_http_handler *h; @@ -1496,19 +1496,19 @@ nni_http_handler_redirect(nng_http_handler **hpp, const char *uri, } hr->code = status; - if ((rv = nni_http_handler_alloc(&h, uri, http_handle_redirect)) != + if ((rv = nng_http_handler_alloc(&h, uri, http_handle_redirect)) != 0) { http_redirect_free(hr); return (rv); } - nni_http_handler_set_method(h, NULL); + nng_http_handler_set_method(h, NULL); - nni_http_handler_set_data(h, hr, http_redirect_free); + nng_http_handler_set_data(h, hr, http_redirect_free); // We don't need to collect the body at all, because the handler // just discards the content and closes the connection. - nni_http_handler_collect_body(h, false, 0); + nng_http_handler_collect_body(h, false, 0); *hpp = h; return (NNG_OK); @@ -1531,8 +1531,8 @@ http_handle_static(nng_http *conn, void *data, nni_aio *aio) } // this cannot fail (no dynamic allocation) - (void) nni_http_set_header(conn, "Content-Type", ctype); - nni_http_set_body(conn, hs->data, hs->size); + (void) nng_http_set_header(conn, "Content-Type", ctype); + nng_http_set_body(conn, hs->data, hs->size); nng_http_set_status(conn, NNG_HTTP_STATUS_OK, NULL); @@ -1552,7 +1552,7 @@ http_static_free(void *arg) } nng_err -nni_http_handler_static(nng_http_handler **hpp, const char *uri, +nng_http_handler_static(nng_http_handler **hpp, const char *uri, const void *data, size_t size, const char *ctype) { nng_http_handler *h; @@ -1570,15 +1570,15 @@ nni_http_handler_static(nng_http_handler **hpp, const char *uri, hs->size = size; memcpy(hs->data, data, size); - if ((rv = nni_http_handler_alloc(&h, uri, http_handle_static)) != 0) { + if ((rv = nng_http_handler_alloc(&h, uri, http_handle_static)) != 0) { http_static_free(hs); return (rv); } - nni_http_handler_set_data(h, hs, http_static_free); + nng_http_handler_set_data(h, hs, http_static_free); // We don't permit a body for getting static data. - nni_http_handler_collect_body(h, true, 0); + nng_http_handler_collect_body(h, true, 0); *hpp = h; return (NNG_OK); diff --git a/src/supplemental/http/http_stubs.c b/src/supplemental/http/http_stubs.c new file mode 100644 index 000000000..f2684e085 --- /dev/null +++ b/src/supplemental/http/http_stubs.c @@ -0,0 +1,431 @@ +// +// Copyright 2025 Staysail Systems, Inc. +// Copyright 2018 Capitar IT Group BV +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +#include "core/defs.h" +#include "nng/http.h" + +// Stubs for common API functions. +// +const char * +nng_http_get_header(nng_http *conn, const char *key) +{ + NNI_ARG_UNUSED(conn); + NNI_ARG_UNUSED(key); + return (NULL); +} + +nng_err +nng_http_set_header(nng_http *conn, const char *key, const char *val) +{ + NNI_ARG_UNUSED(conn); + NNI_ARG_UNUSED(key); + NNI_ARG_UNUSED(val); + return (NNG_ENOTSUP); +} + +nng_err +nng_http_add_header(nng_http *conn, const char *key, const char *val) +{ + NNI_ARG_UNUSED(conn); + NNI_ARG_UNUSED(key); + NNI_ARG_UNUSED(val); + return (NNG_ENOTSUP); +} + +void +nng_http_del_header(nng_http *conn, const char *key) +{ + NNI_ARG_UNUSED(conn); + NNI_ARG_UNUSED(key); +} + +void +nng_http_set_body(nng_http *conn, void *data, size_t sz) +{ + NNI_ARG_UNUSED(conn); + NNI_ARG_UNUSED(data); + NNI_ARG_UNUSED(sz); +} + +nng_err +nng_http_copy_body(nng_http *conn, const void *data, size_t len) +{ + NNI_ARG_UNUSED(conn); + NNI_ARG_UNUSED(data); + NNI_ARG_UNUSED(len); + return (NNG_ENOTSUP); +} + +void +nng_http_get_body(nng_http *conn, void **datap, size_t *lenp) +{ + NNI_ARG_UNUSED(conn); + NNI_ARG_UNUSED(datap); + NNI_ARG_UNUSED(lenp); +} + +const char * +nng_http_get_uri(nng_http *conn) +{ + NNI_ARG_UNUSED(conn); + return (NULL); +} + +nng_err +nng_http_set_uri(nng_http *conn, const char *uri, const char *query) +{ + NNI_ARG_UNUSED(conn); + NNI_ARG_UNUSED(uri); + NNI_ARG_UNUSED(query); + return (NNG_ENOTSUP); +} + +const char * +nng_http_get_version(nng_http *conn) +{ + NNI_ARG_UNUSED(conn); + return (NULL); +} + +void +nng_http_set_status(nng_http *conn, nng_http_status status, const char *reason) +{ + NNI_ARG_UNUSED(conn); + NNI_ARG_UNUSED(status); + NNI_ARG_UNUSED(reason); +} + +nng_http_status +nng_http_get_status(nng_http *conn) +{ + NNI_ARG_UNUSED(conn); + return (0); +} + +const char * +nng_http_get_reason(nng_http *conn) +{ + NNI_ARG_UNUSED(conn); + return (NULL); +} + +nng_err +nng_http_set_version(nng_http *conn, const char *version) +{ + NNI_ARG_UNUSED(conn); + NNI_ARG_UNUSED(version); + return (NNG_ENOTSUP); +} + +void +nng_http_set_method(nng_http *conn, const char *method) +{ + NNI_ARG_UNUSED(conn); + NNI_ARG_UNUSED(method); +} + +const char * +nng_http_get_method(nng_http *conn) +{ + NNI_ARG_UNUSED(conn); + return (NULL); +} + +void +nng_http_close(nng_http *conn) +{ + NNI_ARG_UNUSED(conn); +} + +void +nng_http_read(nng_http *conn, nng_aio *aio) +{ + NNI_ARG_UNUSED(conn); + nng_aio_finish(aio, NNG_ENOTSUP); +} + +void +nng_http_read_all(nng_http *conn, nng_aio *aio) +{ + NNI_ARG_UNUSED(conn); + nng_aio_finish(aio, NNG_ENOTSUP); +} + +void +nng_http_write(nng_http *conn, nng_aio *aio) +{ + NNI_ARG_UNUSED(conn); + nng_aio_finish(aio, NNG_ENOTSUP); +} + +void +nng_http_write_all(nng_http *conn, nng_aio *aio) +{ + NNI_ARG_UNUSED(conn); + nng_aio_finish(aio, NNG_ENOTSUP); +} + +void +nng_http_write_request(nng_http *conn, nng_aio *aio) +{ + NNI_ARG_UNUSED(conn); + nng_aio_finish(aio, NNG_ENOTSUP); +} + +void +nng_http_write_response(nng_http *conn, nng_aio *aio) +{ + NNI_ARG_UNUSED(conn); + nng_aio_finish(aio, NNG_ENOTSUP); +} + +void +nng_http_read_response(nng_http *conn, nng_aio *aio) +{ + NNI_ARG_UNUSED(conn); + nng_aio_finish(aio, NNG_ENOTSUP); +} + +nng_err +nng_http_handler_alloc( + nng_http_handler **hp, const char *uri, nng_http_handler_func cb) +{ + NNI_ARG_UNUSED(hp); + NNI_ARG_UNUSED(uri); + NNI_ARG_UNUSED(cb); + return (NNG_ENOTSUP); +} + +void +nng_http_handler_free(nng_http_handler *h) +{ + NNI_ARG_UNUSED(h); +} + +nng_err +nng_http_handler_file(nng_http_handler **hp, const char *uri, const char *path, + const char *ctype) +{ + NNI_ARG_UNUSED(hp); + NNI_ARG_UNUSED(uri); + NNI_ARG_UNUSED(path); + NNI_ARG_UNUSED(ctype); + return (NNG_ENOTSUP); +} + +nng_err +nng_http_handler_directory( + nng_http_handler **hp, const char *uri, const char *path) +{ + NNI_ARG_UNUSED(hp); + NNI_ARG_UNUSED(uri); + NNI_ARG_UNUSED(path); + return (NNG_ENOTSUP); +} + +nng_err +nng_http_handler_redirect(nng_http_handler **hp, const char *uri, + nng_http_status status, const char *where) +{ + NNI_ARG_UNUSED(hp); + NNI_ARG_UNUSED(uri); + NNI_ARG_UNUSED(status); + NNI_ARG_UNUSED(where); + return (NNG_ENOTSUP); +} + +nng_err +nng_http_handler_static(nng_http_handler **hp, const char *uri, + const void *data, size_t size, const char *ctype) +{ + NNI_ARG_UNUSED(hp); + NNI_ARG_UNUSED(uri); + NNI_ARG_UNUSED(data); + NNI_ARG_UNUSED(size); + NNI_ARG_UNUSED(ctype); + return (NNG_ENOTSUP); +} + +void +nng_http_handler_set_method(nng_http_handler *h, const char *meth) +{ + NNI_ARG_UNUSED(h); + NNI_ARG_UNUSED(meth); +} + +void +nng_http_handler_collect_body(nng_http_handler *h, bool want, size_t len) +{ + NNI_ARG_UNUSED(h); + NNI_ARG_UNUSED(want); + NNI_ARG_UNUSED(len); +} + +void +nng_http_handler_set_host(nng_http_handler *h, const char *host) +{ + NNI_ARG_UNUSED(h); + NNI_ARG_UNUSED(host); +} + +void +nng_http_handler_set_tree(nng_http_handler *h) +{ + NNI_ARG_UNUSED(h); +} + +void +nng_http_handler_set_data(nng_http_handler *h, void *dat, void (*dtor)(void *)) +{ + NNI_ARG_UNUSED(h); + NNI_ARG_UNUSED(dat); + NNI_ARG_UNUSED(dtor); +} + +nng_err +nng_http_server_hold(nng_http_server **srvp, const nng_url *url) +{ + NNI_ARG_UNUSED(srvp); + NNI_ARG_UNUSED(url); + return (NNG_ENOTSUP); +} + +void +nng_http_server_release(nng_http_server *srv) +{ + NNI_ARG_UNUSED(srv); +} + +nng_err +nng_http_server_start(nng_http_server *srv) +{ + NNI_ARG_UNUSED(srv); + return (NNG_ENOTSUP); +} + +void +nng_http_server_stop(nng_http_server *srv) +{ + NNI_ARG_UNUSED(srv); +} + +nng_err +nng_http_server_add_handler(nng_http_server *srv, nng_http_handler *h) +{ + NNI_ARG_UNUSED(srv); + NNI_ARG_UNUSED(h); + return (NNG_ENOTSUP); +} + +nng_err +nng_http_server_del_handler(nng_http_server *srv, nng_http_handler *h) +{ + NNI_ARG_UNUSED(srv); + NNI_ARG_UNUSED(h); + return (NNG_ENOTSUP); +} + +nng_err +nng_http_server_set_error_page( + nng_http_server *srv, nng_http_status code, const char *body) +{ + NNI_ARG_UNUSED(srv); + NNI_ARG_UNUSED(code); + NNI_ARG_UNUSED(body); + return (NNG_ENOTSUP); +} + +nng_err +nng_http_server_set_tls(nng_http_server *srv, nng_tls_config *cfg) +{ + NNI_ARG_UNUSED(srv); + NNI_ARG_UNUSED(cfg); +} + +nng_err +nng_http_server_get_tls(nng_http_server *srv, nng_tls_config **cfg) +{ + NNI_ARG_UNUSED(srv); + NNI_ARG_UNUSED(cfg); + return (NNG_ENOTSUP); +} + +nng_err +nng_http_server_get_addr(nng_http_server *srv, nng_sockaddr *addr) +{ + NNI_ARG_UNUSED(srv); + NNI_ARG_UNUSED(addr); + return (NNG_ENOTSUP); +} + +nng_err +nng_http_server_error(nng_http_server *srv, nng_http *conn) +{ + NNI_ARG_UNUSED(srv); + NNI_ARG_UNUSED(conn); + return (NNG_ENOTSUP); +} + +nng_err +nng_http_hijack(nng_http *conn) +{ + NNI_ARG_UNUSED(conn); + return (NNG_ENOTSUP); +} + +nng_err +nng_http_client_alloc(nng_http_client **clip, const nng_url *url) +{ + NNI_ARG_UNUSED(clip); + NNI_ARG_UNUSED(url); + return (NNG_ENOTSUP); +} + +void +nng_http_client_free(nng_http_client *cli) +{ + NNI_ARG_UNUSED(cli); +} + +nng_err +nng_http_client_set_tls(nng_http_client *cli, nng_tls_config *cfg) +{ + NNI_ARG_UNUSED(cli); + NNI_ARG_UNUSED(cfg); + return (NNG_ENOTSUP); +} + +nng_err +nng_http_client_get_tls(nng_http_client *cli, nng_tls_config **cfgp) +{ + NNI_ARG_UNUSED(cli); + NNI_ARG_UNUSED(cfgp); + return (NNG_ENOTSUP); +} + +void +nng_http_client_connect(nng_http_client *cli, nng_aio *aio) +{ + NNI_ARG_UNUSED(cli); + nng_aio_finish(aio, NNG_ENOTSUP); +} + +void +nng_http_transact(nng_http *conn, nng_aio *aio) +{ + NNI_ARG_UNUSED(conn); + nng_aio_finish(aio, NNG_ENOTSUP); +} + +void +nng_http_reset(nng_http *conn) +{ + NNI_ARG_UNUSED(conn); +} diff --git a/src/supplemental/websocket/websocket.c b/src/supplemental/websocket/websocket.c index 08b3a0710..f40fbb4a4 100644 --- a/src/supplemental/websocket/websocket.c +++ b/src/supplemental/websocket/websocket.c @@ -121,7 +121,7 @@ struct nni_ws_dialer { nng_stream_dialer ops; nni_http_req *req; nni_http_res *res; - nni_http_client *client; + nng_http_client *client; nni_mtx mtx; nni_cv cv; char *proto; @@ -1279,7 +1279,7 @@ ws_http_cb_dialer(nni_ws *ws, nni_aio *aio) return; } - status = nni_http_get_status(ws->http); + status = nng_http_get_status(ws->http); switch (status) { case NNG_HTTP_STATUS_SWITCHING: break; @@ -1585,7 +1585,7 @@ ws_handler(nng_http *conn, void *arg, nng_aio *aio) nni_list_append(&l->reply, ws); nng_http_write_response(conn, &ws->httpaio); - (void) nni_http_hijack(conn); + (void) nng_http_hijack(conn); nni_aio_set_output(aio, 0, NULL); nni_aio_finish(aio, 0, 0); nni_mtx_unlock(&l->mtx); @@ -2026,8 +2026,8 @@ nni_ws_listener_alloc(nng_stream_listener **wslp, const nng_url *url) return (rv); } - nni_http_handler_set_host(l->handler, host); - nni_http_handler_set_data(l->handler, l, 0); + nng_http_handler_set_host(l->handler, host); + nng_http_handler_set_data(l->handler, l, 0); if ((rv = nni_http_server_init(&l->server, url)) != 0) { ws_listener_free(l); @@ -2103,7 +2103,7 @@ ws_conn_cb(void *arg) nni_base64_encode(raw, 16, ws->keybuf, 24); ws->keybuf[24] = '\0'; - if ((rv = nni_http_set_uri( + if ((rv = nng_http_set_uri( ws->http, d->url->u_path, d->url->u_query)) != 0) { goto err; } @@ -2123,7 +2123,7 @@ ws_conn_cb(void *arg) } NNI_LIST_FOREACH (&d->headers, hdr) { - if ((rv = nni_http_set_header( + if ((rv = nng_http_set_header( ws->http, hdr->name, hdr->value)) != 0) { goto err; } @@ -2186,7 +2186,7 @@ ws_dialer_free(void *arg) NNI_FREE_STRUCT(hdr); } if (d->client) { - nni_http_client_fini(d->client); + nng_http_client_free(d->client); } if (d->url) { nng_url_free(d->url); @@ -2243,7 +2243,7 @@ ws_dialer_dial(void *arg, nni_aio *aio) ws->recv_text = d->recv_text; ws->send_text = d->send_text; nni_list_append(&d->wspend, ws); - nni_http_client_connect(d->client, &ws->connaio); + nng_http_client_connect(d->client, &ws->connaio); nni_mtx_unlock(&d->mtx); } @@ -2510,14 +2510,14 @@ static int ws_dialer_get_tls(void *arg, nng_tls_config **cfgp) { nni_ws_dialer *d = arg; - return (nni_http_client_get_tls(d->client, cfgp)); + return (nng_http_client_get_tls(d->client, cfgp)); } static int ws_dialer_set_tls(void *arg, nng_tls_config *cfg) { nni_ws_dialer *d = arg; - return (nni_http_client_set_tls(d->client, cfg)); + return (nng_http_client_set_tls(d->client, cfg)); } int @@ -2540,7 +2540,7 @@ nni_ws_dialer_alloc(nng_stream_dialer **dp, const nng_url *url) return (rv); } - if ((rv = nni_http_client_init(&d->client, url)) != 0) { + if ((rv = nng_http_client_alloc(&d->client, url)) != 0) { ws_dialer_free(d); return (rv); } @@ -2666,7 +2666,7 @@ static int ws_get_request_uri(void *arg, void *buf, size_t *szp, nni_type t) { nni_ws *ws = arg; - return (nni_copyout_str(nni_http_get_uri(ws->http), buf, szp, t)); + return (nni_copyout_str(nng_http_get_uri(ws->http), buf, szp, t)); } static int @@ -2716,7 +2716,7 @@ ws_get_header(nni_ws *ws, const char *nm, void *buf, size_t *szp, nni_type t) { const char *s; nm += strlen(NNG_OPT_WS_HEADER); - s = nni_http_get_header(ws->http, nm); + s = nng_http_get_header(ws->http, nm); if (s == NULL) { return (NNG_ENOENT); }