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-2018 Andy Green <andy@warmcat.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation:
9  * version 2.1 of the License.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  *
21  * included from libwebsockets.h
22  */
23 
24 /* minimal space for typical headers and CSP stuff */
25 
26 #define LWS_RECOMMENDED_MIN_HEADER_SPACE 2048
27 
33 
40 
52 LWS_VISIBLE LWS_EXTERN const char *
53 lws_get_mimetype(const char *file, const struct lws_http_mount *m);
54 
73 LWS_VISIBLE LWS_EXTERN int
74 lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
75  const char *other_headers, int other_headers_len);
76 
77 LWS_VISIBLE LWS_EXTERN int
78 lws_serve_http_file_fragment(struct lws *wsi);
80 
81 
82 enum http_status {
83  HTTP_STATUS_CONTINUE = 100,
84 
85  HTTP_STATUS_OK = 200,
86  HTTP_STATUS_NO_CONTENT = 204,
87  HTTP_STATUS_PARTIAL_CONTENT = 206,
88 
89  HTTP_STATUS_MOVED_PERMANENTLY = 301,
90  HTTP_STATUS_FOUND = 302,
91  HTTP_STATUS_SEE_OTHER = 303,
92  HTTP_STATUS_NOT_MODIFIED = 304,
93 
94  HTTP_STATUS_BAD_REQUEST = 400,
95  HTTP_STATUS_UNAUTHORIZED,
96  HTTP_STATUS_PAYMENT_REQUIRED,
97  HTTP_STATUS_FORBIDDEN,
98  HTTP_STATUS_NOT_FOUND,
99  HTTP_STATUS_METHOD_NOT_ALLOWED,
100  HTTP_STATUS_NOT_ACCEPTABLE,
101  HTTP_STATUS_PROXY_AUTH_REQUIRED,
102  HTTP_STATUS_REQUEST_TIMEOUT,
103  HTTP_STATUS_CONFLICT,
104  HTTP_STATUS_GONE,
105  HTTP_STATUS_LENGTH_REQUIRED,
106  HTTP_STATUS_PRECONDITION_FAILED,
107  HTTP_STATUS_REQ_ENTITY_TOO_LARGE,
108  HTTP_STATUS_REQ_URI_TOO_LONG,
109  HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE,
110  HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE,
111  HTTP_STATUS_EXPECTATION_FAILED,
112 
113  HTTP_STATUS_INTERNAL_SERVER_ERROR = 500,
114  HTTP_STATUS_NOT_IMPLEMENTED,
115  HTTP_STATUS_BAD_GATEWAY,
116  HTTP_STATUS_SERVICE_UNAVAILABLE,
117  HTTP_STATUS_GATEWAY_TIMEOUT,
118  HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED,
119 };
130 
132  char *p;
133  int len;
134  int max_len;
135  int final;
136  int chunked;
138 };
139 
140 typedef const char *(*lws_process_html_state_cb)(void *data, int index);
141 
143  char *start;
144  char swallow[16];
145  int pos;
146  void *data;
147  const char * const *vars;
150  lws_process_html_state_cb replace;
152 };
153 
158 LWS_VISIBLE LWS_EXTERN int
160  struct lws_process_html_state *s);
162 
195 
203 struct lws_tokens {
204  char *token;
205  int len;
206 };
207 
208 /* enum lws_token_indexes
209  * these have to be kept in sync with lextable.h / minilex.c
210  *
211  * NOTE: These public enums are part of the abi. If you want to add one,
212  * add it at where specified so existing users are unaffected.
213  */
214 enum lws_token_indexes {
215  WSI_TOKEN_GET_URI = 0,
216  WSI_TOKEN_POST_URI = 1,
217  WSI_TOKEN_OPTIONS_URI = 2,
218  WSI_TOKEN_HOST = 3,
219  WSI_TOKEN_CONNECTION = 4,
220  WSI_TOKEN_UPGRADE = 5,
221  WSI_TOKEN_ORIGIN = 6,
222  WSI_TOKEN_DRAFT = 7,
223  WSI_TOKEN_CHALLENGE = 8,
224  WSI_TOKEN_EXTENSIONS = 9,
225  WSI_TOKEN_KEY1 = 10,
226  WSI_TOKEN_KEY2 = 11,
227  WSI_TOKEN_PROTOCOL = 12,
228  WSI_TOKEN_ACCEPT = 13,
229  WSI_TOKEN_NONCE = 14,
230  WSI_TOKEN_HTTP = 15,
231  WSI_TOKEN_HTTP2_SETTINGS = 16,
232  WSI_TOKEN_HTTP_ACCEPT = 17,
233  WSI_TOKEN_HTTP_AC_REQUEST_HEADERS = 18,
234  WSI_TOKEN_HTTP_IF_MODIFIED_SINCE = 19,
235  WSI_TOKEN_HTTP_IF_NONE_MATCH = 20,
236  WSI_TOKEN_HTTP_ACCEPT_ENCODING = 21,
237  WSI_TOKEN_HTTP_ACCEPT_LANGUAGE = 22,
238  WSI_TOKEN_HTTP_PRAGMA = 23,
239  WSI_TOKEN_HTTP_CACHE_CONTROL = 24,
240  WSI_TOKEN_HTTP_AUTHORIZATION = 25,
241  WSI_TOKEN_HTTP_COOKIE = 26,
242  WSI_TOKEN_HTTP_CONTENT_LENGTH = 27,
243  WSI_TOKEN_HTTP_CONTENT_TYPE = 28,
244  WSI_TOKEN_HTTP_DATE = 29,
245  WSI_TOKEN_HTTP_RANGE = 30,
246  WSI_TOKEN_HTTP_REFERER = 31,
247  WSI_TOKEN_KEY = 32,
248  WSI_TOKEN_VERSION = 33,
249  WSI_TOKEN_SWORIGIN = 34,
250 
251  WSI_TOKEN_HTTP_COLON_AUTHORITY = 35,
252  WSI_TOKEN_HTTP_COLON_METHOD = 36,
253  WSI_TOKEN_HTTP_COLON_PATH = 37,
254  WSI_TOKEN_HTTP_COLON_SCHEME = 38,
255  WSI_TOKEN_HTTP_COLON_STATUS = 39,
256 
257  WSI_TOKEN_HTTP_ACCEPT_CHARSET = 40,
258  WSI_TOKEN_HTTP_ACCEPT_RANGES = 41,
259  WSI_TOKEN_HTTP_ACCESS_CONTROL_ALLOW_ORIGIN = 42,
260  WSI_TOKEN_HTTP_AGE = 43,
261  WSI_TOKEN_HTTP_ALLOW = 44,
262  WSI_TOKEN_HTTP_CONTENT_DISPOSITION = 45,
263  WSI_TOKEN_HTTP_CONTENT_ENCODING = 46,
264  WSI_TOKEN_HTTP_CONTENT_LANGUAGE = 47,
265  WSI_TOKEN_HTTP_CONTENT_LOCATION = 48,
266  WSI_TOKEN_HTTP_CONTENT_RANGE = 49,
267  WSI_TOKEN_HTTP_ETAG = 50,
268  WSI_TOKEN_HTTP_EXPECT = 51,
269  WSI_TOKEN_HTTP_EXPIRES = 52,
270  WSI_TOKEN_HTTP_FROM = 53,
271  WSI_TOKEN_HTTP_IF_MATCH = 54,
272  WSI_TOKEN_HTTP_IF_RANGE = 55,
273  WSI_TOKEN_HTTP_IF_UNMODIFIED_SINCE = 56,
274  WSI_TOKEN_HTTP_LAST_MODIFIED = 57,
275  WSI_TOKEN_HTTP_LINK = 58,
276  WSI_TOKEN_HTTP_LOCATION = 59,
277  WSI_TOKEN_HTTP_MAX_FORWARDS = 60,
278  WSI_TOKEN_HTTP_PROXY_AUTHENTICATE = 61,
279  WSI_TOKEN_HTTP_PROXY_AUTHORIZATION = 62,
280  WSI_TOKEN_HTTP_REFRESH = 63,
281  WSI_TOKEN_HTTP_RETRY_AFTER = 64,
282  WSI_TOKEN_HTTP_SERVER = 65,
283  WSI_TOKEN_HTTP_SET_COOKIE = 66,
284  WSI_TOKEN_HTTP_STRICT_TRANSPORT_SECURITY = 67,
285  WSI_TOKEN_HTTP_TRANSFER_ENCODING = 68,
286  WSI_TOKEN_HTTP_USER_AGENT = 69,
287  WSI_TOKEN_HTTP_VARY = 70,
288  WSI_TOKEN_HTTP_VIA = 71,
289  WSI_TOKEN_HTTP_WWW_AUTHENTICATE = 72,
290 
291  WSI_TOKEN_PATCH_URI = 73,
292  WSI_TOKEN_PUT_URI = 74,
293  WSI_TOKEN_DELETE_URI = 75,
294 
295  WSI_TOKEN_HTTP_URI_ARGS = 76,
296  WSI_TOKEN_PROXY = 77,
297  WSI_TOKEN_HTTP_X_REAL_IP = 78,
298  WSI_TOKEN_HTTP1_0 = 79,
299  WSI_TOKEN_X_FORWARDED_FOR = 80,
300  WSI_TOKEN_CONNECT = 81,
301  WSI_TOKEN_HEAD_URI = 82,
302  WSI_TOKEN_TE = 83,
303  WSI_TOKEN_REPLAY_NONCE = 84,
304  WSI_TOKEN_COLON_PROTOCOL = 85,
305  WSI_TOKEN_X_AUTH_TOKEN = 86,
306 
307  /****** add new things just above ---^ ******/
308 
309  /* use token storage to stash these internally, not for
310  * user use */
311 
312  _WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
313  _WSI_TOKEN_CLIENT_PEER_ADDRESS,
314  _WSI_TOKEN_CLIENT_URI,
315  _WSI_TOKEN_CLIENT_HOST,
316  _WSI_TOKEN_CLIENT_ORIGIN,
317  _WSI_TOKEN_CLIENT_METHOD,
318  _WSI_TOKEN_CLIENT_IFACE,
319  _WSI_TOKEN_CLIENT_ALPN,
320 
321  /* always last real token index*/
322  WSI_TOKEN_COUNT,
323 
324  /* parser state additions, no storage associated */
325  WSI_TOKEN_NAME_PART,
326 #if defined(LWS_WITH_CUSTOM_HEADERS)
327  WSI_TOKEN_UNKNOWN_VALUE_PART,
328 #endif
329  WSI_TOKEN_SKIPPING,
330  WSI_TOKEN_SKIPPING_SAW_CR,
331  WSI_PARSING_COMPLETE,
332  WSI_INIT_TOKEN_MUXURL,
333 };
334 
336  unsigned short token_limit[WSI_TOKEN_COUNT];
337 };
338 
344 LWS_VISIBLE LWS_EXTERN const unsigned char *
345 lws_token_to_string(enum lws_token_indexes token);
346 
355 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
356 lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h);
357 
367 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
368 lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h,
369  int frag_idx);
370 
385 LWS_VISIBLE LWS_EXTERN int
386 lws_hdr_copy(struct lws *wsi, char *dest, int len, enum lws_token_indexes h);
387 
405 LWS_VISIBLE LWS_EXTERN int
406 lws_hdr_copy_fragment(struct lws *wsi, char *dest, int len,
407  enum lws_token_indexes h, int frag_idx);
408 
425 LWS_VISIBLE LWS_EXTERN int
426 lws_hdr_custom_length(struct lws *wsi, const char *name, int nlen);
427 
446 LWS_VISIBLE LWS_EXTERN int
447 lws_hdr_custom_copy(struct lws *wsi, char *dst, int len, const char *name,
448  int nlen);
449 
460 LWS_VISIBLE LWS_EXTERN const char *
461 lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int len);
463 
478 
480 #define LWSAHH_CODE_MASK ((1 << 16) - 1)
481 #define LWSAHH_FLAG_NO_SERVER_NAME (1 << 30)
482 
497 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
498 lws_add_http_header_status(struct lws *wsi,
499  unsigned int code, unsigned char **p,
500  unsigned char *end);
513 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
514 lws_add_http_header_by_name(struct lws *wsi, const unsigned char *name,
515  const unsigned char *value, int length,
516  unsigned char **p, unsigned char *end);
530 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
531 lws_add_http_header_by_token(struct lws *wsi, enum lws_token_indexes token,
532  const unsigned char *value, int length,
533  unsigned char **p, unsigned char *end);
544 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
545 lws_add_http_header_content_length(struct lws *wsi,
546  lws_filepos_t content_length,
547  unsigned char **p, unsigned char *end);
557 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
558 lws_finalize_http_header(struct lws *wsi, unsigned char **p,
559  unsigned char *end);
560 
572 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
573 lws_finalize_write_http_header(struct lws *wsi, unsigned char *start,
574  unsigned char **p, unsigned char *end);
575 
576 #define LWS_ILLEGAL_HTTP_CONTENT_LEN ((lws_filepos_t)-1ll)
577 
604 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
605 lws_add_http_common_headers(struct lws *wsi, unsigned int code,
606  const char *content_type, lws_filepos_t content_len,
607  unsigned char **p, unsigned char *end);
608 
610 
621 
632 LWS_VISIBLE LWS_EXTERN const char *
633 lws_urlencode(char *escaped, const char *string, int len);
634 
635 /*
636  * URLDECODE 1 / 2
637  *
638  * This simple urldecode only operates until the first '\0' and requires the
639  * data to exist all at once
640  */
656 LWS_VISIBLE LWS_EXTERN int
657 lws_urldecode(char *string, const char *escaped, int len);
659 
669 LWS_VISIBLE LWS_EXTERN int
670 lws_return_http_status(struct lws *wsi, unsigned int code,
671  const char *html_body);
672 
685 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
686 lws_http_redirect(struct lws *wsi, int code, const unsigned char *loc, int len,
687  unsigned char **p, unsigned char *end);
688 
697 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
698 lws_http_transaction_completed(struct lws *wsi);
699 
707 LWS_VISIBLE LWS_EXTERN int
708 lws_http_headers_detach(struct lws *wsi);
709 
718 LWS_VISIBLE LWS_EXTERN int
719 lws_http_mark_sse(struct lws *wsi);
720 
746 LWS_VISIBLE int
747 lws_http_compression_apply(struct lws *wsi, const char *name,
748  unsigned char **p, unsigned char *end, char decomp);
750 
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:136
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:143
int len
Definition: lws-http.h:133
const char *const * vars
Definition: lws-http.h:147
Definition: lws-context-vhost.h:997
int len
Definition: lws-http.h:205
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:145
char swallow[16]
Definition: lws-http.h:144
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:131
unsigned short token_limit[WSI_TOKEN_COUNT]
Definition: lws-http.h:336
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 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:132
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:335
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:148
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:203
LWS_VISIBLE LWS_EXTERN int lws_http_mark_sse(struct lws *wsi)
void * data
Definition: lws-http.h:146
lws_process_html_state_cb replace
Definition: lws-http.h:150
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)
char * token
Definition: lws-http.h:204
int max_len
Definition: lws-http.h:134
Definition: lws-http.h:142