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
159 lws_chunked_html_process(struct lws_process_html_args *args,
160  struct lws_process_html_state *s);
162 
196 
203 struct lws_tokens {
204  unsigned 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 
479 
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
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
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 
632 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
633 lws_http_get_uri_and_method(struct lws *wsi, char **puri_ptr, int *puri_len);
634 
636 
647 
658 LWS_VISIBLE LWS_EXTERN const char *
659 lws_urlencode(char *escaped, const char *string, int len);
660 
661 /*
662  * URLDECODE 1 / 2
663  *
664  * This simple urldecode only operates until the first '\0' and requires the
665  * data to exist all at once
666  */
682 LWS_VISIBLE LWS_EXTERN int
683 lws_urldecode(char *string, const char *escaped, int len);
685 
695 LWS_VISIBLE LWS_EXTERN int
696 lws_return_http_status(struct lws *wsi, unsigned int code,
697  const char *html_body);
698 
711 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
712 lws_http_redirect(struct lws *wsi, int code, const unsigned char *loc, int len,
713  unsigned char **p, unsigned char *end);
714 
723 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
724 lws_http_transaction_completed(struct lws *wsi);
725 
733 LWS_VISIBLE LWS_EXTERN int
734 lws_http_headers_detach(struct lws *wsi);
735 
744 LWS_VISIBLE LWS_EXTERN int
745 lws_http_mark_sse(struct lws *wsi);
746 
772 LWS_VISIBLE int
773 lws_http_compression_apply(struct lws *wsi, const char *name,
774  unsigned char **p, unsigned char *end, char decomp);
776 
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_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_finalize_http_header(struct lws *wsi, 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 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_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_hdr_custom_copy(struct lws *wsi, char *dst, int len, const char *name, int nlen)
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_fragment_length(struct lws *wsi, 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_copy(struct lws *wsi, char *dest, int len, enum lws_token_indexes h)
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_hdr_total_length(struct lws *wsi, 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)
Definition: lws-context-vhost.h:1019
Definition: lws-http.h:131
int chunked
Definition: lws-http.h:136
char * p
Definition: lws-http.h:132
int len
Definition: lws-http.h:133
int max_len
Definition: lws-http.h:134
Definition: lws-http.h:142
const char *const * vars
Definition: lws-http.h:147
int pos
Definition: lws-http.h:145
lws_process_html_state_cb replace
Definition: lws-http.h:150
char swallow[16]
Definition: lws-http.h:144
int count_vars
Definition: lws-http.h:148
char * start
Definition: lws-http.h:143
void * data
Definition: lws-http.h:146
Definition: lws-http.h:335
unsigned short token_limit[WSI_TOKEN_COUNT]
Definition: lws-http.h:336
Definition: lws-http.h:203
int len
Definition: lws-http.h:205
unsigned char * token
Definition: lws-http.h:204