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 , WSI_TOKEN_POST_URI , WSI_TOKEN_HOST , WSI_TOKEN_CONNECTION ,
  WSI_TOKEN_UPGRADE , WSI_TOKEN_ORIGIN , WSI_TOKEN_CHALLENGE , WSI_TOKEN_HTTP ,
  WSI_TOKEN_HTTP_ACCEPT , WSI_TOKEN_HTTP_IF_MODIFIED_SINCE , WSI_TOKEN_HTTP_IF_NONE_MATCH , WSI_TOKEN_HTTP_ACCEPT_ENCODING ,
  WSI_TOKEN_HTTP_ACCEPT_LANGUAGE , WSI_TOKEN_HTTP_PRAGMA , WSI_TOKEN_HTTP_CACHE_CONTROL , WSI_TOKEN_HTTP_AUTHORIZATION ,
  WSI_TOKEN_HTTP_COOKIE , WSI_TOKEN_HTTP_CONTENT_LENGTH , WSI_TOKEN_HTTP_CONTENT_TYPE , WSI_TOKEN_HTTP_DATE ,
  WSI_TOKEN_HTTP_RANGE , WSI_TOKEN_HTTP_ACCEPT_RANGES , WSI_TOKEN_HTTP_AGE , WSI_TOKEN_HTTP_ALLOW ,
  WSI_TOKEN_HTTP_CONTENT_DISPOSITION , WSI_TOKEN_HTTP_CONTENT_ENCODING , WSI_TOKEN_HTTP_CONTENT_LANGUAGE , WSI_TOKEN_HTTP_CONTENT_LOCATION ,
  WSI_TOKEN_HTTP_CONTENT_RANGE , WSI_TOKEN_HTTP_ETAG , WSI_TOKEN_HTTP_EXPECT , WSI_TOKEN_HTTP_EXPIRES ,
  WSI_TOKEN_HTTP_FROM , WSI_TOKEN_HTTP_IF_MATCH , WSI_TOKEN_HTTP_IF_RANGE , WSI_TOKEN_HTTP_IF_UNMODIFIED_SINCE ,
  WSI_TOKEN_HTTP_LAST_MODIFIED , WSI_TOKEN_HTTP_LINK , WSI_TOKEN_HTTP_LOCATION , WSI_TOKEN_HTTP_REFRESH ,
  WSI_TOKEN_HTTP_RETRY_AFTER , WSI_TOKEN_HTTP_SERVER , WSI_TOKEN_HTTP_SET_COOKIE , WSI_TOKEN_HTTP_TRANSFER_ENCODING ,
  WSI_TOKEN_HTTP_URI_ARGS , WSI_TOKEN_HTTP1_0 , WSI_TOKEN_X_FORWARDED_FOR , WSI_TOKEN_CONNECT ,
  WSI_TOKEN_HEAD_URI , WSI_TOKEN_X_AUTH_TOKEN , WSI_TOKEN_DSS_SIGNATURE , _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
}
 
enum  lws_h2_settings {
  H2SET_HEADER_TABLE_SIZE = 1 , H2SET_ENABLE_PUSH , H2SET_MAX_CONCURRENT_STREAMS , H2SET_INITIAL_WINDOW_SIZE ,
  H2SET_MAX_FRAME_SIZE , H2SET_MAX_HEADER_LIST_SIZE , H2SET_RESERVED7 , H2SET_ENABLE_CONNECT_PROTOCOL ,
  H2SET_COUNT
}
 

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)
 

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 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

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

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

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

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

Parameters
wsiwebsocket connection
dstpointer to buffer to receive the copy
lennumber of bytes available at dst
nameheader string (including terminating :)
nlenlength 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_hdr_custom_length()

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

Parameters
wsiwebsocket connection
nameheader string (including terminating :)
nlenlength 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_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 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

Parameters
tokentoken index