aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/ref/api/http.md5
-rw-r--r--include/nng/http.h9
-rw-r--r--src/supplemental/http/http_conn.c32
-rw-r--r--src/supplemental/http/http_public.c15
4 files changed, 59 insertions, 2 deletions
diff --git a/docs/ref/api/http.md b/docs/ref/api/http.md
index bbe33c24..b9ecca83 100644
--- a/docs/ref/api/http.md
+++ b/docs/ref/api/http.md
@@ -188,7 +188,7 @@ Status codes are defined by the IETF. Here are definitions that NNG provides for
```c
const char *nng_http_get_header(nng_http *conn, const char *key);
-bool nng_next_header(nng_http *conn, const char **keyp, const char **valuep, void **next);
+bool nng_http_next_header(nng_http *conn, const char **keyp, const char **valuep, void **next);
```
The {{i:`nng_http_get_header`}} returns the header value matching _key_ that was received over _conn_,
@@ -614,7 +614,8 @@ it may simply let the framework do so on its behalf. The server will perform
this step if the callback has not already done so.
Response headers may be set using [`nng_http_set_header`], and request headers
-may be accessed by using [`nng_http_get_header`].
+may be accessed by using [`nng_http_get_header`]. They can also be iterated
+over using [`nng_http_next_header`]
Likewise the request body may be accessed, using [`nng_http_get_body`], and
the response body may be set using either [`nng_http_set_body`] or [`nng_http_copy_body`].
diff --git a/include/nng/http.h b/include/nng/http.h
index f8bfb679..18fc03ea 100644
--- a/include/nng/http.h
+++ b/include/nng/http.h
@@ -197,6 +197,15 @@ NNG_DECL void nng_http_del_header(nng_http *, const char *);
// It returns NULL if no matching header can be found.
NNG_DECL const char *nng_http_get_header(nng_http *, const char *);
+// nng_http_next_header iterates over HTTP headers. For clients,
+// it iterates over the response header, but for servers it iterates ovre the
+// request header. The key will receive the name of the header, and the value
+// the will receive its value. The caller starts the iteration by setting ptr
+// to NULL, and then the value must be preserved. The function returns NNG_OK
+// on success, or NNG_ENOENT if there are no more headers.
+NNG_DECL nng_err nng_http_next_header(
+ nng_http *, const char **key, const char **val, void **ptr);
+
// nng_http_get_body returns the body sent by the peer, if one is attached.
NNG_DECL void nng_http_get_body(nng_http *, void **, size_t *);
diff --git a/src/supplemental/http/http_conn.c b/src/supplemental/http/http_conn.c
index 93068512..6f2ecef6 100644
--- a/src/supplemental/http/http_conn.c
+++ b/src/supplemental/http/http_conn.c
@@ -1372,6 +1372,38 @@ http_get_header(const nni_list *hdrs, const char *key)
return (NULL);
}
+static bool
+http_next_header(
+ const nni_list *hdrs, const char **key, const char **val, void **ptr)
+{
+ http_header *h;
+
+ if (*ptr == NULL) {
+ h = nni_list_first(hdrs);
+ } else {
+ h = nni_list_next(hdrs, *ptr);
+ }
+ if (h == NULL) {
+ return (false);
+ }
+
+ *ptr = h;
+ *key = h->name;
+ *val = h->value;
+ return (true);
+}
+
+bool
+nni_http_next_header(
+ nng_http *conn, const char **key, const char **val, void **ptr)
+{
+ if (conn->client) {
+ return (http_next_header(&conn->res.data.hdrs, key, val, ptr));
+ } else {
+ return (http_next_header(&conn->req.data.hdrs, key, val, ptr));
+ }
+}
+
const char *
nni_http_get_header(nng_http *conn, const char *key)
{
diff --git a/src/supplemental/http/http_public.c b/src/supplemental/http/http_public.c
index 5c7d8a77..419632fe 100644
--- a/src/supplemental/http/http_public.c
+++ b/src/supplemental/http/http_public.c
@@ -28,6 +28,21 @@ nng_http_get_header(nng_http *conn, const char *key)
}
nng_err
+nng_http_next_header(
+ nng_http *conn, const char **key, const char **val, void **ptr)
+{
+#ifdef NNG_SUP_HTTP
+ return (nni_http_next_header(conn, key, val, ptr));
+#else
+ NNI_ARG_UNUSED(conn);
+ NNI_ARG_UNUSED(key);
+ NNI_ARG_UNUSED(val);
+ NNI_ARG_UNUSED(ptr);
+ return (NNG_ENOTSUP);
+#endif
+}
+
+nng_err
nng_http_set_header(nng_http *conn, const char *key, const char *val)
{
#ifdef NNG_SUPP_HTTP