A shell for running JSON-RPC 2.0 queries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

3220 lines
92 KiB

  1. /*
  2. * json-rpc-test-server.c: JSON-RPC 2.0 demo server
  3. *
  4. * Copyright (c) 2015 - 2020, Přemysl Eric Janouch <p@janouch.name>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  12. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  14. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  15. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. *
  17. */
  18. #define print_fatal_data ((void *) LOG_ERR)
  19. #define print_error_data ((void *) LOG_ERR)
  20. #define print_warning_data ((void *) LOG_WARNING)
  21. #define print_status_data ((void *) LOG_INFO)
  22. #define print_debug_data ((void *) LOG_DEBUG)
  23. #define LIBERTY_WANT_SSL
  24. #define LIBERTY_WANT_PROTO_HTTP
  25. #define LIBERTY_WANT_PROTO_WS
  26. #define LIBERTY_WANT_PROTO_SCGI
  27. #define LIBERTY_WANT_PROTO_FASTCGI
  28. #include "config.h"
  29. #undef PROGRAM_NAME
  30. #define PROGRAM_NAME "json-rpc-test-server"
  31. #include "liberty/liberty.c"
  32. #include <langinfo.h>
  33. #include <locale.h>
  34. #include <signal.h>
  35. #include <strings.h>
  36. #include <ev.h>
  37. #include <jansson.h>
  38. #include <magic.h>
  39. #include "http-parser/http_parser.h"
  40. enum { PIPE_READ, PIPE_WRITE };
  41. #define FIND_CONTAINER(name, pointer, type, member) \
  42. type *name = CONTAINER_OF (pointer, type, member)
  43. // --- Utilities ---------------------------------------------------------------
  44. static bool
  45. flush_queue (struct write_queue *queue, int fd)
  46. {
  47. struct iovec vec[queue->len], *vec_iter = vec;
  48. LIST_FOR_EACH (struct write_req, iter, queue->head)
  49. *vec_iter++ = iter->data;
  50. ssize_t written;
  51. again:
  52. if ((written = writev (fd, vec, N_ELEMENTS (vec))) >= 0)
  53. {
  54. write_queue_processed (queue, written);
  55. return true;
  56. }
  57. if (errno == EINTR)
  58. goto again;
  59. if (errno == EAGAIN)
  60. return true;
  61. return false;
  62. }
  63. // --- Logging -----------------------------------------------------------------
  64. static void
  65. log_message_syslog (void *user_data, const char *quote, const char *fmt,
  66. va_list ap)
  67. {
  68. int prio = (int) (intptr_t) user_data;
  69. va_list va;
  70. va_copy (va, ap);
  71. int size = vsnprintf (NULL, 0, fmt, va);
  72. va_end (va);
  73. if (size < 0)
  74. return;
  75. char buf[size + 1];
  76. if (vsnprintf (buf, sizeof buf, fmt, ap) >= 0)
  77. syslog (prio, "%s%s", quote, buf);
  78. }
  79. // --- FastCGI -----------------------------------------------------------------
  80. /// @defgroup FastCGI
  81. /// @{
  82. enum fcgi_request_state
  83. {
  84. FCGI_REQUEST_PARAMS, ///< Reading headers
  85. FCGI_REQUEST_STDIN ///< Reading input
  86. };
  87. struct fcgi_request
  88. {
  89. struct fcgi_muxer *muxer; ///< The parent muxer
  90. uint16_t request_id; ///< The ID of this request
  91. uint8_t flags; ///< Request flags
  92. enum fcgi_request_state state; ///< Parsing state
  93. struct str_map headers; ///< Headers
  94. struct fcgi_nv_parser hdr_parser; ///< Header parser
  95. struct str output_buffer; ///< Output buffer
  96. void *handler_data; ///< Handler data
  97. };
  98. /// Handles a single FastCGI connection, de/multiplexing requests and responses
  99. struct fcgi_muxer
  100. {
  101. struct fcgi_parser parser; ///< FastCGI message parser
  102. uint32_t active_requests; ///< The number of active requests
  103. bool in_shutdown; ///< Rejecting new requests
  104. // Virtual method callbacks:
  105. /// Write data to the underlying transport. Assumes ownership of data.
  106. void (*write_cb) (struct fcgi_muxer *, void *data, size_t len);
  107. /// Close the underlying transport. You are allowed to destroy the muxer
  108. /// directly from within the callback.
  109. void (*close_cb) (struct fcgi_muxer *);
  110. /// Start processing a request. Return false if no further action is
  111. /// to be done and the request should be finished.
  112. bool (*request_start_cb) (struct fcgi_request *);
  113. /// Handle incoming data. "len == 0" means EOF. Returns false if
  114. /// the underlying transport should be closed, this being the last request.
  115. bool (*request_push_cb)
  116. (struct fcgi_request *, const void *data, size_t len);
  117. /// Destroy the handler's data stored in the request object
  118. void (*request_finalize_cb) (struct fcgi_request *);
  119. /// Requests assigned to request IDs (may not be FCGI_NULL_REQUEST_ID)
  120. struct fcgi_request *requests[1 << 8];
  121. };
  122. static void
  123. fcgi_muxer_send (struct fcgi_muxer *self,
  124. enum fcgi_type type, uint16_t request_id, const void *data, size_t len)
  125. {
  126. hard_assert (len <= UINT16_MAX);
  127. struct str message = str_make ();
  128. static char zeroes[8];
  129. size_t padding = -len & 7;
  130. str_pack_u8 (&message, FCGI_VERSION_1);
  131. str_pack_u8 (&message, type);
  132. str_pack_u16 (&message, request_id);
  133. str_pack_u16 (&message, len); // content length
  134. str_pack_u8 (&message, padding); // padding length
  135. str_pack_u8 (&message, 0); // reserved
  136. str_append_data (&message, data, len);
  137. str_append_data (&message, zeroes, padding);
  138. self->write_cb (self, message.str, message.len);
  139. }
  140. static void
  141. fcgi_muxer_send_end_request (struct fcgi_muxer *self, uint16_t request_id,
  142. uint32_t app_status, enum fcgi_protocol_status protocol_status)
  143. {
  144. uint8_t content[8] = { app_status >> 24, app_status >> 16,
  145. app_status << 8, app_status, protocol_status };
  146. fcgi_muxer_send (self, FCGI_END_REQUEST, request_id,
  147. content, sizeof content);
  148. }
  149. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  150. static struct fcgi_request *
  151. fcgi_request_new (void)
  152. {
  153. struct fcgi_request *self = xcalloc (1, sizeof *self);
  154. self->headers = str_map_make (free);
  155. self->hdr_parser = fcgi_nv_parser_make ();
  156. self->hdr_parser.output = &self->headers;
  157. self->output_buffer = str_make ();
  158. return self;
  159. }
  160. static void
  161. fcgi_request_destroy (struct fcgi_request *self)
  162. {
  163. // TODO: consider the case where it hasn't been started yet
  164. self->muxer->request_finalize_cb (self);
  165. str_map_free (&self->headers);
  166. fcgi_nv_parser_free (&self->hdr_parser);
  167. free (self);
  168. }
  169. static void
  170. fcgi_request_flush (struct fcgi_request *self)
  171. {
  172. if (!self->output_buffer.len)
  173. return;
  174. fcgi_muxer_send (self->muxer, FCGI_STDOUT, self->request_id,
  175. self->output_buffer.str, self->output_buffer.len);
  176. str_reset (&self->output_buffer);
  177. }
  178. static void
  179. fcgi_request_write (struct fcgi_request *self, const void *data, size_t len)
  180. {
  181. // We're buffering the output and splitting it into messages
  182. bool need_flush = true;
  183. while (len)
  184. {
  185. size_t to_write = UINT16_MAX - self->output_buffer.len;
  186. if (to_write > len)
  187. {
  188. to_write = len;
  189. need_flush = false;
  190. }
  191. str_append_data (&self->output_buffer, data, to_write);
  192. data = (uint8_t *) data + to_write;
  193. len -= to_write;
  194. if (need_flush)
  195. fcgi_request_flush (self);
  196. }
  197. }
  198. /// Mark the request as done. Returns false if the underlying transport
  199. /// should be closed, this being the last request.
  200. static bool
  201. fcgi_request_finish (struct fcgi_request *self, int32_t status_code)
  202. {
  203. fcgi_request_flush (self);
  204. fcgi_muxer_send (self->muxer, FCGI_STDOUT, self->request_id, NULL, 0);
  205. // The appStatus is mostly ignored by web servers and it's not even clear
  206. // whether it should be a signed value like it is on Unix, or not
  207. fcgi_muxer_send_end_request (self->muxer, self->request_id,
  208. status_code, FCGI_REQUEST_COMPLETE);
  209. bool should_close = !(self->flags & FCGI_KEEP_CONN);
  210. self->muxer->active_requests--;
  211. self->muxer->requests[self->request_id] = NULL;
  212. fcgi_request_destroy (self);
  213. return !should_close;
  214. }
  215. static bool
  216. fcgi_request_push_params
  217. (struct fcgi_request *self, const void *data, size_t len)
  218. {
  219. if (self->state != FCGI_REQUEST_PARAMS)
  220. {
  221. print_debug ("FastCGI: expected %s, got %s",
  222. STRINGIFY (FCGI_STDIN), STRINGIFY (FCGI_PARAMS));
  223. return false;
  224. }
  225. if (len)
  226. fcgi_nv_parser_push (&self->hdr_parser, data, len);
  227. else
  228. {
  229. if (self->hdr_parser.state != FCGI_NV_PARSER_NAME_LEN)
  230. print_debug ("FastCGI: request headers seem to be cut off");
  231. self->state = FCGI_REQUEST_STDIN;
  232. if (!self->muxer->request_start_cb (self))
  233. return fcgi_request_finish (self, EXIT_SUCCESS);
  234. }
  235. return true;
  236. }
  237. static bool
  238. fcgi_request_push_stdin
  239. (struct fcgi_request *self, const void *data, size_t len)
  240. {
  241. if (self->state != FCGI_REQUEST_STDIN)
  242. {
  243. print_debug ("FastCGI: expected %s, got %s",
  244. STRINGIFY (FCGI_PARAMS), STRINGIFY (FCGI_STDIN));
  245. return false;
  246. }
  247. return self->muxer->request_push_cb (self, data, len);
  248. }
  249. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  250. typedef bool (*fcgi_muxer_handler_fn)
  251. (struct fcgi_muxer *, const struct fcgi_parser *);
  252. static bool
  253. fcgi_muxer_on_get_values
  254. (struct fcgi_muxer *self, const struct fcgi_parser *parser)
  255. {
  256. if (parser->request_id != FCGI_NULL_REQUEST_ID)
  257. {
  258. print_debug ("FastCGI: invalid %s message",
  259. STRINGIFY (FCGI_GET_VALUES));
  260. return false;
  261. }
  262. struct str_map values = str_map_make (free);
  263. struct str_map response = str_map_make (free);
  264. struct fcgi_nv_parser nv_parser = fcgi_nv_parser_make ();
  265. nv_parser.output = &values;
  266. fcgi_nv_parser_push (&nv_parser, parser->content.str, parser->content.len);
  267. fcgi_nv_parser_free (&nv_parser);
  268. const char *key = NULL;
  269. // No real-world servers seem to actually use multiplexing
  270. // or even issue this request, but we will implement it anyway
  271. if (str_map_find (&values, (key = FCGI_MPXS_CONNS)))
  272. str_map_set (&response, key, xstrdup ("1"));
  273. // It's not clear whether FCGI_MAX_REQS means concurrently over all
  274. // connections or over just a single connection (multiplexed), though
  275. // supposedly it's actually per /web server/. Supply the strictest limit.
  276. if (str_map_find (&values, (key = FCGI_MAX_REQS)))
  277. str_map_set (&response, key,
  278. xstrdup_printf ("%zu", N_ELEMENTS (self->requests) - 1));
  279. // FCGI_MAX_CONNS would be basically infinity. We don't limit connections.
  280. struct str content = str_make ();
  281. fcgi_nv_convert (&response, &content);
  282. fcgi_muxer_send (self, FCGI_GET_VALUES_RESULT, parser->request_id,
  283. content.str, content.len);
  284. str_free (&content);
  285. str_map_free (&values);
  286. str_map_free (&response);
  287. return true;
  288. }
  289. static bool
  290. fcgi_muxer_on_begin_request
  291. (struct fcgi_muxer *self, const struct fcgi_parser *parser)
  292. {
  293. struct msg_unpacker unpacker =
  294. msg_unpacker_make (parser->content.str, parser->content.len);
  295. uint16_t role;
  296. uint8_t flags;
  297. bool success = true;
  298. success &= msg_unpacker_u16 (&unpacker, &role);
  299. success &= msg_unpacker_u8 (&unpacker, &flags);
  300. // Ignoring 5 reserved bytes
  301. if (!success)
  302. {
  303. print_debug ("FastCGI: invalid %s message",
  304. STRINGIFY (FCGI_BEGIN_REQUEST));
  305. return false;
  306. }
  307. struct fcgi_request *request = self->requests[parser->request_id];
  308. if (parser->request_id == FCGI_NULL_REQUEST_ID || request)
  309. {
  310. print_debug ("FastCGI: unusable request ID in %s message",
  311. STRINGIFY (FCGI_BEGIN_REQUEST));
  312. return false;
  313. }
  314. // We can only act as a responder, reject everything else up front
  315. if (role != FCGI_RESPONDER)
  316. {
  317. fcgi_muxer_send_end_request (self,
  318. parser->request_id, 0, FCGI_UNKNOWN_ROLE);
  319. return true;
  320. }
  321. if (parser->request_id >= N_ELEMENTS (self->requests)
  322. || self->in_shutdown)
  323. {
  324. fcgi_muxer_send_end_request (self,
  325. parser->request_id, 0, FCGI_OVERLOADED);
  326. return true;
  327. }
  328. request = fcgi_request_new ();
  329. request->muxer = self;
  330. request->request_id = parser->request_id;
  331. request->flags = flags;
  332. self->requests[parser->request_id] = request;
  333. self->active_requests++;
  334. return true;
  335. }
  336. static bool
  337. fcgi_muxer_on_abort_request
  338. (struct fcgi_muxer *self, const struct fcgi_parser *parser)
  339. {
  340. struct fcgi_request *request = self->requests[parser->request_id];
  341. if (parser->request_id == FCGI_NULL_REQUEST_ID || !request)
  342. {
  343. print_debug ("FastCGI: received %s for an unknown request",
  344. STRINGIFY (FCGI_ABORT_REQUEST));
  345. return true; // We might have just rejected it
  346. }
  347. return fcgi_request_finish (request, EXIT_FAILURE);
  348. }
  349. static bool
  350. fcgi_muxer_on_params (struct fcgi_muxer *self, const struct fcgi_parser *parser)
  351. {
  352. struct fcgi_request *request = self->requests[parser->request_id];
  353. if (parser->request_id == FCGI_NULL_REQUEST_ID || !request)
  354. {
  355. print_debug ("FastCGI: received %s for an unknown request",
  356. STRINGIFY (FCGI_PARAMS));
  357. return true; // We might have just rejected it
  358. }
  359. // This may immediately finish and delete the request, but that's fine
  360. return fcgi_request_push_params (request,
  361. parser->content.str, parser->content.len);
  362. }
  363. static bool
  364. fcgi_muxer_on_stdin (struct fcgi_muxer *self, const struct fcgi_parser *parser)
  365. {
  366. struct fcgi_request *request = self->requests[parser->request_id];
  367. if (parser->request_id == FCGI_NULL_REQUEST_ID || !request)
  368. {
  369. print_debug ("FastCGI: received %s for an unknown request",
  370. STRINGIFY (FCGI_STDIN));
  371. return true; // We might have just rejected it
  372. }
  373. // At the end of the stream, a zero-length record is received
  374. return fcgi_request_push_stdin (request,
  375. parser->content.str, parser->content.len);
  376. }
  377. static bool
  378. fcgi_muxer_on_message (const struct fcgi_parser *parser, void *user_data)
  379. {
  380. struct fcgi_muxer *self = user_data;
  381. if (parser->version != FCGI_VERSION_1)
  382. {
  383. print_debug ("FastCGI: unsupported version %d", parser->version);
  384. return false;
  385. }
  386. static const fcgi_muxer_handler_fn handlers[] =
  387. {
  388. [FCGI_GET_VALUES] = fcgi_muxer_on_get_values,
  389. [FCGI_BEGIN_REQUEST] = fcgi_muxer_on_begin_request,
  390. [FCGI_ABORT_REQUEST] = fcgi_muxer_on_abort_request,
  391. [FCGI_PARAMS] = fcgi_muxer_on_params,
  392. [FCGI_STDIN] = fcgi_muxer_on_stdin,
  393. };
  394. fcgi_muxer_handler_fn handler;
  395. if (parser->type >= N_ELEMENTS (handlers)
  396. || !(handler = handlers[parser->type]))
  397. {
  398. // Responding in this way even to application records, unspecified
  399. uint8_t content[8] = { parser->type };
  400. fcgi_muxer_send (self, FCGI_UNKNOWN_TYPE, parser->request_id,
  401. content, sizeof content);
  402. return true;
  403. }
  404. return handler (self, parser);
  405. }
  406. static void
  407. fcgi_muxer_init (struct fcgi_muxer *self)
  408. {
  409. self->parser = fcgi_parser_make ();
  410. self->parser.on_message = fcgi_muxer_on_message;
  411. self->parser.user_data = self;
  412. }
  413. static void
  414. fcgi_muxer_free (struct fcgi_muxer *self)
  415. {
  416. for (size_t i = 0; i < N_ELEMENTS (self->requests); i++)
  417. {
  418. if (!self->active_requests)
  419. break;
  420. if (self->requests[i])
  421. {
  422. fcgi_request_destroy (self->requests[i]);
  423. self->active_requests--;
  424. }
  425. }
  426. fcgi_parser_free (&self->parser);
  427. }
  428. static bool
  429. fcgi_muxer_push (struct fcgi_muxer *self, const void *data, size_t len)
  430. {
  431. return fcgi_parser_push (&self->parser, data, len);
  432. }
  433. /// @}
  434. // --- WebSocket ---------------------------------------------------------------
  435. /// @defgroup WebSocket
  436. /// @{
  437. // WebSocket isn't CGI-compatible, therefore we must handle the initial HTTP
  438. // handshake ourselves. Luckily it's not too much of a bother with http-parser.
  439. // Typically there will be a normal HTTP server in front of us, proxying the
  440. // requests based on the URI.
  441. enum ws_handler_state
  442. {
  443. WS_HANDLER_CONNECTING, ///< Parsing HTTP
  444. WS_HANDLER_OPEN, ///< Parsing WebSocket frames
  445. WS_HANDLER_CLOSING, ///< Partial closure by us
  446. WS_HANDLER_FLUSHING, ///< Just waiting for client EOF
  447. WS_HANDLER_CLOSED ///< Dead, both sides closed
  448. };
  449. struct ws_handler
  450. {
  451. enum ws_handler_state state; ///< State
  452. // HTTP handshake:
  453. http_parser hp; ///< HTTP parser
  454. bool have_header_value; ///< Parsing header value or field?
  455. struct str field; ///< Field part buffer
  456. struct str value; ///< Value part buffer
  457. struct str_map headers; ///< HTTP Headers
  458. struct str url; ///< Request URL
  459. ev_timer handshake_timeout_watcher; ///< Handshake timeout watcher
  460. // WebSocket frame protocol:
  461. struct ws_parser parser; ///< Protocol frame parser
  462. bool expecting_continuation; ///< For non-control traffic
  463. enum ws_opcode message_opcode; ///< Opcode for the current message
  464. struct str message_data; ///< Concatenated message data
  465. ev_timer ping_timer; ///< Ping timer
  466. bool received_pong; ///< Received PONG since the last PING
  467. ev_timer close_timeout_watcher; ///< Close timeout watcher
  468. // Configuration:
  469. unsigned handshake_timeout; ///< How long to wait for the handshake
  470. unsigned close_timeout; ///< How long to wait for TCP close
  471. unsigned ping_interval; ///< Ping interval in seconds
  472. uint64_t max_payload_len; ///< Maximum length of any message
  473. // Event callbacks:
  474. // TODO: void (*on_handshake) (protocols) that will allow the user
  475. // to choose any sub-protocol, if the client has provided any.
  476. // This may render "on_connected" unnecessary.
  477. // Should also enable failing the handshake.
  478. /// Called after successfuly connecting (handshake complete)
  479. bool (*on_connected) (struct ws_handler *);
  480. /// Called upon reception of a single full message
  481. bool (*on_message) (struct ws_handler *,
  482. enum ws_opcode type, const void *data, size_t len);
  483. /// The connection is about to close. @a close_code may, or may not, be one
  484. /// of enum ws_status. The @a reason is never NULL.
  485. void (*on_close) (struct ws_handler *, int close_code, const char *reason);
  486. // Virtual method callbacks:
  487. /// Write a chunk of data to the stream
  488. void (*write_cb) (struct ws_handler *, const void *data, size_t len);
  489. /// Close the connection. If @a half_close is false, you are allowed to
  490. /// destroy the handler directly from within the callback.
  491. void (*close_cb) (struct ws_handler *, bool half_close);
  492. };
  493. static void
  494. ws_handler_send_control (struct ws_handler *self,
  495. enum ws_opcode opcode, const void *data, size_t len)
  496. {
  497. if (len > WS_MAX_CONTROL_PAYLOAD_LEN)
  498. {
  499. print_debug ("truncating output control frame payload"
  500. " from %zu to %zu bytes", len, (size_t) WS_MAX_CONTROL_PAYLOAD_LEN);
  501. len = WS_MAX_CONTROL_PAYLOAD_LEN;
  502. }
  503. uint8_t header[2] = { 0x80 | (opcode & 0x0F), len };
  504. self->write_cb (self, header, sizeof header);
  505. self->write_cb (self, data, len);
  506. }
  507. static void
  508. ws_handler_close (struct ws_handler *self,
  509. enum ws_status close_code, const char *reason, size_t len)
  510. {
  511. hard_assert (self->state == WS_HANDLER_OPEN);
  512. struct str payload = str_make ();
  513. str_pack_u16 (&payload, close_code);
  514. // XXX: maybe accept a null-terminated string on input? Has to be UTF-8 a/w
  515. str_append_data (&payload, reason, len);
  516. ws_handler_send_control (self, WS_OPCODE_CLOSE, payload.str, payload.len);
  517. self->close_cb (self, true /* half_close */);
  518. self->state = WS_HANDLER_CLOSING;
  519. str_free (&payload);
  520. }
  521. static bool
  522. ws_handler_fail_connection (struct ws_handler *self, enum ws_status close_code)
  523. {
  524. hard_assert (self->state == WS_HANDLER_OPEN
  525. || self->state == WS_HANDLER_CLOSING);
  526. if (self->state == WS_HANDLER_OPEN)
  527. ws_handler_close (self, close_code, NULL, 0);
  528. self->state = WS_HANDLER_FLUSHING;
  529. if (self->on_close)
  530. self->on_close (self, WS_STATUS_ABNORMAL_CLOSURE, "");
  531. ev_timer_stop (EV_DEFAULT_ &self->ping_timer);
  532. ev_timer_set (&self->close_timeout_watcher, self->close_timeout, 0.);
  533. ev_timer_start (EV_DEFAULT_ &self->close_timeout_watcher);
  534. return false;
  535. }
  536. // TODO: add support for fragmented responses
  537. static void
  538. ws_handler_send_frame (struct ws_handler *self,
  539. enum ws_opcode opcode, const void *data, size_t len)
  540. {
  541. if (!soft_assert (self->state == WS_HANDLER_OPEN))
  542. return;
  543. struct str header = str_make ();
  544. str_pack_u8 (&header, 0x80 | (opcode & 0x0F));
  545. if (len > UINT16_MAX)
  546. {
  547. str_pack_u8 (&header, 127);
  548. str_pack_u64 (&header, len);
  549. }
  550. else if (len > 125)
  551. {
  552. str_pack_u8 (&header, 126);
  553. str_pack_u16 (&header, len);
  554. }
  555. else
  556. str_pack_u8 (&header, len);
  557. self->write_cb (self, header.str, header.len);
  558. self->write_cb (self, data, len);
  559. str_free (&header);
  560. }
  561. static bool
  562. ws_handler_on_frame_header (void *user_data, const struct ws_parser *parser)
  563. {
  564. struct ws_handler *self = user_data;
  565. // Note that we aren't expected to send any close frame before closing the
  566. // connection when the frame is unmasked
  567. if (parser->reserved_1 || parser->reserved_2 || parser->reserved_3
  568. || !parser->is_masked // client -> server payload must be masked
  569. || (ws_is_control_frame (parser->opcode) &&
  570. (!parser->is_fin || parser->payload_len > WS_MAX_CONTROL_PAYLOAD_LEN))
  571. || (!ws_is_control_frame (parser->opcode) &&
  572. (self->expecting_continuation && parser->opcode != WS_OPCODE_CONT))
  573. || parser->payload_len >= 0x8000000000000000ULL)
  574. return ws_handler_fail_connection (self, WS_STATUS_PROTOCOL_ERROR);
  575. if (parser->payload_len > self->max_payload_len
  576. || (self->expecting_continuation &&
  577. self->message_data.len + parser->payload_len > self->max_payload_len))
  578. return ws_handler_fail_connection (self, WS_STATUS_MESSAGE_TOO_BIG);
  579. return true;
  580. }
  581. static bool
  582. ws_handler_on_control_close
  583. (struct ws_handler *self, const struct ws_parser *parser)
  584. {
  585. hard_assert (self->state == WS_HANDLER_OPEN
  586. || self->state == WS_HANDLER_CLOSING);
  587. struct msg_unpacker unpacker =
  588. msg_unpacker_make (parser->input.str, parser->payload_len);
  589. char *reason = NULL;
  590. uint16_t close_code = WS_STATUS_NO_STATUS_RECEIVED;
  591. if (parser->payload_len >= 2)
  592. {
  593. (void) msg_unpacker_u16 (&unpacker, &close_code);
  594. reason = xstrndup (parser->input.str + 2, parser->payload_len - 2);
  595. }
  596. else
  597. reason = xstrdup ("");
  598. if (close_code < 1000 || close_code > 4999)
  599. // XXX: invalid close code: maybe we should fail the connection instead,
  600. // although the specification doesn't say anything about this case
  601. close_code = WS_STATUS_PROTOCOL_ERROR;
  602. // Update the now potentially different close_code (lol const)
  603. if (parser->payload_len >= 2)
  604. {
  605. parser->input.str[0] = close_code >> 8;
  606. parser->input.str[1] = close_code;
  607. }
  608. if (self->state == WS_HANDLER_OPEN)
  609. {
  610. ws_handler_send_control (self, WS_OPCODE_CLOSE,
  611. parser->input.str, parser->payload_len);
  612. self->state = WS_HANDLER_FLUSHING;
  613. if (self->on_close)
  614. self->on_close (self, close_code, reason);
  615. }
  616. else
  617. // Close initiated by us, flush the write queue and close the transport
  618. self->state = WS_HANDLER_FLUSHING;
  619. free (reason);
  620. ev_timer_stop (EV_DEFAULT_ &self->ping_timer);
  621. ev_timer_set (&self->close_timeout_watcher, self->close_timeout, 0.);
  622. ev_timer_start (EV_DEFAULT_ &self->close_timeout_watcher);
  623. return true;
  624. }
  625. static bool
  626. ws_handler_on_control_frame
  627. (struct ws_handler *self, const struct ws_parser *parser)
  628. {
  629. switch (parser->opcode)
  630. {
  631. case WS_OPCODE_CLOSE:
  632. return ws_handler_on_control_close (self, parser);
  633. case WS_OPCODE_PING:
  634. ws_handler_send_control (self, WS_OPCODE_PONG,
  635. parser->input.str, parser->payload_len);
  636. break;
  637. case WS_OPCODE_PONG:
  638. // TODO: check the payload
  639. self->received_pong = true;
  640. break;
  641. default:
  642. // Unknown control frame
  643. return ws_handler_fail_connection (self, WS_STATUS_PROTOCOL_ERROR);
  644. }
  645. return true;
  646. }
  647. static bool
  648. ws_handler_on_frame (void *user_data, const struct ws_parser *parser)
  649. {
  650. struct ws_handler *self = user_data;
  651. if (ws_is_control_frame (parser->opcode))
  652. return ws_handler_on_control_frame (self, parser);
  653. if (!self->expecting_continuation)
  654. self->message_opcode = parser->opcode;
  655. str_append_data (&self->message_data,
  656. parser->input.str, parser->payload_len);
  657. if ((self->expecting_continuation = !parser->is_fin))
  658. return true;
  659. if (self->message_opcode == WS_OPCODE_TEXT
  660. && !utf8_validate (self->message_data.str, self->message_data.len))
  661. {
  662. return ws_handler_fail_connection
  663. (self, WS_STATUS_INVALID_PAYLOAD_DATA);
  664. }
  665. bool result = true;
  666. if (self->on_message)
  667. result = self->on_message (self, self->message_opcode,
  668. self->message_data.str, self->message_data.len);
  669. str_reset (&self->message_data);
  670. // TODO: if (!result), either replace this with a state check,
  671. // or make sure to change the state
  672. return result;
  673. }
  674. static void
  675. ws_handler_on_ping_timer (EV_P_ ev_timer *watcher, int revents)
  676. {
  677. (void) loop;
  678. (void) revents;
  679. struct ws_handler *self = watcher->data;
  680. if (!self->received_pong)
  681. ws_handler_fail_connection (self, 4000 /* private use code */);
  682. else
  683. {
  684. // TODO: be an annoying server and send a nonce in the data
  685. ws_handler_send_control (self, WS_OPCODE_PING, NULL, 0);
  686. ev_timer_again (EV_A_ watcher);
  687. }
  688. }
  689. static void
  690. ws_handler_on_close_timeout (EV_P_ ev_timer *watcher, int revents)
  691. {
  692. (void) loop;
  693. (void) revents;
  694. struct ws_handler *self = watcher->data;
  695. hard_assert (self->state == WS_HANDLER_OPEN
  696. || self->state == WS_HANDLER_CLOSING);
  697. if (self->state == WS_HANDLER_CLOSING
  698. && self->on_close)
  699. self->on_close (self, WS_STATUS_ABNORMAL_CLOSURE, "close timeout");
  700. self->state = WS_HANDLER_CLOSED;
  701. self->close_cb (self, false /* half_close */);
  702. }
  703. static bool ws_handler_fail_handshake (struct ws_handler *self,
  704. const char *status, ...) ATTRIBUTE_SENTINEL;
  705. #define HTTP_101_SWITCHING_PROTOCOLS "101 Switching Protocols"
  706. #define HTTP_400_BAD_REQUEST "400 Bad Request"
  707. #define HTTP_405_METHOD_NOT_ALLOWED "405 Method Not Allowed"
  708. #define HTTP_408_REQUEST_TIMEOUT "408 Request Timeout"
  709. #define HTTP_417_EXPECTATION_FAILED "407 Expectation Failed"
  710. #define HTTP_426_UPGRADE_REQUIRED "426 Upgrade Required"
  711. #define HTTP_505_VERSION_NOT_SUPPORTED "505 HTTP Version Not Supported"
  712. static void
  713. ws_handler_on_handshake_timeout (EV_P_ ev_timer *watcher, int revents)
  714. {
  715. (void) loop;
  716. (void) revents;
  717. struct ws_handler *self = watcher->data;
  718. ws_handler_fail_handshake (self, HTTP_408_REQUEST_TIMEOUT, NULL);
  719. self->state = WS_HANDLER_CLOSED;
  720. self->close_cb (self, false /* half_close */);
  721. }
  722. static void
  723. ws_handler_init (struct ws_handler *self)
  724. {
  725. memset (self, 0, sizeof *self);
  726. self->state = WS_HANDLER_CONNECTING;
  727. http_parser_init (&self->hp, HTTP_REQUEST);
  728. self->hp.data = self;
  729. self->field = str_make ();
  730. self->value = str_make ();
  731. self->headers = str_map_make (free);
  732. self->headers.key_xfrm = tolower_ascii_strxfrm;
  733. self->url = str_make ();
  734. ev_timer_init (&self->handshake_timeout_watcher,
  735. ws_handler_on_handshake_timeout, 0., 0.);
  736. self->handshake_timeout_watcher.data = self;
  737. self->parser = ws_parser_make ();
  738. self->parser.on_frame_header = ws_handler_on_frame_header;
  739. self->parser.on_frame = ws_handler_on_frame;
  740. self->parser.user_data = self;
  741. self->message_data = str_make ();
  742. ev_timer_init (&self->ping_timer,
  743. ws_handler_on_ping_timer, 0., 0.);
  744. self->ping_timer.data = self;
  745. ev_timer_init (&self->close_timeout_watcher,
  746. ws_handler_on_close_timeout, 0., 0.);
  747. self->ping_timer.data = self;
  748. // So that the first ping timer doesn't timeout the connection
  749. self->received_pong = true;
  750. self->handshake_timeout = self->close_timeout = self->ping_interval = 60;
  751. // This is still ridiculously high. Note that the most significant bit
  752. // must always be zero, i.e. the protocol maximum is 0x7FFF FFFF FFFF FFFF.
  753. self->max_payload_len = UINT32_MAX;
  754. }
  755. /// Stop all timers, not going to use the handler anymore
  756. static void
  757. ws_handler_stop (struct ws_handler *self)
  758. {
  759. ev_timer_stop (EV_DEFAULT_ &self->handshake_timeout_watcher);
  760. ev_timer_stop (EV_DEFAULT_ &self->ping_timer);
  761. ev_timer_stop (EV_DEFAULT_ &self->close_timeout_watcher);
  762. }
  763. static void
  764. ws_handler_free (struct ws_handler *self)
  765. {
  766. ws_handler_stop (self);
  767. str_free (&self->field);
  768. str_free (&self->value);
  769. str_map_free (&self->headers);
  770. str_free (&self->url);
  771. ws_parser_free (&self->parser);
  772. str_free (&self->message_data);
  773. }
  774. static bool
  775. ws_handler_header_field_is_a_list (const char *name)
  776. {
  777. // This must contain all header fields we use for anything
  778. static const char *concatenable[] =
  779. { SEC_WS_PROTOCOL, SEC_WS_EXTENSIONS, "Connection", "Upgrade" };
  780. for (size_t i = 0; i < N_ELEMENTS (concatenable); i++)
  781. if (!strcasecmp_ascii (name, concatenable[i]))
  782. return true;
  783. return false;
  784. }
  785. static void
  786. ws_handler_on_header_read (struct ws_handler *self)
  787. {
  788. // The HTTP parser unfolds values and removes preceding whitespace, but
  789. // otherwise doesn't touch the values or the following whitespace.
  790. // RFC 7230 states that trailing whitespace is not part of a field value
  791. char *value = self->field.str;
  792. size_t len = self->field.len;
  793. while (len--)
  794. if (value[len] == '\t' || value[len] == ' ')
  795. value[len] = '\0';
  796. else
  797. break;
  798. self->field.len = len;
  799. const char *field = self->field.str;
  800. const char *current = str_map_find (&self->headers, field);
  801. if (ws_handler_header_field_is_a_list (field) && current)
  802. str_map_set (&self->headers, field,
  803. xstrdup_printf ("%s, %s", current, self->value.str));
  804. else
  805. // If the field cannot be concatenated, just overwrite the last value.
  806. // Maybe we should issue a warning or something.
  807. str_map_set (&self->headers, field, xstrdup (self->value.str));
  808. }
  809. static int
  810. ws_handler_on_header_field (http_parser *parser, const char *at, size_t len)
  811. {
  812. struct ws_handler *self = parser->data;
  813. if (self->have_header_value)
  814. {
  815. ws_handler_on_header_read (self);
  816. str_reset (&self->field);
  817. str_reset (&self->value);
  818. }
  819. str_append_data (&self->field, at, len);
  820. self->have_header_value = false;
  821. return 0;
  822. }
  823. static int
  824. ws_handler_on_header_value (http_parser *parser, const char *at, size_t len)
  825. {
  826. struct ws_handler *self = parser->data;
  827. str_append_data (&self->value, at, len);
  828. self->have_header_value = true;
  829. return 0;
  830. }
  831. static int
  832. ws_handler_on_headers_complete (http_parser *parser)
  833. {
  834. struct ws_handler *self = parser->data;
  835. if (self->have_header_value)
  836. ws_handler_on_header_read (self);
  837. // We require a protocol upgrade. 1 is for "skip body", 2 is the same
  838. // + "stop processing", return another number to indicate a problem here.
  839. if (!parser->upgrade)
  840. return 3;
  841. return 0;
  842. }
  843. static int
  844. ws_handler_on_url (http_parser *parser, const char *at, size_t len)
  845. {
  846. struct ws_handler *self = parser->data;
  847. str_append_data (&self->url, at, len);
  848. return 0;
  849. }
  850. static void
  851. ws_handler_http_responsev (struct ws_handler *self,
  852. const char *status, char *const *fields)
  853. {
  854. hard_assert (status != NULL);
  855. struct str response = str_make ();
  856. str_append_printf (&response, "HTTP/1.1 %s\r\n", status);
  857. while (*fields)
  858. str_append_printf (&response, "%s\r\n", *fields++);
  859. time_t now = time (NULL);
  860. struct tm ts;
  861. gmtime_r (&now, &ts);
  862. // See RFC 7231, 7.1.1.2. Date
  863. const char *dow[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  864. const char *moy[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  865. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  866. str_append_printf (&response,
  867. "Date: %s, %02d %s %04d %02d:%02d:%02d GMT\r\n",
  868. dow[ts.tm_wday], ts.tm_mday, moy[ts.tm_mon], ts.tm_year + 1900,
  869. ts.tm_hour, ts.tm_min, ts.tm_sec);
  870. str_append (&response, "Server: "
  871. PROGRAM_NAME "/" PROGRAM_VERSION "\r\n\r\n");
  872. self->write_cb (self, response.str, response.len);
  873. str_free (&response);
  874. }
  875. static bool
  876. ws_handler_fail_handshake (struct ws_handler *self, const char *status, ...)
  877. {
  878. va_list ap;
  879. va_start (ap, status);
  880. const char *s;
  881. struct strv v = strv_make ();
  882. while ((s = va_arg (ap, const char *)))
  883. strv_append (&v, s);
  884. strv_append (&v, "Connection: close");
  885. va_end (ap);
  886. ws_handler_http_responsev (self, status, v.vector);
  887. strv_free (&v);
  888. self->close_cb (self, true /* half_close */);
  889. self->state = WS_HANDLER_FLUSHING;
  890. if (self->on_close)
  891. self->on_close (self, WS_STATUS_ABNORMAL_CLOSURE, status);
  892. return false;
  893. }
  894. #define FAIL_HANDSHAKE(...) \
  895. return ws_handler_fail_handshake (self, __VA_ARGS__, NULL)
  896. static bool
  897. ws_handler_finish_handshake (struct ws_handler *self)
  898. {
  899. if (self->hp.method != HTTP_GET)
  900. FAIL_HANDSHAKE (HTTP_405_METHOD_NOT_ALLOWED, "Allow: GET");
  901. // Technically, it must be /at least/ 1.1 but no other 1.x version of HTTP
  902. // is going to happen and 2.x is entirely incompatible
  903. // XXX: we probably shouldn't use 505 to reject the minor version but w/e
  904. if (self->hp.http_major != 1 || self->hp.http_minor != 1)
  905. FAIL_HANDSHAKE (HTTP_505_VERSION_NOT_SUPPORTED);
  906. // Your expectations are way too high
  907. if (str_map_find (&self->headers, "Expect"))
  908. FAIL_HANDSHAKE (HTTP_417_EXPECTATION_FAILED);
  909. // Reject URLs specifying the schema and host; we're not parsing that
  910. // TODO: actually do parse this and let our user decide if it matches
  911. struct http_parser_url url;
  912. if (http_parser_parse_url (self->url.str, self->url.len, false, &url)
  913. || (url.field_set & (1 << UF_SCHEMA | 1 << UF_HOST | 1 << UF_PORT))
  914. || !str_map_find (&self->headers, "Host"))
  915. FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST);
  916. const char *connection = str_map_find (&self->headers, "Connection");
  917. if (!connection || strcasecmp_ascii (connection, "Upgrade"))
  918. FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST);
  919. // Check if we can actually upgrade the protocol to WebSocket
  920. const char *upgrade = str_map_find (&self->headers, "Upgrade");
  921. struct http_protocol *offered_upgrades = NULL;
  922. bool can_upgrade = false;
  923. if (upgrade && http_parse_upgrade (upgrade, &offered_upgrades))
  924. // Case-insensitive according to RFC 6455; neither RFC 2616 nor 7230
  925. // say anything at all about case-sensitivity for this field
  926. LIST_FOR_EACH (struct http_protocol, iter, offered_upgrades)
  927. {
  928. if (!iter->version && !strcasecmp_ascii (iter->name, "websocket"))
  929. can_upgrade = true;
  930. http_protocol_destroy (iter);
  931. }
  932. if (!can_upgrade)
  933. FAIL_HANDSHAKE (HTTP_426_UPGRADE_REQUIRED,
  934. "Upgrade: websocket", SEC_WS_VERSION ": 13");
  935. // Okay, we're finally past the basic HTTP/1.1 stuff
  936. const char *key = str_map_find (&self->headers, SEC_WS_KEY);
  937. const char *version = str_map_find (&self->headers, SEC_WS_VERSION);
  938. /*
  939. const char *protocol = str_map_find (&self->headers, SEC_WS_PROTOCOL);
  940. const char *extensions = str_map_find (&self->headers, SEC_WS_EXTENSIONS);
  941. */
  942. if (!version)
  943. FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST);
  944. if (strcmp (version, "13"))
  945. FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST, SEC_WS_VERSION ": 13");
  946. struct str tmp = str_make ();
  947. bool key_is_valid = key
  948. && base64_decode (key, false, &tmp) && tmp.len == 16;
  949. str_free (&tmp);
  950. if (!key_is_valid)
  951. FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST);
  952. struct strv fields = strv_make ();
  953. strv_append_args (&fields,
  954. "Upgrade: websocket",
  955. "Connection: Upgrade",
  956. NULL);
  957. char *response_key = ws_encode_response_key (key);
  958. strv_append_owned (&fields,
  959. xstrdup_printf (SEC_WS_ACCEPT ": %s", response_key));
  960. free (response_key);
  961. // TODO: make it possible to choose Sec-Websocket-{Extensions,Protocol}
  962. ws_handler_http_responsev (self,
  963. HTTP_101_SWITCHING_PROTOCOLS, fields.vector);
  964. strv_free (&fields);
  965. self->state = WS_HANDLER_OPEN;
  966. ev_timer_init (&self->ping_timer, ws_handler_on_ping_timer,
  967. self->ping_interval, 0);
  968. ev_timer_start (EV_DEFAULT_ &self->ping_timer);
  969. return true;
  970. }
  971. /// Tells the handler that the TCP connection has been established so it can
  972. /// timeout when the client handshake doesn't arrive soon enough
  973. static void
  974. ws_handler_start (struct ws_handler *self)
  975. {
  976. hard_assert (self->state == WS_HANDLER_CONNECTING);
  977. ev_timer_set (&self->handshake_timeout_watcher,
  978. self->handshake_timeout, 0.);
  979. ev_timer_start (EV_DEFAULT_ &self->handshake_timeout_watcher);
  980. }
  981. // The client should normally never close the connection, assume that it's
  982. // either received an EOF from our side, or that it doesn't care about our data
  983. // anymore, having called close() already
  984. static bool
  985. ws_handler_push_eof (struct ws_handler *self)
  986. {
  987. switch (self->state)
  988. {
  989. case WS_HANDLER_CONNECTING:
  990. ev_timer_stop (EV_DEFAULT_ &self->handshake_timeout_watcher);
  991. self->state = WS_HANDLER_FLUSHING;
  992. if (self->on_close)
  993. self->on_close (self, WS_STATUS_ABNORMAL_CLOSURE, "unexpected EOF");
  994. break;
  995. case WS_HANDLER_OPEN:
  996. ev_timer_stop (EV_DEFAULT_ &self->ping_timer);
  997. // Fall-through
  998. case WS_HANDLER_CLOSING:
  999. self->state = WS_HANDLER_CLOSED;
  1000. if (self->on_close)
  1001. self->on_close (self, WS_STATUS_ABNORMAL_CLOSURE, "");
  1002. // Fall-through
  1003. case WS_HANDLER_FLUSHING:
  1004. ev_timer_stop (EV_DEFAULT_ &self->close_timeout_watcher);
  1005. break;
  1006. default:
  1007. soft_assert(self->state != WS_HANDLER_CLOSED);
  1008. }
  1009. self->state = WS_HANDLER_CLOSED;
  1010. return false;
  1011. }
  1012. /// Push data to the WebSocket handler. "len == 0" means EOF.
  1013. /// You are expected to close the connection and dispose of the handler
  1014. /// when the function returns false.
  1015. static bool
  1016. ws_handler_push (struct ws_handler *self, const void *data, size_t len)
  1017. {
  1018. if (!len)
  1019. return ws_handler_push_eof (self);
  1020. if (self->state == WS_HANDLER_FLUSHING)
  1021. // We're waiting for an EOF from the client, must not process data
  1022. return true;
  1023. if (self->state != WS_HANDLER_CONNECTING)
  1024. return soft_assert (self->state != WS_HANDLER_CLOSED)
  1025. && ws_parser_push (&self->parser, data, len);
  1026. // The handshake hasn't been done yet, process HTTP headers
  1027. static const http_parser_settings http_settings =
  1028. {
  1029. .on_header_field = ws_handler_on_header_field,
  1030. .on_header_value = ws_handler_on_header_value,
  1031. .on_headers_complete = ws_handler_on_headers_complete,
  1032. .on_url = ws_handler_on_url,
  1033. };
  1034. size_t n_parsed =
  1035. http_parser_execute (&self->hp, &http_settings, data, len);
  1036. if (self->hp.upgrade)
  1037. {
  1038. ev_timer_stop (EV_DEFAULT_ &self->handshake_timeout_watcher);
  1039. // The handshake hasn't been finished, yet there is more data
  1040. // to be processed after the headers already
  1041. if (len - n_parsed)
  1042. FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST);
  1043. if (!ws_handler_finish_handshake (self))
  1044. return false;
  1045. if (self->on_connected)
  1046. return self->on_connected (self);
  1047. return true;
  1048. }
  1049. enum http_errno err = HTTP_PARSER_ERRNO (&self->hp);
  1050. if (n_parsed != len || err != HPE_OK)
  1051. {
  1052. ev_timer_stop (EV_DEFAULT_ &self->handshake_timeout_watcher);
  1053. if (err == HPE_CB_headers_complete)
  1054. {
  1055. print_debug ("WS handshake failed: %s", "missing `Upgrade' field");
  1056. FAIL_HANDSHAKE (HTTP_426_UPGRADE_REQUIRED,
  1057. "Upgrade: websocket", SEC_WS_VERSION ": 13");
  1058. }
  1059. print_debug ("WS handshake failed: %s", http_errno_description (err));
  1060. FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST);
  1061. }
  1062. return true;
  1063. }
  1064. /// @}
  1065. // --- Server ------------------------------------------------------------------
  1066. static struct simple_config_item g_config_table[] =
  1067. {
  1068. { "bind_host", NULL, "Address of the server" },
  1069. { "port_fastcgi", "9000", "Port to bind for FastCGI" },
  1070. { "port_scgi", NULL, "Port to bind for SCGI" },
  1071. { "port_ws", NULL, "Port to bind for WebSocket" },
  1072. { "pid_file", NULL, "Full path for the PID file" },
  1073. // XXX: here belongs something like a web SPA that interfaces with us
  1074. { "static_root", NULL, "The root for static content" },
  1075. { NULL, NULL, NULL }
  1076. };
  1077. struct server_context
  1078. {
  1079. ev_signal sigterm_watcher; ///< Got SIGTERM
  1080. ev_signal sigint_watcher; ///< Got SIGINT
  1081. ev_timer quit_timeout_watcher; ///< Quit timeout watcher
  1082. bool quitting; ///< User requested quitting
  1083. struct listener *listeners; ///< Listeners
  1084. size_t n_listeners; ///< Number of listening sockets
  1085. struct client *clients; ///< Clients
  1086. unsigned n_clients; ///< Current number of connections
  1087. struct request_handler *handlers; ///< Request handlers
  1088. struct str_map config; ///< Server configuration
  1089. };
  1090. static void initiate_quit (struct server_context *self);
  1091. static void try_finish_quit (struct server_context *self);
  1092. static void on_quit_timeout (EV_P_ ev_timer *watcher, int revents);
  1093. static void close_listeners (struct server_context *self);
  1094. static void
  1095. server_context_init (struct server_context *self)
  1096. {
  1097. memset (self, 0, sizeof *self);
  1098. self->config = str_map_make (NULL);
  1099. simple_config_load_defaults (&self->config, g_config_table);
  1100. ev_timer_init (&self->quit_timeout_watcher, on_quit_timeout, 3., 0.);
  1101. self->quit_timeout_watcher.data = self;
  1102. }
  1103. static void
  1104. server_context_free (struct server_context *self)
  1105. {
  1106. // We really shouldn't attempt a quit without closing the clients first
  1107. soft_assert (!self->clients);
  1108. close_listeners (self);
  1109. free (self->listeners);
  1110. str_map_free (&self->config);
  1111. }
  1112. // --- JSON-RPC ----------------------------------------------------------------
  1113. /// @defgroup JSON-RPC
  1114. /// @{
  1115. #define JSON_RPC_ERROR_TABLE(XX) \
  1116. XX (-32700, PARSE_ERROR, "Parse error") \
  1117. XX (-32600, INVALID_REQUEST, "Invalid Request") \
  1118. XX (-32601, METHOD_NOT_FOUND, "Method not found") \
  1119. XX (-32602, INVALID_PARAMS, "Invalid params") \
  1120. XX (-32603, INTERNAL_ERROR, "Internal error")
  1121. enum json_rpc_error
  1122. {
  1123. #define XX(code, name, message) JSON_RPC_ERROR_ ## name,
  1124. JSON_RPC_ERROR_TABLE (XX)
  1125. #undef XX
  1126. JSON_RPC_ERROR_COUNT
  1127. };
  1128. static json_t *
  1129. json_rpc_error (enum json_rpc_error id, json_t *data)
  1130. {
  1131. #define XX(code, name, message) { code, message },
  1132. static const struct json_rpc_error
  1133. {
  1134. int code;
  1135. const char *message;
  1136. }
  1137. errors[JSON_RPC_ERROR_COUNT] =
  1138. {
  1139. JSON_RPC_ERROR_TABLE (XX)
  1140. };
  1141. #undef XX
  1142. json_t *error = json_object ();
  1143. json_object_set_new (error, "code", json_integer (errors[id].code));
  1144. json_object_set_new (error, "message", json_string (errors[id].message));
  1145. if (data)
  1146. json_object_set_new (error, "data", data);
  1147. return error;
  1148. }
  1149. static json_t *
  1150. json_rpc_response (json_t *id, json_t *result, json_t *error)
  1151. {
  1152. json_t *x = json_object ();
  1153. json_object_set_new (x, "jsonrpc", json_string ("2.0"));
  1154. json_object_set_new (x, "id", id ? id : json_null ());
  1155. if (result) json_object_set_new (x, "result", result);
  1156. if (error) json_object_set_new (x, "error", error);
  1157. return x;
  1158. }
  1159. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1160. static bool
  1161. validate_json_rpc_content_type (const char *content_type)
  1162. {
  1163. char *type = NULL;
  1164. char *subtype = NULL;
  1165. struct str_map parameters = str_map_make (free);
  1166. parameters.key_xfrm = tolower_ascii_strxfrm;
  1167. bool result = http_parse_media_type
  1168. (content_type, &type, &subtype, &parameters);
  1169. if (!result)
  1170. goto end;
  1171. if (strcasecmp_ascii (type, "application")
  1172. || (strcasecmp_ascii (subtype, "json") &&
  1173. strcasecmp_ascii (subtype, "json-rpc" /* obsolete */)))
  1174. result = false;
  1175. const char *charset = str_map_find (&parameters, "charset");
  1176. if (charset && strcasecmp_ascii (charset, "UTF-8"))
  1177. result = false;
  1178. // Currently ignoring all unknown parametrs
  1179. end:
  1180. free (type);
  1181. free (subtype);
  1182. str_map_free (&parameters);
  1183. return result;
  1184. }
  1185. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1186. /// Handlers must not set the `id` field in their responses, that will be filled
  1187. /// in automatically according to whether the request is a notification or not.
  1188. typedef json_t *(*json_rpc_handler_fn) (struct server_context *, json_t *);
  1189. struct json_rpc_handler_info
  1190. {
  1191. const char *method_name; ///< JSON-RPC method name
  1192. json_rpc_handler_fn handler; ///< Method handler
  1193. };
  1194. static int
  1195. json_rpc_handler_info_cmp (const void *first, const void *second)
  1196. {
  1197. return strcmp (((struct json_rpc_handler_info *) first)->method_name,
  1198. ((struct json_rpc_handler_info *) second)->method_name);
  1199. }
  1200. static json_t *
  1201. open_rpc_describe (const char *method, json_t *result)
  1202. {
  1203. return json_pack ("{sssoso}", "name", method, "params", json_pack ("[]"),
  1204. "result", json_pack ("{ssso}", "name", method, "schema", result));
  1205. }
  1206. // This server rarely sees changes and we can afford to hardcode the schema
  1207. static json_t *
  1208. json_rpc_discover (struct server_context *ctx, json_t *params)
  1209. {
  1210. (void) ctx;
  1211. (void) params;
  1212. json_t *info = json_pack ("{ssss}",
  1213. "title", PROGRAM_NAME, "version", PROGRAM_VERSION);
  1214. json_t *methods = json_pack ("[ooo]",
  1215. open_rpc_describe ("date", json_pack ("{ssso}", "type", "object",
  1216. "properties", json_pack ("{s{ss}s{ss}s{ss}s{ss}s{ss}s{ss}}",
  1217. "year", "type", "number",
  1218. "month", "type", "number",
  1219. "day", "type", "number",
  1220. "hours", "type", "number",
  1221. "minutes", "type", "number",
  1222. "seconds", "type", "number"))),
  1223. open_rpc_describe ("ping", json_pack ("{ss}", "type", "string")),
  1224. open_rpc_describe ("rpc.discover", json_pack ("{ss}", "$ref",
  1225. "https://github.com/open-rpc/meta-schema/raw/master/schema.json")));
  1226. return json_rpc_response (NULL, json_pack ("{sssoso}",
  1227. "openrpc", "1.2.6", "info", info, "methods", methods), NULL);
  1228. }
  1229. static json_t *
  1230. json_rpc_ping (struct server_context *ctx, json_t *params)
  1231. {
  1232. (void) ctx;
  1233. if (params && !json_is_null (params))
  1234. return json_rpc_response (NULL, NULL,
  1235. json_rpc_error (JSON_RPC_ERROR_INVALID_PARAMS, NULL));
  1236. return json_rpc_response (NULL, json_string ("pong"), NULL);
  1237. }
  1238. static json_t *
  1239. json_rpc_date (struct server_context *ctx, json_t *params)
  1240. {
  1241. (void) ctx;
  1242. if (params && !json_is_null (params))
  1243. return json_rpc_response (NULL, NULL,
  1244. json_rpc_error (JSON_RPC_ERROR_INVALID_PARAMS, NULL));
  1245. time_t now = time (NULL);
  1246. const struct tm *tm = localtime (&now);
  1247. json_t *x = json_object ();
  1248. json_object_set_new (x, "year", json_integer (tm->tm_year + 1900));
  1249. json_object_set_new (x, "month", json_integer (tm->tm_mon + 1));
  1250. json_object_set_new (x, "day", json_integer (tm->tm_mday));
  1251. json_object_set_new (x, "hours", json_integer (tm->tm_hour));
  1252. json_object_set_new (x, "minutes", json_integer (tm->tm_min));
  1253. json_object_set_new (x, "seconds", json_integer (tm->tm_sec));
  1254. return json_rpc_response (NULL, x, NULL);
  1255. }
  1256. static json_t *
  1257. process_json_rpc_request (struct server_context *ctx, json_t *request)
  1258. {
  1259. // A list of all available methods; this list has to be ordered.
  1260. // Eventually it might be better to move this into a map in the context.
  1261. static struct json_rpc_handler_info handlers[] =
  1262. {
  1263. { "date", json_rpc_date },
  1264. { "ping", json_rpc_ping },
  1265. { "rpc.discover", json_rpc_discover },
  1266. };
  1267. if (!json_is_object (request))
  1268. return json_rpc_response (NULL, NULL,
  1269. json_rpc_error (JSON_RPC_ERROR_INVALID_REQUEST, NULL));
  1270. json_t *v = json_object_get (request, "jsonrpc");
  1271. json_t *m = json_object_get (request, "method");
  1272. json_t *params = json_object_get (request, "params");
  1273. json_t *id = json_object_get (request, "id");
  1274. const char *version;
  1275. const char *method;
  1276. bool ok = true;
  1277. ok &= v && (version = json_string_value (v)) && !strcmp (version, "2.0");
  1278. ok &= m && (method = json_string_value (m));
  1279. ok &= !params || json_is_array (params) || json_is_object (params);
  1280. ok &= !id || json_is_null (id) ||
  1281. json_is_string (id) || json_is_number (id);
  1282. if (!ok)
  1283. return json_rpc_response (id, NULL,
  1284. json_rpc_error (JSON_RPC_ERROR_INVALID_REQUEST, NULL));
  1285. struct json_rpc_handler_info key = { .method_name = method };
  1286. struct json_rpc_handler_info *handler = bsearch (&key, handlers,
  1287. N_ELEMENTS (handlers), sizeof key, json_rpc_handler_info_cmp);
  1288. if (!handler)
  1289. return json_rpc_response (id, NULL,
  1290. json_rpc_error (JSON_RPC_ERROR_METHOD_NOT_FOUND, NULL));
  1291. json_t *response = handler->handler (ctx, params);
  1292. if (id)
  1293. {
  1294. (void) json_object_set (response, "id", id);
  1295. return response;
  1296. }
  1297. // Notifications don't get responses
  1298. json_decref (response);
  1299. return NULL;
  1300. }
  1301. static void
  1302. flush_json (json_t *json, struct str *output)
  1303. {
  1304. char *utf8 = json_dumps (json, JSON_ENCODE_ANY);
  1305. str_append (output, utf8);
  1306. free (utf8);
  1307. json_decref (json);
  1308. }
  1309. static void
  1310. process_json_rpc (struct server_context *ctx,
  1311. const void *data, size_t len, struct str *output)
  1312. {
  1313. json_error_t e;
  1314. json_t *request;
  1315. if (!(request = json_loadb (data, len, JSON_DECODE_ANY, &e)))
  1316. {
  1317. flush_json (json_rpc_response (NULL, NULL,
  1318. json_rpc_error (JSON_RPC_ERROR_PARSE_ERROR, NULL)),
  1319. output);
  1320. return;
  1321. }
  1322. if (json_is_array (request))
  1323. {
  1324. if (!json_array_size (request))
  1325. {
  1326. flush_json (json_rpc_response (NULL, NULL,
  1327. json_rpc_error (JSON_RPC_ERROR_INVALID_REQUEST, NULL)),
  1328. output);
  1329. return;
  1330. }
  1331. json_t *response = json_array ();
  1332. json_t *iter;
  1333. size_t i;
  1334. json_array_foreach (request, i, iter)
  1335. {
  1336. json_t *result = process_json_rpc_request (ctx, iter);
  1337. if (result)
  1338. json_array_append_new (response, result);
  1339. }
  1340. if (json_array_size (response))
  1341. flush_json (response, output);
  1342. else
  1343. json_decref (response);
  1344. }
  1345. else
  1346. {
  1347. json_t *result = process_json_rpc_request (ctx, request);
  1348. if (result)
  1349. flush_json (result, output);
  1350. }
  1351. }
  1352. /// @}
  1353. // --- Requests ----------------------------------------------------------------
  1354. /// @defgroup Requests
  1355. /// @{
  1356. /// A generic CGI request abstraction, writing data indirectly through callbacks
  1357. struct request
  1358. {
  1359. struct server_context *ctx; ///< Server context
  1360. struct request_handler *handler; ///< Assigned request handler
  1361. void *handler_data; ///< User data for the handler
  1362. /// Callback to write some CGI response data to the output
  1363. void (*write_cb) (struct request *, const void *data, size_t len);
  1364. /// Callback to close the CGI response, simulates end of program execution.
  1365. /// CALLING THIS MAY CAUSE THE REQUEST TO BE DESTROYED.
  1366. void (*finish_cb) (struct request *);
  1367. };
  1368. /// An interface to detect and handle specific kinds of CGI requests.
  1369. /// The server walks through a list of them until it finds one that can serve
  1370. /// a particular request. If unsuccessful, the remote client gets a 404
  1371. /// (the default handling).
  1372. struct request_handler
  1373. {
  1374. LIST_HEADER (struct request_handler)
  1375. /// Install ourselves as the handler for the request, if applicable.
  1376. /// If the request contains data, check it against CONTENT_LENGTH.
  1377. /// ("Transfer-Encoding: chunked" should be dechunked by the HTTP server,
  1378. /// however it is possible that it mishandles this situation.)
  1379. /// Sets @a continue_ to false if further processing should be stopped,
  1380. /// meaning the request has already been handled.
  1381. /// Note that starting the response before receiving all data denies you
  1382. /// the option of returning error status codes based on the data.
  1383. bool (*try_handle) (struct request *request,
  1384. struct str_map *headers, bool *continue_);
  1385. /// Handle incoming data. "len == 0" means EOF.
  1386. /// Returns false if there is no more processing to be done.
  1387. /// EOF is never delivered on a network error (see client_read_loop()).
  1388. // XXX: the EOF may or may not be delivered when the request is cut short:
  1389. // - client_scgi delivers an EOF when it itself receives an EOF without
  1390. // considering any mismatch, and it can deliver another one earlier
  1391. // when the counter just goes down to 0... depends on what we return
  1392. // from here upon the first occasion (whether we want to close).
  1393. // - FCGI_ABORT_REQUEST /might/ not close the stdin and it /might/ cover
  1394. // a CONTENT_LENGTH mismatch, since this callback wouldn't get invoked.
  1395. // The FastCGI specification explicitly says to compare CONTENT_LENGTH
  1396. // against the number of received bytes, which may only be smaller.
  1397. //
  1398. // We might want to adjust client_scgi and client_fcgi to not invoke
  1399. // request_push(EOF) when CONTENT_LENGTH hasn't been reached and remove
  1400. // the extra EOF generation from client_scgi (why is it there, does the
  1401. // server keep the connection open, or is it just a precaution?)
  1402. //
  1403. // The finalization callback takes care of any needs to destruct data.
  1404. // If we handle this reliably in all clients, try_handle won't have to,
  1405. // as it will run in a stricter-than-CGI scenario.
  1406. bool (*push_cb) (struct request *request, const void *data, size_t len);
  1407. /// Destroy the handler's data stored in the request object
  1408. void (*finalize_cb) (struct request *request);
  1409. };
  1410. static void
  1411. request_init (struct request *self)
  1412. {
  1413. memset (self, 0, sizeof *self);
  1414. }
  1415. static void
  1416. request_free (struct request *self)
  1417. {
  1418. if (self->handler)
  1419. self->handler->finalize_cb (self);
  1420. }
  1421. /// Write request CGI response data, intended for use by request handlers
  1422. static void
  1423. request_write (struct request *self, const void *data, size_t len)
  1424. {
  1425. self->write_cb (self, data, len);
  1426. }
  1427. /// This function is only intended to be run from asynchronous event handlers
  1428. /// such as timers, not as a direct result of starting the request or receiving
  1429. /// request data. CALLING THIS MAY CAUSE THE REQUEST TO BE DESTROYED.
  1430. static void
  1431. request_finish (struct request *self)
  1432. {
  1433. self->finish_cb (self);
  1434. }
  1435. /// Starts processing a request. Returns false if no further action is to be
  1436. /// done and the request should be finished.
  1437. static bool
  1438. request_start (struct request *self, struct str_map *headers)
  1439. {
  1440. // XXX: it feels like this should rather be two steps:
  1441. // bool (*can_handle) (request *, headers)
  1442. // ... install the handler ...
  1443. // bool (*handle) (request *)
  1444. //
  1445. // However that might cause some stuff to be done twice.
  1446. //
  1447. // Another way we could get rid of the continue_ argument is via adding
  1448. // some way of marking the request as finished from within the handler.
  1449. if (g_debug_mode)
  1450. {
  1451. struct str_map_iter iter = str_map_iter_make (headers);
  1452. const char *value;
  1453. while ((value = str_map_iter_next (&iter)))
  1454. print_debug ("%s: %s", iter.link->key, value);
  1455. print_debug ("--");
  1456. }
  1457. bool continue_ = true;
  1458. LIST_FOR_EACH (struct request_handler, handler, self->ctx->handlers)
  1459. if (handler->try_handle (self, headers, &continue_))
  1460. {
  1461. self->handler = handler;
  1462. return continue_;
  1463. }
  1464. // Unable to serve the request
  1465. struct str response = str_make ();
  1466. str_append (&response, "Status: 404 Not Found\n");
  1467. str_append (&response, "Content-Type: text/plain\n\n");
  1468. request_write (self, response.str, response.len);
  1469. str_free (&response);
  1470. return false;
  1471. }
  1472. static bool
  1473. request_push (struct request *self, const void *data, size_t len)
  1474. {
  1475. if (!soft_assert (self->handler))
  1476. // No handler, nothing to do with any data
  1477. return false;
  1478. return self->handler->push_cb (self, data, len);
  1479. }
  1480. /// @}
  1481. // --- Requests handlers -------------------------------------------------------
  1482. static bool
  1483. request_handler_json_rpc_try_handle
  1484. (struct request *request, struct str_map *headers, bool *continue_)
  1485. {
  1486. const char *content_type = str_map_find (headers, "CONTENT_TYPE");
  1487. const char *method = str_map_find (headers, "REQUEST_METHOD");
  1488. if (!method || strcmp (method, "POST")
  1489. || !content_type || !validate_json_rpc_content_type (content_type))
  1490. return false;
  1491. struct str *buf = xcalloc (1, sizeof *buf);
  1492. *buf = str_make ();
  1493. request->handler_data = buf;
  1494. *continue_ = true;
  1495. return true;
  1496. }
  1497. static bool
  1498. request_handler_json_rpc_push
  1499. (struct request *request, const void *data, size_t len)
  1500. {
  1501. struct str *buf = request->handler_data;
  1502. if (len)
  1503. {
  1504. str_append_data (buf, data, len);
  1505. return true;
  1506. }
  1507. // TODO: check buf.len against CONTENT_LENGTH; if it's less, then the
  1508. // client hasn't been successful in transferring all of its data.
  1509. // See also comment on request_handler::push_cb. For JSON-RPC, though,
  1510. // it shouldn't matter as an incomplete request will be invalid and
  1511. // clients have no reason to append unnecessary trailing bytes.
  1512. struct str response = str_make ();
  1513. str_append (&response, "Status: 200 OK\n");
  1514. str_append_printf (&response, "Content-Type: %s\n\n", "application/json");
  1515. process_json_rpc (request->ctx, buf->str, buf->len, &response);
  1516. request_write (request, response.str, response.len);
  1517. str_free (&response);
  1518. return false;
  1519. }
  1520. static void
  1521. request_handler_json_rpc_finalize (struct request *request)
  1522. {
  1523. struct str *buf = request->handler_data;
  1524. str_free (buf);
  1525. free (buf);
  1526. request->handler_data = NULL;
  1527. }
  1528. struct request_handler g_request_handler_json_rpc =
  1529. {
  1530. .try_handle = request_handler_json_rpc_try_handle,
  1531. .push_cb = request_handler_json_rpc_push,
  1532. .finalize_cb = request_handler_json_rpc_finalize,
  1533. };
  1534. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1535. /// Make a URL path canonical. The resulting path always begins with a slash,
  1536. /// and any trailing slashes are lost.
  1537. static char *
  1538. canonicalize_url_path (const char *path)
  1539. {
  1540. struct strv v = strv_make ();
  1541. cstr_split (path, "/", true, &v);
  1542. struct strv canonical = strv_make ();
  1543. strv_append (&canonical, "");
  1544. for (size_t i = 0; i < v.len; i++)
  1545. {
  1546. const char *dir = v.vector[i];
  1547. if (!strcmp (dir, "."))
  1548. continue;
  1549. if (strcmp (dir, ".."))
  1550. strv_append (&canonical, dir);
  1551. else if (canonical.len > 1)
  1552. // ".." never goes above the root
  1553. strv_remove (&canonical, canonical.len - 1);
  1554. }
  1555. strv_free (&v);
  1556. char *joined = strv_join (&canonical, "/");
  1557. strv_free (&canonical);
  1558. return joined;
  1559. }
  1560. static char *
  1561. detect_magic (const void *data, size_t len)
  1562. {
  1563. magic_t cookie;
  1564. char *mime_type = NULL;
  1565. if (!(cookie = magic_open (MAGIC_MIME)))
  1566. return NULL;
  1567. const char *magic = NULL;
  1568. if (!magic_load (cookie, NULL)
  1569. && (magic = magic_buffer (cookie, data, len)))
  1570. mime_type = xstrdup (magic);
  1571. else
  1572. print_debug ("MIME type detection failed: %s", magic_error (cookie));
  1573. magic_close (cookie);
  1574. return mime_type;
  1575. }
  1576. static bool
  1577. request_handler_static_try_handle
  1578. (struct request *request, struct str_map *headers, bool *continue_)
  1579. {
  1580. // Serving static files is actually quite complicated as it turns out;
  1581. // but this is only meant to serve a few tiny text files
  1582. struct server_context *ctx = request->ctx;
  1583. const char *root = str_map_find (&ctx->config, "static_root");
  1584. if (!root)
  1585. {
  1586. print_debug ("static document root not configured");
  1587. return false;
  1588. }
  1589. // TODO: implement HEAD, we don't get that for free;
  1590. // probably implies adding Content-Length
  1591. const char *method = str_map_find (headers, "REQUEST_METHOD");
  1592. if (!method || strcmp (method, "GET"))
  1593. return false;
  1594. // TODO: look at <SCRIPT_NAME, PATH_INFO>, REQUEST_URI in the headers
  1595. const char *path_info = str_map_find (headers, "PATH_INFO");
  1596. if (!path_info)
  1597. path_info = str_map_find (headers, "REQUEST_URI");
  1598. if (!path_info)
  1599. {
  1600. print_debug ("neither PATH_INFO nor REQUEST_URI was defined");
  1601. return false;
  1602. }
  1603. // We need to filter the path to stay in our root
  1604. // Being able to read /etc/passwd would be rather embarrasing
  1605. char *suffix = canonicalize_url_path (path_info);
  1606. char *path = xstrdup_printf ("%s%s", root, suffix);
  1607. print_debug ("trying to statically serve %s", path);
  1608. FILE *fp = fopen (path, "rb");
  1609. struct stat st = {};
  1610. if (fp && !fstat (fileno (fp), &st) && !S_ISREG (st.st_mode))
  1611. {
  1612. fclose (fp);
  1613. fp = NULL;
  1614. }
  1615. if (!fp)
  1616. {
  1617. struct str response = str_make ();
  1618. str_append (&response, "Status: 404 Not Found\n");
  1619. str_append (&response, "Content-Type: text/plain\n\n");
  1620. str_append_printf (&response,
  1621. "File %s was not found on this server\n", suffix);
  1622. request_write (request, response.str, response.len);
  1623. str_free (&response);
  1624. free (suffix);
  1625. free (path);
  1626. return false;
  1627. }
  1628. free (suffix);
  1629. free (path);
  1630. uint8_t buf[8192];
  1631. size_t len;
  1632. // Try to detect the Content-Type from the actual contents
  1633. char *mime_type = NULL;
  1634. if ((len = fread (buf, 1, sizeof buf, fp)))
  1635. mime_type = detect_magic (buf, len);
  1636. if (!mime_type)
  1637. mime_type = xstrdup ("application/octet_stream");
  1638. struct str response = str_make ();
  1639. str_append (&response, "Status: 200 OK\n");
  1640. str_append_printf (&response, "Content-Type: %s\n\n", mime_type);
  1641. request_write (request, response.str, response.len);
  1642. str_free (&response);
  1643. free (mime_type);
  1644. // Write the chunk we've used to help us with magic detection;
  1645. // obviously we have to do it after we've written the headers
  1646. if (len)
  1647. request_write (request, buf, len);
  1648. while ((len = fread (buf, 1, sizeof buf, fp)))
  1649. request_write (request, buf, len);
  1650. fclose (fp);
  1651. // TODO: this should rather not be returned all at once but in chunks
  1652. // (consider Transfer-Encoding); file read requests never return EAGAIN
  1653. // TODO: actual file data should really be returned by a callback when
  1654. // the socket is writable with nothing to be sent (pumping the entire
  1655. // file all at once won't really work if it's huge).
  1656. *continue_ = false;
  1657. return true;
  1658. }
  1659. static bool
  1660. request_handler_static_push
  1661. (struct request *request, const void *data, size_t len)
  1662. {
  1663. (void) request;
  1664. (void) data;
  1665. if (len == 0)
  1666. return true;
  1667. // Aborting on content; we shouldn't receive any (GET).
  1668. // In fact, we will only get here once try_handle stops dumping everything
  1669. // into the write queue at once.
  1670. print_debug ("the static file handler received data but shouldn't have");
  1671. return false;
  1672. }
  1673. static void
  1674. request_handler_static_finalize (struct request *request)
  1675. {
  1676. (void) request;
  1677. // Nothing to dispose of this far
  1678. }
  1679. struct request_handler g_request_handler_static =
  1680. {
  1681. .try_handle = request_handler_static_try_handle,
  1682. .push_cb = request_handler_static_push,
  1683. .finalize_cb = request_handler_static_finalize,
  1684. };
  1685. // --- Client communication handlers -------------------------------------------
  1686. /// A virtual class for client connections coming either from the web server
  1687. /// or directly from the end-client, depending on the protocol in use
  1688. struct client
  1689. {
  1690. LIST_HEADER (struct client)
  1691. struct client_vtable *vtable; ///< Client behaviour
  1692. int socket_fd; ///< The network socket
  1693. bool received_eof; ///< Whether EOF has been received yet
  1694. bool flushing; ///< No more data to write, send FIN
  1695. bool closing; ///< No more data to read or write
  1696. bool half_closed; ///< Conn. half-closed while flushing
  1697. struct write_queue write_queue; ///< Write queue
  1698. ev_timer close_timeout_watcher; ///< Write queue flush timer
  1699. ev_io read_watcher; ///< The socket can be read from
  1700. ev_io write_watcher; ///< The socket can be written to
  1701. };
  1702. /// The concrete behaviour to serve a particular client's requests
  1703. struct client_vtable
  1704. {
  1705. /// Process incoming data; "len == 0" means EOF.
  1706. /// If the method returns false, client_close() is called by the caller.
  1707. bool (*push) (struct client *client, const void *data, size_t len);
  1708. // TODO: optional push_error() to inform about network I/O errors
  1709. /// Attempt a graceful shutdown: make any appropriate steps before
  1710. /// the client connection times out and gets torn down by force.
  1711. /// The client is allowed to destroy itself immediately.
  1712. void (*shutdown) (struct client *client);
  1713. /// Do any additional cleanup for the concrete class before destruction
  1714. void (*finalize) (struct client *client);
  1715. };
  1716. static void
  1717. client_destroy (struct client *self)
  1718. {
  1719. // XXX: this codebase halfway pretends there could be other contexts
  1720. struct server_context *ctx = ev_userdata (EV_DEFAULT);
  1721. LIST_UNLINK (ctx->clients, self);
  1722. ctx->n_clients--;
  1723. // First uninitialize the higher-level implementation
  1724. self->vtable->finalize (self);
  1725. ev_io_stop (EV_DEFAULT_ &self->read_watcher);
  1726. ev_io_stop (EV_DEFAULT_ &self->write_watcher);
  1727. xclose (self->socket_fd);
  1728. write_queue_free (&self->write_queue);
  1729. ev_timer_stop (EV_DEFAULT_ &self->close_timeout_watcher);
  1730. free (self);
  1731. try_finish_quit (ctx);
  1732. }
  1733. static void
  1734. client_write_unsafe (struct client *self, void *data, size_t len)
  1735. {
  1736. struct write_req *req = xcalloc (1, sizeof *req);
  1737. req->data.iov_base = data;
  1738. req->data.iov_len = len;
  1739. write_queue_add (&self->write_queue, req);
  1740. ev_io_start (EV_DEFAULT_ &self->write_watcher);
  1741. }
  1742. static void
  1743. client_write_owned (struct client *self, void *data, size_t len)
  1744. {
  1745. if (soft_assert (!self->flushing) && len != 0)
  1746. client_write_unsafe (self, data, len);
  1747. else
  1748. free (data);
  1749. }
  1750. static void
  1751. client_write (struct client *self, const void *data, size_t len)
  1752. {
  1753. if (soft_assert (!self->flushing) && len != 0)
  1754. client_write_unsafe (self, memcpy (xmalloc (len), data, len), len);
  1755. }
  1756. /// Half-close the connection from our side once the write_queue is flushed.
  1757. /// It is the caller's responsibility to destroy the connection upon EOF.
  1758. // XXX: or we might change on_client_readable to do it anyway, seems safe
  1759. static void
  1760. client_shutdown (struct client *self)
  1761. {
  1762. self->flushing = true;
  1763. // In case this shutdown is immediately followed by a close, try our best
  1764. (void) flush_queue (&self->write_queue, self->socket_fd);
  1765. ev_feed_event (EV_DEFAULT_ &self->write_watcher, EV_WRITE);
  1766. }
  1767. /// Try to cleanly close the connection, waiting for the remote client to close
  1768. /// its own side of the connection as a sign that it has processed all the data
  1769. /// it wanted to. The client implementation will not receive any further data.
  1770. /// May directly call client_destroy().
  1771. static void
  1772. client_close (struct client *self)
  1773. {
  1774. if (self->closing)
  1775. return;
  1776. self->closing = true;
  1777. ev_timer_start (EV_DEFAULT_ &self->close_timeout_watcher);
  1778. client_shutdown (self);
  1779. // We assume the remote client doesn't want our data if it half-closes
  1780. if (self->received_eof)
  1781. client_destroy (self);
  1782. }
  1783. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1784. static bool
  1785. client_read_loop (EV_P_ struct client *client, ev_io *watcher)
  1786. {
  1787. char buf[8192];
  1788. ssize_t n_read;
  1789. again:
  1790. while ((n_read = recv (watcher->fd, buf, sizeof buf, 0)) >= 0)
  1791. {
  1792. if (!n_read)
  1793. {
  1794. // Don't deliver the EOF condition repeatedly
  1795. ev_io_stop (EV_A_ watcher);
  1796. client->received_eof = true;
  1797. }
  1798. if (!client->closing
  1799. && !client->vtable->push (client, buf, n_read))
  1800. {
  1801. client_close (client);
  1802. return false;
  1803. }
  1804. if (!n_read)
  1805. return true;
  1806. }
  1807. if (errno == EINTR)
  1808. goto again;
  1809. if (errno == EAGAIN)
  1810. return true;
  1811. client_destroy (client);
  1812. return false;
  1813. }
  1814. static void
  1815. on_client_readable (EV_P_ ev_io *watcher, int revents)
  1816. {
  1817. struct client *client = watcher->data;
  1818. (void) revents;
  1819. if (client_read_loop (EV_A_ client, watcher)
  1820. && client->closing && client->received_eof)
  1821. client_destroy (client);
  1822. }
  1823. static void
  1824. on_client_writable (EV_P_ ev_io *watcher, int revents)
  1825. {
  1826. struct client *client = watcher->data;
  1827. (void) loop;
  1828. (void) revents;
  1829. // TODO: some sort of "on_buffers_flushed" callback for streaming huge
  1830. // chunks of external (or generated) data. That will need to be
  1831. // forwarded to "struct request_handler".
  1832. if (!flush_queue (&client->write_queue, watcher->fd))
  1833. {
  1834. client_destroy (client);
  1835. return;
  1836. }
  1837. if (!write_queue_is_empty (&client->write_queue))
  1838. return;
  1839. ev_io_stop (EV_A_ watcher);
  1840. if (client->flushing && !client->half_closed)
  1841. {
  1842. if (!shutdown (client->socket_fd, SHUT_WR))
  1843. client->half_closed = true;
  1844. else
  1845. client_destroy (client);
  1846. }
  1847. }
  1848. static void
  1849. on_client_timeout (EV_P_ ev_timer *watcher, int revents)
  1850. {
  1851. (void) loop;
  1852. (void) revents;
  1853. client_destroy (watcher->data);
  1854. }
  1855. /// Create a new instance of a subclass with the given size.
  1856. /// The superclass is assumed to be the first member of the structure.
  1857. static void *
  1858. client_new (EV_P_ size_t size, int sock_fd)
  1859. {
  1860. struct server_context *ctx = ev_userdata (loop);
  1861. struct client *self = xcalloc (1, size);
  1862. self->write_queue = write_queue_make ();
  1863. ev_timer_init (&self->close_timeout_watcher, on_client_timeout, 5., 0.);
  1864. self->close_timeout_watcher.data = self;
  1865. set_blocking (sock_fd, false);
  1866. self->socket_fd = sock_fd;
  1867. ev_io_init (&self->read_watcher, on_client_readable, sock_fd, EV_READ);
  1868. ev_io_init (&self->write_watcher, on_client_writable, sock_fd, EV_WRITE);
  1869. self->read_watcher.data = self;
  1870. self->write_watcher.data = self;
  1871. // We're only interested in reading as the write queue is empty now
  1872. ev_io_start (EV_A_ &self->read_watcher);
  1873. LIST_PREPEND (ctx->clients, self);
  1874. ctx->n_clients++;
  1875. return self;
  1876. }
  1877. // --- FastCGI client handler --------------------------------------------------
  1878. struct client_fcgi
  1879. {
  1880. struct client client; ///< Parent class
  1881. struct fcgi_muxer muxer; ///< FastCGI de/multiplexer
  1882. };
  1883. struct client_fcgi_request
  1884. {
  1885. struct fcgi_request *fcgi_request; ///< FastCGI request
  1886. struct request request; ///< Request
  1887. };
  1888. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1889. static void
  1890. client_fcgi_request_write_cb (struct request *req, const void *data, size_t len)
  1891. {
  1892. FIND_CONTAINER (self, req, struct client_fcgi_request, request);
  1893. fcgi_request_write (self->fcgi_request, data, len);
  1894. }
  1895. // XXX: it should be possible to pass a specific status code but we'd have to
  1896. // allow it in multiple places over this code base, notably request_push()
  1897. static void
  1898. client_fcgi_request_finish_cb (struct request *req)
  1899. {
  1900. FIND_CONTAINER (self, req, struct client_fcgi_request, request);
  1901. struct fcgi_muxer *muxer = self->fcgi_request->muxer;
  1902. // No more data to send, terminate the substream/request,
  1903. // and also the transport if the client didn't specifically ask to keep it
  1904. if (!fcgi_request_finish (self->fcgi_request, EXIT_SUCCESS))
  1905. muxer->close_cb (muxer);
  1906. }
  1907. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1908. static bool
  1909. client_fcgi_request_start (struct fcgi_request *fcgi_request)
  1910. {
  1911. struct client_fcgi_request *request =
  1912. fcgi_request->handler_data = xcalloc (1, sizeof *request);
  1913. request->fcgi_request = fcgi_request;
  1914. request_init (&request->request);
  1915. request->request.ctx = ev_userdata (EV_DEFAULT);
  1916. request->request.write_cb = client_fcgi_request_write_cb;
  1917. request->request.finish_cb = client_fcgi_request_finish_cb;
  1918. return request_start (&request->request, &fcgi_request->headers);
  1919. }
  1920. static bool
  1921. client_fcgi_request_push
  1922. (struct fcgi_request *req, const void *data, size_t len)
  1923. {
  1924. struct client_fcgi_request *request = req->handler_data;
  1925. return request_push (&request->request, data, len)
  1926. || fcgi_request_finish (req, EXIT_SUCCESS);
  1927. }
  1928. static void
  1929. client_fcgi_request_finalize (struct fcgi_request *req)
  1930. {
  1931. struct client_fcgi_request *request = req->handler_data;
  1932. request_free (&request->request);
  1933. free (request);
  1934. }
  1935. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1936. static void
  1937. client_fcgi_write_cb (struct fcgi_muxer *mux, void *data, size_t len)
  1938. {
  1939. FIND_CONTAINER (self, mux, struct client_fcgi, muxer);
  1940. client_write_owned (&self->client, data, len);
  1941. }
  1942. static void
  1943. client_fcgi_close_cb (struct fcgi_muxer *mux)
  1944. {
  1945. FIND_CONTAINER (self, mux, struct client_fcgi, muxer);
  1946. client_close (&self->client);
  1947. }
  1948. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1949. static bool
  1950. client_fcgi_push (struct client *client, const void *data, size_t len)
  1951. {
  1952. FIND_CONTAINER (self, client, struct client_fcgi, client);
  1953. return fcgi_muxer_push (&self->muxer, data, len);
  1954. }
  1955. static void
  1956. client_fcgi_shutdown (struct client *client)
  1957. {
  1958. FIND_CONTAINER (self, client, struct client_fcgi, client);
  1959. self->muxer.in_shutdown = true;
  1960. // TODO: respond with FCGI_END_REQUEST: FCGI_REQUEST_COMPLETE to everything?
  1961. // The FastCGI specification isn't very clear about what we should do.
  1962. }
  1963. static void
  1964. client_fcgi_finalize (struct client *client)
  1965. {
  1966. FIND_CONTAINER (self, client, struct client_fcgi, client);
  1967. fcgi_muxer_free (&self->muxer);
  1968. }
  1969. static struct client_vtable client_fcgi_vtable =
  1970. {
  1971. .push = client_fcgi_push,
  1972. .shutdown = client_fcgi_shutdown,
  1973. .finalize = client_fcgi_finalize,
  1974. };
  1975. static struct client *
  1976. client_fcgi_create (EV_P_ int sock_fd)
  1977. {
  1978. struct client_fcgi *self = client_new (EV_A_ sizeof *self, sock_fd);
  1979. self->client.vtable = &client_fcgi_vtable;
  1980. fcgi_muxer_init (&self->muxer);
  1981. self->muxer.write_cb = client_fcgi_write_cb;
  1982. self->muxer.close_cb = client_fcgi_close_cb;
  1983. self->muxer.request_start_cb = client_fcgi_request_start;
  1984. self->muxer.request_push_cb = client_fcgi_request_push;
  1985. self->muxer.request_finalize_cb = client_fcgi_request_finalize;
  1986. return &self->client;
  1987. }
  1988. // --- SCGI client handler -----------------------------------------------------
  1989. struct client_scgi
  1990. {
  1991. struct client client; ///< Parent class
  1992. struct scgi_parser parser; ///< SCGI stream parser
  1993. struct request request; ///< Request (only one per connection)
  1994. unsigned long remaining_content; ///< Length of input data to be seen
  1995. };
  1996. static void
  1997. client_scgi_write_cb (struct request *req, const void *data, size_t len)
  1998. {
  1999. FIND_CONTAINER (self, req, struct client_scgi, request);
  2000. client_write (&self->client, data, len);
  2001. }
  2002. static void
  2003. client_scgi_finish_cb (struct request *req)
  2004. {
  2005. FIND_CONTAINER (self, req, struct client_scgi, request);
  2006. client_close (&self->client);
  2007. }
  2008. static bool
  2009. client_scgi_on_headers_read (void *user_data)
  2010. {
  2011. struct client_scgi *self = user_data;
  2012. const char *cl = str_map_find (&self->parser.headers, "CONTENT_LENGTH");
  2013. if (!cl || !xstrtoul (&self->remaining_content, cl, 10))
  2014. {
  2015. print_debug ("SCGI request with invalid or missing CONTENT_LENGTH");
  2016. return false;
  2017. }
  2018. return request_start (&self->request, &self->parser.headers);
  2019. }
  2020. static bool
  2021. client_scgi_on_content (void *user_data, const void *data, size_t len)
  2022. {
  2023. struct client_scgi *self = user_data;
  2024. if (len > self->remaining_content)
  2025. {
  2026. print_debug ("SCGI request got more data than CONTENT_LENGTH");
  2027. return false;
  2028. }
  2029. // We're in a slight disagreement with the SCGI specification since
  2030. // this tries to write output before it has read all the input
  2031. if (!request_push (&self->request, data, len))
  2032. return false;
  2033. if ((self->remaining_content -= len))
  2034. return true;
  2035. // Signalise end of input to the request handler
  2036. return request_push (&self->request, NULL, 0);
  2037. }
  2038. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  2039. static bool
  2040. client_scgi_push (struct client *client, const void *data, size_t len)
  2041. {
  2042. struct client_scgi *self = (struct client_scgi *) client;
  2043. struct error *e = NULL;
  2044. if (scgi_parser_push (&self->parser, data, len, &e))
  2045. return true;
  2046. if (e != NULL)
  2047. {
  2048. print_debug ("SCGI parser failed: %s", e->message);
  2049. error_free (e);
  2050. }
  2051. return false;
  2052. }
  2053. static void
  2054. client_scgi_finalize (struct client *client)
  2055. {
  2056. struct client_scgi *self = (struct client_scgi *) client;
  2057. request_free (&self->request);
  2058. scgi_parser_free (&self->parser);
  2059. }
  2060. static struct client_vtable client_scgi_vtable =
  2061. {
  2062. .push = client_scgi_push,
  2063. .finalize = client_scgi_finalize,
  2064. };
  2065. static struct client *
  2066. client_scgi_create (EV_P_ int sock_fd)
  2067. {
  2068. struct client_scgi *self = client_new (EV_A_ sizeof *self, sock_fd);
  2069. self->client.vtable = &client_scgi_vtable;
  2070. request_init (&self->request);
  2071. self->request.ctx = ev_userdata (EV_DEFAULT);
  2072. self->request.write_cb = client_scgi_write_cb;
  2073. self->request.finish_cb = client_scgi_finish_cb;
  2074. self->parser = scgi_parser_make ();
  2075. self->parser.on_headers_read = client_scgi_on_headers_read;
  2076. self->parser.on_content = client_scgi_on_content;
  2077. self->parser.user_data = self;
  2078. return &self->client;
  2079. }
  2080. // --- WebSocket client handler ------------------------------------------------
  2081. struct client_ws
  2082. {
  2083. struct client client; ///< Parent class
  2084. struct ws_handler handler; ///< WebSocket connection handler
  2085. };
  2086. static bool
  2087. client_ws_on_message (struct ws_handler *handler,
  2088. enum ws_opcode type, const void *data, size_t len)
  2089. {
  2090. FIND_CONTAINER (self, handler, struct client_ws, handler);
  2091. if (type != WS_OPCODE_TEXT)
  2092. {
  2093. return ws_handler_fail_connection
  2094. (&self->handler, WS_STATUS_UNSUPPORTED_DATA);
  2095. }
  2096. struct server_context *ctx = ev_userdata (EV_DEFAULT);
  2097. struct str response = str_make ();
  2098. process_json_rpc (ctx, data, len, &response);
  2099. if (response.len)
  2100. ws_handler_send_frame (&self->handler,
  2101. WS_OPCODE_TEXT, response.str, response.len);
  2102. str_free (&response);
  2103. return true;
  2104. }
  2105. static void
  2106. client_ws_write_cb (struct ws_handler *handler, const void *data, size_t len)
  2107. {
  2108. FIND_CONTAINER (self, handler, struct client_ws, handler);
  2109. client_write (&self->client, data, len);
  2110. }
  2111. static void
  2112. client_ws_close_cb (struct ws_handler *handler, bool half_close)
  2113. {
  2114. FIND_CONTAINER (self, handler, struct client_ws, handler);
  2115. (half_close ? client_shutdown : client_destroy) (&self->client);
  2116. }
  2117. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  2118. static bool
  2119. client_ws_push (struct client *client, const void *data, size_t len)
  2120. {
  2121. FIND_CONTAINER (self, client, struct client_ws, client);
  2122. // client_close() will correctly destroy the client on EOF
  2123. return ws_handler_push (&self->handler, data, len);
  2124. }
  2125. static void
  2126. client_ws_shutdown (struct client *client)
  2127. {
  2128. FIND_CONTAINER (self, client, struct client_ws, client);
  2129. if (self->handler.state == WS_HANDLER_CONNECTING)
  2130. // No on_close, no problem
  2131. client_destroy (&self->client);
  2132. else if (self->handler.state == WS_HANDLER_OPEN)
  2133. ws_handler_close (&self->handler, WS_STATUS_GOING_AWAY, NULL, 0);
  2134. }
  2135. static void
  2136. client_ws_finalize (struct client *client)
  2137. {
  2138. FIND_CONTAINER (self, client, struct client_ws, client);
  2139. ws_handler_free (&self->handler);
  2140. }
  2141. static struct client_vtable client_ws_vtable =
  2142. {
  2143. .push = client_ws_push,
  2144. .shutdown = client_ws_shutdown,
  2145. .finalize = client_ws_finalize,
  2146. };
  2147. static struct client *
  2148. client_ws_create (EV_P_ int sock_fd)
  2149. {
  2150. struct client_ws *self = client_new (EV_A_ sizeof *self, sock_fd);
  2151. self->client.vtable = &client_ws_vtable;
  2152. ws_handler_init (&self->handler);
  2153. self->handler.on_message = client_ws_on_message;
  2154. self->handler.write_cb = client_ws_write_cb;
  2155. self->handler.close_cb = client_ws_close_cb;
  2156. // One mebibyte seems to be a reasonable value
  2157. self->handler.max_payload_len = 1 << 10;
  2158. ws_handler_start (&self->handler);
  2159. return &self->client;
  2160. }
  2161. // --- Co-process client -------------------------------------------------------
  2162. // This is mostly copied over from json-rpc-shell.c, only a bit simplified.
  2163. // We're giving up on header parsing in order to keep this small.
  2164. struct co_context
  2165. {
  2166. struct server_context *ctx; ///< Server context
  2167. struct str message; ///< Message data
  2168. struct http_parser parser; ///< HTTP parser
  2169. bool pending_fake_starter; ///< Start of message?
  2170. };
  2171. static int
  2172. client_co_on_message_begin (http_parser *parser)
  2173. {
  2174. struct co_context *self = parser->data;
  2175. str_reset (&self->message);
  2176. return 0;
  2177. }
  2178. static int
  2179. client_co_on_body (http_parser *parser, const char *at, size_t len)
  2180. {
  2181. struct co_context *self = parser->data;
  2182. str_append_data (&self->message, at, len);
  2183. return 0;
  2184. }
  2185. static int
  2186. client_co_on_message_complete (http_parser *parser)
  2187. {
  2188. struct co_context *self = parser->data;
  2189. http_parser_pause (&self->parser, true);
  2190. return 0;
  2191. }
  2192. // The LSP incorporates a very thin subset of RFC 822, and it so happens
  2193. // that we may simply reuse the full HTTP parser here, with a small hack.
  2194. static const http_parser_settings client_co_http_settings =
  2195. {
  2196. .on_message_begin = client_co_on_message_begin,
  2197. .on_body = client_co_on_body,
  2198. .on_message_complete = client_co_on_message_complete,
  2199. };
  2200. static void
  2201. client_co_respond (const struct str *buf)
  2202. {
  2203. struct str wrapped = str_make();
  2204. str_append_printf (&wrapped,
  2205. "Content-Length: %zu\r\n"
  2206. "Content-Type: application/json; charset=utf-8\r\n"
  2207. "\r\n", buf->len);
  2208. str_append_data (&wrapped, buf->str, buf->len);
  2209. if (write (STDOUT_FILENO, wrapped.str, wrapped.len)
  2210. != (ssize_t) wrapped.len)
  2211. exit_fatal ("write: %s", strerror (errno));
  2212. str_free (&wrapped);
  2213. }
  2214. static void
  2215. client_co_inject_starter (struct co_context *self)
  2216. {
  2217. // The default "Connection: keep-alive" maps well here.
  2218. // We cannot feed this line into the parser from within callbacks.
  2219. static const char starter[] = "POST / HTTP/1.1\r\n";
  2220. http_parser_pause (&self->parser, false);
  2221. size_t n_parsed = http_parser_execute (&self->parser,
  2222. &client_co_http_settings, starter, sizeof starter - 1);
  2223. enum http_errno err = HTTP_PARSER_ERRNO (&self->parser);
  2224. if (n_parsed != sizeof starter - 1 || err != HPE_OK)
  2225. exit_fatal ("protocol failure: %s", http_errno_description (err));
  2226. }
  2227. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  2228. static void
  2229. client_co_process (struct co_context *self)
  2230. {
  2231. struct str *message = &self->message;
  2232. struct str response = str_make ();
  2233. process_json_rpc (self->ctx, message->str, message->len, &response);
  2234. if (response.len)
  2235. client_co_respond (&response);
  2236. str_free (&response);
  2237. }
  2238. static void
  2239. client_co_parse (struct co_context *self, const char *data, size_t len,
  2240. size_t *n_parsed)
  2241. {
  2242. if (self->pending_fake_starter)
  2243. {
  2244. self->pending_fake_starter = false;
  2245. client_co_inject_starter (self);
  2246. }
  2247. *n_parsed = http_parser_execute
  2248. (&self->parser, &client_co_http_settings, data, len);
  2249. if (self->parser.upgrade)
  2250. exit_fatal ("protocol failure: %s", "unsupported upgrade attempt");
  2251. enum http_errno err = HTTP_PARSER_ERRNO (&self->parser);
  2252. if (err == HPE_PAUSED)
  2253. {
  2254. self->pending_fake_starter = true;
  2255. client_co_process (self);
  2256. }
  2257. else if (err != HPE_OK)
  2258. exit_fatal ("protocol failure: %s", http_errno_description (err));
  2259. }
  2260. static void
  2261. client_co_on_data (struct co_context *self, const char *data, size_t len)
  2262. {
  2263. size_t n_parsed = 0;
  2264. do
  2265. {
  2266. client_co_parse (self, data, len, &n_parsed);
  2267. data += n_parsed;
  2268. }
  2269. while ((len -= n_parsed));
  2270. }
  2271. static void
  2272. client_co_run (struct server_context *ctx)
  2273. {
  2274. struct co_context self = {};
  2275. self.ctx = ctx;
  2276. self.message = str_make ();
  2277. http_parser_init (&self.parser, HTTP_REQUEST);
  2278. self.parser.data = &self;
  2279. self.pending_fake_starter = true;
  2280. hard_assert (set_blocking (STDIN_FILENO, false));
  2281. struct str buf = str_make ();
  2282. struct pollfd pfd = { .fd = STDIN_FILENO, .events = POLLIN };
  2283. while (true)
  2284. {
  2285. if (poll (&pfd, 1, -1) <= 0)
  2286. exit_fatal ("poll: %s", strerror (errno));
  2287. str_remove_slice (&buf, 0, buf.len);
  2288. enum socket_io_result result = socket_io_try_read (pfd.fd, &buf);
  2289. int errno_saved = errno;
  2290. if (buf.len)
  2291. client_co_on_data (&self, buf.str, buf.len);
  2292. if (result == SOCKET_IO_ERROR)
  2293. exit_fatal ("read: %s", strerror (errno_saved));
  2294. if (result == SOCKET_IO_EOF)
  2295. break;
  2296. }
  2297. str_free (&buf);
  2298. str_free (&self.message);
  2299. }
  2300. // --- Basic server stuff ------------------------------------------------------
  2301. typedef struct client *(*client_create_fn) (EV_P_ int sock_fd);
  2302. struct listener
  2303. {
  2304. int fd; ///< Listening socket FD
  2305. ev_io watcher; ///< New connection available
  2306. client_create_fn create; ///< Client constructor
  2307. };
  2308. static void
  2309. close_listeners (struct server_context *self)
  2310. {
  2311. for (size_t i = 0; i < self->n_listeners; i++)
  2312. {
  2313. struct listener *listener = &self->listeners[i];
  2314. if (listener->fd == -1)
  2315. continue;
  2316. ev_io_stop (EV_DEFAULT_ &listener->watcher);
  2317. xclose (listener->fd);
  2318. listener->fd = -1;
  2319. }
  2320. }
  2321. static void
  2322. try_finish_quit (struct server_context *self)
  2323. {
  2324. if (!self->quitting || self->clients)
  2325. return;
  2326. ev_timer_stop (EV_DEFAULT_ &self->quit_timeout_watcher);
  2327. ev_break (EV_DEFAULT_ EVBREAK_ALL);
  2328. }
  2329. static void
  2330. on_quit_timeout (EV_P_ ev_timer *watcher, int revents)
  2331. {
  2332. struct server_context *self = watcher->data;
  2333. (void) loop;
  2334. (void) revents;
  2335. LIST_FOR_EACH (struct client, iter, self->clients)
  2336. client_destroy (iter);
  2337. }
  2338. static void
  2339. initiate_quit (struct server_context *self)
  2340. {
  2341. self->quitting = true;
  2342. close_listeners (self);
  2343. // Wait a little while for all clients to clean up, if necessary
  2344. LIST_FOR_EACH (struct client, iter, self->clients)
  2345. if (iter->vtable->shutdown)
  2346. iter->vtable->shutdown (iter);
  2347. ev_timer_start (EV_DEFAULT_ &self->quit_timeout_watcher);
  2348. try_finish_quit (self);
  2349. }
  2350. static void
  2351. on_client_available (EV_P_ ev_io *watcher, int revents)
  2352. {
  2353. struct server_context *ctx = ev_userdata (loop);
  2354. struct listener *listener = watcher->data;
  2355. (void) revents;
  2356. while (true)
  2357. {
  2358. int sock_fd = accept (watcher->fd, NULL, NULL);
  2359. if (sock_fd != -1)
  2360. listener->create (EV_A_ sock_fd);
  2361. else if (errno == EAGAIN)
  2362. return;
  2363. else if (errno != EINTR && errno != EMFILE
  2364. && errno != ECONNRESET && errno != ECONNABORTED)
  2365. break;
  2366. }
  2367. // Stop accepting connections to prevent busy looping
  2368. ev_io_stop (EV_A_ watcher);
  2369. print_fatal ("%s: %s", "accept", strerror (errno));
  2370. initiate_quit (ctx);
  2371. }
  2372. // --- Application setup -------------------------------------------------------
  2373. /// This function handles values that require validation before their first use,
  2374. /// or some kind of a transformation (such as conversion to an integer) needs
  2375. /// to be done before they can be used directly.
  2376. static bool
  2377. parse_config (struct server_context *ctx, struct error **e)
  2378. {
  2379. (void) ctx;
  2380. (void) e;
  2381. return true;
  2382. }
  2383. static int
  2384. listener_bind (struct addrinfo *gai_iter)
  2385. {
  2386. int fd = socket (gai_iter->ai_family,
  2387. gai_iter->ai_socktype, gai_iter->ai_protocol);
  2388. if (fd == -1)
  2389. return -1;
  2390. set_cloexec (fd);
  2391. int yes = 1;
  2392. soft_assert (setsockopt (fd, SOL_SOCKET, SO_KEEPALIVE,
  2393. &yes, sizeof yes) != -1);
  2394. soft_assert (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
  2395. &yes, sizeof yes) != -1);
  2396. char host[NI_MAXHOST], port[NI_MAXSERV];
  2397. host[0] = port[0] = '\0';
  2398. int err = getnameinfo (gai_iter->ai_addr, gai_iter->ai_addrlen,
  2399. host, sizeof host, port, sizeof port,
  2400. NI_NUMERICHOST | NI_NUMERICSERV);
  2401. if (err)
  2402. print_debug ("%s: %s", "getnameinfo", gai_strerror (err));
  2403. char *address = format_host_port_pair (host, port);
  2404. if (bind (fd, gai_iter->ai_addr, gai_iter->ai_addrlen))
  2405. print_error ("bind to %s failed: %s", address, strerror (errno));
  2406. else if (listen (fd, 16 /* arbitrary number */))
  2407. print_error ("listen on %s failed: %s", address, strerror (errno));
  2408. else
  2409. {
  2410. print_status ("listening on %s", address);
  2411. free (address);
  2412. return fd;
  2413. }
  2414. free (address);
  2415. xclose (fd);
  2416. return -1;
  2417. }
  2418. static void
  2419. listener_add (struct server_context *ctx, const char *host, const char *port,
  2420. const struct addrinfo *gai_hints, client_create_fn create)
  2421. {
  2422. struct addrinfo *gai_result, *gai_iter;
  2423. int err = getaddrinfo (host, port, gai_hints, &gai_result);
  2424. if (err)
  2425. {
  2426. char *address = format_host_port_pair (host, port);
  2427. print_error ("bind to %s failed: %s: %s",
  2428. address, "getaddrinfo", gai_strerror (err));
  2429. free (address);
  2430. return;
  2431. }
  2432. int fd;
  2433. for (gai_iter = gai_result; gai_iter; gai_iter = gai_iter->ai_next)
  2434. {
  2435. if ((fd = listener_bind (gai_iter)) == -1)
  2436. continue;
  2437. set_blocking (fd, false);
  2438. struct listener *listener = &ctx->listeners[ctx->n_listeners++];
  2439. ev_io_init (&listener->watcher, on_client_available, fd, EV_READ);
  2440. ev_io_start (EV_DEFAULT_ &listener->watcher);
  2441. listener->watcher.data = listener;
  2442. listener->create = create;
  2443. listener->fd = fd;
  2444. break;
  2445. }
  2446. freeaddrinfo (gai_result);
  2447. }
  2448. static void
  2449. get_ports_from_config (struct server_context *ctx,
  2450. const char *key, struct strv *out)
  2451. {
  2452. const char *ports;
  2453. if ((ports = str_map_find (&ctx->config, key)))
  2454. cstr_split (ports, ",", true, out);
  2455. }
  2456. static bool
  2457. setup_listen_fds (struct server_context *ctx, struct error **e)
  2458. {
  2459. static const struct addrinfo gai_hints =
  2460. {
  2461. .ai_socktype = SOCK_STREAM,
  2462. .ai_flags = AI_PASSIVE,
  2463. };
  2464. struct strv ports_fcgi = strv_make ();
  2465. struct strv ports_scgi = strv_make ();
  2466. struct strv ports_ws = strv_make ();
  2467. get_ports_from_config (ctx, "port_fastcgi", &ports_fcgi);
  2468. get_ports_from_config (ctx, "port_scgi", &ports_scgi);
  2469. get_ports_from_config (ctx, "port_ws", &ports_ws);
  2470. const char *bind_host = str_map_find (&ctx->config, "bind_host");
  2471. size_t n_ports = ports_fcgi.len + ports_scgi.len + ports_ws.len;
  2472. ctx->listeners = xcalloc (n_ports, sizeof *ctx->listeners);
  2473. for (size_t i = 0; i < ports_fcgi.len; i++)
  2474. listener_add (ctx, bind_host, ports_fcgi.vector[i],
  2475. &gai_hints, client_fcgi_create);
  2476. for (size_t i = 0; i < ports_scgi.len; i++)
  2477. listener_add (ctx, bind_host, ports_scgi.vector[i],
  2478. &gai_hints, client_scgi_create);
  2479. for (size_t i = 0; i < ports_ws.len; i++)
  2480. listener_add (ctx, bind_host, ports_ws.vector[i],
  2481. &gai_hints, client_ws_create);
  2482. strv_free (&ports_fcgi);
  2483. strv_free (&ports_scgi);
  2484. strv_free (&ports_ws);
  2485. if (!ctx->n_listeners)
  2486. {
  2487. error_set (e, "%s: %s",
  2488. "network setup failed", "no ports to listen on");
  2489. return false;
  2490. }
  2491. return true;
  2492. }
  2493. static bool
  2494. app_lock_pid_file (struct server_context *ctx, struct error **e)
  2495. {
  2496. const char *path = str_map_find (&ctx->config, "pid_file");
  2497. if (!path)
  2498. return true;
  2499. char *resolved = resolve_filename (path, resolve_relative_runtime_filename);
  2500. bool result = lock_pid_file (resolved, e) != -1;
  2501. free (resolved);
  2502. return result;
  2503. }
  2504. // --- Tests -------------------------------------------------------------------
  2505. static void
  2506. test_misc (void)
  2507. {
  2508. soft_assert ( validate_json_rpc_content_type
  2509. ("application/JSON; charset=\"utf-8\""));
  2510. soft_assert (!validate_json_rpc_content_type
  2511. ("text/html; charset=\"utf-8\""));
  2512. char *canon = canonicalize_url_path ("///../../../etc/./passwd");
  2513. soft_assert (!strcmp (canon, "/etc/passwd"));
  2514. free (canon);
  2515. }
  2516. int
  2517. test_main (int argc, char *argv[])
  2518. {
  2519. struct test test;
  2520. test_init (&test, argc, argv);
  2521. test_add_simple (&test, "/misc", NULL, test_misc);
  2522. // TODO: write more tests
  2523. // TODO: test the server handler (happy path)
  2524. return test_run (&test);
  2525. }
  2526. // --- Main program ------------------------------------------------------------
  2527. static void
  2528. on_termination_signal (EV_P_ ev_signal *handle, int revents)
  2529. {
  2530. struct server_context *ctx = ev_userdata (loop);
  2531. (void) handle;
  2532. (void) revents;
  2533. if (ctx->quitting)
  2534. {
  2535. // Double C-c from the terminal accelerates the process
  2536. LIST_FOR_EACH (struct client, iter, ctx->clients)
  2537. client_destroy (iter);
  2538. }
  2539. else
  2540. initiate_quit (ctx);
  2541. }
  2542. static void
  2543. setup_signal_handlers (struct server_context *ctx)
  2544. {
  2545. ev_signal_init (&ctx->sigterm_watcher, on_termination_signal, SIGTERM);
  2546. ev_signal_start (EV_DEFAULT_ &ctx->sigterm_watcher);
  2547. ev_signal_init (&ctx->sigint_watcher, on_termination_signal, SIGINT);
  2548. ev_signal_start (EV_DEFAULT_ &ctx->sigint_watcher);
  2549. (void) signal (SIGPIPE, SIG_IGN);
  2550. }
  2551. static void
  2552. daemonize (struct server_context *ctx)
  2553. {
  2554. print_status ("daemonizing...");
  2555. if (chdir ("/"))
  2556. exit_fatal ("%s: %s", "chdir", strerror (errno));
  2557. // Because of systemd, we need to exit the parent process _after_ writing
  2558. // a PID file, otherwise our grandchild would receive a SIGTERM
  2559. int sync_pipe[2];
  2560. if (pipe (sync_pipe))
  2561. exit_fatal ("%s: %s", "pipe", strerror (errno));
  2562. pid_t pid;
  2563. if ((pid = fork ()) < 0)
  2564. exit_fatal ("%s: %s", "fork", strerror (errno));
  2565. else if (pid)
  2566. {
  2567. // Wait until all write ends of the pipe are closed, which can mean
  2568. // either success or failure, we don't need to care
  2569. xclose (sync_pipe[PIPE_WRITE]);
  2570. char dummy;
  2571. if (read (sync_pipe[PIPE_READ], &dummy, 1) < 0)
  2572. exit_fatal ("%s: %s", "read", strerror (errno));
  2573. exit (EXIT_SUCCESS);
  2574. }
  2575. setsid ();
  2576. signal (SIGHUP, SIG_IGN);
  2577. if ((pid = fork ()) < 0)
  2578. exit_fatal ("%s: %s", "fork", strerror (errno));
  2579. else if (pid)
  2580. exit (EXIT_SUCCESS);
  2581. openlog (PROGRAM_NAME, LOG_NDELAY | LOG_NOWAIT | LOG_PID, 0);
  2582. g_log_message_real = log_message_syslog;
  2583. // Write the PID file (if so configured) and get rid of the pipe, so that
  2584. // the read() in our grandparent finally returns zero (no write ends)
  2585. struct error *e = NULL;
  2586. if (!app_lock_pid_file (ctx, &e))
  2587. exit_fatal ("%s", e->message);
  2588. xclose (sync_pipe[PIPE_READ]);
  2589. xclose (sync_pipe[PIPE_WRITE]);
  2590. // XXX: we may close our own descriptors this way, crippling ourselves;
  2591. // there is no real guarantee that we will start with all three
  2592. // descriptors open. In theory we could try to enumerate the descriptors
  2593. // at the start of main().
  2594. for (int i = 0; i < 3; i++)
  2595. xclose (i);
  2596. int tty = open ("/dev/null", O_RDWR);
  2597. if (tty != 0 || dup (0) != 1 || dup (0) != 2)
  2598. exit_fatal ("failed to reopen FD's: %s", strerror (errno));
  2599. }
  2600. static void
  2601. parse_program_arguments (int argc, char **argv, bool *running_as_slave)
  2602. {
  2603. static const struct opt opts[] =
  2604. {
  2605. { 't', "test", NULL, 0, "self-test" },
  2606. { 's', "slave", NULL, 0, "co-process mode" },
  2607. { 'd', "debug", NULL, 0, "run in debug mode" },
  2608. { 'h', "help", NULL, 0, "display this help and exit" },
  2609. { 'V', "version", NULL, 0, "output version information and exit" },
  2610. { 'w', "write-default-cfg", "FILENAME",
  2611. OPT_OPTIONAL_ARG | OPT_LONG_ONLY,
  2612. "write a default configuration file and exit" },
  2613. { 0, NULL, NULL, 0, NULL }
  2614. };
  2615. struct opt_handler oh =
  2616. opt_handler_make (argc, argv, opts, NULL, "JSON-RPC 2.0 demo server.");
  2617. int c;
  2618. while ((c = opt_handler_get (&oh)) != -1)
  2619. switch (c)
  2620. {
  2621. case 't':
  2622. test_main (argc, argv);
  2623. exit (EXIT_SUCCESS);
  2624. case 's':
  2625. *running_as_slave = true;
  2626. break;
  2627. case 'd':
  2628. g_debug_mode = true;
  2629. break;
  2630. case 'h':
  2631. opt_handler_usage (&oh, stdout);
  2632. exit (EXIT_SUCCESS);
  2633. case 'V':
  2634. printf (PROGRAM_NAME " " PROGRAM_VERSION "\n");
  2635. exit (EXIT_SUCCESS);
  2636. case 'w':
  2637. call_simple_config_write_default (optarg, g_config_table);
  2638. exit (EXIT_SUCCESS);
  2639. default:
  2640. print_error ("wrong options");
  2641. opt_handler_usage (&oh, stderr);
  2642. exit (EXIT_FAILURE);
  2643. }
  2644. argc -= optind;
  2645. argv += optind;
  2646. if (argc)
  2647. {
  2648. opt_handler_usage (&oh, stderr);
  2649. exit (EXIT_FAILURE);
  2650. }
  2651. opt_handler_free (&oh);
  2652. }
  2653. int
  2654. main (int argc, char *argv[])
  2655. {
  2656. bool running_as_a_slave = false;
  2657. parse_program_arguments (argc, argv, &running_as_a_slave);
  2658. print_status (PROGRAM_NAME " " PROGRAM_VERSION " starting");
  2659. struct server_context ctx;
  2660. server_context_init (&ctx);
  2661. struct error *e = NULL;
  2662. if (!simple_config_update_from_file (&ctx.config, &e))
  2663. {
  2664. print_error ("error loading configuration: %s", e->message);
  2665. error_free (e);
  2666. exit (EXIT_FAILURE);
  2667. }
  2668. // There's a lot of unnecessary left-over scaffolding in this program,
  2669. // for testing purposes assume that everything is synchronous
  2670. if (running_as_a_slave)
  2671. {
  2672. client_co_run (&ctx);
  2673. server_context_free (&ctx);
  2674. return EXIT_SUCCESS;
  2675. }
  2676. struct ev_loop *loop;
  2677. if (!(loop = EV_DEFAULT))
  2678. exit_fatal ("libev initialization failed");
  2679. ev_set_userdata (loop, &ctx);
  2680. setup_signal_handlers (&ctx);
  2681. LIST_PREPEND (ctx.handlers, &g_request_handler_static);
  2682. LIST_PREPEND (ctx.handlers, &g_request_handler_json_rpc);
  2683. if (!parse_config (&ctx, &e)
  2684. || !setup_listen_fds (&ctx, &e))
  2685. {
  2686. print_error ("%s", e->message);
  2687. error_free (e);
  2688. exit (EXIT_FAILURE);
  2689. }
  2690. if (!g_debug_mode)
  2691. daemonize (&ctx);
  2692. else if (!app_lock_pid_file (&ctx, &e))
  2693. exit_fatal ("%s", e->message);
  2694. ev_run (loop, 0);
  2695. ev_loop_destroy (loop);
  2696. server_context_free (&ctx);
  2697. return EXIT_SUCCESS;
  2698. }