diff options
| -rw-r--r-- | demo/rest/server.c | 33 | ||||
| -rw-r--r-- | docs/man/nng_http_req_set_method.3http.adoc | 15 | ||||
| -rw-r--r-- | docs/ref/migrate/nng1.md | 4 | ||||
| -rw-r--r-- | include/nng/supplemental/http/http.h | 2 | ||||
| -rw-r--r-- | src/supplemental/http/http_api.h | 2 | ||||
| -rw-r--r-- | src/supplemental/http/http_msg.c | 30 | ||||
| -rw-r--r-- | src/supplemental/http/http_public.c | 5 | ||||
| -rw-r--r-- | src/supplemental/http/http_server_test.c | 14 |
8 files changed, 51 insertions, 54 deletions
diff --git a/demo/rest/server.c b/demo/rest/server.c index 5a59756c..ee4047b5 100644 --- a/demo/rest/server.c +++ b/demo/rest/server.c @@ -73,11 +73,11 @@ typedef enum { } job_state; typedef struct rest_job { - nng_aio * http_aio; // aio from HTTP we must reply to - nng_http_res * http_res; // HTTP response object + nng_aio *http_aio; // aio from HTTP we must reply to + nng_http_res *http_res; // HTTP response object job_state state; // 0 = sending, 1 = receiving - nng_msg * msg; // request message - nng_aio * aio; // request flow + nng_msg *msg; // request message + nng_aio *aio; // request flow nng_ctx ctx; // context on the request socket struct rest_job *next; // next on the freelist } rest_job; @@ -86,7 +86,7 @@ nng_socket req_sock; // We maintain a queue of free jobs. This way we don't have to // deallocate them from the callback; we just reuse them. -nng_mtx * job_lock; +nng_mtx *job_lock; rest_job *job_freelist; static void rest_job_cb(void *arg); @@ -139,7 +139,7 @@ static void rest_http_fatal(rest_job *job, const char *fmt, int rv) { char buf[128]; - nng_aio * aio = job->http_aio; + nng_aio *aio = job->http_aio; nng_http_res *res = job->http_res; job->http_res = NULL; @@ -156,7 +156,7 @@ static void rest_job_cb(void *arg) { rest_job *job = arg; - nng_aio * aio = job->aio; + nng_aio *aio = job->aio; int rv; switch (job->state) { @@ -205,13 +205,13 @@ void rest_handle(nng_aio *aio) { struct rest_job *job; - nng_http_req * req = nng_aio_get_input(aio, 0); - nng_http_conn * conn = nng_aio_get_input(aio, 2); - const char * clen; + nng_http_req *req = nng_aio_get_input(aio, 0); + nng_http_conn *conn = nng_aio_get_input(aio, 2); + const char *clen; size_t sz; nng_iov iov; int rv; - void * data; + void *data; if ((job = rest_get_job()) == NULL) { nng_aio_finish(aio, NNG_ENOMEM); @@ -241,10 +241,10 @@ rest_handle(nng_aio *aio) void rest_start(uint16_t port) { - nng_http_server * server; + nng_http_server *server; nng_http_handler *handler; char rest_addr[128]; - nng_url * url; + nng_url *url; int rv; if ((rv = nng_mtx_alloc(&job_lock)) != 0) { @@ -282,9 +282,8 @@ rest_start(uint16_t port) fatal("nng_http_handler_alloc", rv); } - if ((rv = nng_http_handler_set_method(handler, "POST")) != 0) { - fatal("nng_http_handler_set_method", rv); - } + nng_http_handler_set_method(handler, "POST"); + // We want to collect the body, and we (arbitrarily) limit this to // 128KB. The default limit is 1MB. You can explicitly collect // the data yourself with another HTTP read transaction by disabling @@ -317,7 +316,7 @@ inproc_server(void *arg) { nng_socket s; int rv; - nng_msg * msg; + nng_msg *msg; if (((rv = nng_rep0_open(&s)) != 0) || ((rv = nng_listen(s, INPROC_URL, NULL, 0)) != 0)) { diff --git a/docs/man/nng_http_req_set_method.3http.adoc b/docs/man/nng_http_req_set_method.3http.adoc index bc80b832..9cd4f638 100644 --- a/docs/man/nng_http_req_set_method.3http.adoc +++ b/docs/man/nng_http_req_set_method.3http.adoc @@ -20,7 +20,7 @@ nng_http_req_set_method - set HTTP request method #include <nng/nng.h> #include <nng/supplemental/http/http.h> -int nng_http_req_set_method(nng_http_req *req, const char *method); +void nng_http_req_set_method(nng_http_req *req, const char *method); ---- == DESCRIPTION @@ -32,17 +32,10 @@ be upper case. The default value method for newly allocated requests is "GET". -A local copy of the _method_ is made in the request _req_. - -== RETURN VALUES - -This function returns 0 on success, and non-zero otherwise. +If the method is longer than 32 bytes, it may be silently truncated. +(There are no methods defined that are this long.) -== ERRORS - -[horizontal] -`NNG_ENOMEM`:: Insufficient memory to perform the operation. -`NNG_ENOTSUP`:: No support for HTTP in the library. +A local copy of the _method_ is made in the request _req_. == SEE ALSO diff --git a/docs/ref/migrate/nng1.md b/docs/ref/migrate/nng1.md index bff5b581..bca68e38 100644 --- a/docs/ref/migrate/nng1.md +++ b/docs/ref/migrate/nng1.md @@ -219,6 +219,10 @@ accessors functions are provided: - `u_host` is removed - use [`nng_url_hostname`] and [`nng_url_port`] to construct if needed - `u_rawurl` is removed - a "cooked" URL can be obtained from the new [`nng_url_sprintf`] function. +## HTTP API + +The [`nng_http_req_set_method`] no longer returns a value. It never fails, but it may truncate an unreasonably long value. + ## Security Descriptors (Windows Only) The `NNG_OPT_IPC_SECURITY_DESCRIPTOR` option is removed, and replaced diff --git a/include/nng/supplemental/http/http.h b/include/nng/supplemental/http/http.h index bb163949..8cceae9e 100644 --- a/include/nng/supplemental/http/http.h +++ b/include/nng/supplemental/http/http.h @@ -136,7 +136,7 @@ NNG_DECL const char *nng_http_req_get_header( // nng_http_req_set_method is used to change the method of a request. // The method should be an upper case HTTP method, like POST, or DELETE. // Null sets the default ("GET"). -NNG_DECL int nng_http_req_set_method(nng_http_req *, const char *); +NNG_DECL void nng_http_req_set_method(nng_http_req *, const char *); // nng_http_req_set_version is used to change the version of a request. // Normally the version is "HTTP/1.1". Note that the framework does diff --git a/src/supplemental/http/http_api.h b/src/supplemental/http/http_api.h index 46a02211..27559a48 100644 --- a/src/supplemental/http/http_api.h +++ b/src/supplemental/http/http_api.h @@ -135,7 +135,7 @@ extern int nni_http_res_alloc_data(nni_http_res *, size_t); extern const char *nni_http_req_get_method(const nni_http_req *); extern const char *nni_http_req_get_version(const nni_http_req *); extern const char *nni_http_req_get_uri(const nni_http_req *); -extern int nni_http_req_set_method(nni_http_req *, const char *); +extern void nni_http_req_set_method(nni_http_req *, const char *); extern int nni_http_req_set_version(nni_http_req *, const char *); extern int nni_http_req_set_uri(nni_http_req *, const char *); extern uint16_t nni_http_res_get_status(const nni_http_res *); diff --git a/src/supplemental/http/http_msg.c b/src/supplemental/http/http_msg.c index afe2a3b6..564a4763 100644 --- a/src/supplemental/http/http_msg.c +++ b/src/supplemental/http/http_msg.c @@ -38,7 +38,7 @@ typedef struct nni_http_entity { struct nng_http_req { nni_list hdrs; nni_http_entity data; - char *meth; + char meth[32]; char *uri; const char *vers; char *buf; @@ -100,14 +100,14 @@ nni_http_req_reset(nni_http_req *req) { http_headers_reset(&req->hdrs); http_entity_reset(&req->data); - nni_strfree(req->meth); nni_strfree(req->uri); - req->meth = req->uri = NULL; + req->uri = NULL; nni_free(req->buf, req->bufsz); + nni_http_req_set_method(req, NULL); + nni_http_req_set_version(req, NNG_HTTP_VERSION_1_1); req->bufsz = 0; req->buf = NULL; req->parsed = false; - req->vers = NNG_HTTP_VERSION_1_1; } void @@ -532,7 +532,7 @@ http_req_prepare(nni_http_req *req) return (NNG_EINVAL); } rv = http_asprintf(&req->buf, &req->bufsz, &req->hdrs, "%s %s %s\r\n", - req->meth != NULL ? req->meth : "GET", req->uri, req->vers); + req->meth, req->uri, req->vers); return (rv); } @@ -611,9 +611,9 @@ nni_http_req_alloc(nni_http_req **reqp, const nng_url *url) req->data.data = NULL; req->data.size = 0; req->data.own = false; - req->vers = NNG_HTTP_VERSION_1_1; - req->meth = NULL; req->uri = NULL; + nni_http_req_set_version(req, NNG_HTTP_VERSION_1_1); + nni_http_req_set_method(req, "GET"); if (url != NULL) { const char *host; char host_buf[264]; // 256 + 8 for port @@ -673,7 +673,7 @@ nni_http_res_alloc(nni_http_res **resp) const char * nni_http_req_get_method(const nni_http_req *req) { - return (req->meth != NULL ? req->meth : "GET"); + return (req->meth); } const char * @@ -735,13 +735,15 @@ nni_http_req_set_uri(nni_http_req *req, const char *uri) return (http_set_string(&req->uri, uri)); } -int +void nni_http_req_set_method(nni_http_req *req, const char *meth) { - if ((meth != NULL) && (strcmp(meth, "GET") == 0)) { - meth = NULL; + if (meth == NULL) { + meth = "GET"; } - return (http_set_string(&req->meth, meth)); + // this may truncate the method, but nobody should be sending + // methods so long. + (void) snprintf(req->meth, sizeof(req->meth), "%s", meth); } int @@ -811,8 +813,8 @@ http_req_parse_line(nni_http_req *req, void *line) *version = '\0'; version++; - if (((rv = nni_http_req_set_method(req, method)) != 0) || - ((rv = nni_http_req_set_uri(req, uri)) != 0) || + nni_http_req_set_method(req, method); + if (((rv = nni_http_req_set_uri(req, uri)) != 0) || ((rv = nni_http_req_set_version(req, version)) != 0)) { return (rv); } diff --git a/src/supplemental/http/http_public.c b/src/supplemental/http/http_public.c index 161aefd8..c9983df5 100644 --- a/src/supplemental/http/http_public.c +++ b/src/supplemental/http/http_public.c @@ -280,15 +280,14 @@ nng_http_req_get_uri(const nng_http_req *req) #endif } -int +void nng_http_req_set_method(nng_http_req *req, const char *meth) { #ifdef NNG_SUPP_HTTP - return (nni_http_req_set_method(req, meth)); + nni_http_req_set_method(req, meth); #else NNI_ARG_UNUSED(req); NNI_ARG_UNUSED(meth); - return (NNG_ENOTSUP); #endif } diff --git a/src/supplemental/http/http_server_test.c b/src/supplemental/http/http_server_test.c index 2ed04c4d..ddae77d3 100644 --- a/src/supplemental/http/http_server_test.c +++ b/src/supplemental/http/http_server_test.c @@ -363,7 +363,7 @@ test_server_wrong_method(void) server_setup(&st, h); - NUTS_PASS(nng_http_req_set_method(st.req, "POST")); + nng_http_req_set_method(st.req, "POST"); NUTS_PASS(nng_http_req_set_uri(st.req, "/home.html")); nng_http_conn_write_req(st.conn, st.req, st.aio); @@ -400,7 +400,7 @@ test_server_post_handler(void) snprintf(txdata, sizeof(txdata), "1234"); nng_http_req_set_uri(st.req, "/post"); nng_http_req_set_data(st.req, txdata, strlen(txdata)); - NUTS_PASS(nng_http_req_set_method(st.req, "POST")); + nng_http_req_set_method(st.req, "POST"); NUTS_PASS(httpdo(st.url, st.req, st.res, (void **) &rxdata, &size)); NUTS_TRUE(nng_http_res_get_status(st.res) == NNG_HTTP_STATUS_OK); NUTS_TRUE(size == strlen(txdata)); @@ -410,7 +410,7 @@ test_server_post_handler(void) server_reset(&st); NUTS_PASS(nng_http_req_set_uri(st.req, "/post")); - NUTS_PASS(nng_http_req_set_method(st.req, "GET")); + nng_http_req_set_method(st.req, "GET"); NUTS_PASS(nng_http_req_set_data(st.req, txdata, strlen(txdata))); NUTS_PASS(httpdo(st.url, st.req, st.res, &data, &size)); @@ -501,7 +501,7 @@ test_server_post_redirect(void) snprintf(txdata, sizeof(txdata), "1234"); NUTS_PASS(nng_http_req_set_uri(st.req, "/here")); nng_http_req_set_data(st.req, txdata, strlen(txdata)); - NUTS_PASS(nng_http_req_set_method(st.req, "POST")); + nng_http_req_set_method(st.req, "POST"); NUTS_PASS(httpdo(st.url, st.req, st.res, (void **) &data, &size)); NUTS_TRUE(nng_http_res_get_status(st.res) == 301); dest = nng_http_res_get_header(st.res, "Location"); @@ -521,14 +521,14 @@ test_server_post_echo_tree(void) char *rxdata; NUTS_PASS(nng_http_handler_alloc(&h, "/", httpecho)); - NUTS_PASS(nng_http_handler_set_method(h, "POST")); + nng_http_handler_set_method(h, "POST"); NUTS_PASS(nng_http_handler_set_tree(h)); server_setup(&st, h); snprintf(txdata, sizeof(txdata), "1234"); nng_http_req_set_data(st.req, txdata, strlen(txdata)); - NUTS_PASS(nng_http_req_set_method(st.req, "POST")); + nng_http_req_set_method(st.req, "POST"); NUTS_PASS(nng_http_req_set_uri(st.req, "/some_sub/directory")); NUTS_PASS(httpdo(st.url, st.req, st.res, (void **) &rxdata, &size)); NUTS_TRUE(nng_http_res_get_status(st.res) == NNG_HTTP_STATUS_OK); @@ -866,7 +866,7 @@ test_serve_index_not_post(void) server_setup(&st, h); NUTS_PASS(nng_http_req_set_uri(st.req, "/subdir2/index.html")); - NUTS_PASS(nng_http_req_set_method(st.req, "POST")); + nng_http_req_set_method(st.req, "POST"); NUTS_PASS(httpget(&st, &data, &size, &stat, &ctype)); NUTS_TRUE(stat == NNG_HTTP_STATUS_METHOD_NOT_ALLOWED); nng_strfree(ctype); |
