diff --git a/include/libwebsockets/lws-http.h b/include/libwebsockets/lws-http.h index d1ecacf8f..9d6090322 100644 --- a/include/libwebsockets/lws-http.h +++ b/include/libwebsockets/lws-http.h @@ -494,6 +494,26 @@ LWS_VISIBLE LWS_EXTERN int lws_hdr_custom_copy(struct lws *wsi, char *dst, int len, const char *name, int nlen); +typedef void (*lws_hdr_custom_fe_cb_t)(const char *name, int nlen, void *opaque); +/** + * lws_hdr_custom_name_foreach() - Iterate the custom header names + * + * \param wsi: websocket connection + * \param cb: callback for each custom header name + * \param opaque: ignored by lws except to pass to callback + * + * Lws knows about 100 common http headers, and parses them into indexes when + * it recognizes them. When it meets a header that it doesn't know, it stores + * the name and value directly, and you can look them up using + * lws_hdr_custom_length() and lws_hdr_custom_copy(). + * + * This api returns -1 on error else 0. Use lws_hdr_custom_copy() to get the + * values of headers. Lws must be built with LWS_WITH_CUSTOM_HEADERS (on by + * default) to use this api. + */ +LWS_VISIBLE LWS_EXTERN int +lws_hdr_custom_name_foreach(struct lws *wsi, lws_hdr_custom_fe_cb_t cb, void *opaque); + /** * lws_get_urlarg_by_name_safe() - get copy and return length of y for x=y urlargs * diff --git a/lib/roles/http/parsers.c b/lib/roles/http/parsers.c index fef1c475e..18e5b5aab 100644 --- a/lib/roles/http/parsers.c +++ b/lib/roles/http/parsers.c @@ -656,6 +656,31 @@ lws_hdr_custom_copy(struct lws *wsi, char *dst, int len, const char *name, return -1; } + +int +lws_hdr_custom_name_foreach(struct lws *wsi, lws_hdr_custom_fe_cb_t cb, + void *custom) +{ + ah_data_idx_t ll; + + if (!wsi->http.ah || wsi->mux_substream) + return -1; + + ll = wsi->http.ah->unk_ll_head; + + while (ll) { + if (ll >= wsi->http.ah->data_length) + return -1; + + cb(&wsi->http.ah->data[ll + UHO_NAME], + lws_ser_ru16be((uint8_t *)&wsi->http.ah->data[ll + UHO_NLEN]), + custom); + + ll = lws_ser_ru32be((uint8_t *)&wsi->http.ah->data[ll + UHO_LL]); + } + + return 0; +} #endif char *lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h)