test-server-http.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*
  2. * libwebsockets-test-server - libwebsockets test implementation
  3. *
  4. * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. *
  9. * The person who associated a work with this deed has dedicated
  10. * the work to the public domain by waiving all of his or her rights
  11. * to the work worldwide under copyright law, including all related
  12. * and neighboring rights, to the extent allowed by law. You can copy,
  13. * modify, distribute and perform the work, even for commercial purposes,
  14. * all without asking permission.
  15. *
  16. * The test apps are intended to be adapted for use in your code, which
  17. * may be proprietary. So unlike the library itself, they are licensed
  18. * Public Domain.
  19. */
  20. #include "test-server.h"
  21. /*
  22. * This demo server shows how to use libwebsockets for one or more
  23. * websocket protocols in the same server
  24. *
  25. * It defines the following websocket protocols:
  26. *
  27. * dumb-increment-protocol: once the socket is opened, an incrementing
  28. * ascii string is sent down it every 50ms.
  29. * If you send "reset\n" on the websocket, then
  30. * the incrementing number is reset to 0.
  31. *
  32. * lws-mirror-protocol: copies any received packet to every connection also
  33. * using this protocol, including the sender
  34. */
  35. #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
  36. /* location of the certificate revocation list */
  37. extern char crl_path[1024];
  38. #endif
  39. extern int debug_level;
  40. enum demo_protocols {
  41. /* always first */
  42. PROTOCOL_HTTP = 0,
  43. PROTOCOL_DUMB_INCREMENT,
  44. PROTOCOL_LWS_MIRROR,
  45. /* always last */
  46. DEMO_PROTOCOL_COUNT
  47. };
  48. /*
  49. * We take a strict whitelist approach to stop ../ attacks
  50. */
  51. struct serveable {
  52. const char *urlpath;
  53. const char *mimetype;
  54. };
  55. /*
  56. * this is just an example of parsing handshake headers, you don't need this
  57. * in your code unless you will filter allowing connections by the header
  58. * content
  59. */
  60. void
  61. dump_handshake_info(struct lws *wsi)
  62. {
  63. int n = 0, len;
  64. char buf[256];
  65. const unsigned char *c;
  66. do {
  67. c = lws_token_to_string(n);
  68. if (!c) {
  69. n++;
  70. continue;
  71. }
  72. len = lws_hdr_total_length(wsi, n);
  73. if (!len || len > sizeof(buf) - 1) {
  74. n++;
  75. continue;
  76. }
  77. lws_hdr_copy(wsi, buf, sizeof buf, n);
  78. buf[sizeof(buf) - 1] = '\0';
  79. fprintf(stderr, " %s = %s\n", (char *)c, buf);
  80. n++;
  81. } while (c);
  82. }
  83. const char * get_mimetype(const char *file)
  84. {
  85. int n = strlen(file);
  86. if (n < 5)
  87. return NULL;
  88. if (!strcmp(&file[n - 4], ".ico"))
  89. return "image/x-icon";
  90. if (!strcmp(&file[n - 4], ".png"))
  91. return "image/png";
  92. if (!strcmp(&file[n - 5], ".html"))
  93. return "text/html";
  94. if (!strcmp(&file[n - 4], ".css"))
  95. return "text/css";
  96. if (!strcmp(&file[n - 3], ".js"))
  97. return "text/javascript";
  98. return NULL;
  99. }
  100. static const char * const param_names[] = {
  101. "text",
  102. "send",
  103. "file",
  104. "upload",
  105. };
  106. enum enum_param_names {
  107. EPN_TEXT,
  108. EPN_SEND,
  109. EPN_FILE,
  110. EPN_UPLOAD,
  111. };
  112. static int
  113. file_upload_cb(void *data, const char *name, const char *filename,
  114. char *buf, int len, enum lws_spa_fileupload_states state)
  115. {
  116. struct per_session_data__http *pss =
  117. (struct per_session_data__http *)data;
  118. int n;
  119. switch (state) {
  120. case LWS_UFS_OPEN:
  121. strncpy(pss->filename, filename, sizeof(pss->filename) - 1);
  122. /* we get the original filename in @filename arg, but for
  123. * simple demo use a fixed name so we don't have to deal with
  124. * attacks */
  125. pss->post_fd = (lws_filefd_type)open("/tmp/post-file",
  126. O_CREAT | O_TRUNC | O_RDWR, 0600);
  127. break;
  128. case LWS_UFS_FINAL_CONTENT:
  129. case LWS_UFS_CONTENT:
  130. if (len) {
  131. pss->file_length += len;
  132. /* if the file length is too big, drop it */
  133. if (pss->file_length > 100000)
  134. return 1;
  135. n = write((int)pss->post_fd, buf, len);
  136. lwsl_notice("%s: write %d says %d\n", __func__, len, n);
  137. }
  138. if (state == LWS_UFS_CONTENT)
  139. break;
  140. close((int)pss->post_fd);
  141. pss->post_fd = LWS_INVALID_FILE;
  142. break;
  143. }
  144. return 0;
  145. }
  146. /* this protocol server (always the first one) handles HTTP,
  147. *
  148. * Some misc callbacks that aren't associated with a protocol also turn up only
  149. * here on the first protocol server.
  150. */
  151. int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
  152. void *in, size_t len)
  153. {
  154. struct per_session_data__http *pss =
  155. (struct per_session_data__http *)user;
  156. unsigned char buffer[4096 + LWS_PRE];
  157. lws_filepos_t amount, file_len, sent;
  158. char leaf_path[1024];
  159. const char *mimetype;
  160. char *other_headers;
  161. unsigned char *end, *start;
  162. struct timeval tv;
  163. unsigned char *p;
  164. #ifndef LWS_NO_CLIENT
  165. struct per_session_data__http *pss1;
  166. struct lws *wsi1;
  167. #endif
  168. char buf[256];
  169. char b64[64];
  170. int n, m;
  171. #ifdef EXTERNAL_POLL
  172. struct lws_pollargs *pa = (struct lws_pollargs *)in;
  173. #endif
  174. switch (reason) {
  175. case LWS_CALLBACK_HTTP:
  176. lwsl_info("lws_http_serve: %s\n", (const char *)in);
  177. if (debug_level & LLL_INFO) {
  178. dump_handshake_info(wsi);
  179. /* dump the individual URI Arg parameters */
  180. n = 0;
  181. while (lws_hdr_copy_fragment(wsi, buf, sizeof(buf),
  182. WSI_TOKEN_HTTP_URI_ARGS, n) > 0) {
  183. lwsl_notice("URI Arg %d: %s\n", ++n, buf);
  184. }
  185. }
  186. {
  187. lws_get_peer_simple(wsi, buf, sizeof(buf));
  188. lwsl_info("HTTP connect from %s\n", buf);
  189. }
  190. if (len < 1) {
  191. lws_return_http_status(wsi,
  192. HTTP_STATUS_BAD_REQUEST, NULL);
  193. goto try_to_reuse;
  194. }
  195. #ifndef LWS_NO_CLIENT
  196. if (!strncmp(in, "/proxytest", 10)) {
  197. struct lws_client_connect_info i;
  198. char *rootpath = "/";
  199. const char *p = (const char *)in;
  200. if (lws_get_child(wsi))
  201. break;
  202. pss->client_finished = 0;
  203. memset(&i,0, sizeof(i));
  204. i.context = lws_get_context(wsi);
  205. i.address = "git.libwebsockets.org";
  206. i.port = 80;
  207. i.ssl_connection = 0;
  208. if (p[10])
  209. i.path = (char *)in + 10;
  210. else
  211. i.path = rootpath;
  212. i.host = "git.libwebsockets.org";
  213. i.origin = NULL;
  214. i.method = "GET";
  215. i.parent_wsi = wsi;
  216. i.uri_replace_from = "git.libwebsockets.org/";
  217. i.uri_replace_to = "/proxytest/";
  218. if (!lws_client_connect_via_info(&i)) {
  219. lwsl_err("proxy connect fail\n");
  220. break;
  221. }
  222. break;
  223. }
  224. #endif
  225. #if 1
  226. /* this example server has no concept of directories */
  227. if (strchr((const char *)in + 1, '/')) {
  228. lws_return_http_status(wsi, HTTP_STATUS_NOT_ACCEPTABLE, NULL);
  229. goto try_to_reuse;
  230. }
  231. #endif
  232. /* if a legal POST URL, let it continue and accept data */
  233. if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI))
  234. return 0;
  235. /* check for the "send a big file by hand" example case */
  236. if (!strcmp((const char *)in, "/leaf.jpg")) {
  237. lws_fop_flags_t flags = LWS_O_RDONLY;
  238. if (strlen(resource_path) > sizeof(leaf_path) - 10)
  239. return -1;
  240. sprintf(leaf_path, "%s/leaf.jpg", resource_path);
  241. /* well, let's demonstrate how to send the hard way */
  242. p = buffer + LWS_PRE;
  243. end = p + sizeof(buffer) - LWS_PRE;
  244. pss->fop_fd = lws_vfs_file_open(
  245. lws_get_fops(lws_get_context(wsi)),
  246. leaf_path, &flags);
  247. if (!pss->fop_fd) {
  248. lwsl_err("failed to open file %s\n", leaf_path);
  249. return -1;
  250. }
  251. file_len = lws_vfs_get_length(pss->fop_fd);
  252. /*
  253. * we will send a big jpeg file, but it could be
  254. * anything. Set the Content-Type: appropriately
  255. * so the browser knows what to do with it.
  256. *
  257. * Notice we use the APIs to build the header, which
  258. * will do the right thing for HTTP 1/1.1 and HTTP2
  259. * depending on what connection it happens to be working
  260. * on
  261. */
  262. if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
  263. return 1;
  264. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_SERVER,
  265. (unsigned char *)"libwebsockets",
  266. 13, &p, end))
  267. return 1;
  268. if (lws_add_http_header_by_token(wsi,
  269. WSI_TOKEN_HTTP_CONTENT_TYPE,
  270. (unsigned char *)"image/jpeg",
  271. 10, &p, end))
  272. return 1;
  273. if (lws_add_http_header_content_length(wsi,
  274. file_len, &p,
  275. end))
  276. return 1;
  277. if (lws_finalize_http_header(wsi, &p, end))
  278. return 1;
  279. /*
  280. * send the http headers...
  281. * this won't block since it's the first payload sent
  282. * on the connection since it was established
  283. * (too small for partial)
  284. *
  285. * Notice they are sent using LWS_WRITE_HTTP_HEADERS
  286. * which also means you can't send body too in one step,
  287. * this is mandated by changes in HTTP2
  288. */
  289. *p = '\0';
  290. lwsl_info("%s\n", buffer + LWS_PRE);
  291. n = lws_write(wsi, buffer + LWS_PRE,
  292. p - (buffer + LWS_PRE),
  293. LWS_WRITE_HTTP_HEADERS);
  294. if (n < 0) {
  295. lws_vfs_file_close(&pss->fop_fd);
  296. return -1;
  297. }
  298. /*
  299. * book us a LWS_CALLBACK_HTTP_WRITEABLE callback
  300. */
  301. lws_callback_on_writable(wsi);
  302. break;
  303. }
  304. /* if not, send a file the easy way */
  305. if (!strncmp(in, "/cgit-data/", 11)) {
  306. in = (char *)in + 11;
  307. strcpy(buf, "/usr/share/cgit");
  308. } else
  309. strcpy(buf, resource_path);
  310. if (strcmp(in, "/")) {
  311. if (*((const char *)in) != '/')
  312. strcat(buf, "/");
  313. strncat(buf, in, sizeof(buf) - strlen(buf) - 1);
  314. } else /* default file to serve */
  315. strcat(buf, "/test.html");
  316. buf[sizeof(buf) - 1] = '\0';
  317. /* refuse to serve files we don't understand */
  318. mimetype = get_mimetype(buf);
  319. if (!mimetype) {
  320. lwsl_err("Unknown mimetype for %s\n", buf);
  321. lws_return_http_status(wsi,
  322. HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE, NULL);
  323. return -1;
  324. }
  325. /* demonstrates how to set a cookie on / */
  326. other_headers = leaf_path;
  327. p = (unsigned char *)leaf_path;
  328. if (!strcmp((const char *)in, "/") &&
  329. !lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COOKIE)) {
  330. /* this isn't very unguessable but it'll do for us */
  331. gettimeofday(&tv, NULL);
  332. n = sprintf(b64, "test=LWS_%u_%u_COOKIE;Max-Age=360000",
  333. (unsigned int)tv.tv_sec,
  334. (unsigned int)tv.tv_usec);
  335. if (lws_add_http_header_by_name(wsi,
  336. (unsigned char *)"set-cookie:",
  337. (unsigned char *)b64, n, &p,
  338. (unsigned char *)leaf_path + sizeof(leaf_path)))
  339. return 1;
  340. }
  341. if (lws_is_ssl(wsi) && lws_add_http_header_by_name(wsi,
  342. (unsigned char *)
  343. "Strict-Transport-Security:",
  344. (unsigned char *)
  345. "max-age=15768000 ; "
  346. "includeSubDomains", 36, &p,
  347. (unsigned char *)leaf_path +
  348. sizeof(leaf_path)))
  349. return 1;
  350. n = (char *)p - leaf_path;
  351. n = lws_serve_http_file(wsi, buf, mimetype, other_headers, n);
  352. if (n < 0 || ((n > 0) && lws_http_transaction_completed(wsi)))
  353. return -1; /* error or can't reuse connection: close the socket */
  354. /*
  355. * notice that the sending of the file completes asynchronously,
  356. * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when
  357. * it's done
  358. */
  359. break;
  360. case LWS_CALLBACK_HTTP_BODY:
  361. /* create the POST argument parser if not already existing */
  362. if (!pss->spa) {
  363. pss->spa = lws_spa_create(wsi, param_names,
  364. ARRAY_SIZE(param_names), 1024,
  365. file_upload_cb, pss);
  366. if (!pss->spa)
  367. return -1;
  368. pss->filename[0] = '\0';
  369. pss->file_length = 0;
  370. }
  371. /* let it parse the POST data */
  372. if (lws_spa_process(pss->spa, in, len))
  373. return -1;
  374. break;
  375. case LWS_CALLBACK_HTTP_BODY_COMPLETION:
  376. lwsl_debug("LWS_CALLBACK_HTTP_BODY_COMPLETION\n");
  377. /*
  378. * the whole of the sent body arrived,
  379. * respond to the client with a redirect to show the
  380. * results
  381. */
  382. /* call to inform no more payload data coming */
  383. lws_spa_finalize(pss->spa);
  384. p = (unsigned char *)pss->result + LWS_PRE;
  385. end = p + sizeof(pss->result) - LWS_PRE - 1;
  386. p += sprintf((char *)p,
  387. "<html><body><h1>Form results (after urldecoding)</h1>"
  388. "<table><tr><td>Name</td><td>Length</td><td>Value</td></tr>");
  389. for (n = 0; n < ARRAY_SIZE(param_names); n++)
  390. p += lws_snprintf((char *)p, end - p,
  391. "<tr><td><b>%s</b></td><td>%d</td><td>%s</td></tr>",
  392. param_names[n],
  393. lws_spa_get_length(pss->spa, n),
  394. lws_spa_get_string(pss->spa, n));
  395. p += lws_snprintf((char *)p, end - p, "</table><br><b>filename:</b> %s, <b>length</b> %ld",
  396. pss->filename, pss->file_length);
  397. p += lws_snprintf((char *)p, end - p, "</body></html>");
  398. pss->result_len = p - (unsigned char *)(pss->result + LWS_PRE);
  399. p = buffer + LWS_PRE;
  400. start = p;
  401. end = p + sizeof(buffer) - LWS_PRE;
  402. if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
  403. return 1;
  404. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
  405. (unsigned char *)"text/html", 9, &p, end))
  406. return 1;
  407. if (lws_add_http_header_content_length(wsi, pss->result_len, &p, end))
  408. return 1;
  409. if (lws_finalize_http_header(wsi, &p, end))
  410. return 1;
  411. n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
  412. if (n < 0)
  413. return 1;
  414. n = lws_write(wsi, (unsigned char *)pss->result + LWS_PRE,
  415. pss->result_len, LWS_WRITE_HTTP);
  416. if (n < 0)
  417. return 1;
  418. goto try_to_reuse;
  419. case LWS_CALLBACK_HTTP_DROP_PROTOCOL:
  420. lwsl_debug("LWS_CALLBACK_HTTP_DROP_PROTOCOL\n");
  421. /* called when our wsi user_space is going to be destroyed */
  422. if (pss->spa) {
  423. lws_spa_destroy(pss->spa);
  424. pss->spa = NULL;
  425. }
  426. break;
  427. case LWS_CALLBACK_HTTP_FILE_COMPLETION:
  428. goto try_to_reuse;
  429. case LWS_CALLBACK_HTTP_WRITEABLE:
  430. lwsl_info("LWS_CALLBACK_HTTP_WRITEABLE\n");
  431. if (pss->client_finished)
  432. return -1;
  433. if (!pss->fop_fd)
  434. goto try_to_reuse;
  435. #ifndef LWS_NO_CLIENT
  436. if (pss->reason_bf & 2) {
  437. char *px = buf + LWS_PRE;
  438. int lenx = sizeof(buf) - LWS_PRE;
  439. /*
  440. * our sink is writeable and our source has something
  441. * to read. So read a lump of source material of
  442. * suitable size to send or what's available, whichever
  443. * is the smaller.
  444. */
  445. pss->reason_bf &= ~2;
  446. wsi1 = lws_get_child(wsi);
  447. if (!wsi1)
  448. break;
  449. if (lws_http_client_read(wsi1, &px, &lenx) < 0)
  450. goto bail;
  451. if (pss->client_finished)
  452. return -1;
  453. break;
  454. }
  455. #endif
  456. /*
  457. * we can send more of whatever it is we were sending
  458. */
  459. sent = 0;
  460. do {
  461. /* we'd like the send this much */
  462. n = sizeof(buffer) - LWS_PRE;
  463. /* but if the peer told us he wants less, we can adapt */
  464. m = lws_get_peer_write_allowance(wsi);
  465. /* -1 means not using a protocol that has this info */
  466. if (m == 0)
  467. /* right now, peer can't handle anything */
  468. goto later;
  469. if (m != -1 && m < n)
  470. /* he couldn't handle that much */
  471. n = m;
  472. n = lws_vfs_file_read(pss->fop_fd,
  473. &amount, buffer + LWS_PRE, n);
  474. /* problem reading, close conn */
  475. if (n < 0) {
  476. lwsl_err("problem reading file\n");
  477. goto bail;
  478. }
  479. n = (int)amount;
  480. /* sent it all, close conn */
  481. if (n == 0)
  482. goto penultimate;
  483. /*
  484. * To support HTTP2, must take care about preamble space
  485. *
  486. * identification of when we send the last payload frame
  487. * is handled by the library itself if you sent a
  488. * content-length header
  489. */
  490. m = lws_write(wsi, buffer + LWS_PRE, n, LWS_WRITE_HTTP);
  491. if (m < 0) {
  492. lwsl_err("write failed\n");
  493. /* write failed, close conn */
  494. goto bail;
  495. }
  496. if (m) /* while still active, extend timeout */
  497. lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT, 5);
  498. sent += m;
  499. } while (!lws_send_pipe_choked(wsi) && (sent < 1024 * 1024));
  500. later:
  501. lws_callback_on_writable(wsi);
  502. break;
  503. penultimate:
  504. lws_vfs_file_close(&pss->fop_fd);
  505. pss->fop_fd = NULL;
  506. goto try_to_reuse;
  507. bail:
  508. lws_vfs_file_close(&pss->fop_fd);
  509. return -1;
  510. /*
  511. * callback for confirming to continue with client IP appear in
  512. * protocol 0 callback since no websocket protocol has been agreed
  513. * yet. You can just ignore this if you won't filter on client IP
  514. * since the default unhandled callback return is 0 meaning let the
  515. * connection continue.
  516. */
  517. case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
  518. /* if we returned non-zero from here, we kill the connection */
  519. break;
  520. #ifndef LWS_NO_CLIENT
  521. case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP: {
  522. char ctype[64], ctlen = 0;
  523. lwsl_err("LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP\n");
  524. p = buffer + LWS_PRE;
  525. end = p + sizeof(buffer) - LWS_PRE;
  526. if (lws_add_http_header_status(lws_get_parent(wsi), HTTP_STATUS_OK, &p, end))
  527. return 1;
  528. if (lws_add_http_header_by_token(lws_get_parent(wsi),
  529. WSI_TOKEN_HTTP_SERVER,
  530. (unsigned char *)"libwebsockets",
  531. 13, &p, end))
  532. return 1;
  533. ctlen = lws_hdr_copy(wsi, ctype, sizeof(ctype), WSI_TOKEN_HTTP_CONTENT_TYPE);
  534. if (ctlen > 0) {
  535. if (lws_add_http_header_by_token(lws_get_parent(wsi),
  536. WSI_TOKEN_HTTP_CONTENT_TYPE,
  537. (unsigned char *)ctype, ctlen, &p, end))
  538. return 1;
  539. }
  540. #if 0
  541. if (lws_add_http_header_content_length(lws_get_parent(wsi),
  542. file_len, &p, end))
  543. return 1;
  544. #endif
  545. if (lws_finalize_http_header(lws_get_parent(wsi), &p, end))
  546. return 1;
  547. *p = '\0';
  548. lwsl_info("%s\n", buffer + LWS_PRE);
  549. n = lws_write(lws_get_parent(wsi), buffer + LWS_PRE,
  550. p - (buffer + LWS_PRE),
  551. LWS_WRITE_HTTP_HEADERS);
  552. if (n < 0)
  553. return -1;
  554. break; }
  555. case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
  556. //lwsl_err("LWS_CALLBACK_CLOSED_CLIENT_HTTP\n");
  557. return -1;
  558. break;
  559. case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
  560. //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP: wsi %p\n", wsi);
  561. assert(lws_get_parent(wsi));
  562. if (!lws_get_parent(wsi))
  563. break;
  564. // lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP: wsi %p: sock: %d, parent_wsi: %p, parent_sock:%d, len %d\n",
  565. // wsi, lws_get_socket_fd(wsi),
  566. // lws_get_parent(wsi),
  567. // lws_get_socket_fd(lws_get_parent(wsi)), len);
  568. pss1 = lws_wsi_user(lws_get_parent(wsi));
  569. pss1->reason_bf |= 2;
  570. lws_callback_on_writable(lws_get_parent(wsi));
  571. break;
  572. case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
  573. //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ len %d\n", len);
  574. assert(lws_get_parent(wsi));
  575. m = lws_write(lws_get_parent(wsi), (unsigned char *)in,
  576. len, LWS_WRITE_HTTP);
  577. if (m < 0)
  578. return -1;
  579. break;
  580. case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
  581. //lwsl_err("LWS_CALLBACK_COMPLETED_CLIENT_HTTP\n");
  582. assert(lws_get_parent(wsi));
  583. if (!lws_get_parent(wsi))
  584. break;
  585. pss1 = lws_wsi_user(lws_get_parent(wsi));
  586. pss1->client_finished = 1;
  587. break;
  588. #endif
  589. /*
  590. * callbacks for managing the external poll() array appear in
  591. * protocol 0 callback
  592. */
  593. case LWS_CALLBACK_LOCK_POLL:
  594. /*
  595. * lock mutex to protect pollfd state
  596. * called before any other POLL related callback
  597. * if protecting wsi lifecycle change, len == 1
  598. */
  599. test_server_lock(len);
  600. break;
  601. case LWS_CALLBACK_UNLOCK_POLL:
  602. /*
  603. * unlock mutex to protect pollfd state when
  604. * called after any other POLL related callback
  605. * if protecting wsi lifecycle change, len == 1
  606. */
  607. test_server_unlock(len);
  608. break;
  609. #ifdef EXTERNAL_POLL
  610. case LWS_CALLBACK_ADD_POLL_FD:
  611. if (count_pollfds >= max_poll_elements) {
  612. lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n");
  613. return 1;
  614. }
  615. fd_lookup[pa->fd] = count_pollfds;
  616. pollfds[count_pollfds].fd = pa->fd;
  617. pollfds[count_pollfds].events = pa->events;
  618. pollfds[count_pollfds++].revents = 0;
  619. break;
  620. case LWS_CALLBACK_DEL_POLL_FD:
  621. if (!--count_pollfds)
  622. break;
  623. m = fd_lookup[pa->fd];
  624. /* have the last guy take up the vacant slot */
  625. pollfds[m] = pollfds[count_pollfds];
  626. fd_lookup[pollfds[count_pollfds].fd] = m;
  627. break;
  628. case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
  629. pollfds[fd_lookup[pa->fd]].events = pa->events;
  630. break;
  631. #endif
  632. case LWS_CALLBACK_GET_THREAD_ID:
  633. /*
  634. * if you will call "lws_callback_on_writable"
  635. * from a different thread, return the caller thread ID
  636. * here so lws can use this information to work out if it
  637. * should signal the poll() loop to exit and restart early
  638. */
  639. /* return pthread_getthreadid_np(); */
  640. break;
  641. #if defined(LWS_OPENSSL_SUPPORT)
  642. case LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION:
  643. /* Verify the client certificate */
  644. if (!len || (SSL_get_verify_result((SSL*)in) != X509_V_OK)) {
  645. int err = X509_STORE_CTX_get_error((X509_STORE_CTX*)user);
  646. int depth = X509_STORE_CTX_get_error_depth((X509_STORE_CTX*)user);
  647. const char* msg = X509_verify_cert_error_string(err);
  648. lwsl_err("LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION: SSL error: %s (%d), depth: %d\n", msg, err, depth);
  649. return 1;
  650. }
  651. break;
  652. #if defined(LWS_HAVE_SSL_CTX_set1_param)
  653. case LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS:
  654. if (crl_path[0]) {
  655. /* Enable CRL checking */
  656. X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();
  657. X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
  658. SSL_CTX_set1_param((SSL_CTX*)user, param);
  659. X509_STORE *store = SSL_CTX_get_cert_store((SSL_CTX*)user);
  660. X509_LOOKUP *lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
  661. n = X509_load_cert_crl_file(lookup, crl_path, X509_FILETYPE_PEM);
  662. X509_VERIFY_PARAM_free(param);
  663. if (n != 1) {
  664. char errbuf[256];
  665. n = ERR_get_error();
  666. lwsl_err("LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS: SSL error: %s (%d)\n", ERR_error_string(n, errbuf), n);
  667. return 1;
  668. }
  669. }
  670. break;
  671. #endif
  672. #endif
  673. default:
  674. break;
  675. }
  676. return 0;
  677. /* if we're on HTTP1.1 or 2.0, will keep the idle connection alive */
  678. try_to_reuse:
  679. if (lws_http_transaction_completed(wsi))
  680. return -1;
  681. return 0;
  682. }