diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index 82a47f56..35361252 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -1556,11 +1556,25 @@ lws_b64_decode_string(const char *in, char *out, int out_size); LWS_VISIBLE LWS_EXTERN const char * lws_get_library_version(void); -/* access to headers... only valid while headers valid */ +/* + * Access to http headers + * + * In lws the client http headers are temporarily malloc'd only for the + * duration of the http part of the handshake. It's because in most cases, + * the header content is ignored for the whole rest of the connection lifetime + * and would then just be taking up space needlessly. + * + * During LWS_CALLBACK_HTTP when the URI path is delivered is the last time + * the http headers are still allocated, you can use these apis then to + * look at and copy out interesting header content (cookies, etc) + */ LWS_VISIBLE LWS_EXTERN int lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h); +LWS_VISIBLE LWS_EXTERN int +lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx); + /* * copies the whole, aggregated header, even if it was delivered in * several actual headers piece by piece @@ -1577,6 +1591,7 @@ LWS_VISIBLE LWS_EXTERN int lws_hdr_copy_fragment(struct lws *wsi, char *dest, int len, enum lws_token_indexes h, int frag_idx); + /* get the active file operations struct */ LWS_VISIBLE LWS_EXTERN struct lws_plat_file_ops * lws_get_fops(struct lws_context *context); diff --git a/lib/parsers.c b/lib/parsers.c index 2b63fae5..18037d0d 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -81,6 +81,23 @@ int lws_free_header_table(struct lws *wsi) return 0; }; +LWS_VISIBLE int +lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx) +{ + int n; + + n = wsi->u.hdr.ah->frag_index[h]; + if (!n) + return 0; + do { + if (!frag_idx) + return wsi->u.hdr.ah->frags[n].len; + n = wsi->u.hdr.ah->frags[n].nfrag; + } while (frag_idx-- && n); + + return 0; +} + LWS_VISIBLE int lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h) { int n;