libwebsockets
Lightweight C library for HTML5 websockets
HTTP headers: read

Data Structures

struct  lws_tokens
 
struct  lws_token_limits
 

Enumerations

enum  lws_token_indexes {
  WSI_TOKEN_GET_URI = 0, WSI_TOKEN_POST_URI = 1, WSI_TOKEN_OPTIONS_URI = 2, WSI_TOKEN_HOST = 3,
  WSI_TOKEN_CONNECTION = 4, WSI_TOKEN_UPGRADE = 5, WSI_TOKEN_ORIGIN = 6, WSI_TOKEN_DRAFT = 7,
  WSI_TOKEN_CHALLENGE = 8, WSI_TOKEN_EXTENSIONS = 9, WSI_TOKEN_KEY1 = 10, WSI_TOKEN_KEY2 = 11,
  WSI_TOKEN_PROTOCOL = 12, WSI_TOKEN_ACCEPT = 13, WSI_TOKEN_NONCE = 14, WSI_TOKEN_HTTP = 15,
  WSI_TOKEN_HTTP2_SETTINGS = 16, WSI_TOKEN_HTTP_ACCEPT = 17, WSI_TOKEN_HTTP_AC_REQUEST_HEADERS = 18, WSI_TOKEN_HTTP_IF_MODIFIED_SINCE = 19,
  WSI_TOKEN_HTTP_IF_NONE_MATCH = 20, WSI_TOKEN_HTTP_ACCEPT_ENCODING = 21, WSI_TOKEN_HTTP_ACCEPT_LANGUAGE = 22, WSI_TOKEN_HTTP_PRAGMA = 23,
  WSI_TOKEN_HTTP_CACHE_CONTROL = 24, WSI_TOKEN_HTTP_AUTHORIZATION = 25, WSI_TOKEN_HTTP_COOKIE = 26, WSI_TOKEN_HTTP_CONTENT_LENGTH = 27,
  WSI_TOKEN_HTTP_CONTENT_TYPE = 28, WSI_TOKEN_HTTP_DATE = 29, WSI_TOKEN_HTTP_RANGE = 30, WSI_TOKEN_HTTP_REFERER = 31,
  WSI_TOKEN_KEY = 32, WSI_TOKEN_VERSION = 33, WSI_TOKEN_SWORIGIN = 34, WSI_TOKEN_HTTP_COLON_AUTHORITY = 35,
  WSI_TOKEN_HTTP_COLON_METHOD = 36, WSI_TOKEN_HTTP_COLON_PATH = 37, WSI_TOKEN_HTTP_COLON_SCHEME = 38, WSI_TOKEN_HTTP_COLON_STATUS = 39,
  WSI_TOKEN_HTTP_ACCEPT_CHARSET = 40, WSI_TOKEN_HTTP_ACCEPT_RANGES = 41, WSI_TOKEN_HTTP_ACCESS_CONTROL_ALLOW_ORIGIN = 42, WSI_TOKEN_HTTP_AGE = 43,
  WSI_TOKEN_HTTP_ALLOW = 44, WSI_TOKEN_HTTP_CONTENT_DISPOSITION = 45, WSI_TOKEN_HTTP_CONTENT_ENCODING = 46, WSI_TOKEN_HTTP_CONTENT_LANGUAGE = 47,
  WSI_TOKEN_HTTP_CONTENT_LOCATION = 48, WSI_TOKEN_HTTP_CONTENT_RANGE = 49, WSI_TOKEN_HTTP_ETAG = 50, WSI_TOKEN_HTTP_EXPECT = 51,
  WSI_TOKEN_HTTP_EXPIRES = 52, WSI_TOKEN_HTTP_FROM = 53, WSI_TOKEN_HTTP_IF_MATCH = 54, WSI_TOKEN_HTTP_IF_RANGE = 55,
  WSI_TOKEN_HTTP_IF_UNMODIFIED_SINCE = 56, WSI_TOKEN_HTTP_LAST_MODIFIED = 57, WSI_TOKEN_HTTP_LINK = 58, WSI_TOKEN_HTTP_LOCATION = 59,
  WSI_TOKEN_HTTP_MAX_FORWARDS = 60, WSI_TOKEN_HTTP_PROXY_AUTHENTICATE = 61, WSI_TOKEN_HTTP_PROXY_AUTHORIZATION = 62, WSI_TOKEN_HTTP_REFRESH = 63,
  WSI_TOKEN_HTTP_RETRY_AFTER = 64, WSI_TOKEN_HTTP_SERVER = 65, WSI_TOKEN_HTTP_SET_COOKIE = 66, WSI_TOKEN_HTTP_STRICT_TRANSPORT_SECURITY = 67,
  WSI_TOKEN_HTTP_TRANSFER_ENCODING = 68, WSI_TOKEN_HTTP_USER_AGENT = 69, WSI_TOKEN_HTTP_VARY = 70, WSI_TOKEN_HTTP_VIA = 71,
  WSI_TOKEN_HTTP_WWW_AUTHENTICATE = 72, WSI_TOKEN_PATCH_URI = 73, WSI_TOKEN_PUT_URI = 74, WSI_TOKEN_DELETE_URI = 75,
  WSI_TOKEN_HTTP_URI_ARGS = 76, WSI_TOKEN_PROXY = 77, WSI_TOKEN_HTTP_X_REAL_IP = 78, WSI_TOKEN_HTTP1_0 = 79,
  WSI_TOKEN_X_FORWARDED_FOR = 80, WSI_TOKEN_CONNECT = 81, WSI_TOKEN_HEAD_URI = 82, WSI_TOKEN_TE = 83,
  WSI_TOKEN_REPLAY_NONCE = 84, WSI_TOKEN_COLON_PROTOCOL = 85, WSI_TOKEN_X_AUTH_TOKEN = 86, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
  _WSI_TOKEN_CLIENT_PEER_ADDRESS, _WSI_TOKEN_CLIENT_URI, _WSI_TOKEN_CLIENT_HOST, _WSI_TOKEN_CLIENT_ORIGIN,
  _WSI_TOKEN_CLIENT_METHOD, _WSI_TOKEN_CLIENT_IFACE, _WSI_TOKEN_CLIENT_ALPN, WSI_TOKEN_COUNT,
  WSI_TOKEN_NAME_PART, WSI_TOKEN_SKIPPING, WSI_TOKEN_SKIPPING_SAW_CR, WSI_PARSING_COMPLETE,
  WSI_INIT_TOKEN_MUXURL
}
 

Functions

LWS_VISIBLE const LWS_EXTERN 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 const LWS_EXTERN char * lws_get_urlarg_by_name (struct lws *wsi, const char *name, char *buf, int len)
 

Detailed Description

HTTP header releated functions

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().

Function Documentation

◆ lws_get_urlarg_by_name()

LWS_VISIBLE const LWS_EXTERN 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

Parameters
wsithe connection to check
namethe arg name, like "token="
bufthe buffer to receive the urlarg (including the name= part)
lenthe 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_hdr_copy()

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.

Parameters
wsiwebsocket connection
destdestination buffer
lenlength of destination buffer
hwhich 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_hdr_copy_fragment()

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.

Parameters
wsiwebsocket connection
destdestination buffer
lenlength of destination buffer
hwhich header index we are interested in
frag_idxwhich 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_hdr_fragment_length()

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'

Parameters
wsiwebsocket connection
hwhich header index we are interested in
frag_idxwhich fragment of h we want to get the length of

◆ lws_hdr_total_length()

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'

Parameters
wsiwebsocket connection
hwhich header index we are interested in

◆ lws_token_to_string()

LWS_VISIBLE const LWS_EXTERN 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

Parameters
tokentoken index