2
0

header.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. #include "core/private.h"
  22. #include "lextable-strings.h"
  23. const unsigned char *
  24. lws_token_to_string(enum lws_token_indexes token)
  25. {
  26. if ((unsigned int)token >= LWS_ARRAY_SIZE(set))
  27. return NULL;
  28. return (unsigned char *)set[token];
  29. }
  30. int
  31. lws_add_http_header_by_name(struct lws *wsi, const unsigned char *name,
  32. const unsigned char *value, int length,
  33. unsigned char **p, unsigned char *end)
  34. {
  35. #ifdef LWS_WITH_HTTP2
  36. if (lwsi_role_h2(wsi) || lwsi_role_h2_ENCAPSULATION(wsi))
  37. return lws_add_http2_header_by_name(wsi, name,
  38. value, length, p, end);
  39. #else
  40. (void)wsi;
  41. #endif
  42. if (name) {
  43. while (*p < end && *name)
  44. *((*p)++) = *name++;
  45. if (*p == end)
  46. return 1;
  47. *((*p)++) = ' ';
  48. }
  49. if (*p + length + 3 >= end)
  50. return 1;
  51. memcpy(*p, value, length);
  52. *p += length;
  53. *((*p)++) = '\x0d';
  54. *((*p)++) = '\x0a';
  55. return 0;
  56. }
  57. int lws_finalize_http_header(struct lws *wsi, unsigned char **p,
  58. unsigned char *end)
  59. {
  60. #ifdef LWS_WITH_HTTP2
  61. if (lwsi_role_h2(wsi) || lwsi_role_h2_ENCAPSULATION(wsi))
  62. return 0;
  63. #else
  64. (void)wsi;
  65. #endif
  66. if ((lws_intptr_t)(end - *p) < 3)
  67. return 1;
  68. *((*p)++) = '\x0d';
  69. *((*p)++) = '\x0a';
  70. return 0;
  71. }
  72. int
  73. lws_finalize_write_http_header(struct lws *wsi, unsigned char *start,
  74. unsigned char **pp, unsigned char *end)
  75. {
  76. unsigned char *p;
  77. int len;
  78. if (lws_finalize_http_header(wsi, pp, end))
  79. return 1;
  80. p = *pp;
  81. len = lws_ptr_diff(p, start);
  82. if (lws_write(wsi, start, len, LWS_WRITE_HTTP_HEADERS) != len)
  83. return 1;
  84. return 0;
  85. }
  86. int
  87. lws_add_http_header_by_token(struct lws *wsi, enum lws_token_indexes token,
  88. const unsigned char *value, int length,
  89. unsigned char **p, unsigned char *end)
  90. {
  91. const unsigned char *name;
  92. #ifdef LWS_WITH_HTTP2
  93. if (lwsi_role_h2(wsi) || lwsi_role_h2_ENCAPSULATION(wsi))
  94. return lws_add_http2_header_by_token(wsi, token, value,
  95. length, p, end);
  96. #endif
  97. name = lws_token_to_string(token);
  98. if (!name)
  99. return 1;
  100. return lws_add_http_header_by_name(wsi, name, value, length, p, end);
  101. }
  102. int
  103. lws_add_http_header_content_length(struct lws *wsi,
  104. lws_filepos_t content_length,
  105. unsigned char **p, unsigned char *end)
  106. {
  107. char b[24];
  108. int n;
  109. n = sprintf(b, "%llu", (unsigned long long)content_length);
  110. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH,
  111. (unsigned char *)b, n, p, end))
  112. return 1;
  113. wsi->http.tx_content_length = content_length;
  114. wsi->http.tx_content_remain = content_length;
  115. lwsl_info("%s: wsi %p: tx_content_length/remain %llu\n", __func__,
  116. wsi, (unsigned long long)content_length);
  117. return 0;
  118. }
  119. int
  120. lws_add_http_common_headers(struct lws *wsi, unsigned int code,
  121. const char *content_type, lws_filepos_t content_len,
  122. unsigned char **p, unsigned char *end)
  123. {
  124. const char *ka[] = { "close", "keep-alive" };
  125. int types[] = { HTTP_CONNECTION_CLOSE, HTTP_CONNECTION_KEEP_ALIVE },
  126. t = 0;
  127. if (lws_add_http_header_status(wsi, code, p, end))
  128. return 1;
  129. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
  130. (unsigned char *)content_type,
  131. (int)strlen(content_type), p, end))
  132. return 1;
  133. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  134. if (!wsi->http.lcs &&
  135. (!strncmp(content_type, "text/", 5) ||
  136. !strcmp(content_type, "application/javascript") ||
  137. !strcmp(content_type, "image/svg+xml")))
  138. lws_http_compression_apply(wsi, NULL, p, end, 0);
  139. #endif
  140. /*
  141. * if we decided to compress it, we don't know the content length...
  142. * the compressed data will go out chunked on h1
  143. */
  144. if (
  145. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  146. !wsi->http.lcs &&
  147. #endif
  148. content_len != LWS_ILLEGAL_HTTP_CONTENT_LEN) {
  149. if (lws_add_http_header_content_length(wsi, content_len,
  150. p, end))
  151. return 1;
  152. } else {
  153. /* there was no length... it normally means CONNECTION_CLOSE */
  154. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  155. if (!wsi->http2_substream && wsi->http.lcs) {
  156. /* so...
  157. * - h1 connection
  158. * - http compression transform active
  159. * - did not send content length
  160. *
  161. * then mark as chunked...
  162. */
  163. wsi->http.comp_ctx.chunking = 1;
  164. if (lws_add_http_header_by_token(wsi,
  165. WSI_TOKEN_HTTP_TRANSFER_ENCODING,
  166. (unsigned char *)"chunked", 7, p, end))
  167. return -1;
  168. /* ... but h1 compression is chunked, if active we can
  169. * still pipeline
  170. */
  171. if (wsi->http.lcs &&
  172. wsi->http.conn_type == HTTP_CONNECTION_KEEP_ALIVE)
  173. t = 1;
  174. }
  175. #endif
  176. if (!wsi->http2_substream) {
  177. if (lws_add_http_header_by_token(wsi,
  178. WSI_TOKEN_CONNECTION,
  179. (unsigned char *)ka[t],
  180. (int)strlen(ka[t]), p, end))
  181. return 1;
  182. wsi->http.conn_type = types[t];
  183. }
  184. }
  185. return 0;
  186. }
  187. static const char * const err400[] = {
  188. "Bad Request",
  189. "Unauthorized",
  190. "Payment Required",
  191. "Forbidden",
  192. "Not Found",
  193. "Method Not Allowed",
  194. "Not Acceptable",
  195. "Proxy Auth Required",
  196. "Request Timeout",
  197. "Conflict",
  198. "Gone",
  199. "Length Required",
  200. "Precondition Failed",
  201. "Request Entity Too Large",
  202. "Request URI too Long",
  203. "Unsupported Media Type",
  204. "Requested Range Not Satisfiable",
  205. "Expectation Failed"
  206. };
  207. static const char * const err500[] = {
  208. "Internal Server Error",
  209. "Not Implemented",
  210. "Bad Gateway",
  211. "Service Unavailable",
  212. "Gateway Timeout",
  213. "HTTP Version Not Supported"
  214. };
  215. /* security best practices from Mozilla Observatory */
  216. static const
  217. struct lws_protocol_vhost_options pvo_hsbph[] = {{
  218. NULL, NULL, "referrer-policy:", "no-referrer"
  219. }, {
  220. &pvo_hsbph[0], NULL, "x-frame-options:", "deny"
  221. }, {
  222. &pvo_hsbph[1], NULL, "x-xss-protection:", "1; mode=block"
  223. }, {
  224. &pvo_hsbph[2], NULL, "x-content-type-options:", "nosniff"
  225. }, {
  226. &pvo_hsbph[3], NULL, "content-security-policy:",
  227. "default-src 'none'; img-src 'self' data: ; "
  228. "script-src 'self'; font-src 'self'; "
  229. "style-src 'self'; connect-src 'self'; "
  230. "frame-ancestors 'none'; base-uri 'none';"
  231. "form-action 'self';"
  232. }};
  233. int
  234. lws_add_http_header_status(struct lws *wsi, unsigned int _code,
  235. unsigned char **p, unsigned char *end)
  236. {
  237. static const char * const hver[] = {
  238. "HTTP/1.0", "HTTP/1.1", "HTTP/2"
  239. };
  240. const struct lws_protocol_vhost_options *headers;
  241. unsigned int code = _code & LWSAHH_CODE_MASK;
  242. const char *description = "", *p1;
  243. unsigned char code_and_desc[60];
  244. int n;
  245. #ifdef LWS_WITH_ACCESS_LOG
  246. wsi->http.access_log.response = code;
  247. #endif
  248. #ifdef LWS_WITH_HTTP2
  249. if (lwsi_role_h2(wsi) || lwsi_role_h2_ENCAPSULATION(wsi)) {
  250. n = lws_add_http2_header_status(wsi, code, p, end);
  251. if (n)
  252. return n;
  253. } else
  254. #endif
  255. {
  256. if (code >= 400 && code < (400 + LWS_ARRAY_SIZE(err400)))
  257. description = err400[code - 400];
  258. if (code >= 500 && code < (500 + LWS_ARRAY_SIZE(err500)))
  259. description = err500[code - 500];
  260. if (code == 100)
  261. description = "Continue";
  262. if (code == 200)
  263. description = "OK";
  264. if (code == 304)
  265. description = "Not Modified";
  266. else
  267. if (code >= 300 && code < 400)
  268. description = "Redirect";
  269. if (wsi->http.request_version < LWS_ARRAY_SIZE(hver))
  270. p1 = hver[wsi->http.request_version];
  271. else
  272. p1 = hver[0];
  273. n = sprintf((char *)code_and_desc, "%s %u %s", p1, code,
  274. description);
  275. if (lws_add_http_header_by_name(wsi, NULL, code_and_desc, n, p,
  276. end))
  277. return 1;
  278. }
  279. headers = wsi->vhost->headers;
  280. while (headers) {
  281. if (lws_add_http_header_by_name(wsi,
  282. (const unsigned char *)headers->name,
  283. (unsigned char *)headers->value,
  284. (int)strlen(headers->value), p, end))
  285. return 1;
  286. headers = headers->next;
  287. }
  288. if (wsi->vhost->options &
  289. LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE) {
  290. headers = &pvo_hsbph[LWS_ARRAY_SIZE(pvo_hsbph) - 1];
  291. while (headers) {
  292. if (lws_add_http_header_by_name(wsi,
  293. (const unsigned char *)headers->name,
  294. (unsigned char *)headers->value,
  295. (int)strlen(headers->value), p, end))
  296. return 1;
  297. headers = headers->next;
  298. }
  299. }
  300. if (wsi->context->server_string &&
  301. !(_code & LWSAHH_FLAG_NO_SERVER_NAME))
  302. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_SERVER,
  303. (unsigned char *)wsi->context->server_string,
  304. wsi->context->server_string_len, p, end))
  305. return 1;
  306. if (wsi->vhost->options & LWS_SERVER_OPTION_STS)
  307. if (lws_add_http_header_by_name(wsi, (unsigned char *)
  308. "Strict-Transport-Security:",
  309. (unsigned char *)"max-age=15768000 ; "
  310. "includeSubDomains", 36, p, end))
  311. return 1;
  312. if (*p >= (end - 2)) {
  313. lwsl_err("%s: reached end of buffer\n", __func__);
  314. return 1;
  315. }
  316. return 0;
  317. }
  318. LWS_VISIBLE int
  319. lws_return_http_status(struct lws *wsi, unsigned int code,
  320. const char *html_body)
  321. {
  322. struct lws_context *context = lws_get_context(wsi);
  323. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  324. unsigned char *p = pt->serv_buf + LWS_PRE;
  325. unsigned char *start = p;
  326. unsigned char *end = p + context->pt_serv_buf_size - LWS_PRE;
  327. char *body = (char *)start + context->pt_serv_buf_size - 512;
  328. int n = 0, m = 0, len;
  329. char slen[20];
  330. if (!wsi->vhost) {
  331. lwsl_err("%s: wsi not bound to vhost\n", __func__);
  332. return 1;
  333. }
  334. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  335. if (!wsi->handling_404 &&
  336. wsi->vhost->http.error_document_404 &&
  337. code == HTTP_STATUS_NOT_FOUND)
  338. /* we should do a redirect, and do the 404 there */
  339. if (lws_http_redirect(wsi, HTTP_STATUS_FOUND,
  340. (uint8_t *)wsi->vhost->http.error_document_404,
  341. (int)strlen(wsi->vhost->http.error_document_404),
  342. &p, end) > 0)
  343. return 0;
  344. #endif
  345. /* if the redirect failed, just do a simple status */
  346. p = start;
  347. if (!html_body)
  348. html_body = "";
  349. if (lws_add_http_header_status(wsi, code, &p, end))
  350. return 1;
  351. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
  352. (unsigned char *)"text/html", 9,
  353. &p, end))
  354. return 1;
  355. len = lws_snprintf(body, 510, "<html><head>"
  356. "<meta charset=utf-8 http-equiv=\"Content-Language\" "
  357. "content=\"en\"/>"
  358. "<link rel=\"stylesheet\" type=\"text/css\" "
  359. "href=\"/error.css\"/>"
  360. "</head><body><h1>%u</h1>%s</body></html>", code, html_body);
  361. n = sprintf(slen, "%d", len);
  362. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH,
  363. (unsigned char *)slen, n, &p, end))
  364. return 1;
  365. if (lws_finalize_http_header(wsi, &p, end))
  366. return 1;
  367. #if defined(LWS_WITH_HTTP2)
  368. if (wsi->http2_substream) {
  369. /*
  370. * for HTTP/2, the headers must be sent separately, since they
  371. * go out in their own frame. That puts us in a bind that
  372. * we won't always be able to get away with two lws_write()s in
  373. * sequence, since the first may use up the writability due to
  374. * the pipe being choked or SSL_WANT_.
  375. *
  376. * However we do need to send the human-readable body, and the
  377. * END_STREAM.
  378. *
  379. * Solve it by writing the headers now...
  380. */
  381. m = lws_write(wsi, start, lws_ptr_diff(p, start),
  382. LWS_WRITE_HTTP_HEADERS);
  383. if (m != lws_ptr_diff(p, start))
  384. return 1;
  385. /*
  386. * ... but stash the body and send it as a priority next
  387. * handle_POLLOUT
  388. */
  389. wsi->http.tx_content_length = len;
  390. wsi->http.tx_content_remain = len;
  391. wsi->h2.pending_status_body = lws_malloc(len + LWS_PRE + 1,
  392. "pending status body");
  393. if (!wsi->h2.pending_status_body)
  394. return -1;
  395. strcpy(wsi->h2.pending_status_body + LWS_PRE, body);
  396. lws_callback_on_writable(wsi);
  397. return 0;
  398. } else
  399. #endif
  400. {
  401. /*
  402. * for http/1, we can just append the body after the finalized
  403. * headers and send it all in one go.
  404. */
  405. n = lws_ptr_diff(p, start) + len;
  406. memcpy(p, body, len);
  407. m = lws_write(wsi, start, n, LWS_WRITE_HTTP);
  408. if (m != n)
  409. return 1;
  410. }
  411. return m != n;
  412. }
  413. LWS_VISIBLE int
  414. lws_http_redirect(struct lws *wsi, int code, const unsigned char *loc, int len,
  415. unsigned char **p, unsigned char *end)
  416. {
  417. unsigned char *start = *p;
  418. if (lws_add_http_header_status(wsi, code, p, end))
  419. return -1;
  420. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_LOCATION, loc, len,
  421. p, end))
  422. return -1;
  423. /*
  424. * if we're going with http/1.1 and keepalive, we have to give fake
  425. * content metadata so the client knows we completed the transaction and
  426. * it can do the redirect...
  427. */
  428. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
  429. (unsigned char *)"text/html", 9, p,
  430. end))
  431. return -1;
  432. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH,
  433. (unsigned char *)"0", 1, p, end))
  434. return -1;
  435. if (lws_finalize_http_header(wsi, p, end))
  436. return -1;
  437. return lws_write(wsi, start, *p - start, LWS_WRITE_HTTP_HEADERS |
  438. LWS_WRITE_H2_STREAM_END);
  439. }
  440. #if !defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  441. LWS_VISIBLE int
  442. lws_http_compression_apply(struct lws *wsi, const char *name,
  443. unsigned char **p, unsigned char *end, char decomp)
  444. {
  445. (void)wsi;
  446. (void)name;
  447. (void)p;
  448. (void)end;
  449. (void)decomp;
  450. return 0;
  451. }
  452. #endif