diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index bfc28e3d..d667a24b 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -652,7 +652,7 @@ enum lws_token_indexes { }; struct lws_token_limits { - unsigned short token_limit[WSI_TOKEN_COUNT]; + unsigned short token_limit[WSI_TOKEN_COUNT]; }; /* @@ -1622,9 +1622,22 @@ lws_get_library_version(void); LWS_VISIBLE LWS_EXTERN int lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h); +/* + * copies the whole, aggregated header, even if it was delivered in + * several actual headers piece by piece + */ LWS_VISIBLE LWS_EXTERN int lws_hdr_copy(struct lws *wsi, char *dest, int len, enum lws_token_indexes h); +/* + * copies only fragment frag_idx of a header. Normally this is only useful + * to parse URI arguments like ?x=1&y=2, oken index WSI_TOKEN_HTTP_URI_ARGS + * fragment 0 will contain "x=1" and fragment 1 "y=2" + */ +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 f20364f0..c33eccc2 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -97,6 +97,29 @@ LWS_VISIBLE int lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h) return len; } +LWS_VISIBLE int lws_hdr_copy_fragment(struct lws *wsi, char *dst, int len, + enum lws_token_indexes h, int frag_idx) +{ + int n = 0; + int f = wsi->u.hdr.ah->frag_index[h]; + + while (n < frag_idx) { + f = wsi->u.hdr.ah->frags[f].nfrag; + if (!f) + return -1; + n++; + } + + if (wsi->u.hdr.ah->frags[f].len >= (len - 1)) + return -1; + + memcpy(dst, &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[f].offset], + wsi->u.hdr.ah->frags[f].len); + dst[wsi->u.hdr.ah->frags[f].len] = '\0'; + + return wsi->u.hdr.ah->frags[f].len; +} + LWS_VISIBLE int lws_hdr_copy(struct lws *wsi, char *dst, int len, enum lws_token_indexes h) {