diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-02-01 16:48:20 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-02-01 20:07:09 -0800 |
| commit | 5e5f814d63d4e00365b0ae726bc18831aa28e88f (patch) | |
| tree | 804a930d4f9f941cd75e156f169a52e7110ef1e9 | |
| parent | 3dae30ed5e543dc73fc993334ef56b9b157b9b3c (diff) | |
| download | nng-5e5f814d63d4e00365b0ae726bc18831aa28e88f.tar.gz nng-5e5f814d63d4e00365b0ae726bc18831aa28e88f.tar.bz2 nng-5e5f814d63d4e00365b0ae726bc18831aa28e88f.zip | |
fixes #174 Define public HTTP client API
| -rw-r--r-- | src/nng.c | 21 | ||||
| -rw-r--r-- | src/nng.h | 38 | ||||
| -rw-r--r-- | src/supplemental/http/http.h | 5 | ||||
| -rw-r--r-- | src/supplemental/http/http_client.c | 4 | ||||
| -rw-r--r-- | src/supplemental/http/http_public.c | 87 | ||||
| -rw-r--r-- | tests/httpclient.c | 11 | ||||
| -rw-r--r-- | tests/httpserver.c | 21 |
7 files changed, 150 insertions, 37 deletions
@@ -1100,6 +1100,27 @@ nng_aio_set_input(nng_aio *aio, unsigned index, void *arg) nni_aio_set_input(aio, index, arg); return (0); } +int +nng_aio_set_output(nng_aio *aio, unsigned index, void *arg) +{ + if (index > 3) { + return (NNG_EINVAL); + } + nni_aio_set_output(aio, index, arg); + return (0); +} + +void * +nng_aio_get_input(nng_aio *aio, unsigned index) +{ + return (nni_aio_get_input(aio, index)); +} + +void * +nng_aio_get_output(nng_aio *aio, unsigned index) +{ + return (nni_aio_get_output(aio, index)); +} #if 0 int @@ -322,14 +322,6 @@ NNG_DECL int nng_aio_set_output(nng_aio *, unsigned, void *); // nng_aio_get_output retrieves the output result at the given index. NNG_DECL void *nng_aio_get_output(nng_aio *, unsigned); -// nng_aio_set_data sets an opaque data at the given index. The intention -// is to allow consumers to pass additional state for use in callback -// functions. -NNG_DECL int nng_aio_set_data(nng_aio *, unsigned, void *); - -// nng_aio_get_data retrieves the data that was previously stored. -NNG_DECL void *nng_aio_get_output(nng_aio *, unsigned); - // nng_aio_set_timeout sets a timeout on the AIO. This should be called for // operations that should time out after a period. The timeout should be // either a positive number of milliseconds, or NNG_DURATION_INFINITE to @@ -1184,6 +1176,36 @@ NNG_DECL int nng_http_server_get_tls(nng_http_server *, nng_tls_config **); NNG_DECL int nng_http_hijack(nng_http_conn *); +// nng_http_client represents a "client" object. Clients can be used +// to create HTTP connections. At present, connections are not cached +// or reused, but that could change in the future. +typedef struct nng_http_client nng_http_client; + +// nng_http_client_alloc allocates a client object, associated with +// the given URL. +NNG_DECL int nng_http_client_alloc(nng_http_client **, const nng_url *); + +// nng_http_client_free frees the client. Connections created by the +// the client are not necessarily closed. +NNG_DECL void nng_http_client_free(nng_http_client *); + +// nng_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. +NNG_DECL int nng_http_client_set_tls(nng_http_client *, nng_tls_config *); + +// nng_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. +NNG_DECL int nng_http_client_get_tls(nng_http_client *, nng_tls_config **); + +// nng_http_client_connect establishes a new connection with the server +// named in the URL used when the client was created. Once the connection +// is established, the associated nng_http_conn object pointer is returned +// in the first (index 0) output for the aio. +NNG_DECL void nng_http_client_connect(nng_http_client *, nng_aio *); + #ifdef __cplusplus } #endif diff --git a/src/supplemental/http/http.h b/src/supplemental/http/http.h index e44ddc76..7fadf705 100644 --- a/src/supplemental/http/http.h +++ b/src/supplemental/http/http.h @@ -20,6 +20,7 @@ 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; // These functions are private to the internal framework, and really should // not be used elsewhere. @@ -259,9 +260,7 @@ extern const char *nni_http_handler_get_uri(nni_http_handler *); // Client stuff. -typedef struct nni_http_client nni_http_client; - -extern int nni_http_client_init(nni_http_client **, nni_url *); +extern int nni_http_client_init(nni_http_client **, const nni_url *); extern void nni_http_client_fini(nni_http_client *); // nni_http_client_set_tls sets the TLS configuration. This wipes out diff --git a/src/supplemental/http/http_client.c b/src/supplemental/http/http_client.c index 345e5947..918b7b09 100644 --- a/src/supplemental/http/http_client.c +++ b/src/supplemental/http/http_client.c @@ -18,7 +18,7 @@ #include "http.h" -struct nni_http_client { +struct nng_http_client { nni_list aios; nni_mtx mtx; bool closed; @@ -95,7 +95,7 @@ nni_http_client_fini(nni_http_client *c) } int -nni_http_client_init(nni_http_client **cp, nni_url *url) +nni_http_client_init(nni_http_client **cp, const nni_url *url) { int rv; nni_http_client *c; diff --git a/src/supplemental/http/http_public.c b/src/supplemental/http/http_public.c index f19b83b1..6c28e835 100644 --- a/src/supplemental/http/http_public.c +++ b/src/supplemental/http/http_public.c @@ -377,7 +377,9 @@ nng_http_conn_read(nng_http_conn *conn, nng_aio *aio) nni_http_read(conn, aio); #else NNI_ARG_UNUSED(conn); - NNI_ARG_UNUSED(aio); + if (nni_aio_start(aio, NULL, NULL)) { + nni_aio_finish_error(aio, NNG_ENOTSUP); + } #endif } @@ -388,7 +390,9 @@ nng_http_conn_read_all(nng_http_conn *conn, nng_aio *aio) nni_http_read_full(conn, aio); #else NNI_ARG_UNUSED(conn); - NNI_ARG_UNUSED(aio); + if (nni_aio_start(aio, NULL, NULL)) { + nni_aio_finish_error(aio, NNG_ENOTSUP); + } #endif } @@ -399,7 +403,9 @@ nng_http_conn_write(nng_http_conn *conn, nng_aio *aio) nni_http_write(conn, aio); #else NNI_ARG_UNUSED(conn); - NNI_ARG_UNUSED(aio); + if (nni_aio_start(aio, NULL, NULL)) { + nni_aio_finish_error(aio, NNG_ENOTSUP); + } #endif } @@ -422,7 +428,9 @@ nng_http_conn_write_req(nng_http_conn *conn, nng_http_req *req, nng_aio *aio) #else NNI_ARG_UNUSED(conn); NNI_ARG_UNUSED(req); - NNI_ARG_UNUSED(aio); + if (nni_aio_start(aio, NULL, NULL)) { + nni_aio_finish_error(aio, NNG_ENOTSUP); + } #endif } @@ -434,7 +442,9 @@ nng_http_conn_write_res(nng_http_conn *conn, nng_http_res *res, nng_aio *aio) #else NNI_ARG_UNUSED(conn); NNI_ARG_UNUSED(res); - NNI_ARG_UNUSED(aio); + if (nni_aio_start(aio, NULL, NULL)) { + nni_aio_finish_error(aio, NNG_ENOTSUP); + } #endif } @@ -446,7 +456,9 @@ nng_http_conn_read_req(nng_http_conn *conn, nng_http_req *req, nng_aio *aio) #else NNI_ARG_UNUSED(conn); NNI_ARG_UNUSED(req); - NNI_ARG_UNUSED(aio); + if (nni_aio_start(aio, NULL, NULL)) { + nni_aio_finish_error(aio, NNG_ENOTSUP); + } #endif } @@ -458,7 +470,9 @@ nng_http_conn_read_res(nng_http_conn *conn, nng_http_res *res, nng_aio *aio) #else NNI_ARG_UNUSED(conn); NNI_ARG_UNUSED(res); - NNI_ARG_UNUSED(aio); + if (nni_aio_start(aio, NULL, NULL)) { + nni_aio_finish_error(aio, NNG_ENOTSUP); + } #endif } @@ -666,3 +680,62 @@ nng_http_server_get_tls(nng_http_server *srv, nng_tls_config **cfgp) return (NNG_ENOTSUP); #endif } + +int +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 +} + +int +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 +} + +int +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 + return (nni_http_client_connect(cli, aio)); +#else + NNI_ARG_UNUSED(cli); + if (nni_aio_start(aio, NULL, NULL)) { + nni_aio_finish_error(aio, NNG_ENOTSUP); + } +#endif +} diff --git a/tests/httpclient.c b/tests/httpclient.c index f26674f6..2438ab4c 100644 --- a/tests/httpclient.c +++ b/tests/httpclient.c @@ -17,7 +17,6 @@ // Basic HTTP client tests. #include "core/nng_impl.h" -#include "supplemental/http/http.h" #include "supplemental/sha1/sha1.h" const uint8_t utf8_sha1sum[20] = { 0x54, 0xf3, 0xb8, 0xbb, 0xfe, 0xda, 0x6f, @@ -31,7 +30,7 @@ TestMain("HTTP Client", { Convey("Given a TCP connection to httpbin.org", { nng_aio * aio; - nni_http_client *cli; + nng_http_client *cli; nng_http_conn * http; nng_url * url; @@ -39,13 +38,13 @@ TestMain("HTTP Client", { So(nng_url_parse(&url, "http://httpbin.org/encoding/utf8") == 0); - So(nni_http_client_init(&cli, url) == 0); - nni_http_client_connect(cli, aio); + So(nng_http_client_alloc(&cli, url) == 0); + nng_http_client_connect(cli, aio); nng_aio_wait(aio); So(nng_aio_result(aio) == 0); - http = nni_aio_get_output(aio, 0); + http = nng_aio_get_output(aio, 0); Reset({ - nni_http_client_fini(cli); + nng_http_client_free(cli); nng_http_conn_close(http); nng_aio_free(aio); nng_url_free(url); diff --git a/tests/httpserver.c b/tests/httpserver.c index 1008058f..a62bae25 100644 --- a/tests/httpserver.c +++ b/tests/httpserver.c @@ -17,7 +17,6 @@ // Basic HTTP server tests. #include "core/nng_impl.h" -#include "supplemental/http/http.h" const char *doc1 = "<html><body>Someone <b>is</b> home!</body</html>"; const char *doc2 = "This is a text file."; @@ -35,23 +34,23 @@ httpdo(nng_url *url, nng_http_req *req, nng_http_res *res, void **datap, { int rv; nng_aio * aio = NULL; - nni_http_client *cli = NULL; + nng_http_client *cli = NULL; nng_http_conn * h = NULL; size_t clen = 0; void * data = NULL; const char * ptr; if (((rv = nng_aio_alloc(&aio, NULL, NULL)) != 0) || - ((rv = nni_http_client_init(&cli, url)) != 0)) { + ((rv = nng_http_client_alloc(&cli, url)) != 0)) { goto fail; } - nni_http_client_connect(cli, aio); + nng_http_client_connect(cli, aio); nng_aio_wait(aio); - if ((rv = nni_aio_result(aio)) != 0) { + if ((rv = nng_aio_result(aio)) != 0) { goto fail; } - h = nni_aio_get_output(aio, 0); + h = nng_aio_get_output(aio, 0); nng_http_conn_write_req(h, req, aio); nng_aio_wait(aio); @@ -94,7 +93,7 @@ fail: nng_http_conn_close(h); } if (cli != NULL) { - nni_http_client_fini(cli); + nng_http_client_free(cli); } return (rv); @@ -191,13 +190,13 @@ TestMain("HTTP Server", { So(nng_http_server_start(s) == 0); Convey("We can connect a client to it", { - nni_http_client *cli; + nng_http_client *cli; nng_http_conn * h; nng_http_req * req; nng_http_res * res; - So(nni_http_client_init(&cli, url) == 0); - nni_http_client_connect(cli, aio); + So(nng_http_client_alloc(&cli, url) == 0); + nng_http_client_connect(cli, aio); nng_aio_wait(aio); So(nng_aio_result(aio) == 0); @@ -207,7 +206,7 @@ TestMain("HTTP Server", { So(nng_http_res_alloc(&res) == 0); Reset({ - nni_http_client_fini(cli); + nng_http_client_free(cli); nng_http_conn_close(h); nng_http_req_free(req); nng_http_res_free(res); |
