![]() |
libwebsockets
Lightweight C library for HTML5 websockets
|
Data Structures | |
struct | lws_tokens |
struct | lws_token_limits |
Functions | |
LWS_VISIBLE LWS_EXTERN const unsigned char * | lws_token_to_string (enum lws_token_indexes token) |
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT | lws_hdr_total_length (struct lws *wsi, enum lws_token_indexes h) |
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT | lws_hdr_fragment_length (struct lws *wsi, enum lws_token_indexes h, int frag_idx) |
LWS_VISIBLE LWS_EXTERN int | lws_hdr_copy (struct lws *wsi, char *dest, int len, enum lws_token_indexes h) |
LWS_VISIBLE LWS_EXTERN int | lws_hdr_copy_fragment (struct lws *wsi, char *dest, int len, enum lws_token_indexes h, int frag_idx) |
LWS_VISIBLE LWS_EXTERN const char * | lws_get_urlarg_by_name (struct lws *wsi, const char *name, char *buf, int len) |
In lws the client http headers are temporarily stored in a pool, 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)
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.
In the special case of URL arguments, like ?x=1&y=2, the arguments are stored in a token named for the method, eg, WSI_TOKEN_GET_URI if it was a GET or WSI_TOKEN_POST_URI if POST. You can check the total length to confirm the method.
For URL arguments, each argument is stored urldecoded in a "fragment", so you can use the fragment-aware api lws_hdr_copy_fragment() to access each argument in turn: the fragments contain urldecoded strings like x=1 or y=2.
As a convenience, lws has an api that will find the fragment with a given name= part, lws_get_urlarg_by_name().
LWS_VISIBLE LWS_EXTERN const char* lws_get_urlarg_by_name | ( | struct lws * | wsi, |
const char * | name, | ||
char * | buf, | ||
int | len | ||
) |
#include <lib/libwebsockets.h>
lws_get_urlarg_by_name() - return pointer to arg value if present
wsi | the connection to check |
name | the arg name, like "token=" |
buf | the buffer to receive the urlarg (including the name= part) |
len | the length of the buffer to receive the urlarg Returns NULL if not found or a pointer inside buf to just after the name= part. |
LWS_VISIBLE LWS_EXTERN int lws_hdr_copy | ( | struct lws * | wsi, |
char * | dest, | ||
int | len, | ||
enum lws_token_indexes | h | ||
) |
#include <lib/libwebsockets.h>
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 |
dest | destination buffer |
len | length of destination buffer |
h | which header index we are interested in |
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_fragment | ( | struct lws * | wsi, |
char * | dest, | ||
int | len, | ||
enum lws_token_indexes | h, | ||
int | frag_idx | ||
) |
#include <lib/libwebsockets.h>
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 |
dest | destination buffer |
len | length of destination buffer |
h | which header index we are interested in |
frag_idx | which fragment of h we want to copy |
Normally this is only useful to parse URI arguments like ?x=1&y=2, token index WSI_TOKEN_HTTP_URI_ARGS fragment 0 will contain "x=1" and fragment 1 "y=2"
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_hdr_fragment_length | ( | struct lws * | wsi, |
enum lws_token_indexes | h, | ||
int | frag_idx | ||
) |
#include <lib/libwebsockets.h>
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 LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_hdr_total_length | ( | struct lws * | wsi, |
enum lws_token_indexes | h | ||
) |
#include <lib/libwebsockets.h>
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 LWS_EXTERN const unsigned char* lws_token_to_string | ( | enum lws_token_indexes | token | ) |
#include <lib/libwebsockets.h>
lws_token_to_string() - returns a textual representation of a hdr token index