diff options
| -rw-r--r-- | docs/man/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | docs/man/libnng.3.adoc | 2 | ||||
| -rw-r--r-- | docs/man/nng_http_req_alloc.3http.adoc | 5 | ||||
| -rw-r--r-- | docs/man/nng_http_req_free.3http.adoc | 4 | ||||
| -rw-r--r-- | docs/man/nng_http_req_reset.3http.adoc | 48 | ||||
| -rw-r--r-- | docs/man/nng_http_res_alloc.3http.adoc | 5 | ||||
| -rw-r--r-- | docs/man/nng_http_res_free.3http.adoc | 6 | ||||
| -rw-r--r-- | docs/man/nng_http_res_reset.3http.adoc | 44 | ||||
| -rw-r--r-- | src/supplemental/http/http.h | 6 | ||||
| -rw-r--r-- | src/supplemental/http/http_client.c | 1 | ||||
| -rw-r--r-- | src/supplemental/http/http_msg.c | 25 | ||||
| -rw-r--r-- | src/supplemental/http/http_public.c | 16 |
12 files changed, 146 insertions, 18 deletions
diff --git a/docs/man/CMakeLists.txt b/docs/man/CMakeLists.txt index e39f2da3..5381f1e5 100644 --- a/docs/man/CMakeLists.txt +++ b/docs/man/CMakeLists.txt @@ -238,6 +238,7 @@ if (NNG_ENABLE_DOC) nng_http_req_get_method nng_http_req_get_uri nng_http_req_get_version + nng_http_req_reset nng_http_req_set_data nng_http_req_set_header nng_http_req_set_method @@ -254,6 +255,7 @@ if (NNG_ENABLE_DOC) nng_http_res_get_reason nng_http_res_get_status nng_http_res_get_version + nng_http_res_reset nng_http_res_set_data nng_http_res_set_header nng_http_res_set_reason diff --git a/docs/man/libnng.3.adoc b/docs/man/libnng.3.adoc index 299b73ef..0a62f931 100644 --- a/docs/man/libnng.3.adoc +++ b/docs/man/libnng.3.adoc @@ -311,6 +311,7 @@ and connections. |<<nng_http_req_get_method.3http#,nng_http_req_get_method()>>|return HTTP request method |<<nng_http_req_get_uri.3http#,nng_http_req_get_uri()>>|return HTTP request URI |<<nng_http_req_get_version.3http#,nng_http_req_get_version()>>|return HTTP request protocol version +|<<nng_http_req_reset.3http#,nng_http_req_reset()>>|reset HTTP request structure |<<nng_http_req_set_data.3http#,nng_http_req_set_data()>>|set HTTP request body |<<nng_http_req_set_header.3http#,nng_http_req_set_header()>>|set HTTP request header |<<nng_http_req_set_method.3http#,nng_http_req_set_method()>>|set HTTP request method @@ -327,6 +328,7 @@ and connections. |<<nng_http_res_get_reason.3http#,nng_http_res_get_reason()>>|return HTTP response reason |<<nng_http_res_get_status.3http#,nng_http_res_get_status()>>|return HTTP response status |<<nng_http_res_get_version.3http#,nng_http_res_get_version()>>|return HTTP response protocol version +|<<nng_http_res_reset.3http#,nng_http_res_reset()>>|reset HTTP response structure |<<nng_http_res_set_data.3http#,nng_http_res_set_data()>>|set HTTP response body |<<nng_http_res_set_header.3http#,nng_http_res_set_header()>>|set HTTP response header |<<nng_http_res_set_reason.3http#,nng_http_res_set_reason()>>|set HTTP response reason diff --git a/docs/man/nng_http_req_alloc.3http.adoc b/docs/man/nng_http_req_alloc.3http.adoc index 3a522b3e..fe7458de 100644 --- a/docs/man/nng_http_req_alloc.3http.adoc +++ b/docs/man/nng_http_req_alloc.3http.adoc @@ -30,6 +30,10 @@ and stores a pointer to it in __reqp__. The request will be initialized to perform an HTTP/1.1 `GET` operation using the URL specified in __url__. +TIP: It is possible to specify `NULL` for the URL. +In this case the URI for the request must be specified by a subsequent call +to `<<nng_http_req_set_uri.3http#,nng_http_req_set_uri()>>`. + == RETURN VALUES This function returns 0 on success, and non-zero otherwise. @@ -53,6 +57,7 @@ This function returns 0 on success, and non-zero otherwise. <<nng_http_req_get_method.3http#,nng_http_req_get_method(3http)>>, <<nng_http_req_get_uri.3http#,nng_http_req_get_uri(3http)>>, <<nng_http_req_get_version.3http#,nng_http_req_get_version(3http)>>, +<<nng_http_req_reset.3http#,nng_http_req_reset(3http)>>, <<nng_http_req_set_data.3http#,nng_http_req_set_data(3http)>>, <<nng_http_req_set_method.3http#,nng_http_req_set_method(3http)>>, <<nng_http_req_set_uri.3http#,nng_http_req_set_uri(3http)>>, diff --git a/docs/man/nng_http_req_free.3http.adoc b/docs/man/nng_http_req_free.3http.adoc index df86c5da..2564c422 100644 --- a/docs/man/nng_http_req_free.3http.adoc +++ b/docs/man/nng_http_req_free.3http.adoc @@ -28,6 +28,9 @@ void nng_http_req_free(nng_http_req *req); The `nng_http_req_free()` function deallocates the HTTP request structure _req_ entirely. +TIP: Instead of freeing and reallocating request structures, it is possible +to reuse _req_ with `<<nng_http_req_reset.3http#,nng_http_req_reset()>>`. + == RETURN VALUES None. @@ -40,4 +43,5 @@ None. [.text-left] <<nng_http_req_alloc.3http#,nng_http_req_alloc(3http)>>, +<<nng_http_req_reset.3http#,nng_http_req_reset(3http)>>, <<nng.7#,nng(7)>> diff --git a/docs/man/nng_http_req_reset.3http.adoc b/docs/man/nng_http_req_reset.3http.adoc new file mode 100644 index 00000000..9aab704a --- /dev/null +++ b/docs/man/nng_http_req_reset.3http.adoc @@ -0,0 +1,48 @@ += nng_http_req_reset(3http) +// +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// +// 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_req_reset - reset HTTP request structure + +== SYNOPSIS + +[source, c] +---- +#include <nng/nng.h> +#include <nng/supplemental/http/http.h> + +void nng_http_req_reset(nng_http_req *req); +---- + +== DESCRIPTION + +The `nng_http_req_reset()` function resets the request __req__ so that it +is just as if it had been freshly allocated with +`<<nng_http_req_alloc.3http#,nng_http_req_alloc()>>` with a `NULL` URL. + +NOTE: Before using this with an HTTP operation, the URI must be set using +`<<nng_http_req_set_uri.3http#,nng_http_req_set_uri()>>`. + +== RETURN VALUES + +None. + +== ERRORS + +None. + +== SEE ALSO + +[.text-left] +<<nng_http_req_alloc.3http#,nng_http_req_alloc(3http)>>, +<<nng_http_req_set_uri.3http#,nng_http_req_set_uri(3http)>>, +<<nng.7#,nng(7)>> diff --git a/docs/man/nng_http_res_alloc.3http.adoc b/docs/man/nng_http_res_alloc.3http.adoc index 2b7699a7..b395b502 100644 --- a/docs/man/nng_http_res_alloc.3http.adoc +++ b/docs/man/nng_http_res_alloc.3http.adoc @@ -28,8 +28,8 @@ int nng_http_res_alloc(nng_http_res **resp); The `nng_http_res_alloc()` function allocates a new HTTP response structure and stores a pointer to it in __resp__. The response will be initialized -with status code 200 (`NNG_HTTP_STATUS_OK`), and a reason phrase of "OK", -and HTTP protocol version "HTTP/1.1". +with status code 200 (`NNG_HTTP_STATUS_OK`), and a reason phrase of `"OK`", +and HTTP protocol version `"HTTP/1.1`". TIP: When an error response is needed, consider using `<<nng_http_res_alloc_error.3http#,nng_http_res_alloc_error()>>` instead. @@ -59,6 +59,7 @@ This function returns 0 on success, and non-zero otherwise. <<nng_http_res_get_reason.3http#,nng_http_res_get_reason(3http)>>, <<nng_http_res_get_status.3http#,nng_http_res_get_status(3http)>>, <<nng_http_res_get_version.3http#,nng_http_res_get_version(3http)>>, +<<nng_http_res_reset.3http#,nng_http_res_reset(3http)>>, <<nng_http_res_set_data.3http#,nng_http_res_set_data(3http)>>, <<nng_http_res_set_reason.3http#,nng_http_res_set_reason(3http)>>, <<nng_http_res_set_status.3http#,nng_http_res_set_status(3http)>>, diff --git a/docs/man/nng_http_res_free.3http.adoc b/docs/man/nng_http_res_free.3http.adoc index 0c7f2445..e97f0be6 100644 --- a/docs/man/nng_http_res_free.3http.adoc +++ b/docs/man/nng_http_res_free.3http.adoc @@ -28,6 +28,9 @@ void nng_http_res_free(nng_http_res *req); The `nng_http_res_free()` function deallocates the HTTP response structure _res_ entirely. +TIP: Instead of freeing and reallocating response structures, it is possible +to reuse _res_ with `<<nng_http_res_reset.3http#,nng_http_res_reset()>>`. + == RETURN VALUES None. @@ -39,5 +42,6 @@ None. == SEE ALSO [.text-left] -<<nng_http_res_alloc.3http#,nng_http_req_alloc(3http)>>, +<<nng_http_res_alloc.3http#,nng_http_res_alloc(3http)>>, +<<nng_http_res_reset.3http#,nng_http_res_reset(3http)>>, <<nng.7#,nng(7)>> diff --git a/docs/man/nng_http_res_reset.3http.adoc b/docs/man/nng_http_res_reset.3http.adoc new file mode 100644 index 00000000..f33a0207 --- /dev/null +++ b/docs/man/nng_http_res_reset.3http.adoc @@ -0,0 +1,44 @@ += nng_http_res_reset(3http) +// +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// +// 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_res_reset - reset HTTP response structure + +== SYNOPSIS + +[source, c] +---- +#include <nng/nng.h> +#include <nng/supplemental/http/http.h> + +void nng_http_res_reset(nng_http_res *res); +---- + +== DESCRIPTION + +The `nng_http_res_reset()` function resets the response __res__ so that it +is just as if it had been freshly allocated with +`<<nng_http_res_alloc.3http#,nng_http_res_alloc()>>`. + +== RETURN VALUES + +None. + +== ERRORS + +None. + +== SEE ALSO + +[.text-left] +<<nng_http_res_alloc.3http#,nng_http_res_alloc(3http)>>, +<<nng.7#,nng(7)>> diff --git a/src/supplemental/http/http.h b/src/supplemental/http/http.h index f6656fce..cffd1a8b 100644 --- a/src/supplemental/http/http.h +++ b/src/supplemental/http/http.h @@ -275,6 +275,12 @@ NNG_DECL void nng_http_conn_read_req( NNG_DECL void nng_http_conn_read_res( nng_http_conn *, nng_http_res *, nng_aio *); +// nng_http_req_reset resets the request to an initially allocated state. +NNG_DECL void nng_http_req_reset(nng_http_req *); + +// nng_http_res_reset resets the response to an initially allocated state. +NNG_DECL void nng_http_res_reset(nng_http_res *); + // nng_http_handler is a handler used on the server side to handle HTTP // requests coming into a specific URL. typedef struct nng_http_handler nng_http_handler; diff --git a/src/supplemental/http/http_client.c b/src/supplemental/http/http_client.c index f8b1c8ab..a61f7884 100644 --- a/src/supplemental/http/http_client.c +++ b/src/supplemental/http/http_client.c @@ -303,6 +303,7 @@ http_txn_reap(void *arg) // We only close the connection if we created it. if (txn->conn != NULL) { nni_http_conn_fini(txn->conn); + txn->conn = NULL; } } nni_aio_fini(txn->aio); diff --git a/src/supplemental/http/http_msg.c b/src/supplemental/http/http_msg.c index 1a651352..6d7e9f8a 100644 --- a/src/supplemental/http/http_msg.c +++ b/src/supplemental/http/http_msg.c @@ -103,9 +103,9 @@ nni_http_req_reset(nni_http_req *req) nni_strfree(req->meth); nni_strfree(req->uri); req->vers = req->meth = req->uri = NULL; - if (req->bufsz) { - req->buf[0] = '\0'; - } + nni_free(req->buf, req->bufsz); + req->bufsz = 0; + req->buf = NULL; req->parsed = false; } @@ -120,18 +120,15 @@ nni_http_res_reset(nni_http_res *res) res->rsn = NULL; res->code = 0; res->parsed = false; - if (res->bufsz) { - res->buf[0] = '\0'; - } + nni_free(res->buf, res->bufsz); + res->buf = NULL; + res->bufsz = 0; } void nni_http_req_free(nni_http_req *req) { nni_http_req_reset(req); - if (req->bufsz) { - nni_free(req->buf, req->bufsz); - } NNI_FREE_STRUCT(req); } @@ -139,9 +136,6 @@ void nni_http_res_free(nni_http_res *res) { nni_http_res_reset(res); - if (res->bufsz) { - nni_free(res->buf, res->bufsz); - } NNI_FREE_STRUCT(res); } @@ -490,7 +484,7 @@ http_asprintf(char **bufp, size_t *szp, nni_list *hdrs, const char *fmt, ...) va_end(ap); len += http_sprintf_headers(NULL, 0, hdrs); - len += 5; // \r\n\r\n\0 + len += 3; // \r\n\0 if (len <= *szp) { buf = *bufp; @@ -511,6 +505,7 @@ http_asprintf(char **bufp, size_t *szp, nni_list *hdrs, const char *fmt, ...) buf += n; len -= n; snprintf(buf, len, "\r\n"); + NNI_ASSERT(len == 3); return (0); } @@ -572,7 +567,7 @@ nni_http_req_get_buf(nni_http_req *req, void **data, size_t *szp) return (rv); } *data = req->buf; - *szp = strlen(req->buf); + *szp = req->bufsz - 1; // exclude terminating NUL return (0); } @@ -585,7 +580,7 @@ nni_http_res_get_buf(nni_http_res *res, void **data, size_t *szp) return (rv); } *data = res->buf; - *szp = strlen(res->buf); + *szp = res->bufsz - 1; // exclude terminating NUL return (0); } diff --git a/src/supplemental/http/http_public.c b/src/supplemental/http/http_public.c index 84811e54..389b74c8 100644 --- a/src/supplemental/http/http_public.c +++ b/src/supplemental/http/http_public.c @@ -867,3 +867,19 @@ nng_http_conn_transact( } #endif } + +void +nng_http_req_reset(nng_http_req *req) +{ +#ifdef NNG_SUPP_HTTP + nni_http_req_reset(req); +#endif +} + +void +nng_http_res_reset(nng_http_res *res) +{ +#ifdef NNG_SUPP_HTTP + nni_http_res_reset(res); +#endif +} |
