libwebsockets
Lightweight C library for HTML5 websockets
|
Go to the source code of this file.
Data Structures | |
struct | lws_process_html_args |
struct | lws_process_html_state |
struct | lws_tokens |
struct | lws_token_limits |
Macros | |
#define | LWS_RECOMMENDED_MIN_HEADER_SPACE 2048 |
#define | LWSAHH_CODE_MASK ((1 << 16) - 1) |
#define | LWSAHH_FLAG_NO_SERVER_NAME (1 << 30) |
#define | LWS_ILLEGAL_HTTP_CONTENT_LEN ((lws_filepos_t)-1ll) |
#define | lws_http_client_http_resp_is_error(code) (!(code < 400)) |
#define | LWS_H2_STREAM_SID -1 |
Typedefs | |
typedef const char *(* | lws_process_html_state_cb) (void *data, int index) |
typedef void(* | lws_hdr_custom_fe_cb_t) (const char *name, int nlen, void *opaque) |
Functions | |
LWS_VISIBLE LWS_EXTERN const char * | lws_get_mimetype (const char *file, const struct lws_http_mount *m) |
LWS_VISIBLE LWS_EXTERN int | lws_serve_http_file (struct lws *wsi, const char *file, const char *content_type, const char *other_headers, int other_headers_len) |
LWS_VISIBLE LWS_EXTERN int | lws_serve_http_file_fragment (struct lws *wsi) |
LWS_VISIBLE LWS_EXTERN int | lws_chunked_html_process (struct lws_process_html_args *args, struct lws_process_html_state *s) |
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_hdr_custom_name_foreach (struct lws *wsi, lws_hdr_custom_fe_cb_t cb, void *opaque) |
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) |
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT | lws_add_http_header_status (struct lws *wsi, unsigned int code, unsigned char **p, unsigned char *end) |
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT | lws_add_http_header_by_name (struct lws *wsi, const unsigned char *name, const unsigned char *value, int length, unsigned char **p, unsigned char *end) |
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT | lws_add_http_header_by_token (struct lws *wsi, enum lws_token_indexes token, const unsigned char *value, int length, unsigned char **p, unsigned char *end) |
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT | lws_add_http_header_content_length (struct lws *wsi, lws_filepos_t content_length, unsigned char **p, unsigned char *end) |
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT | lws_finalize_http_header (struct lws *wsi, unsigned char **p, unsigned char *end) |
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT | lws_finalize_write_http_header (struct lws *wsi, unsigned char *start, unsigned char **p, unsigned char *end) |
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT | lws_add_http_common_headers (struct lws *wsi, unsigned int code, const char *content_type, lws_filepos_t content_len, unsigned char **p, unsigned char *end) |
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT | lws_http_get_uri_and_method (struct lws *wsi, char **puri_ptr, int *puri_len) |
LWS_VISIBLE LWS_EXTERN const char * | lws_urlencode (char *escaped, const char *string, int len) |
LWS_VISIBLE LWS_EXTERN int | lws_urldecode (char *string, const char *escaped, int len) |
LWS_VISIBLE LWS_EXTERN int | lws_http_date_render_from_unix (char *buf, size_t len, const time_t *t) |
LWS_VISIBLE LWS_EXTERN int | lws_http_date_parse_unix (const char *b, size_t len, time_t *t) |
LWS_VISIBLE LWS_EXTERN int | lws_http_check_retry_after (struct lws *wsi, lws_usec_t *us_interval_in_out) |
LWS_VISIBLE LWS_EXTERN int | lws_return_http_status (struct lws *wsi, unsigned int code, const char *html_body) |
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT | lws_http_redirect (struct lws *wsi, int code, const unsigned char *loc, int len, unsigned char **p, unsigned char *end) |
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT | lws_http_transaction_completed (struct lws *wsi) |
LWS_VISIBLE LWS_EXTERN int | lws_http_headers_detach (struct lws *wsi) |
LWS_VISIBLE LWS_EXTERN int | lws_http_mark_sse (struct lws *wsi) |
LWS_VISIBLE LWS_EXTERN int | lws_h2_client_stream_long_poll_rxonly (struct lws *wsi) |
LWS_VISIBLE LWS_EXTERN int | lws_http_compression_apply (struct lws *wsi, const char *name, unsigned char **p, unsigned char *end, char decomp) |
LWS_VISIBLE LWS_EXTERN int | lws_http_is_redirected_to_get (struct lws *wsi) |
LWS_VISIBLE LWS_EXTERN int | lws_http_cookie_get (struct lws *wsi, const char *name, char *buf, size_t *max) |
LWS_VISIBLE LWS_EXTERN int | lws_h2_update_peer_txcredit (struct lws *wsi, unsigned int sid, int bump) |
LWS_VISIBLE LWS_EXTERN int | lws_h2_get_peer_txcredit_estimate (struct lws *wsi) |
struct lws_process_html_args |
Definition at line 132 of file lws-http.h.
struct lws_process_html_state |
Definition at line 143 of file lws-http.h.
Data Fields | ||
---|---|---|
char * | start |
pointer to start of match |
char | swallow[16] |
matched character buffer |
int | pos |
position in match |
void * | data |
opaque pointer |
const char *const * | vars |
list of variable names |
int | count_vars |
count of variable names |
lws_process_html_state_cb | replace |
called on match to perform substitution |
#define LWS_RECOMMENDED_MIN_HEADER_SPACE 2048 |
Definition at line 27 of file lws-http.h.
#define lws_http_client_http_resp_is_error | ( | code | ) | (!(code < 400)) |
lws_http_client_http_error() - determine if the response code indicates an error
code | the response code to test |
Returns nonzero if the code indicates an error, else zero if reflects a non-error condition
Definition at line 982 of file lws-http.h.
#define LWS_H2_STREAM_SID -1 |
lws_h2_update_peer_txcredit() - manually update stream peer tx credit
wsi | the h2 child stream whose peer credit to change |
sid | the stream ID, or LWS_H2_STREAM_SID for the wsi stream ID |
bump | signed change to confer upon peer tx credit for sid |
In conjunction with LCCSCF_H2_MANUAL_RXFLOW flag, allows the user code to selectively starve the remote peer of the ability to send us data on a client connection.
Normally lws sends an initial window size for the peer to send to it of 0, but during the header phase it sends a WINDOW_UPDATE to increase the amount available. LCCSCF_H2_MANUAL_RXFLOW restricts this initial increase in tx credit for the stream, before it has been asked to send us anything, to the amount specified in the client info .manual_initial_tx_credit member, and this api can be called to send the other side permission to send us up to bump
additional bytes.
The nwsi tx credit is updated automatically for exactly what was sent to us on a stream with LCCSCF_H2_MANUAL_RXFLOW flag, but the stream's own tx credit must be handled manually by user code via this api.
Returns 0 for success or nonzero for failure.
Definition at line 1009 of file lws-http.h.
typedef const char*(* lws_process_html_state_cb) (void *data, int index) |
Definition at line 141 of file lws-http.h.
enum http_status |
Definition at line 83 of file lws-http.h.
LWS_VISIBLE LWS_EXTERN const char* lws_get_mimetype | ( | const char * | file, |
const struct lws_http_mount * | m | ||
) |
lws_get_mimetype() - Determine mimetype to use from filename
file | filename |
m | NULL, or mount context |
This uses a canned list of known filetypes first, if no match and m is non-NULL, then tries a list of per-mount file suffix to mimtype mappings.
Returns either NULL or a pointer to the mimetype matching the file.
LWS_VISIBLE LWS_EXTERN int lws_serve_http_file | ( | struct lws * | wsi, |
const char * | file, | ||
const char * | content_type, | ||
const char * | other_headers, | ||
int | other_headers_len | ||
) |
lws_serve_http_file() - Send a file back to the client using http
wsi | Websocket instance (available from user callback) |
file | The file to issue over http |
content_type | The http content type, eg, text/html |
other_headers | NULL or pointer to header string |
other_headers_len | length of the other headers if non-NULL This function is intended to be called from the callback in response to http requests from the client. It allows the callback to issue local files down the http link in a single step. Returning <0 indicates error and the wsi should be closed. Returning >0 indicates the file was completely sent and lws_http_transaction_completed() called on the wsi (and close if != 0) ==0 indicates the file transfer is started and needs more service later, the wsi should be left alone. |
LWS_VISIBLE LWS_EXTERN int lws_serve_http_file_fragment | ( | struct lws * | wsi | ) |
LWS_VISIBLE LWS_EXTERN int lws_chunked_html_process | ( | struct lws_process_html_args * | args, |
struct lws_process_html_state * | s | ||
) |
lws_chunked_html_process() - generic chunked substitution
args | buffer to process using chunked encoding |
s | current processing state |
LWS_VISIBLE LWS_EXTERN const char* lws_urlencode | ( | char * | escaped, |
const char * | string, | ||
int | len | ||
) |
lws_urlencode() - like strncpy but with urlencoding
escaped | output buffer |
string | input buffer ('/0' terminated) |
len | output buffer max length |
Because urlencoding expands the output string, it's not possible to do it in-place, ie, with escaped == string
LWS_VISIBLE LWS_EXTERN int lws_urldecode | ( | char * | string, |
const char * | escaped, | ||
int | len | ||
) |
lws_urldecode() - like strncpy but with urldecoding
string | output buffer |
escaped | input buffer ('\0' terminated) |
len | output buffer max length |
This is only useful for '\0' terminated strings
Since urldecoding only shrinks the output string, it is possible to do it in-place, ie, string == escaped
Returns 0 if completed OK or nonzero for urldecode violation (non-hex chars where hex required, etc)
LWS_VISIBLE LWS_EXTERN int lws_http_date_render_from_unix | ( | char * | buf, |
size_t | len, | ||
const time_t * | t | ||
) |
lws_http_date_render_from_unix() - render unixtime as RFC7231 date string
buf | Destination string buffer |
len | avilable length of dest string buffer in bytes |
t | pointer to the time_t to render |
Returns 0 if time_t is rendered into the string buffer successfully, else nonzero.
LWS_VISIBLE LWS_EXTERN int lws_http_date_parse_unix | ( | const char * | b, |
size_t | len, | ||
time_t * | t | ||
) |
lws_http_date_parse_unix() - parse a RFC7231 date string into unixtime
b | Source string buffer |
len | avilable length of source string buffer in bytes |
t | pointer to the destination time_t to set |
Returns 0 if string buffer parsed as RFC7231 time successfully, and *t set to the parsed unixtime, else return nonzero.
LWS_VISIBLE LWS_EXTERN int lws_http_check_retry_after | ( | struct lws * | wsi, |
lws_usec_t * | us_interval_in_out | ||
) |
lws_http_check_retry_after() - increase a timeout if retry-after present
wsi | http stream this relates to |
us_interval_in_out | default us retry interval on entry may be updated |
This function may extend the incoming retry interval if the server has requested that using retry-after: header. It won't reduce the incoming retry interval, only leave it alone or increase it.
*us_interval_in_out should be set to a default retry interval on entry, if the wsi has a retry-after time or interval that resolves to an interval longer than the entry *us_interval_in_out, that will be updated to the longer interval and return 0.
If no usable retry-after or the time is now or in the past, *us_interval_in_out is left alone and the function returns nonzero.
LWS_VISIBLE LWS_EXTERN int lws_return_http_status | ( | struct lws * | wsi, |
unsigned int | code, | ||
const char * | html_body | ||
) |
lws_return_http_status() - Return simple http status
wsi | Websocket instance (available from user callback) |
code | Status index, eg, 404 |
html_body | User-readable HTML description < 1KB, or NULL Helper to report HTTP errors back to the client cleanly and consistently |
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_http_redirect | ( | struct lws * | wsi, |
int | code, | ||
const unsigned char * | loc, | ||
int | len, | ||
unsigned char ** | p, | ||
unsigned char * | end | ||
) |
lws_http_redirect() - write http redirect out on wsi
wsi | websocket connection |
code | HTTP response code (eg, 301) |
loc | where to redirect to |
len | length of loc |
p | pointer current position in buffer (updated as we write) |
end | pointer to end of buffer |
Returns amount written, or < 0 indicating fatal write failure.
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_http_transaction_completed | ( | struct lws * | wsi | ) |
lws_http_transaction_completed() - wait for new http transaction or close
wsi | websocket connection Returns nonzero if the HTTP connection must close now Returns 0 and resets connection to wait for new HTTP header / transaction if possible |
LWS_VISIBLE LWS_EXTERN int lws_http_headers_detach | ( | struct lws * | wsi | ) |
lws_http_headers_detach() - drop the associated headers storage and allow it to be reused by another connection
wsi | http connection |
If the wsi has an ah headers struct attached, detach it.
LWS_VISIBLE LWS_EXTERN int lws_http_mark_sse | ( | struct lws * | wsi | ) |
lws_http_mark_sse() - called to indicate this http stream is now doing SSE
wsi | http connection |
Cancel any timeout on the wsi, and for h2, mark the network connection as containing an immortal stream for the duration the SSE stream is open.
LWS_VISIBLE LWS_EXTERN int lws_h2_client_stream_long_poll_rxonly | ( | struct lws * | wsi | ) |
lws_h2_client_stream_long_poll_rxonly() - h2 stream to immortal read-only
wsi | h2 stream client wsi |
Send END_STREAM-flagged zero-length DATA frame to set client stream wsi into half-closed (local) and remote into half-closed (remote). Set the client stream wsi to be immortal (not subject to timeouts).
Used if the remote server supports immortal long poll to put the stream into a read-only state where it can wait as long as needed for rx.
Returns 0 if the process (which happens asynchronously) started or non-zero if it wasn't an h2 stream.
LWS_VISIBLE LWS_EXTERN int lws_http_compression_apply | ( | struct lws * | wsi, |
const char * | name, | ||
unsigned char ** | p, | ||
unsigned char * | end, | ||
char | decomp | ||
) |
lws_http_compression_apply() - apply an http compression transform
wsi | the wsi to apply the compression transform to |
name | NULL, or the name of the compression transform, eg, "deflate" |
p | pointer to pointer to headers buffer |
end | pointer to end of headers buffer |
decomp | 0 = add compressor to wsi, 1 = add decompressor |
This allows transparent compression of dynamically generated HTTP. The requested compression (eg, "deflate") is only applied if the client headers indicated it was supported (and it has support in lws), otherwise it's a NOP.
If the requested compression method is NULL, then the supported compression formats are tried, and for non-decompression (server) mode the first that's found on the client's accept-encoding header is chosen.
NOTE: the compression transform, same as h2 support, relies on the user code using LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal lws fileserving code already does this.
If the library was built without the cmake option LWS_WITH_HTTP_STREAM_COMPRESSION set, then a NOP is provided for this api, allowing user code to build either way and use compression if available.
LWS_VISIBLE LWS_EXTERN int lws_http_is_redirected_to_get | ( | struct lws * | wsi | ) |
lws_http_is_redirected_to_get() - true if redirected to GET
wsi | the wsi to check |
Check if the wsi is currently in GET mode, after, eg, doing a POST and receiving a 303.
LWS_VISIBLE LWS_EXTERN int lws_http_cookie_get | ( | struct lws * | wsi, |
const char * | name, | ||
char * | buf, | ||
size_t * | max | ||
) |
lws_http_cookie_get() - return copy of named cookie if present
wsi | the wsi to check |
name | name of the cookie |
buf | buffer to store the cookie contents into |
max_len | on entry, maximum length of buf... on exit, used len of buf |
If no cookie header, or no cookie of the requested name, or the value is larger than can fit in buf, returns nonzero.
If the cookie is found, copies its value into buf with a terminating NUL, sets *max_len to the used length, and returns 0.
This handles the parsing of the possibly multi-cookie header string and terminating the requested cookie at the next ; if present.
LWS_VISIBLE LWS_EXTERN int lws_h2_update_peer_txcredit | ( | struct lws * | wsi, |
unsigned int | sid, | ||
int | bump | ||
) |
LWS_VISIBLE LWS_EXTERN int lws_h2_get_peer_txcredit_estimate | ( | struct lws * | wsi | ) |
lws_h2_get_peer_txcredit_estimate() - return peer tx credit estimate
wsi | the h2 child stream whose peer credit estimate to return |
Returns the estimated amount of tx credit at the peer, in other words the number of bytes the peer is authorized to send to us.
It's an 'estimate' because we don't know how much is already in flight towards us and actually already used.