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 int | lws_hdr_custom_length (struct lws *wsi, const char *name, int nlen) |
LWS_VISIBLE LWS_EXTERN int | lws_hdr_custom_copy (struct lws *wsi, char *dst, int len, const char *name, int nlen) |
LWS_VISIBLE LWS_EXTERN int | lws_get_urlarg_by_name_safe (struct lws *wsi, const char *name, char *buf, int len) |
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 <include/libwebsockets/lws-http.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.This assumed the argument can be represented with a NUL-terminated string. It can't correctly deal with binary values encoded with XX, eg. %00 will be understood to terminate the string. |
Use lws_get_urlarg_by_name_safe() instead of this, which returns the length.
LWS_VISIBLE LWS_EXTERN int lws_get_urlarg_by_name_safe | ( | struct lws * | wsi, |
const char * | name, | ||
char * | buf, | ||
int | len | ||
) |
#include <include/libwebsockets/lws-http.h>
lws_get_urlarg_by_name_safe() - get copy and return length of y for x=y urlargs
wsi | the connection to check |
name | the arg name, like "token" or "token=" |
buf | the buffer to receive the urlarg (including the name= part) |
len | the length of the buffer to receive the urlarg |
Returns -1 if not present, else the length of y in the urlarg name=y. If zero or greater, then buf contains a copy of the string y. Any = after the name match is trimmed off if the name does not end with = itself.
This returns the explicit length and so can deal with binary blobs that are percent-encoded. It also makes sure buf has a NUL just after the valid length so it can work with NUL-based apis if you don't care about truncation.
buf may have been written even when -1 is returned indicating no match.
Use this in place of lws_get_urlarg_by_name() that does not return an explicit length.
Use lws_get_urlarg_by_name_safe() instead of this, which returns the length.
LWS_VISIBLE LWS_EXTERN int lws_hdr_copy | ( | struct lws * | wsi, |
char * | dest, | ||
int | len, | ||
enum lws_token_indexes | h | ||
) |
#include <include/libwebsockets/lws-http.h>
lws_hdr_copy() - copy all fragments 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. Returns -1 or length of the whole header.
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 <include/libwebsockets/lws-http.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_hdr_custom_copy | ( | struct lws * | wsi, |
char * | dst, | ||
int | len, | ||
const char * | name, | ||
int | nlen | ||
) |
#include <include/libwebsockets/lws-http.h>
lws_hdr_custom_copy() - copy value part of a custom header
wsi | websocket connection |
dst | pointer to buffer to receive the copy |
len | number of bytes available at dst |
name | header string (including terminating :) |
nlen | length of name |
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, or the length of the string it copied into dst if it was big enough to contain both the string and an extra terminating NUL. Lws must be built with LWS_WITH_CUSTOM_HEADERS (on by default) to use this api.
LWS_VISIBLE LWS_EXTERN int lws_hdr_custom_length | ( | struct lws * | wsi, |
const char * | name, | ||
int | nlen | ||
) |
#include <include/libwebsockets/lws-http.h>
lws_hdr_custom_length() - return length of a custom header
wsi | websocket connection |
name | header string (including terminating :) |
nlen | length of name |
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, or the length of the value part of the header if it exists. Lws must be built with LWS_WITH_CUSTOM_HEADERS (on by default) to use this api.
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 <include/libwebsockets/lws-http.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 <include/libwebsockets/lws-http.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 <include/libwebsockets/lws-http.h>
lws_token_to_string() - returns a textual representation of a hdr token index
token | token index |