libwebsockets
Lightweight C library for HTML5 websockets
lws-http.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 /* minimal space for typical headers and CSP stuff */
26 
27 #define LWS_RECOMMENDED_MIN_HEADER_SPACE 2048
28 
34 
41 
53 LWS_VISIBLE LWS_EXTERN const char *
54 lws_get_mimetype(const char *file, const struct lws_http_mount *m);
55 
74 LWS_VISIBLE LWS_EXTERN int
75 lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
76  const char *other_headers, int other_headers_len);
77 
78 LWS_VISIBLE LWS_EXTERN int
79 lws_serve_http_file_fragment(struct lws *wsi);
81 
82 
83 enum http_status {
84  HTTP_STATUS_CONTINUE = 100,
85 
86  HTTP_STATUS_OK = 200,
87  HTTP_STATUS_NO_CONTENT = 204,
88  HTTP_STATUS_PARTIAL_CONTENT = 206,
89 
90  HTTP_STATUS_MOVED_PERMANENTLY = 301,
91  HTTP_STATUS_FOUND = 302,
92  HTTP_STATUS_SEE_OTHER = 303,
93  HTTP_STATUS_NOT_MODIFIED = 304,
94 
95  HTTP_STATUS_BAD_REQUEST = 400,
96  HTTP_STATUS_UNAUTHORIZED,
97  HTTP_STATUS_PAYMENT_REQUIRED,
98  HTTP_STATUS_FORBIDDEN,
99  HTTP_STATUS_NOT_FOUND,
100  HTTP_STATUS_METHOD_NOT_ALLOWED,
101  HTTP_STATUS_NOT_ACCEPTABLE,
102  HTTP_STATUS_PROXY_AUTH_REQUIRED,
103  HTTP_STATUS_REQUEST_TIMEOUT,
104  HTTP_STATUS_CONFLICT,
105  HTTP_STATUS_GONE,
106  HTTP_STATUS_LENGTH_REQUIRED,
107  HTTP_STATUS_PRECONDITION_FAILED,
108  HTTP_STATUS_REQ_ENTITY_TOO_LARGE,
109  HTTP_STATUS_REQ_URI_TOO_LONG,
110  HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE,
111  HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE,
112  HTTP_STATUS_EXPECTATION_FAILED,
113 
114  HTTP_STATUS_INTERNAL_SERVER_ERROR = 500,
115  HTTP_STATUS_NOT_IMPLEMENTED,
116  HTTP_STATUS_BAD_GATEWAY,
117  HTTP_STATUS_SERVICE_UNAVAILABLE,
118  HTTP_STATUS_GATEWAY_TIMEOUT,
119  HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED,
120 };
131 
133  char *p;
134  int len;
135  int max_len;
136  int final;
137  int chunked;
139 };
140 
141 typedef const char *(*lws_process_html_state_cb)(void *data, int index);
142 
144  char *start;
145  char swallow[16];
146  int pos;
147  void *data;
148  const char * const *vars;
151  lws_process_html_state_cb replace;
153 };
154 
159 LWS_VISIBLE LWS_EXTERN int
161  struct lws_process_html_state *s);
163 
196 
204 struct lws_tokens {
205  unsigned char *token;
206  int len;
207 };
208 
209 /* enum lws_token_indexes
210  * these have to be kept in sync with lextable.h / minilex.c
211  *
212  * NOTE: These public enums are part of the abi. If you want to add one,
213  * add it at where specified so existing users are unaffected.
214  */
215 enum lws_token_indexes {
216  WSI_TOKEN_GET_URI = 0,
217  WSI_TOKEN_POST_URI = 1,
218  WSI_TOKEN_OPTIONS_URI = 2,
219  WSI_TOKEN_HOST = 3,
220  WSI_TOKEN_CONNECTION = 4,
221  WSI_TOKEN_UPGRADE = 5,
222  WSI_TOKEN_ORIGIN = 6,
223  WSI_TOKEN_DRAFT = 7,
224  WSI_TOKEN_CHALLENGE = 8,
225  WSI_TOKEN_EXTENSIONS = 9,
226  WSI_TOKEN_KEY1 = 10,
227  WSI_TOKEN_KEY2 = 11,
228  WSI_TOKEN_PROTOCOL = 12,
229  WSI_TOKEN_ACCEPT = 13,
230  WSI_TOKEN_NONCE = 14,
231  WSI_TOKEN_HTTP = 15,
232  WSI_TOKEN_HTTP2_SETTINGS = 16,
233  WSI_TOKEN_HTTP_ACCEPT = 17,
234  WSI_TOKEN_HTTP_AC_REQUEST_HEADERS = 18,
235  WSI_TOKEN_HTTP_IF_MODIFIED_SINCE = 19,
236  WSI_TOKEN_HTTP_IF_NONE_MATCH = 20,
237  WSI_TOKEN_HTTP_ACCEPT_ENCODING = 21,
238  WSI_TOKEN_HTTP_ACCEPT_LANGUAGE = 22,
239  WSI_TOKEN_HTTP_PRAGMA = 23,
240  WSI_TOKEN_HTTP_CACHE_CONTROL = 24,
241  WSI_TOKEN_HTTP_AUTHORIZATION = 25,
242  WSI_TOKEN_HTTP_COOKIE = 26,
243  WSI_TOKEN_HTTP_CONTENT_LENGTH = 27,
244  WSI_TOKEN_HTTP_CONTENT_TYPE = 28,
245  WSI_TOKEN_HTTP_DATE = 29,
246  WSI_TOKEN_HTTP_RANGE = 30,
247  WSI_TOKEN_HTTP_REFERER = 31,
248  WSI_TOKEN_KEY = 32,
249  WSI_TOKEN_VERSION = 33,
250  WSI_TOKEN_SWORIGIN = 34,
251 
252  WSI_TOKEN_HTTP_COLON_AUTHORITY = 35,
253  WSI_TOKEN_HTTP_COLON_METHOD = 36,
254  WSI_TOKEN_HTTP_COLON_PATH = 37,
255  WSI_TOKEN_HTTP_COLON_SCHEME = 38,
256  WSI_TOKEN_HTTP_COLON_STATUS = 39,
257 
258  WSI_TOKEN_HTTP_ACCEPT_CHARSET = 40,
259  WSI_TOKEN_HTTP_ACCEPT_RANGES = 41,
260  WSI_TOKEN_HTTP_ACCESS_CONTROL_ALLOW_ORIGIN = 42,
261  WSI_TOKEN_HTTP_AGE = 43,
262  WSI_TOKEN_HTTP_ALLOW = 44,
263  WSI_TOKEN_HTTP_CONTENT_DISPOSITION = 45,
264  WSI_TOKEN_HTTP_CONTENT_ENCODING = 46,
265  WSI_TOKEN_HTTP_CONTENT_LANGUAGE = 47,
266  WSI_TOKEN_HTTP_CONTENT_LOCATION = 48,
267  WSI_TOKEN_HTTP_CONTENT_RANGE = 49,
268  WSI_TOKEN_HTTP_ETAG = 50,
269  WSI_TOKEN_HTTP_EXPECT = 51,
270  WSI_TOKEN_HTTP_EXPIRES = 52,
271  WSI_TOKEN_HTTP_FROM = 53,
272  WSI_TOKEN_HTTP_IF_MATCH = 54,
273  WSI_TOKEN_HTTP_IF_RANGE = 55,
274  WSI_TOKEN_HTTP_IF_UNMODIFIED_SINCE = 56,
275  WSI_TOKEN_HTTP_LAST_MODIFIED = 57,
276  WSI_TOKEN_HTTP_LINK = 58,
277  WSI_TOKEN_HTTP_LOCATION = 59,
278  WSI_TOKEN_HTTP_MAX_FORWARDS = 60,
279  WSI_TOKEN_HTTP_PROXY_AUTHENTICATE = 61,
280  WSI_TOKEN_HTTP_PROXY_AUTHORIZATION = 62,
281  WSI_TOKEN_HTTP_REFRESH = 63,
282  WSI_TOKEN_HTTP_RETRY_AFTER = 64,
283  WSI_TOKEN_HTTP_SERVER = 65,
284  WSI_TOKEN_HTTP_SET_COOKIE = 66,
285  WSI_TOKEN_HTTP_STRICT_TRANSPORT_SECURITY = 67,
286  WSI_TOKEN_HTTP_TRANSFER_ENCODING = 68,
287  WSI_TOKEN_HTTP_USER_AGENT = 69,
288  WSI_TOKEN_HTTP_VARY = 70,
289  WSI_TOKEN_HTTP_VIA = 71,
290  WSI_TOKEN_HTTP_WWW_AUTHENTICATE = 72,
291 
292  WSI_TOKEN_PATCH_URI = 73,
293  WSI_TOKEN_PUT_URI = 74,
294  WSI_TOKEN_DELETE_URI = 75,
295 
296  WSI_TOKEN_HTTP_URI_ARGS = 76,
297  WSI_TOKEN_PROXY = 77,
298  WSI_TOKEN_HTTP_X_REAL_IP = 78,
299  WSI_TOKEN_HTTP1_0 = 79,
300  WSI_TOKEN_X_FORWARDED_FOR = 80,
301  WSI_TOKEN_CONNECT = 81,
302  WSI_TOKEN_HEAD_URI = 82,
303  WSI_TOKEN_TE = 83,
304  WSI_TOKEN_REPLAY_NONCE = 84,
305  WSI_TOKEN_COLON_PROTOCOL = 85,
306  WSI_TOKEN_X_AUTH_TOKEN = 86,
307 
308  /****** add new things just above ---^ ******/
309 
310  /* use token storage to stash these internally, not for
311  * user use */
312 
313  _WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
314  _WSI_TOKEN_CLIENT_PEER_ADDRESS,
315  _WSI_TOKEN_CLIENT_URI,
316  _WSI_TOKEN_CLIENT_HOST,
317  _WSI_TOKEN_CLIENT_ORIGIN,
318  _WSI_TOKEN_CLIENT_METHOD,
319  _WSI_TOKEN_CLIENT_IFACE,
320  _WSI_TOKEN_CLIENT_ALPN,
321 
322  /* always last real token index*/
323  WSI_TOKEN_COUNT,
324 
325  /* parser state additions, no storage associated */
326  WSI_TOKEN_NAME_PART,
327 #if defined(LWS_WITH_CUSTOM_HEADERS)
328  WSI_TOKEN_UNKNOWN_VALUE_PART,
329 #endif
330  WSI_TOKEN_SKIPPING,
331  WSI_TOKEN_SKIPPING_SAW_CR,
332  WSI_PARSING_COMPLETE,
333  WSI_INIT_TOKEN_MUXURL,
334 };
335 
337  unsigned short token_limit[WSI_TOKEN_COUNT];
338 };
339 
345 LWS_VISIBLE LWS_EXTERN const unsigned char *
346 lws_token_to_string(enum lws_token_indexes token);
347 
356 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
357 lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h);
358 
368 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
369 lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h,
370  int frag_idx);
371 
386 LWS_VISIBLE LWS_EXTERN int
387 lws_hdr_copy(struct lws *wsi, char *dest, int len, enum lws_token_indexes h);
388 
406 LWS_VISIBLE LWS_EXTERN int
407 lws_hdr_copy_fragment(struct lws *wsi, char *dest, int len,
408  enum lws_token_indexes h, int frag_idx);
409 
426 LWS_VISIBLE LWS_EXTERN int
427 lws_hdr_custom_length(struct lws *wsi, const char *name, int nlen);
428 
447 LWS_VISIBLE LWS_EXTERN int
448 lws_hdr_custom_copy(struct lws *wsi, char *dst, int len, const char *name,
449  int nlen);
450 
461 LWS_VISIBLE LWS_EXTERN const char *
462 lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int len);
464 
479 
481 #define LWSAHH_CODE_MASK ((1 << 16) - 1)
482 #define LWSAHH_FLAG_NO_SERVER_NAME (1 << 30)
483 
498 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
499 lws_add_http_header_status(struct lws *wsi,
500  unsigned int code, unsigned char **p,
501  unsigned char *end);
514 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
515 lws_add_http_header_by_name(struct lws *wsi, const unsigned char *name,
516  const unsigned char *value, int length,
517  unsigned char **p, unsigned char *end);
531 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
532 lws_add_http_header_by_token(struct lws *wsi, enum lws_token_indexes token,
533  const unsigned char *value, int length,
534  unsigned char **p, unsigned char *end);
545 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
546 lws_add_http_header_content_length(struct lws *wsi,
547  lws_filepos_t content_length,
548  unsigned char **p, unsigned char *end);
558 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
559 lws_finalize_http_header(struct lws *wsi, unsigned char **p,
560  unsigned char *end);
561 
573 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
574 lws_finalize_write_http_header(struct lws *wsi, unsigned char *start,
575  unsigned char **p, unsigned char *end);
576 
577 #define LWS_ILLEGAL_HTTP_CONTENT_LEN ((lws_filepos_t)-1ll)
578 
605 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
606 lws_add_http_common_headers(struct lws *wsi, unsigned int code,
607  const char *content_type, lws_filepos_t content_len,
608  unsigned char **p, unsigned char *end);
609 
633 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
634 lws_http_get_uri_and_method(struct lws *wsi, char **puri_ptr, int *puri_len);
635 
637 
648 
659 LWS_VISIBLE LWS_EXTERN const char *
660 lws_urlencode(char *escaped, const char *string, int len);
661 
662 /*
663  * URLDECODE 1 / 2
664  *
665  * This simple urldecode only operates until the first '\0' and requires the
666  * data to exist all at once
667  */
683 LWS_VISIBLE LWS_EXTERN int
684 lws_urldecode(char *string, const char *escaped, int len);
686 
696 LWS_VISIBLE LWS_EXTERN int
697 lws_return_http_status(struct lws *wsi, unsigned int code,
698  const char *html_body);
699 
712 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
713 lws_http_redirect(struct lws *wsi, int code, const unsigned char *loc, int len,
714  unsigned char **p, unsigned char *end);
715 
724 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
725 lws_http_transaction_completed(struct lws *wsi);
726 
734 LWS_VISIBLE LWS_EXTERN int
735 lws_http_headers_detach(struct lws *wsi);
736 
745 LWS_VISIBLE LWS_EXTERN int
746 lws_http_mark_sse(struct lws *wsi);
747 
773 LWS_VISIBLE int
774 lws_http_compression_apply(struct lws *wsi, const char *name,
775  unsigned char **p, unsigned char *end, char decomp);
777 
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_urldecode(char *string, const char *escaped, int len)
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h)
int chunked
Definition: lws-http.h:137
LWS_VISIBLE LWS_EXTERN int lws_http_headers_detach(struct lws *wsi)
LWS_VISIBLE LWS_EXTERN const char * lws_get_mimetype(const char *file, const struct lws_http_mount *m)
char * start
Definition: lws-http.h:144
int len
Definition: lws-http.h:134
const char *const * vars
Definition: lws-http.h:148
Definition: lws-context-vhost.h:1019
int len
Definition: lws-http.h:206
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 const char * lws_urlencode(char *escaped, const char *string, int len)
LWS_VISIBLE int lws_http_compression_apply(struct lws *wsi, const char *name, unsigned char **p, unsigned char *end, char decomp)
int pos
Definition: lws-http.h:146
char swallow[16]
Definition: lws-http.h:145
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_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx)
Definition: lws-http.h:132
unsigned short token_limit[WSI_TOKEN_COUNT]
Definition: lws-http.h:337
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)
unsigned char * token
Definition: lws-http.h:205
LWS_VISIBLE LWS_EXTERN const unsigned char * lws_token_to_string(enum lws_token_indexes token)
LWS_VISIBLE LWS_EXTERN int lws_chunked_html_process(struct lws_process_html_args *args, struct lws_process_html_state *s)
char * p
Definition: lws-http.h:133
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_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_hdr_custom_length(struct lws *wsi, const char *name, int nlen)
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)
Definition: lws-http.h:336
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_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_hdr_copy(struct lws *wsi, char *dest, int len, enum lws_token_indexes h)
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_transaction_completed(struct lws *wsi)
int count_vars
Definition: lws-http.h:149
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)
Definition: lws-http.h:204
LWS_VISIBLE LWS_EXTERN int lws_http_mark_sse(struct lws *wsi)
void * data
Definition: lws-http.h:147
lws_process_html_state_cb replace
Definition: lws-http.h:151
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_http_get_uri_and_method(struct lws *wsi, char **puri_ptr, int *puri_len)
int max_len
Definition: lws-http.h:135
Definition: lws-http.h:143