diff options
| author | Garrett D'Amore <garrett@damore.org> | 2025-01-12 15:33:59 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2025-01-12 15:33:59 -0800 |
| commit | 9368d350f363597fb59ac85cada364c42793f0e2 (patch) | |
| tree | e6099f1da52c6399b396d48aceb4313bff89c7bc /src | |
| parent | e198d155ae66fb0940362e7a6b8251999956059b (diff) | |
| download | nng-9368d350f363597fb59ac85cada364c42793f0e2.tar.gz nng-9368d350f363597fb59ac85cada364c42793f0e2.tar.bz2 nng-9368d350f363597fb59ac85cada364c42793f0e2.zip | |
http: use nng_err throughout
Diffstat (limited to 'src')
| -rw-r--r-- | src/supplemental/http/http_api.h | 88 | ||||
| -rw-r--r-- | src/supplemental/http/http_chunk.c | 54 | ||||
| -rw-r--r-- | src/supplemental/http/http_client.c | 28 | ||||
| -rw-r--r-- | src/supplemental/http/http_conn.c | 96 | ||||
| -rw-r--r-- | src/supplemental/http/http_msg.c | 38 | ||||
| -rw-r--r-- | src/supplemental/http/http_server.c | 118 |
6 files changed, 203 insertions, 219 deletions
diff --git a/src/supplemental/http/http_api.h b/src/supplemental/http/http_api.h index 846d1c19..61c6e45f 100644 --- a/src/supplemental/http/http_api.h +++ b/src/supplemental/http/http_api.h @@ -34,20 +34,20 @@ typedef struct nng_http_chunks nni_http_chunks; // These functions are private to the internal framework, and really should // not be used elsewhere. -extern void nni_http_req_init(nni_http_req *); -extern void nni_http_req_reset(nni_http_req *); -extern int nni_http_req_parse(nng_http *, void *, size_t, size_t *); +extern void nni_http_req_init(nni_http_req *); +extern void nni_http_req_reset(nni_http_req *); +extern nng_err nni_http_req_parse(nng_http *, void *, size_t, size_t *); -extern void nni_http_res_init(nni_http_res *); -extern void nni_http_res_reset(nni_http_res *); -extern int nni_http_res_parse(nng_http *, void *, size_t, size_t *); +extern void nni_http_res_init(nni_http_res *); +extern void nni_http_res_reset(nni_http_res *); +extern nng_err nni_http_res_parse(nng_http *, void *, size_t, size_t *); // Chunked transfer encoding. For the moment this is not part of our public // API. We can change that later. // nni_http_chunk_list_init creates a list of chunks, which shall not exceed // the specified overall size. (Size 0 means no limit.) -extern int nni_http_chunks_init(nni_http_chunks **, size_t); +extern nng_err nni_http_chunks_init(nni_http_chunks **, size_t); extern void nni_http_chunks_free(nni_http_chunks *); @@ -64,7 +64,8 @@ extern size_t nni_http_chunk_size(nni_http_chunk *); // nni_http_chunk_data returns a pointer to the data. extern void *nni_http_chunk_data(nni_http_chunk *); -extern int nni_http_chunks_parse(nni_http_chunks *, void *, size_t, size_t *); +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 *); @@ -93,7 +94,7 @@ extern void *nni_http_conn_get_ctx(nni_http_conn *); // These initialization functions create stream for HTTP transactions. // They should only be used by the server or client HTTP implementations, // and are not for use by other code. -extern int nni_http_conn_init(nng_http **, nng_stream *, bool); +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 *); @@ -105,19 +106,14 @@ 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 int nni_http_req_alloc(nni_http_req **, const nng_url *); -extern int nni_http_res_set_error(nni_http_res *, nng_http_status); -extern void nni_http_req_free(nni_http_req *); -extern void nni_http_res_free(nni_http_res *); 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 int nni_http_req_add_header(nni_http_req *, const char *, const char *); -extern int nni_http_req_alloc_data(nni_http_req *, size_t); -extern int nni_http_res_alloc_data(nni_http_res *, size_t); +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 *); @@ -126,14 +122,14 @@ 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 int nni_http_add_header(nng_http *, const char *, const char *); -extern int nni_http_set_header(nng_http *, const char *, const char *); +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 int nni_http_copy_body(nng_http *, const void *, size_t); +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); // prune body clears the outgoing body (0 bytes), but leaves content-length // intact if present for the benefit of HEAD. @@ -147,7 +143,7 @@ extern void nni_http_prune_body(nng_http *); // a restricted binding is required, we recommend using a URL consisting // of an empty host name, such as http:// or https:// -- this would // convert to binding to the default port on all interfaces on the host. -extern int nni_http_server_init(nni_http_server **, const nng_url *); +extern nng_err nni_http_server_init(nni_http_server **, const nng_url *); // nni_http_server_fini drops the reference count on the server, and // if this was the last reference, closes down the server and frees @@ -158,13 +154,15 @@ extern void nni_http_server_fini(nni_http_server *); // This function will return NNG_EADDRINUSE if a conflicting handler // is already registered (i.e. a handler with the same value for Host, // Method, and URL.) -extern int nni_http_server_add_handler(nni_http_server *, nni_http_handler *); +extern nng_err nni_http_server_add_handler( + nni_http_server *, nni_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 int nni_http_server_del_handler(nni_http_server *, nni_http_handler *); +extern nng_err nni_http_server_del_handler( + nni_http_server *, nni_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 @@ -172,12 +170,13 @@ extern int nni_http_server_del_handler(nni_http_server *, nni_http_handler *); // server 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 int nni_http_server_set_tls(nni_http_server *, struct nng_tls_config *); +extern nng_err nni_http_server_set_tls( + nni_http_server *, struct nng_tls_config *); // nni_http_server_get_tls obtains the TLS configuration if one is present, // or returns NNG_EINVAL. The TLS configuration is invalidated if the // nni_http_server_set_tls function is called, so be careful. -extern int nni_http_server_get_tls( +extern nng_err nni_http_server_get_tls( nni_http_server *, struct nng_tls_config **); extern int nni_http_server_set( @@ -186,7 +185,7 @@ extern int nni_http_server_get( nni_http_server *, const char *, void *, size_t *, nni_type); // nni_http_server_start starts listening on the supplied port. -extern int nni_http_server_start(nni_http_server *); +extern nng_err nni_http_server_start(nni_http_server *); // nni_http_server_stop stops the server, closing the listening socket. // Connections that have been "upgraded" are unaffected. Connections @@ -201,13 +200,13 @@ extern void nni_http_server_stop(nni_http_server *); extern void nni_http_server_close(nni_http_server *); // nni_http_server_set_error_page sets an error page for the named status. -extern int nni_http_server_set_error_page( +extern nng_err nni_http_server_set_error_page( nni_http_server *, nng_http_status, const char *); // nni_http_server_res_error takes replaces the body of the res with // a custom error page previously set for the server, using the status // of the res. The res must have the status set first. -extern int nni_http_server_error(nni_http_server *, nng_http *); +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 @@ -219,7 +218,7 @@ extern int nni_http_server_error(nni_http_server *, nng_http *); // 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 int nni_http_hijack(nni_http_conn *); +extern nng_err nni_http_hijack(nni_http_conn *); // nni_http_handler_init creates a server handler object, for the supplied // URI (path only) with the callback. @@ -227,17 +226,17 @@ extern int nni_http_hijack(nni_http_conn *); // 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 int nni_http_handler_init( +extern nng_err nni_http_handler_init( nni_http_handler **, const char *, nng_http_handler_func); // nni_http_handler_init_file creates a handler with a function to serve // up a file named in the last argument. -extern int nni_http_handler_init_file( +extern nng_err nni_http_handler_init_file( nni_http_handler **, 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 int nni_http_handler_init_file_ctype( +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 @@ -245,16 +244,16 @@ extern int nni_http_handler_init_file_ctype( // 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 int nni_http_handler_init_directory( +extern nng_err nni_http_handler_init_directory( nni_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 int nni_http_handler_init_static( +extern nng_err nni_http_handler_init_static( nni_http_handler **, const char *, const void *, size_t, const char *); // nni_http_handler_init_redirect creates a handler that redirects the request. -extern int nni_http_handler_init_redirect( +extern nng_err nni_http_handler_init_redirect( nni_http_handler **, const char *, nng_http_status, const char *); // nni_http_handler_fini destroys a handler. This should only be done before @@ -304,19 +303,20 @@ extern const char *nni_http_handler_get_uri(nni_http_handler *); // Client stuff. -extern int nni_http_client_init(nni_http_client **, const nng_url *); -extern void nni_http_client_fini(nni_http_client *); +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 int nni_http_client_set_tls(nni_http_client *, struct nng_tls_config *); +extern nng_err nni_http_client_set_tls( + nni_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 int nni_http_client_get_tls( +extern nng_err nni_http_client_get_tls( nni_http_client *, struct nng_tls_config **); extern int nni_http_client_set( @@ -363,24 +363,22 @@ extern const char *nni_http_get_reason(nng_http *); // 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. -extern int nni_http_set_error(nng_http *conn, nng_http_status status, +extern nng_err nni_http_set_error(nng_http *conn, nng_http_status status, const char *reason, const char *body); // nni_http_set_redirect is used to set the redirection. // It uses a built-in error page, with a message about the redirection, and // sets the response Location: header accordingly. -extern int nni_http_set_redirect(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 int nni_http_set_uri( +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); -extern int nni_http_add_header( - nng_http *conn, const char *key, const char *val); extern void nni_http_set_static_header( nng_http *conn, nni_http_header *header, const char *key, const char *val); diff --git a/src/supplemental/http/http_chunk.c b/src/supplemental/http/http_chunk.c index 20644d37..1d4335db 100644 --- a/src/supplemental/http/http_chunk.c +++ b/src/supplemental/http/http_chunk.c @@ -1,5 +1,5 @@ // -// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2025 Staysail Systems, Inc. <info@staysail.tech> // Copyright 2018 Capitar IT Group BV <info@capitar.com> // // This software is supplied under the terms of the MIT License, a @@ -54,10 +54,10 @@ struct nng_http_chunk { size_t c_size; size_t c_alloc; size_t c_resid; // residual data to transfer - char * c_data; + char *c_data; }; -int +nng_err nni_http_chunks_init(nni_http_chunks **clp, size_t maxsz) { nni_http_chunks *cl; @@ -68,7 +68,7 @@ nni_http_chunks_init(nni_http_chunks **clp, size_t maxsz) NNI_LIST_INIT(&cl->cl_chunks, nni_http_chunk, c_node); cl->cl_maxsz = maxsz; *clp = cl; - return (0); + return (NNG_OK); } void @@ -120,7 +120,7 @@ nni_http_chunk_data(nni_http_chunk *ch) return (ch->c_data); } -static int +static nng_err chunk_ingest_len(nni_http_chunks *cl, char c) { if (isdigit(c)) { @@ -139,10 +139,10 @@ chunk_ingest_len(nni_http_chunks *cl, char c) } else { return (NNG_EPROTO); } - return (0); + return (NNG_OK); } -static int +static nng_err chunk_ingest_ext(nni_http_chunks *cl, char c) { if (c == '\r') { @@ -150,10 +150,10 @@ chunk_ingest_ext(nni_http_chunks *cl, char c) } else if (!isprint(c)) { return (NNG_EPROTO); } - return (0); + return (NNG_OK); } -static int +static nng_err chunk_ingest_newline(nni_http_chunks *cl, char c) { nni_http_chunk *chunk; @@ -164,7 +164,7 @@ chunk_ingest_newline(nni_http_chunks *cl, char c) if (cl->cl_size == 0) { cl->cl_line = 0; cl->cl_state = CS_TRLR; - return (0); + return (NNG_OK); } if ((cl->cl_maxsz > 0) && ((nni_http_chunks_size(cl) + cl->cl_size) > cl->cl_maxsz)) { @@ -188,24 +188,24 @@ chunk_ingest_newline(nni_http_chunks *cl, char c) chunk->c_resid = chunk->c_alloc; nni_list_append(&cl->cl_chunks, chunk); - return (0); + return (NNG_OK); } -static int +static nng_err chunk_ingest_trailer(nni_http_chunks *cl, char c) { if (c == '\r') { cl->cl_state = CS_TRLRCR; - return (0); + return (NNG_OK); } if (!isprint(c)) { return (NNG_EPROTO); } cl->cl_line++; - return (0); + return (NNG_OK); } -static int +static nng_err chunk_ingest_trailercr(nni_http_chunks *cl, char c) { if (c != '\n') { @@ -217,13 +217,13 @@ chunk_ingest_trailercr(nni_http_chunks *cl, char c) } cl->cl_line = 0; cl->cl_state = CS_TRLR; - return (0); + return (NNG_OK); } -static int +static nng_err chunk_ingest_char(nni_http_chunks *cl, char c) { - int rv; + nng_err rv; switch (cl->cl_state) { case CS_INIT: if (!isalnum(c)) { @@ -257,12 +257,12 @@ chunk_ingest_char(nni_http_chunks *cl, char c) return (rv); } -static int +static nng_err chunk_ingest_data(nni_http_chunks *cl, char *buf, size_t n, size_t *lenp) { nni_http_chunk *chunk; size_t offset; - char * dest; + char *dest; chunk = nni_list_last(&cl->cl_chunks); @@ -288,20 +288,20 @@ chunk_ingest_data(nni_http_chunks *cl, char *buf, size_t n, size_t *lenp) cl->cl_size = 0; cl->cl_line = 0; *lenp = n; - return (0); + return (NNG_OK); } memcpy(dest, buf, n); chunk->c_resid -= n; *lenp = n; - return (0); + return (NNG_OK); } -int +nng_err nni_http_chunks_parse(nni_http_chunks *cl, void *buf, size_t n, size_t *lenp) { size_t i = 0; - char * src = buf; + char *src = buf; // Format of this data is <hexdigits> [ ; <ascii> CRLF ] // The <ascii> are chunk extensions, and we don't support any. @@ -315,8 +315,8 @@ nni_http_chunks_parse(nni_http_chunks *cl, void *buf, size_t n, size_t *lenp) break; case CS_DATA: - if ((rv = chunk_ingest_data(cl, src + i, n - i, &cnt)) != - 0) { + if ((rv = chunk_ingest_data( + cl, src + i, n - i, &cnt)) != 0) { return (rv); } i += cnt; @@ -337,5 +337,5 @@ nni_http_chunks_parse(nni_http_chunks *cl, void *buf, size_t n, size_t *lenp) if (cl->cl_state != CS_DONE) { return (NNG_EAGAIN); } - return (0); + return (NNG_OK); } diff --git a/src/supplemental/http/http_client.c b/src/supplemental/http/http_client.c index 4247f4b1..1f5d8fbd 100644 --- a/src/supplemental/http/http_client.c +++ b/src/supplemental/http/http_client.c @@ -44,7 +44,7 @@ http_dial_cb(void *arg) { nni_http_client *c = arg; nni_aio *aio; - int rv; + nng_err rv; nng_stream *stream; nni_http_conn *conn; @@ -61,7 +61,7 @@ http_dial_cb(void *arg) return; } - if (rv != 0) { + if (rv != NNG_OK) { nni_aio_list_remove(aio); http_dial_start(c); nni_mtx_unlock(&c->mtx); @@ -73,20 +73,20 @@ http_dial_cb(void *arg) stream = nni_aio_get_output(&c->aio, 0); NNI_ASSERT(stream != NULL); - rv = nni_http_conn_init(&conn, stream, true); + rv = nni_http_init(&conn, stream, true); // set up the host header http_dial_start(c); nni_mtx_unlock(&c->mtx); - if (rv != 0) { + if (rv != NNG_OK) { // the conn_init function will have already discard stream. nni_aio_finish_error(aio, rv); return; } nni_http_set_host(conn, c->host); nni_aio_set_output(aio, 0, conn); - nni_aio_finish(aio, 0, 0); + nni_aio_finish(aio, NNG_OK, 0); } void @@ -100,10 +100,10 @@ nni_http_client_fini(nni_http_client *c) NNI_FREE_STRUCT(c); } -int +nng_err nni_http_client_init(nni_http_client **cp, const nng_url *url) { - int rv; + nng_err rv; nni_http_client *c; nng_url my_url; const char *scheme; @@ -144,16 +144,16 @@ nni_http_client_init(nni_http_client **cp, const nng_url *url) } *cp = c; - return (0); + return (NNG_OK); } -int +nng_err nni_http_client_set_tls(nni_http_client *c, nng_tls_config *tls) { return (nng_stream_dialer_set_tls(c->dialer, tls)); } -int +nng_err nni_http_client_get_tls(nni_http_client *c, nng_tls_config **tlsp) { return (nng_stream_dialer_get_tls(c->dialer, tlsp)); @@ -268,7 +268,7 @@ http_txn_cb(void *arg) http_txn *txn = arg; const char *str; char *end; - int rv; + nng_err rv; uint64_t len; nni_iov iov; char *dst; @@ -276,7 +276,7 @@ http_txn_cb(void *arg) nni_http_chunk *chunk = NULL; nni_mtx_lock(&http_txn_lk); - if ((rv = nni_aio_result(&txn->aio)) != 0) { + if ((rv = nni_aio_result(&txn->aio)) != NNG_OK) { http_txn_finish_aios(txn, rv); nni_mtx_unlock(&http_txn_lk); http_txn_fini(txn); @@ -299,7 +299,7 @@ http_txn_cb(void *arg) (strstr(str, "chunked") != NULL)) { if ((rv = nni_http_chunks_init(&txn->chunks, 0)) != - 0) { + NNG_OK) { goto error; } txn->state = HTTP_RECVING_CHUNKS; @@ -323,7 +323,7 @@ http_txn_cb(void *arg) } if ((rv = nni_http_res_alloc_data(txn->res, (size_t) len)) != - 0) { + NNG_OK) { goto error; } nni_http_get_body(txn->conn, &iov.iov_buf, &iov.iov_len); diff --git a/src/supplemental/http/http_conn.c b/src/supplemental/http/http_conn.c index 169ac75e..00961852 100644 --- a/src/supplemental/http/http_conn.c +++ b/src/supplemental/http/http_conn.c @@ -174,13 +174,13 @@ http_buf_pull_up(nni_http_conn *conn) } // http_rd_buf attempts to satisfy the read from data in the buffer. -static int +static nng_err http_rd_buf(nni_http_conn *conn, nni_aio *aio) { size_t cnt = conn->rd_put - conn->rd_get; size_t n; uint8_t *rbuf = conn->buf; - int rv; + nng_err rv; bool raw = false; nni_iov *iov; unsigned nio; @@ -218,7 +218,7 @@ http_rd_buf(nni_http_conn *conn, nni_aio *aio) // Finished the read. (We are finished if we either // got *all* the data, or we got *some* data for // a raw read.) - return (0); + return (NNG_OK); } // No more data left in the buffer, so use a physio. @@ -247,7 +247,7 @@ http_rd_buf(nni_http_conn *conn, nni_aio *aio) nng_stream_recv(conn->sock, &conn->rd_aio); return (NNG_EAGAIN); } - return (0); + return (NNG_OK); case HTTP_RD_REQ: conn->client = true; @@ -349,7 +349,7 @@ http_rd_start(nni_http_conn *conn) return; case 0: conn->rd_uaio = NULL; - nni_aio_finish(aio, 0, nni_aio_count(aio)); + nni_aio_finish(aio, NNG_OK, nni_aio_count(aio)); break; default: conn->rd_uaio = NULL; @@ -367,7 +367,7 @@ http_rd_cb(void *arg) nni_aio *aio = &conn->rd_aio; nni_aio *uaio; size_t cnt; - int rv; + nng_err rv; unsigned niov; nni_iov *iov; @@ -495,14 +495,14 @@ http_wr_cb(void *arg) nni_http_conn *conn = arg; nni_aio *aio = &conn->wr_aio; nni_aio *uaio; - int rv; + nng_err rv; size_t n; nni_mtx_lock(&conn->mtx); uaio = conn->wr_uaio; - if ((rv = nni_aio_result(aio)) != 0) { + if ((rv = nni_aio_result(aio)) != NNG_OK) { // We failed to complete the aio. if (uaio != NULL) { conn->wr_uaio = NULL; @@ -540,7 +540,7 @@ http_wr_cb(void *arg) done: conn->wr_uaio = NULL; - nni_aio_finish(uaio, 0, nni_aio_count(uaio)); + nni_aio_finish(uaio, NNG_OK, nni_aio_count(uaio)); // Start next write if another is ready. http_wr_start(conn); @@ -724,7 +724,7 @@ http_snprintf(nng_http *conn, char *buf, size_t sz) return (len); } -static int +static nng_err http_prepare(nng_http *conn, void **data, size_t *szp) { size_t len; @@ -738,7 +738,7 @@ http_prepare(nng_http *conn, void **data, size_t *szp) http_snprintf(conn, (char *) conn->buf, conn->bufsz); *data = conn->buf; *szp = len; - return (0); + return (NNG_OK); } // we have to allocate. @@ -747,13 +747,13 @@ http_prepare(nng_http *conn, void **data, size_t *szp) } http_snprintf(conn, *data, len + 1); *szp = len; // this does not include the terminating null - return (0); + return (NNG_OK); } void nni_http_write_req(nng_http *conn, nni_aio *aio) { - int rv; + nng_err rv; void *buf; size_t bufsz; nni_iov iov[2]; @@ -786,7 +786,7 @@ nni_http_write_req(nng_http *conn, nni_aio *aio) void nni_http_write_res(nng_http *conn, nni_aio *aio) { - int rv; + nng_err rv; void *buf; size_t bufsz; nni_iov iov[2]; @@ -857,7 +857,7 @@ nni_http_set_version(nng_http *conn, const char *vers) for (int i = 0; http_versions[i] != NULL; i++) { if (strcmp(vers, http_versions[i]) == 0) { conn->vers = http_versions[i]; - return (0); + return (NNG_OK); } } return (NNG_ENOTSUP); @@ -883,10 +883,7 @@ nni_http_get_method(nng_http *conn) nng_http_status nni_http_get_status(nng_http *conn) { - if (conn->code == 0) { - return (NNG_HTTP_STATUS_OK); - } - return (conn->code); + return (conn->code ? conn->code : NNG_HTTP_STATUS_OK); } const char * @@ -1076,19 +1073,20 @@ 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"); - return (nni_http_copy_body(conn, body, strlen(body))); + // if the follow fails, live with it (ENOMEM, so no body). + (void) nni_http_copy_body(conn, body, strlen(body)); } return (0); } -int +nng_err nni_http_set_error(nng_http *conn, nng_http_status status, const char *reason, const char *body) { return (http_conn_set_error(conn, status, reason, body, NULL)); } -int +nng_err nni_http_set_redirect(nng_http *conn, nng_http_status status, const char *reason, const char *redirect) { @@ -1158,7 +1156,7 @@ nni_http_get_uri(nng_http *conn) return ((conn->uri && conn->uri[0]) ? conn->uri : "/"); } -int +nng_err nni_http_set_uri(nng_http *conn, const char *uri, const char *query) { size_t needed; @@ -1176,7 +1174,7 @@ nni_http_set_uri(nng_http *conn, const char *uri, const char *query) if (conn->uri != NULL && (strcmp(uri, conn->uri) == 0) && strlen(query) == 0) { // no change, do nothing - return (0); + return (NNG_OK); } if (conn->uri != NULL && conn->uri != conn->ubuf) { nni_strfree(conn->uri); @@ -1186,17 +1184,17 @@ nni_http_set_uri(nng_http *conn, const char *uri, const char *query) if (needed < sizeof(conn->ubuf)) { snprintf(conn->ubuf, sizeof(conn->ubuf), fmt, uri, query); conn->uri = conn->ubuf; - return (0); + return (NNG_OK); } // too big, we have to allocate it (slow path) if (nni_asprintf(&conn->uri, fmt, uri, query) != 0) { return (NNG_ENOMEM); } - return (0); + return (NNG_OK); } -static int +static nng_err http_set_header(nng_http *conn, const char *key, const char *val) { nni_http_entity *data = @@ -1214,7 +1212,7 @@ http_set_header(nng_http *conn, const char *key, const char *val) h->value = NULL; } h->value = news; - return (0); + return (NNG_OK); } } @@ -1232,10 +1230,10 @@ http_set_header(nng_http *conn, const char *key, const char *val) return (NNG_ENOMEM); } nni_list_append(&data->hdrs, h); - return (0); + return (NNG_OK); } -static int +static nng_err http_add_header(nng_http *conn, const char *key, const char *val) { nni_http_entity *data = @@ -1246,14 +1244,14 @@ http_add_header(nng_http *conn, const char *key, const char *val) char *news; int rv; rv = nni_asprintf(&news, "%s, %s", h->value, val); - if (rv != 0) { + if (rv != NNG_OK) { return (rv); } if (!h->static_value) { nni_strfree(h->value); } h->value = news; - return (0); + return (NNG_OK); } } @@ -1271,7 +1269,7 @@ http_add_header(nng_http *conn, const char *key, const char *val) return (NNG_ENOMEM); } nni_list_append(&data->hdrs, h); - return (0); + return (NNG_OK); } static bool @@ -1299,11 +1297,11 @@ http_set_known_header(nng_http *conn, const char *key, const char *val) return (false); } -int +nng_err nni_http_add_header(nng_http *conn, const char *key, const char *val) { if (http_set_known_header(conn, key, val)) { - return (0); + return (NNG_OK); } return (http_add_header(conn, key, val)); @@ -1330,7 +1328,7 @@ nni_http_set_static_header( nni_list_append(headers, h); } -int +nng_err nni_http_set_header(nng_http *conn, const char *key, const char *val) { if (http_set_known_header(conn, key, val)) { @@ -1407,7 +1405,7 @@ http_set_data(nni_http_entity *entity, const void *data, size_t size) entity->own = false; } -static int +static nng_err http_alloc_data(nni_http_entity *entity, size_t size) { void *newdata; @@ -1418,13 +1416,13 @@ http_alloc_data(nni_http_entity *entity, size_t size) } http_set_data(entity, newdata, size); entity->own = true; - return (0); + return (NNG_OK); } -static int +static nng_err http_copy_data(nni_http_entity *entity, const void *data, size_t size) { - int rv; + nng_err rv; if ((rv = http_alloc_data(entity, size)) == 0) { memcpy(entity->data, data, size); } @@ -1454,16 +1452,16 @@ nni_http_prune_body(nng_http *conn) } } -int +nng_err nni_http_copy_body(nng_http *conn, const void *data, size_t size) { - int rv; + nng_err rv; if (conn->client) { rv = http_copy_data(&conn->req.data, data, size); } else { rv = http_copy_data(&conn->res.data, data, size); } - if (rv == 0) { + if (rv == NNG_OK) { nni_http_set_content_length(conn, size); } return (rv); @@ -1506,7 +1504,7 @@ nni_http_conn_fini(nni_http_conn *conn) NNI_FREE_STRUCT(conn); } -static int +static nng_err http_init(nni_http_conn **connp, nng_stream *data, bool client) { nni_http_conn *conn; @@ -1536,14 +1534,14 @@ http_init(nni_http_conn **connp, nng_stream *data, bool client) *connp = conn; - return (0); + return (NNG_OK); } -int -nni_http_conn_init(nni_http_conn **connp, nng_stream *stream, bool client) +nng_err +nni_http_init(nng_http **connp, nng_stream *stream, bool client) { - int rv; - if ((rv = http_init(connp, stream, client)) != 0) { + nng_err rv; + if ((rv = http_init(connp, stream, client)) != NNG_OK) { nng_stream_free(stream); } return (rv); diff --git a/src/supplemental/http/http_msg.c b/src/supplemental/http/http_msg.c index 0cef876a..09470e31 100644 --- a/src/supplemental/http/http_msg.c +++ b/src/supplemental/http/http_msg.c @@ -88,7 +88,7 @@ http_entity_set_data(nni_http_entity *entity, const void *data, size_t size) entity->own = false; } -static int +static nng_err http_entity_alloc_data(nni_http_entity *entity, size_t size) { void *newdata; @@ -99,35 +99,25 @@ http_entity_alloc_data(nni_http_entity *entity, size_t size) } http_entity_set_data(entity, newdata, size); entity->own = true; - return (0); + return (NNG_OK); } -int +nng_err nni_http_req_alloc_data(nni_http_req *req, size_t size) { - int rv; - - if ((rv = http_entity_alloc_data(&req->data, size)) != 0) { - return (rv); - } - return (0); + return (http_entity_alloc_data(&req->data, size)); } // nni_http_res_alloc_data allocates the data region, but does not update any // headers. The intended use is for client implementations that want to // allocate a buffer to receive the entity into. -int +nng_err nni_http_res_alloc_data(nni_http_res *res, size_t size) { - int rv; - - if ((rv = http_entity_alloc_data(&res->data, size)) != 0) { - return (rv); - } - return (0); + return (http_entity_alloc_data(&res->data, size)); } -static int +static nng_err http_parse_header(nng_http *conn, void *line) { char *key = line; @@ -219,31 +209,31 @@ http_req_parse_line(nng_http *conn, void *line) if (nni_http_get_status(conn) >= NNG_HTTP_STATUS_BAD_REQUEST) { // we've already failed it, nothing else for us to do - return (0); + return (NNG_OK); } method = line; if ((uri = strchr(method, ' ')) == NULL) { nni_http_set_status(conn, NNG_HTTP_STATUS_BAD_REQUEST, NULL); - return (0); + return (NNG_OK); } *uri = '\0'; uri++; if ((version = strchr(uri, ' ')) == NULL) { nni_http_set_status(conn, NNG_HTTP_STATUS_BAD_REQUEST, NULL); - return (0); + return (NNG_OK); } *version = '\0'; version++; if (nni_url_canonify_uri(uri) != 0) { nni_http_set_status(conn, NNG_HTTP_STATUS_BAD_REQUEST, NULL); - return (0); + return (NNG_OK); } if (nni_http_set_version(conn, version)) { nni_http_set_status( conn, NNG_HTTP_STATUS_HTTP_VERSION_NOT_SUPP, NULL); - return (0); + return (NNG_OK); } nni_http_set_method(conn, method); @@ -289,7 +279,7 @@ http_res_parse_line(nng_http *conn, uint8_t *line) // required, or NNG_ENOMEM on memory exhaustion. Note that lenp may // be updated even in the face of errors (esp. NNG_EAGAIN, which is // not an error so much as a request for more data.) -int +nng_err nni_http_req_parse(nng_http *conn, void *buf, size_t n, size_t *lenp) { @@ -328,7 +318,7 @@ nni_http_req_parse(nng_http *conn, void *buf, size_t n, size_t *lenp) return (rv); } -int +nng_err nni_http_res_parse(nng_http *conn, void *buf, size_t n, size_t *lenp) { diff --git a/src/supplemental/http/http_server.c b/src/supplemental/http/http_server.c index ce696ac5..53da78c2 100644 --- a/src/supplemental/http/http_server.c +++ b/src/supplemental/http/http_server.c @@ -104,7 +104,7 @@ static nni_reap_list http_server_reap_list = { .rl_func = (nni_cb) http_server_fini, }; -int +nng_err nni_http_handler_init( nni_http_handler **hp, const char *uri, nng_http_handler_func cb) { @@ -131,7 +131,7 @@ nni_http_handler_init( (void) strcpy(h->method, "GET"); (void) strcpy(h->host, ""); *hp = h; - return (0); + return (NNG_OK); } // nni_http_handler_fini just drops the reference count, only destroying @@ -287,7 +287,7 @@ http_sconn_txdatdone(void *arg) http_sconn *sc = arg; nni_aio *aio = &sc->txdataio; - if (nni_aio_result(aio) != 0) { + if (nni_aio_result(aio) != NNG_OK) { http_sconn_close(sc); return; } @@ -307,7 +307,7 @@ http_sconn_txdone(void *arg) http_sconn *sc = arg; nni_aio *aio = &sc->txaio; - if (nni_aio_result(aio) != 0) { + if (nni_aio_result(aio) != NNG_OK) { http_sconn_close(sc); return; } @@ -342,7 +342,7 @@ http_sconn_error(http_sconn *sc, nng_http_status err) nni_http_write_res(sc->conn, &sc->txaio); } -int +nng_err nni_http_hijack(nni_http_conn *conn) { http_sconn *sc; @@ -356,7 +356,7 @@ nni_http_hijack(nni_http_conn *conn) sc->conn = NULL; nni_mtx_unlock(&s->mtx); } - return (0); + return (NNG_OK); } static bool @@ -432,7 +432,7 @@ http_sconn_rxdone(void *arg) const char *host; const char *cls; - if ((rv = nni_aio_result(aio)) != 0) { + if ((rv = nni_aio_result(aio)) != NNG_OK) { http_sconn_close(sc); return; } @@ -680,11 +680,11 @@ http_sconn_cbdone(void *arg) } } -static int +static nng_err http_sconn_init(http_sconn **scp, nng_stream *stream) { http_sconn *sc; - int rv; + nng_err rv; if ((sc = NNI_ALLOC_STRUCT(sc)) == NULL) { nng_stream_free(stream); @@ -696,7 +696,7 @@ http_sconn_init(http_sconn **scp, nng_stream *stream) nni_aio_init(&sc->txdataio, http_sconn_txdatdone, sc); nni_aio_init(&sc->cbaio, http_sconn_cbdone, sc); - if ((rv = nni_http_conn_init(&sc->conn, stream, false)) != 0) { + if ((rv = nni_http_init(&sc->conn, stream, false)) != 0) { // Can't even accept the incoming request. Hard close. http_sconn_close(sc); return (rv); @@ -704,7 +704,7 @@ http_sconn_init(http_sconn **scp, nng_stream *stream) nni_http_conn_set_ctx(sc->conn, sc); *scp = sc; - return (0); + return (NNG_OK); } static void @@ -780,11 +780,11 @@ http_server_fini(nni_http_server *s) NNI_FREE_STRUCT(s); } -static int +static nng_err http_server_init(nni_http_server **serverp, const nng_url *url) { nni_http_server *s; - int rv; + nng_err rv; nng_url my_url; const char *scheme; @@ -822,13 +822,13 @@ http_server_init(nni_http_server **serverp, const nng_url *url) s->refcnt = 1; *serverp = s; - return (0); + return (NNG_OK); } -int +nng_err nni_http_server_init(nni_http_server **serverp, const nng_url *url) { - int rv; + nng_err rv; nni_http_server *s; nni_mtx_lock(&http_servers_lk); @@ -840,7 +840,7 @@ nni_http_server_init(nni_http_server **serverp, const nng_url *url) s->refcnt++; nni_mtx_unlock(&s->mtx); nni_mtx_unlock(&http_servers_lk); - return (0); + return (NNG_OK); } nni_mtx_unlock(&s->mtx); } @@ -855,10 +855,10 @@ nni_http_server_init(nni_http_server **serverp, const nng_url *url) return (rv); } -static int +static nng_err http_server_start(nni_http_server *s) { - int rv; + nng_err rv; if ((rv = nng_stream_listener_listen(s->listener)) != 0) { return (rv); } @@ -869,19 +869,19 @@ http_server_start(nni_http_server *s) s->port = (uint32_t) port; } nng_stream_listener_accept(s->listener, &s->accaio); - return (0); + return (NNG_OK); } -int +nng_err nni_http_server_start(nni_http_server *s) { - int rv = 0; + int rv = NNG_OK; nni_mtx_lock(&s->mtx); if (s->starts == 0) { rv = http_server_start(s); } - if (rv == 0) { + if (rv == NNG_OK) { s->starts++; } nni_mtx_unlock(&s->mtx); @@ -946,7 +946,7 @@ nni_http_server_close(nni_http_server *s) nni_mtx_unlock(&s->mtx); } -static int +static nng_err http_server_set_err(nni_http_server *s, nng_http_status code, char *body) { http_error *epage; @@ -968,15 +968,15 @@ http_server_set_err(nni_http_server *s, nng_http_status code, char *body) nni_strfree(epage->body); epage->body = body; nni_mtx_unlock(&s->errors_mtx); - return (0); + return (NNG_OK); } -int +nng_err nni_http_server_set_error_page( nni_http_server *s, nng_http_status code, const char *html) { - char *body; - int rv; + char *body; + nng_err rv; // We copy the content, without the trailing NUL. if ((body = nni_strdup(html)) == NULL) { @@ -988,13 +988,13 @@ nni_http_server_set_error_page( return (rv); } -int +nng_err 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); - int rv; + nng_err rv; nni_mtx_lock(&s->errors_mtx); NNI_LIST_FOREACH (&s->errors, epage) { @@ -1008,7 +1008,7 @@ nni_http_server_error(nni_http_server *s, nng_http *conn) return (rv); } -int +nng_err nni_http_server_add_handler(nni_http_server *s, nni_http_handler *h) { nni_http_handler *h2; @@ -1062,13 +1062,13 @@ nni_http_server_add_handler(nni_http_server *s, nni_http_handler *h) nni_atomic_set_bool(&h->busy, true); nni_mtx_unlock(&s->mtx); - return (0); + return (NNG_OK); } -int +nng_err nni_http_server_del_handler(nni_http_server *s, nni_http_handler *h) { - int rv = NNG_ENOENT; + nng_err rv = NNG_ENOENT; nni_http_handler *srch; nni_mtx_lock(&s->mtx); NNI_LIST_FOREACH (&s->handlers, srch) { @@ -1076,7 +1076,7 @@ nni_http_server_del_handler(nni_http_server *s, nni_http_handler *h) // NB: We are giving the caller our reference // on the handler. nni_list_remove(&s->handlers, h); - rv = 0; + rv = NNG_OK; break; } } @@ -1187,7 +1187,7 @@ http_handle_file(nng_http *conn, void *arg, nni_aio *aio) nni_aio_finish_error(aio, rv); return; } - nni_aio_finish(aio, 0, 0); + nni_aio_finish(aio, NNG_OK, 0); return; } if (((rv = nni_http_set_header(conn, "Content-Type", ctype)) != 0) || @@ -1200,7 +1200,7 @@ http_handle_file(nng_http *conn, void *arg, nni_aio *aio) nng_http_set_status(conn, NNG_HTTP_STATUS_OK, NULL); nni_free(data, size); - nni_aio_finish(aio, 0, 0); + nni_aio_finish(aio, NNG_OK, 0); } static void @@ -1215,13 +1215,13 @@ http_file_free(void *arg) } } -int +nng_err nni_http_handler_init_file_ctype(nni_http_handler **hpp, const char *uri, const char *path, const char *ctype) { nni_http_handler *h; http_file *hf; - int rv; + nng_err rv; if ((hf = NNI_ALLOC_STRUCT(hf)) == NULL) { return (NNG_ENOMEM); @@ -1252,10 +1252,10 @@ nni_http_handler_init_file_ctype(nni_http_handler **hpp, const char *uri, nni_http_handler_collect_body(h, true, 0); *hpp = h; - return (0); + return (NNG_OK); } -int +nng_err nni_http_handler_init_file( nni_http_handler **hpp, const char *uri, const char *path) { @@ -1267,7 +1267,7 @@ http_handle_dir(nng_http *conn, void *arg, nng_aio *aio) { void *data; size_t size; - int rv; + nng_err rv; http_file *hf = arg; const char *path = hf->path; const char *base = hf->base; @@ -1337,7 +1337,7 @@ http_handle_dir(nng_http *conn, void *arg, nng_aio *aio) } } - if (rv == 0) { + if (rv == NNG_OK) { rv = nni_file_get(pn, &data, &size); } else { data = NULL; @@ -1349,7 +1349,7 @@ http_handle_dir(nng_http *conn, void *arg, nng_aio *aio) } nni_free(pn, pnsz); - if (rv != 0) { + if (rv != NNG_OK) { nng_http_status status; switch (rv) { @@ -1384,16 +1384,16 @@ http_handle_dir(nng_http *conn, void *arg, nng_aio *aio) nng_http_set_status(conn, NNG_HTTP_STATUS_OK, NULL); nni_free(data, size); - nni_aio_finish(aio, 0, 0); + nni_aio_finish(aio, NNG_OK, 0); } -int +nng_err nni_http_handler_init_directory( nni_http_handler **hpp, const char *uri, const char *path) { http_file *hf; nni_http_handler *h; - int rv; + nng_err rv; if ((hf = NNI_ALLOC_STRUCT(hf)) == NULL) { return (NNG_ENOMEM); @@ -1414,7 +1414,7 @@ nni_http_handler_init_directory( nng_http_handler_set_data(h, hf, http_file_free); *hpp = h; - return (0); + return (NNG_OK); } typedef struct http_redirect { @@ -1426,7 +1426,6 @@ typedef struct http_redirect { static void http_handle_redirect(nng_http *conn, void *data, nng_aio *aio) { - nni_http_res *res = nni_http_conn_res(conn); char *loc = NULL; http_redirect *hr = data; int rv; @@ -1439,7 +1438,7 @@ http_handle_redirect(nng_http *conn, void *data, nng_aio *aio) // If we are doing a full tree, then include the entire suffix. if (strncmp(uri, base, strlen(base)) == 0) { rv = nni_asprintf(&loc, "%s%s", hr->where, uri + strlen(base)); - if (rv != 0) { + if (rv != NNG_OK) { nni_aio_finish_error(aio, rv); return; } @@ -1465,8 +1464,7 @@ http_handle_redirect(nng_http *conn, void *data, nng_aio *aio) if (loc != hr->where) { nni_strfree(loc); } - nni_aio_set_output(aio, 0, res); - nni_aio_finish(aio, 0, 0); + nni_aio_finish(aio, NNG_OK, 0); } static void @@ -1481,12 +1479,12 @@ http_redirect_free(void *arg) } } -int +nng_err nni_http_handler_init_redirect(nni_http_handler **hpp, const char *uri, nng_http_status status, const char *where) { nni_http_handler *h; - int rv; + nng_err rv; http_redirect *hr; if ((hr = NNI_ALLOC_STRUCT(hr)) == NULL) { @@ -1516,7 +1514,7 @@ nni_http_handler_init_redirect(nni_http_handler **hpp, const char *uri, nni_http_handler_collect_body(h, false, 0); *hpp = h; - return (0); + return (NNG_OK); } typedef struct http_static { @@ -1556,12 +1554,12 @@ http_static_free(void *arg) } } -int +nng_err nni_http_handler_init_static(nni_http_handler **hpp, const char *uri, const void *data, size_t size, const char *ctype) { nni_http_handler *h; - int rv; + nng_err rv; http_static *hs; if ((hs = NNI_ALLOC_STRUCT(hs)) == NULL) { @@ -1586,16 +1584,16 @@ nni_http_handler_init_static(nni_http_handler **hpp, const char *uri, nni_http_handler_collect_body(h, true, 0); *hpp = h; - return (0); + return (NNG_OK); } -int +nng_err nni_http_server_set_tls(nni_http_server *s, nng_tls_config *tls) { return (nng_stream_listener_set_tls(s->listener, tls)); } -int +nng_err nni_http_server_get_tls(nni_http_server *s, nng_tls_config **tlsp) { return (nng_stream_listener_get_tls(s->listener, tlsp)); |
