private.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2018 Andy Green <[email protected]>
  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. * This is included from core/private.h if either H1 or H2 roles are
  22. * enabled
  23. */
  24. #if defined(LWS_WITH_HUBBUB)
  25. #include <hubbub/hubbub.h>
  26. #include <hubbub/parser.h>
  27. #endif
  28. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  29. #include "roles/http/compression/private.h"
  30. #endif
  31. #define lwsi_role_http(wsi) (lwsi_role_h1(wsi) || lwsi_role_h2(wsi))
  32. enum http_version {
  33. HTTP_VERSION_1_0,
  34. HTTP_VERSION_1_1,
  35. HTTP_VERSION_2
  36. };
  37. enum http_conn_type {
  38. HTTP_CONNECTION_CLOSE,
  39. HTTP_CONNECTION_KEEP_ALIVE
  40. };
  41. /*
  42. * This is totally opaque to code using the library. It's exported as a
  43. * forward-reference pointer-only declaration; the user can use the pointer with
  44. * other APIs to get information out of it.
  45. */
  46. #if defined(LWS_WITH_ESP32)
  47. typedef uint16_t ah_data_idx_t;
  48. #else
  49. typedef uint32_t ah_data_idx_t;
  50. #endif
  51. struct lws_fragments {
  52. ah_data_idx_t offset;
  53. uint16_t len;
  54. uint8_t nfrag; /* which ah->frag[] continues this content, or 0 */
  55. uint8_t flags; /* only http2 cares */
  56. };
  57. #if defined(LWS_WITH_RANGES)
  58. enum range_states {
  59. LWSRS_NO_ACTIVE_RANGE,
  60. LWSRS_BYTES_EQ,
  61. LWSRS_FIRST,
  62. LWSRS_STARTING,
  63. LWSRS_ENDING,
  64. LWSRS_COMPLETED,
  65. LWSRS_SYNTAX,
  66. };
  67. struct lws_range_parsing {
  68. unsigned long long start, end, extent, agg, budget;
  69. const char buf[128];
  70. int pos;
  71. enum range_states state;
  72. char start_valid, end_valid, ctr, count_ranges, did_try, inside, send_ctr;
  73. };
  74. int
  75. lws_ranges_init(struct lws *wsi, struct lws_range_parsing *rp,
  76. unsigned long long extent);
  77. int
  78. lws_ranges_next(struct lws_range_parsing *rp);
  79. void
  80. lws_ranges_reset(struct lws_range_parsing *rp);
  81. #endif
  82. /*
  83. * these are assigned from a pool held in the context.
  84. * Both client and server mode uses them for http header analysis
  85. */
  86. struct allocated_headers {
  87. struct allocated_headers *next; /* linked list */
  88. struct lws *wsi; /* owner */
  89. char *data; /* prepared by context init to point to dedicated storage */
  90. ah_data_idx_t data_length;
  91. /*
  92. * the randomly ordered fragments, indexed by frag_index and
  93. * lws_fragments->nfrag for continuation.
  94. */
  95. struct lws_fragments frags[WSI_TOKEN_COUNT];
  96. time_t assigned;
  97. /*
  98. * for each recognized token, frag_index says which frag[] his data
  99. * starts in (0 means the token did not appear)
  100. * the actual header data gets dumped as it comes in, into data[]
  101. */
  102. uint8_t frag_index[WSI_TOKEN_COUNT];
  103. #ifndef LWS_NO_CLIENT
  104. char initial_handshake_hash_base64[30];
  105. #endif
  106. uint32_t pos;
  107. uint32_t http_response;
  108. uint32_t current_token_limit;
  109. int hdr_token_idx;
  110. int16_t lextable_pos;
  111. uint8_t in_use;
  112. uint8_t nfrag;
  113. char /*enum uri_path_states */ ups;
  114. char /*enum uri_esc_states */ ues;
  115. char esc_stash;
  116. char post_literal_equal;
  117. uint8_t /* enum lws_token_indexes */ parser_state;
  118. };
  119. #if defined(LWS_WITH_HUBBUB)
  120. struct lws_rewrite {
  121. hubbub_parser *parser;
  122. hubbub_parser_optparams params;
  123. const char *from, *to;
  124. int from_len, to_len;
  125. unsigned char *p, *end;
  126. struct lws *wsi;
  127. };
  128. static LWS_INLINE int hstrcmp(hubbub_string *s, const char *p, int len)
  129. {
  130. if ((int)s->len != len)
  131. return 1;
  132. return strncmp((const char *)s->ptr, p, len);
  133. }
  134. typedef hubbub_error (*hubbub_callback_t)(const hubbub_token *token, void *pw);
  135. LWS_EXTERN struct lws_rewrite *
  136. lws_rewrite_create(struct lws *wsi, hubbub_callback_t cb, const char *from, const char *to);
  137. LWS_EXTERN void
  138. lws_rewrite_destroy(struct lws_rewrite *r);
  139. LWS_EXTERN int
  140. lws_rewrite_parse(struct lws_rewrite *r, const unsigned char *in, int in_len);
  141. #endif
  142. struct lws_pt_role_http {
  143. struct allocated_headers *ah_list;
  144. struct lws *ah_wait_list;
  145. #ifdef LWS_WITH_CGI
  146. struct lws_cgi *cgi_list;
  147. #endif
  148. int ah_wait_list_length;
  149. uint32_t ah_pool_length;
  150. int ah_count_in_use;
  151. };
  152. struct lws_peer_role_http {
  153. uint32_t count_ah;
  154. uint32_t total_ah;
  155. };
  156. struct lws_vhost_role_http {
  157. char http_proxy_address[128];
  158. const struct lws_http_mount *mount_list;
  159. const char *error_document_404;
  160. unsigned int http_proxy_port;
  161. };
  162. #ifdef LWS_WITH_ACCESS_LOG
  163. struct lws_access_log {
  164. char *header_log;
  165. char *user_agent;
  166. char *referrer;
  167. unsigned long sent;
  168. int response;
  169. };
  170. #endif
  171. #define LWS_HTTP_CHUNK_HDR_MAX_SIZE (6 + 2) /* 6 hex digits and then CRLF */
  172. #define LWS_HTTP_CHUNK_TRL_MAX_SIZE (2 + 5) /* CRLF, then maybe 0 CRLF CRLF */
  173. struct _lws_http_mode_related {
  174. struct lws *new_wsi_list;
  175. unsigned char *pending_return_headers;
  176. size_t pending_return_headers_len;
  177. #if defined(LWS_WITH_HTTP_PROXY)
  178. struct lws_rewrite *rw;
  179. #endif
  180. struct allocated_headers *ah;
  181. struct lws *ah_wait_list;
  182. lws_filepos_t filepos;
  183. lws_filepos_t filelen;
  184. lws_fop_fd_t fop_fd;
  185. #if defined(LWS_WITH_RANGES)
  186. struct lws_range_parsing range;
  187. char multipart_content_type[64];
  188. #endif
  189. #ifdef LWS_WITH_ACCESS_LOG
  190. struct lws_access_log access_log;
  191. #endif
  192. #ifdef LWS_WITH_CGI
  193. struct lws_cgi *cgi; /* wsi being cgi master have one of these */
  194. #endif
  195. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  196. struct lws_compression_support *lcs;
  197. lws_comp_ctx_t comp_ctx;
  198. unsigned char comp_accept_mask;
  199. #endif
  200. enum http_version request_version;
  201. enum http_conn_type conn_type;
  202. lws_filepos_t tx_content_length;
  203. lws_filepos_t tx_content_remain;
  204. lws_filepos_t rx_content_length;
  205. lws_filepos_t rx_content_remain;
  206. #if defined(LWS_WITH_HTTP_PROXY)
  207. unsigned int perform_rewrite:1;
  208. unsigned int proxy_clientside:1;
  209. unsigned int proxy_parent_chunked:1;
  210. #endif
  211. unsigned int deferred_transaction_completed:1;
  212. unsigned int content_length_explicitly_zero:1;
  213. unsigned int did_stream_close:1;
  214. };
  215. #ifndef LWS_NO_CLIENT
  216. enum lws_chunk_parser {
  217. ELCP_HEX,
  218. ELCP_CR,
  219. ELCP_CONTENT,
  220. ELCP_POST_CR,
  221. ELCP_POST_LF,
  222. };
  223. #endif
  224. enum lws_parse_urldecode_results {
  225. LPUR_CONTINUE,
  226. LPUR_SWALLOW,
  227. LPUR_FORBID,
  228. LPUR_EXCESSIVE,
  229. };
  230. int
  231. lws_read_h1(struct lws *wsi, unsigned char *buf, lws_filepos_t len);
  232. void
  233. _lws_header_table_reset(struct allocated_headers *ah);
  234. LWS_EXTERN int
  235. _lws_destroy_ah(struct lws_context_per_thread *pt, struct allocated_headers *ah);