diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index 129f776d..3199ec45 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -1616,6 +1616,12 @@ lws_get_library_version(void); * 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) + * + * Notice that the header total length reported does not include a terminating + * '\0', however you must allocate for it when using the _copy apis. So the + * length reported for a header containing "123" is 3, but you must provide + * a buffer of length 4 so that "123\0" may be copied into it, or the copy + * will fail with a nonzero return code. */ LWS_VISIBLE LWS_EXTERN int diff --git a/lib/parsers.c b/lib/parsers.c index ed36ecde..9df7a0e7 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -136,6 +136,16 @@ int lws_free_header_table(struct lws *wsi) return 0; } +/** + * lws_hdr_fragment_length: report length of a single fragment of a header + * The returned length does not include the space for a + * terminating '\0' + * + * @wsi: websocket connection + * @h: which header index we are interested in + * @frag_idx: which fragment of @h we want to get the length of + */ + LWS_VISIBLE int lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx) { @@ -153,6 +163,15 @@ lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx) return 0; } +/** + * lws_hdr_total_length: report length of all fragments of a header totalled up + * The returned length does not include the space for a + * terminating '\0' + * + * @wsi: websocket connection + * @h: which header index we are interested in + */ + LWS_VISIBLE int lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h) { int n; @@ -169,6 +188,20 @@ LWS_VISIBLE int lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h) return len; } +/** + * lws_hdr_copy_fragment: copy a single fragment of the given header to a buffer + * The buffer length @len must include space for an additional + * terminating '\0', or it will fail returning -1. + * If the requested fragment index is not present, it fails + * returning -1. + * + * @wsi: websocket connection + * @dst: destination buffer + * @len: length of destination buffer + * @h: which header index we are interested in + * @frag_index: which fragment of @h we want to copy + */ + LWS_VISIBLE int lws_hdr_copy_fragment(struct lws *wsi, char *dst, int len, enum lws_token_indexes h, int frag_idx) { @@ -192,6 +225,17 @@ LWS_VISIBLE int lws_hdr_copy_fragment(struct lws *wsi, char *dst, int len, return wsi->u.hdr.ah->frags[f].len; } +/** + * lws_hdr_copy: copy a single fragment of the given header to a buffer + * The buffer length @len must include space for an additional + * terminating '\0', or it will fail returning -1. + * + * @wsi: websocket connection + * @dst: destination buffer + * @len: length of destination buffer + * @h: which header index we are interested in + */ + LWS_VISIBLE int lws_hdr_copy(struct lws *wsi, char *dst, int len, enum lws_token_indexes h) { diff --git a/libwebsockets-api-doc.html b/libwebsockets-api-doc.html index 489f8310..779c84e0 100644 --- a/libwebsockets-api-doc.html +++ b/libwebsockets-api-doc.html @@ -271,6 +271,96 @@ would call it with a timeout_ms of 0, so it returns immediately if nothing is pending, or as soon as it services whatever was pending.
+The returned length does not include the space for a +terminating '\0' ++
+The returned length does not include the space for a +terminating '\0' ++
+The buffer length len must include space for an additional +terminating '\0', or it will fail returning -1. +If the requested fragment index is not present, it fails +returning -1. ++
+The buffer length len must include space for an additional +terminating '\0', or it will fail returning -1. ++