From 5e5f814d63d4e00365b0ae726bc18831aa28e88f Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Thu, 1 Feb 2018 16:48:20 -0800 Subject: fixes #174 Define public HTTP client API --- src/nng.c | 21 +++++++++ src/nng.h | 38 ++++++++++++---- src/supplemental/http/http.h | 5 +-- src/supplemental/http/http_client.c | 4 +- src/supplemental/http/http_public.c | 87 ++++++++++++++++++++++++++++++++++--- 5 files changed, 135 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/nng.c b/src/nng.c index 16de18d5..92d19d87 100644 --- a/src/nng.c +++ b/src/nng.c @@ -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 diff --git a/src/nng.h b/src/nng.h index 47cd3bcc..98c42dc6 100644 --- a/src/nng.h +++ b/src/nng.h @@ -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 +} -- cgit v1.2.3-70-g09d2