diff options
| author | Garrett D'Amore <garrett@damore.org> | 2024-11-09 15:24:43 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2024-11-09 16:27:45 -0800 |
| commit | 0058b766b91f08b34dcef3c7bb55b216099f3f66 (patch) | |
| tree | 5ac1fa105d707018cdfa9f5244da5837a8c31601 /src/supplemental/http | |
| parent | 150d80c2c62ce3693dbbd0256c16337879c7d825 (diff) | |
| download | nng-0058b766b91f08b34dcef3c7bb55b216099f3f66.tar.gz nng-0058b766b91f08b34dcef3c7bb55b216099f3f66.tar.bz2 nng-0058b766b91f08b34dcef3c7bb55b216099f3f66.zip | |
TLS configuration changed to use discret _set_tls and _get_tls functions.
This is simpler, and more reliable than using socket options.
Diffstat (limited to 'src/supplemental/http')
| -rw-r--r-- | src/supplemental/http/http_client.c | 33 | ||||
| -rw-r--r-- | src/supplemental/http/http_server.c | 156 |
2 files changed, 93 insertions, 96 deletions
diff --git a/src/supplemental/http/http_client.c b/src/supplemental/http/http_client.c index b156794c..0fbe3ef9 100644 --- a/src/supplemental/http/http_client.c +++ b/src/supplemental/http/http_client.c @@ -1,5 +1,5 @@ // -// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech> // Copyright 2018 Capitar IT Group BV <info@capitar.com> // Copyright 2019 Devolutions <info@devolutions.net> // @@ -25,7 +25,7 @@ struct nng_http_client { nni_list aios; nni_mtx mtx; bool closed; - nni_aio * aio; + nni_aio *aio; nng_stream_dialer *dialer; }; @@ -42,10 +42,10 @@ static void http_dial_cb(void *arg) { nni_http_client *c = arg; - nni_aio * aio; + nni_aio *aio; int rv; - nng_stream * stream; - nni_http_conn * conn; + nng_stream *stream; + nni_http_conn *conn; nni_mtx_lock(&c->mtx); rv = nni_aio_result(c->aio); @@ -101,7 +101,7 @@ nni_http_client_init(nni_http_client **cp, const nni_url *url) int rv; nni_http_client *c; nng_url my_url; - const char * scheme; + const char *scheme; if ((scheme = nni_http_stream_scheme(url->u_scheme)) == NULL) { return (NNG_EADDRINVAL); @@ -138,14 +138,13 @@ nni_http_client_init(nni_http_client **cp, const nni_url *url) int nni_http_client_set_tls(nni_http_client *c, nng_tls_config *tls) { - return (nng_stream_dialer_set_ptr(c->dialer, NNG_OPT_TLS_CONFIG, tls)); + return (nng_stream_dialer_set_tls(c->dialer, tls)); } int nni_http_client_get_tls(nni_http_client *c, nng_tls_config **tlsp) { - return (nng_stream_dialer_get_ptr( - c->dialer, NNG_OPT_TLS_CONFIG, (void **) tlsp)); + return (nng_stream_dialer_get_tls(c->dialer, tlsp)); } int @@ -207,12 +206,12 @@ typedef enum http_txn_state { } http_txn_state; typedef struct http_txn { - nni_aio * aio; // lower level aio + nni_aio *aio; // lower level aio nni_list aios; // upper level aio(s) -- maximum one nni_http_client *client; - nni_http_conn * conn; - nni_http_req * req; - nni_http_res * res; + nni_http_conn *conn; + nni_http_req *req; + nni_http_res *res; nni_http_chunks *chunks; http_txn_state state; } http_txn; @@ -246,13 +245,13 @@ http_txn_finish_aios(http_txn *txn, int rv) static void http_txn_cb(void *arg) { - http_txn * txn = arg; - const char * str; - char * end; + http_txn *txn = arg; + const char *str; + char *end; int rv; uint64_t len; nni_iov iov; - char * dst; + char *dst; size_t sz; nni_http_chunk *chunk = NULL; diff --git a/src/supplemental/http/http_server.c b/src/supplemental/http/http_server.c index 42ff5dd9..ac57cf5b 100644 --- a/src/supplemental/http/http_server.c +++ b/src/supplemental/http/http_server.c @@ -1,5 +1,5 @@ // -// Copyright 2023 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech> // Copyright 2018 Capitar IT Group BV <info@capitar.com> // Copyright 2018 QXSoftware <lh563566994@126.com> // Copyright 2019 Devolutions <info@devolutions.net> @@ -24,9 +24,9 @@ struct nng_http_handler { nni_list_node node; - char * uri; - char * method; - char * host; + char *uri; + char *method; + char *host; nng_sockaddr host_addr; bool host_ip; bool tree; @@ -35,33 +35,33 @@ struct nng_http_handler { nni_atomic_bool busy; size_t maxbody; bool getbody; - void * data; + void *data; nni_cb dtor; void (*cb)(nni_aio *); }; typedef struct http_sconn { nni_list_node node; - nni_http_conn * conn; - nni_http_server * server; - nni_http_req * req; - nni_http_res * res; + nni_http_conn *conn; + nni_http_server *server; + nni_http_req *req; + nni_http_res *res; nni_http_handler *handler; // set if we deferred to read body nni_http_handler *release; // set if we dispatched handler bool close; bool closed; bool finished; - nni_aio * cbaio; - nni_aio * rxaio; - nni_aio * txaio; - nni_aio * txdataio; + nni_aio *cbaio; + nni_aio *rxaio; + nni_aio *txaio; + nni_aio *txdataio; nni_reap_node reap; } http_sconn; typedef struct http_error { nni_list_node node; uint16_t code; - void * body; + void *body; size_t len; } http_error; @@ -75,10 +75,10 @@ struct nng_http_server { nni_mtx mtx; bool closed; bool fini; // if nni_http_server_fini was called - nni_aio * accaio; + nni_aio *accaio; nng_stream_listener *listener; int port; // native order - char * hostname; + char *hostname; nni_list errors; nni_mtx errors_mtx; nni_reap_node reap; @@ -277,7 +277,7 @@ static nni_mtx http_servers_lk = NNI_MTX_INITIALIZER; static void http_sc_reap(void *arg) { - http_sconn * sc = arg; + http_sconn *sc = arg; nni_http_server *s = sc->server; NNI_ASSERT(!sc->finished); sc->finished = true; @@ -346,7 +346,7 @@ static void http_sconn_txdatdone(void *arg) { http_sconn *sc = arg; - nni_aio * aio = sc->txdataio; + nni_aio *aio = sc->txdataio; if (nni_aio_result(aio) != 0) { http_sconn_close(sc); @@ -370,7 +370,7 @@ static void http_sconn_txdone(void *arg) { http_sconn *sc = arg; - nni_aio * aio = sc->txaio; + nni_aio *aio = sc->txaio; if (nni_aio_result(aio) != 0) { http_sconn_close(sc); @@ -572,21 +572,21 @@ http_handler_host_match(nni_http_handler *h, const char *host) static void http_sconn_rxdone(void *arg) { - http_sconn * sc = arg; - nni_http_server * s = sc->server; - nni_aio * aio = sc->rxaio; + http_sconn *sc = arg; + nni_http_server *s = sc->server; + nni_aio *aio = sc->rxaio; int rv; nni_http_handler *h = NULL; nni_http_handler *head = NULL; - const char * val; - nni_http_req * req = sc->req; - char * uri; + const char *val; + nni_http_req *req = sc->req; + char *uri; size_t urisz; - char * path; + char *path; bool badmeth = false; bool needhost = false; - const char * host; - const char * cls; + const char *host; + const char *cls; if ((rv = nni_aio_result(aio)) != 0) { http_sconn_close(sc); @@ -712,7 +712,7 @@ http_sconn_rxdone(void *arg) if ((h->getbody) && ((cls = nni_http_req_get_header(req, "Content-Length")) != NULL)) { uint64_t len; - char * end; + char *end; len = strtoull(cls, &end, 10); if ((end == NULL) || (*end != '\0') || (len > h->maxbody)) { @@ -762,11 +762,11 @@ finish: static void http_sconn_cbdone(void *arg) { - http_sconn * sc = arg; - nni_aio * aio = sc->cbaio; - nni_http_res * res; + http_sconn *sc = arg; + nni_aio *aio = sc->cbaio; + nni_http_res *res; nni_http_handler *h; - nni_http_server * s = sc->server; + nni_http_server *s = sc->server; // Get the handler. It may be set regardless of success or // failure. Clear it, and drop our reference, since we're @@ -804,7 +804,7 @@ http_sconn_cbdone(void *arg) } sc->res = res; if (strcmp(nni_http_req_get_method(sc->req), "HEAD") == 0) { - void * data; + void *data; size_t size; // prune off the data, but preserve the content-length // header. By passing NULL here, we leave off the old @@ -863,9 +863,9 @@ static void http_server_acccb(void *arg) { nni_http_server *s = arg; - nni_aio * aio = s->accaio; - nng_stream * stream; - http_sconn * sc; + nni_aio *aio = s->accaio; + nng_stream *stream; + http_sconn *sc; int rv; nni_mtx_lock(&s->mtx); @@ -904,7 +904,7 @@ static void http_server_fini(nni_http_server *s) { nni_http_handler *h; - http_error * epage; + http_error *epage; nni_aio_stop(s->accaio); @@ -937,7 +937,7 @@ http_server_init(nni_http_server **serverp, const nni_url *url) nni_http_server *s; int rv; nng_url my_url; - const char * scheme; + const char *scheme; if ((scheme = nni_http_stream_scheme(url->u_scheme)) == NULL) { return (NNG_EADDRINVAL); @@ -1127,7 +1127,7 @@ int nni_http_server_set_error_page( nni_http_server *s, uint16_t code, const char *html) { - char * body; + char *body; int rv; size_t len; @@ -1147,7 +1147,7 @@ int nni_http_server_set_error_file( nni_http_server *s, uint16_t code, const char *path) { - void * body; + void *body; size_t len; int rv; if ((rv = nni_file_get(path, &body, &len)) != 0) { @@ -1163,8 +1163,8 @@ int nni_http_server_res_error(nni_http_server *s, nni_http_res *res) { http_error *epage; - char * body = NULL; - char * html = NULL; + char *body = NULL; + char *html = NULL; size_t len = 0; uint16_t code = nni_http_res_get_status(res); int rv; @@ -1394,12 +1394,12 @@ static void http_handle_file(nni_aio *aio) { nni_http_handler *h = nni_aio_get_input(aio, 1); - nni_http_res * res = NULL; - void * data; + nni_http_res *res = NULL; + void *data; size_t size; int rv; - http_file * hf = nni_http_handler_get_data(h); - const char * ctype; + http_file *hf = nni_http_handler_get_data(h); + const char *ctype; if ((ctype = hf->ctype) == NULL) { ctype = "application/octet-stream"; @@ -1466,7 +1466,7 @@ 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; + http_file *hf; int rv; if ((hf = NNI_ALLOC_STRUCT(hf)) == NULL) { @@ -1515,21 +1515,21 @@ nni_http_handler_init_file( static void http_handle_dir(nni_aio *aio) { - nni_http_req * req = nni_aio_get_input(aio, 0); + nni_http_req *req = nni_aio_get_input(aio, 0); nni_http_handler *h = nni_aio_get_input(aio, 1); - nni_http_res * res = NULL; - void * data; + nni_http_res *res = NULL; + void *data; size_t size; int rv; - http_file * hf = nni_http_handler_get_data(h); - const char * path = hf->path; - const char * base = nni_http_handler_get_uri(h); // base uri - const char * uri = nni_http_req_get_uri(req); - const char * ctype; - char * dst; + http_file *hf = nni_http_handler_get_data(h); + const char *path = hf->path; + const char *base = nni_http_handler_get_uri(h); // base uri + const char *uri = nni_http_req_get_uri(req); + const char *ctype; + char *dst; size_t len; size_t pnsz; - char * pn; + char *pn; len = strlen(base); if (base[1] != '\0' && // Allows "/" as base @@ -1647,7 +1647,7 @@ int nni_http_handler_init_directory( nni_http_handler **hpp, const char *uri, const char *path) { - http_file * hf; + http_file *hf; nni_http_handler *h; int rv; @@ -1679,22 +1679,22 @@ nni_http_handler_init_directory( typedef struct http_redirect { uint16_t code; - char * where; + char *where; } http_redirect; static void http_handle_redirect(nni_aio *aio) { - nni_http_res * r = NULL; - char * html = NULL; - char * msg = NULL; - char * loc = NULL; - http_redirect * hr; + nni_http_res *r = NULL; + char *html = NULL; + char *msg = NULL; + char *loc = NULL; + http_redirect *hr; nni_http_handler *h; int rv; - nni_http_req * req; - const char * base; - const char * uri; + nni_http_req *req; + const char *base; + const char *uri; req = nni_aio_get_input(aio, 0); h = nni_aio_get_input(aio, 1); @@ -1766,7 +1766,7 @@ nni_http_handler_init_redirect(nni_http_handler **hpp, const char *uri, { nni_http_handler *h; int rv; - http_redirect * hr; + http_redirect *hr; if ((hr = NNI_ALLOC_STRUCT(hr)) == NULL) { return (NNG_ENOMEM); @@ -1802,18 +1802,18 @@ nni_http_handler_init_redirect(nni_http_handler **hpp, const char *uri, } typedef struct http_static { - void * data; + void *data; size_t size; - char * ctype; + char *ctype; } http_static; static void http_handle_static(nni_aio *aio) { - http_static * hs; - const char * ctype; + http_static *hs; + const char *ctype; nni_http_handler *h; - nni_http_res * r = NULL; + nni_http_res *r = NULL; int rv; h = nni_aio_get_input(aio, 1); @@ -1854,7 +1854,7 @@ nni_http_handler_init_static(nni_http_handler **hpp, const char *uri, { nni_http_handler *h; int rv; - http_static * hs; + http_static *hs; if ((hs = NNI_ALLOC_STRUCT(hs)) == NULL) { return (NNG_ENOMEM); @@ -1888,15 +1888,13 @@ nni_http_handler_init_static(nni_http_handler **hpp, const char *uri, int nni_http_server_set_tls(nni_http_server *s, nng_tls_config *tls) { - return ( - nng_stream_listener_set_ptr(s->listener, NNG_OPT_TLS_CONFIG, tls)); + return (nng_stream_listener_set_tls(s->listener, tls)); } int nni_http_server_get_tls(nni_http_server *s, nng_tls_config **tlsp) { - return (nng_stream_listener_get_ptr( - s->listener, NNG_OPT_TLS_CONFIG, (void **) tlsp)); + return (nng_stream_listener_get_tls(s->listener, tlsp)); } int |
