2015-03-02 08:49:21 +01:00
|
|
|
/*
|
2018-10-18 07:27:55 +02:00
|
|
|
* json-rpc-test-server.c: JSON-RPC 2.0 demo server
|
2015-03-02 08:49:21 +01:00
|
|
|
*
|
2020-09-01 21:03:36 +02:00
|
|
|
* Copyright (c) 2015 - 2020, Přemysl Eric Janouch <p@janouch.name>
|
2015-03-02 08:49:21 +01:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
2018-06-24 00:21:10 +02:00
|
|
|
* purpose with or without fee is hereby granted.
|
2015-03-02 08:49:21 +01:00
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define print_fatal_data ((void *) LOG_ERR)
|
|
|
|
#define print_error_data ((void *) LOG_ERR)
|
|
|
|
#define print_warning_data ((void *) LOG_WARNING)
|
|
|
|
#define print_status_data ((void *) LOG_INFO)
|
|
|
|
#define print_debug_data ((void *) LOG_DEBUG)
|
|
|
|
|
2015-03-09 23:32:01 +01:00
|
|
|
#define LIBERTY_WANT_SSL
|
2015-03-29 03:14:20 +02:00
|
|
|
#define LIBERTY_WANT_PROTO_HTTP
|
|
|
|
#define LIBERTY_WANT_PROTO_WS
|
|
|
|
#define LIBERTY_WANT_PROTO_SCGI
|
|
|
|
#define LIBERTY_WANT_PROTO_FASTCGI
|
2015-03-09 23:32:01 +01:00
|
|
|
|
2015-03-02 08:49:21 +01:00
|
|
|
#include "config.h"
|
2018-10-18 07:44:09 +02:00
|
|
|
#undef PROGRAM_NAME
|
|
|
|
#define PROGRAM_NAME "json-rpc-test-server"
|
|
|
|
|
2015-03-02 08:49:21 +01:00
|
|
|
#include "liberty/liberty.c"
|
|
|
|
|
|
|
|
#include <langinfo.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <strings.h>
|
|
|
|
|
|
|
|
#include <ev.h>
|
|
|
|
#include <jansson.h>
|
2015-03-08 09:41:10 +01:00
|
|
|
#include <magic.h>
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2015-03-15 04:32:04 +01:00
|
|
|
#include "http-parser/http_parser.h"
|
2015-03-09 23:32:01 +01:00
|
|
|
|
2016-01-16 22:16:25 +01:00
|
|
|
enum { PIPE_READ, PIPE_WRITE };
|
2015-03-22 22:35:58 +01:00
|
|
|
|
2018-10-15 20:38:48 +02:00
|
|
|
#define FIND_CONTAINER(name, pointer, type, member) \
|
|
|
|
type *name = CONTAINER_OF (pointer, type, member)
|
|
|
|
|
2018-10-17 08:40:18 +02:00
|
|
|
// --- Utilities ---------------------------------------------------------------
|
2015-03-02 23:11:29 +01:00
|
|
|
|
|
|
|
static bool
|
2018-10-17 08:40:18 +02:00
|
|
|
flush_queue (struct write_queue *queue, int fd)
|
2015-03-02 23:11:29 +01:00
|
|
|
{
|
|
|
|
struct iovec vec[queue->len], *vec_iter = vec;
|
2016-01-17 04:42:16 +01:00
|
|
|
LIST_FOR_EACH (struct write_req, iter, queue->head)
|
2015-03-02 23:11:29 +01:00
|
|
|
*vec_iter++ = iter->data;
|
|
|
|
|
|
|
|
ssize_t written;
|
|
|
|
again:
|
2018-10-17 08:40:18 +02:00
|
|
|
if ((written = writev (fd, vec, N_ELEMENTS (vec))) >= 0)
|
2015-03-02 23:11:29 +01:00
|
|
|
{
|
2018-10-17 08:40:18 +02:00
|
|
|
write_queue_processed (queue, written);
|
|
|
|
return true;
|
2015-03-02 23:11:29 +01:00
|
|
|
}
|
2018-10-17 08:40:18 +02:00
|
|
|
if (errno == EINTR)
|
|
|
|
goto again;
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return true;
|
2015-03-02 23:11:29 +01:00
|
|
|
|
2018-10-17 08:40:18 +02:00
|
|
|
return false;
|
2015-03-02 08:49:21 +01:00
|
|
|
}
|
|
|
|
|
2015-03-29 03:14:20 +02:00
|
|
|
// --- Logging -----------------------------------------------------------------
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
static void
|
2015-03-29 03:14:20 +02:00
|
|
|
log_message_syslog (void *user_data, const char *quote, const char *fmt,
|
|
|
|
va_list ap)
|
2015-03-08 05:39:57 +01:00
|
|
|
{
|
2015-03-29 03:14:20 +02:00
|
|
|
int prio = (int) (intptr_t) user_data;
|
2015-03-08 05:39:57 +01:00
|
|
|
|
2015-03-29 03:14:20 +02:00
|
|
|
va_list va;
|
|
|
|
va_copy (va, ap);
|
|
|
|
int size = vsnprintf (NULL, 0, fmt, va);
|
|
|
|
va_end (va);
|
|
|
|
if (size < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char buf[size + 1];
|
|
|
|
if (vsnprintf (buf, sizeof buf, fmt, ap) >= 0)
|
|
|
|
syslog (prio, "%s%s", quote, buf);
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2015-03-29 03:14:20 +02:00
|
|
|
// --- FastCGI -----------------------------------------------------------------
|
2018-10-15 05:07:57 +02:00
|
|
|
/// @defgroup FastCGI
|
|
|
|
/// @{
|
2015-03-05 08:47:20 +01:00
|
|
|
|
|
|
|
enum fcgi_request_state
|
|
|
|
{
|
|
|
|
FCGI_REQUEST_PARAMS, ///< Reading headers
|
|
|
|
FCGI_REQUEST_STDIN ///< Reading input
|
|
|
|
};
|
|
|
|
|
2015-03-02 08:49:21 +01:00
|
|
|
struct fcgi_request
|
|
|
|
{
|
|
|
|
struct fcgi_muxer *muxer; ///< The parent muxer
|
|
|
|
uint16_t request_id; ///< The ID of this request
|
2015-03-08 05:39:57 +01:00
|
|
|
uint8_t flags; ///< Request flags
|
|
|
|
|
2015-03-05 08:47:20 +01:00
|
|
|
enum fcgi_request_state state; ///< Parsing state
|
|
|
|
struct str_map headers; ///< Headers
|
|
|
|
struct fcgi_nv_parser hdr_parser; ///< Header parser
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
struct str output_buffer; ///< Output buffer
|
|
|
|
|
|
|
|
void *handler_data; ///< Handler data
|
|
|
|
};
|
2015-03-05 08:47:20 +01:00
|
|
|
|
2018-10-17 02:57:08 +02:00
|
|
|
/// Handles a single FastCGI connection, de/multiplexing requests and responses
|
2015-03-02 08:49:21 +01:00
|
|
|
struct fcgi_muxer
|
|
|
|
{
|
|
|
|
struct fcgi_parser parser; ///< FastCGI message parser
|
2018-10-17 02:57:08 +02:00
|
|
|
uint32_t active_requests; ///< The number of active requests
|
|
|
|
bool in_shutdown; ///< Rejecting new requests
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2018-10-17 02:57:08 +02:00
|
|
|
// Virtual method callbacks:
|
2015-03-11 00:24:20 +01:00
|
|
|
|
2018-10-19 01:15:12 +02:00
|
|
|
/// Write data to the underlying transport. Assumes ownership of data.
|
|
|
|
void (*write_cb) (struct fcgi_muxer *, void *data, size_t len);
|
2018-10-17 02:57:08 +02:00
|
|
|
|
2018-10-17 08:40:18 +02:00
|
|
|
/// Close the underlying transport. You are allowed to destroy the muxer
|
|
|
|
/// directly from within the callback.
|
2018-10-15 20:38:48 +02:00
|
|
|
void (*close_cb) (struct fcgi_muxer *);
|
2015-03-08 05:39:57 +01:00
|
|
|
|
2018-10-17 02:57:08 +02:00
|
|
|
/// Start processing a request. Return false if no further action is
|
|
|
|
/// to be done and the request should be finished.
|
2018-10-17 08:40:18 +02:00
|
|
|
bool (*request_start_cb) (struct fcgi_request *);
|
2018-10-17 02:57:08 +02:00
|
|
|
|
2018-10-17 08:40:18 +02:00
|
|
|
/// Handle incoming data. "len == 0" means EOF. Returns false if
|
|
|
|
/// the underlying transport should be closed, this being the last request.
|
|
|
|
bool (*request_push_cb)
|
2018-10-17 02:57:08 +02:00
|
|
|
(struct fcgi_request *, const void *data, size_t len);
|
|
|
|
|
|
|
|
/// Destroy the handler's data stored in the request object
|
2018-10-17 03:53:07 +02:00
|
|
|
void (*request_finalize_cb) (struct fcgi_request *);
|
2015-03-08 05:39:57 +01:00
|
|
|
|
2018-10-15 03:02:49 +02:00
|
|
|
/// Requests assigned to request IDs (may not be FCGI_NULL_REQUEST_ID)
|
|
|
|
struct fcgi_request *requests[1 << 8];
|
2015-03-02 08:49:21 +01:00
|
|
|
};
|
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
static void
|
|
|
|
fcgi_muxer_send (struct fcgi_muxer *self,
|
|
|
|
enum fcgi_type type, uint16_t request_id, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
hard_assert (len <= UINT16_MAX);
|
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
struct str message = str_make ();
|
2018-10-13 04:08:09 +02:00
|
|
|
static char zeroes[8];
|
|
|
|
size_t padding = -len & 7;
|
2015-03-08 05:39:57 +01:00
|
|
|
|
|
|
|
str_pack_u8 (&message, FCGI_VERSION_1);
|
|
|
|
str_pack_u8 (&message, type);
|
|
|
|
str_pack_u16 (&message, request_id);
|
2018-10-13 04:08:09 +02:00
|
|
|
str_pack_u16 (&message, len); // content length
|
|
|
|
str_pack_u8 (&message, padding); // padding length
|
|
|
|
str_pack_u8 (&message, 0); // reserved
|
2015-03-08 05:39:57 +01:00
|
|
|
|
|
|
|
str_append_data (&message, data, len);
|
2018-10-13 04:08:09 +02:00
|
|
|
str_append_data (&message, zeroes, padding);
|
2015-03-08 05:39:57 +01:00
|
|
|
|
2018-10-15 20:38:48 +02:00
|
|
|
self->write_cb (self, message.str, message.len);
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fcgi_muxer_send_end_request (struct fcgi_muxer *self, uint16_t request_id,
|
|
|
|
uint32_t app_status, enum fcgi_protocol_status protocol_status)
|
|
|
|
{
|
|
|
|
uint8_t content[8] = { app_status >> 24, app_status >> 16,
|
|
|
|
app_status << 8, app_status, protocol_status };
|
|
|
|
fcgi_muxer_send (self, FCGI_END_REQUEST, request_id,
|
|
|
|
content, sizeof content);
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2018-10-17 02:57:08 +02:00
|
|
|
static struct fcgi_request *
|
|
|
|
fcgi_request_new (void)
|
2015-03-08 05:39:57 +01:00
|
|
|
{
|
2018-10-17 02:57:08 +02:00
|
|
|
struct fcgi_request *self = xcalloc (1, sizeof *self);
|
2015-03-08 05:39:57 +01:00
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
self->headers = str_map_make (free);
|
2015-03-08 05:39:57 +01:00
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
self->hdr_parser = fcgi_nv_parser_make ();
|
2015-03-08 05:39:57 +01:00
|
|
|
self->hdr_parser.output = &self->headers;
|
2018-10-18 04:13:59 +02:00
|
|
|
|
|
|
|
self->output_buffer = str_make ();
|
2018-10-17 02:57:08 +02:00
|
|
|
return self;
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-10-17 02:57:08 +02:00
|
|
|
fcgi_request_destroy (struct fcgi_request *self)
|
2015-03-08 05:39:57 +01:00
|
|
|
{
|
2018-10-17 02:57:08 +02:00
|
|
|
// TODO: consider the case where it hasn't been started yet
|
2018-10-17 03:53:07 +02:00
|
|
|
self->muxer->request_finalize_cb (self);
|
2018-10-17 02:57:08 +02:00
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
str_map_free (&self->headers);
|
|
|
|
fcgi_nv_parser_free (&self->hdr_parser);
|
2018-10-17 02:57:08 +02:00
|
|
|
free (self);
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fcgi_request_flush (struct fcgi_request *self)
|
|
|
|
{
|
|
|
|
if (!self->output_buffer.len)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fcgi_muxer_send (self->muxer, FCGI_STDOUT, self->request_id,
|
|
|
|
self->output_buffer.str, self->output_buffer.len);
|
|
|
|
str_reset (&self->output_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fcgi_request_write (struct fcgi_request *self, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
// We're buffering the output and splitting it into messages
|
|
|
|
bool need_flush = true;
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
size_t to_write = UINT16_MAX - self->output_buffer.len;
|
|
|
|
if (to_write > len)
|
|
|
|
{
|
|
|
|
to_write = len;
|
|
|
|
need_flush = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
str_append_data (&self->output_buffer, data, to_write);
|
|
|
|
data = (uint8_t *) data + to_write;
|
|
|
|
len -= to_write;
|
|
|
|
|
|
|
|
if (need_flush)
|
|
|
|
fcgi_request_flush (self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:40:18 +02:00
|
|
|
/// Mark the request as done. Returns false if the underlying transport
|
|
|
|
/// should be closed, this being the last request.
|
|
|
|
static bool
|
2018-10-19 01:15:12 +02:00
|
|
|
fcgi_request_finish (struct fcgi_request *self, int32_t status_code)
|
2015-03-14 19:36:37 +01:00
|
|
|
{
|
2018-10-17 02:57:08 +02:00
|
|
|
fcgi_request_flush (self);
|
|
|
|
fcgi_muxer_send (self->muxer, FCGI_STDOUT, self->request_id, NULL, 0);
|
|
|
|
|
2018-10-19 01:15:12 +02:00
|
|
|
// The appStatus is mostly ignored by web servers and it's not even clear
|
|
|
|
// whether it should be a signed value like it is on Unix, or not
|
2018-10-17 02:57:08 +02:00
|
|
|
fcgi_muxer_send_end_request (self->muxer, self->request_id,
|
2018-10-19 01:15:12 +02:00
|
|
|
status_code, FCGI_REQUEST_COMPLETE);
|
2018-10-17 02:57:08 +02:00
|
|
|
|
2018-10-17 08:40:18 +02:00
|
|
|
bool should_close = !(self->flags & FCGI_KEEP_CONN);
|
2018-10-17 02:57:08 +02:00
|
|
|
|
|
|
|
self->muxer->active_requests--;
|
|
|
|
self->muxer->requests[self->request_id] = NULL;
|
|
|
|
fcgi_request_destroy (self);
|
2018-10-17 08:40:18 +02:00
|
|
|
|
|
|
|
return !should_close;
|
2015-03-14 19:36:37 +01:00
|
|
|
}
|
|
|
|
|
2018-10-18 04:13:59 +02:00
|
|
|
static bool
|
|
|
|
fcgi_request_push_params
|
|
|
|
(struct fcgi_request *self, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
if (self->state != FCGI_REQUEST_PARAMS)
|
|
|
|
{
|
|
|
|
print_debug ("FastCGI: expected %s, got %s",
|
|
|
|
STRINGIFY (FCGI_STDIN), STRINGIFY (FCGI_PARAMS));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len)
|
|
|
|
fcgi_nv_parser_push (&self->hdr_parser, data, len);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (self->hdr_parser.state != FCGI_NV_PARSER_NAME_LEN)
|
|
|
|
print_debug ("FastCGI: request headers seem to be cut off");
|
|
|
|
|
|
|
|
self->state = FCGI_REQUEST_STDIN;
|
|
|
|
if (!self->muxer->request_start_cb (self))
|
2018-10-19 01:15:12 +02:00
|
|
|
return fcgi_request_finish (self, EXIT_SUCCESS);
|
2018-10-18 04:13:59 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
fcgi_request_push_stdin
|
|
|
|
(struct fcgi_request *self, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
if (self->state != FCGI_REQUEST_STDIN)
|
|
|
|
{
|
|
|
|
print_debug ("FastCGI: expected %s, got %s",
|
|
|
|
STRINGIFY (FCGI_PARAMS), STRINGIFY (FCGI_STDIN));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self->muxer->request_push_cb (self, data, len);
|
|
|
|
}
|
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2018-10-18 04:13:59 +02:00
|
|
|
typedef bool (*fcgi_muxer_handler_fn)
|
2015-03-08 05:39:57 +01:00
|
|
|
(struct fcgi_muxer *, const struct fcgi_parser *);
|
|
|
|
|
2018-10-18 04:13:59 +02:00
|
|
|
static bool
|
2015-03-08 05:39:57 +01:00
|
|
|
fcgi_muxer_on_get_values
|
|
|
|
(struct fcgi_muxer *self, const struct fcgi_parser *parser)
|
|
|
|
{
|
2018-10-15 03:04:39 +02:00
|
|
|
if (parser->request_id != FCGI_NULL_REQUEST_ID)
|
|
|
|
{
|
2018-10-18 04:13:59 +02:00
|
|
|
print_debug ("FastCGI: invalid %s message",
|
2018-10-15 03:04:39 +02:00
|
|
|
STRINGIFY (FCGI_GET_VALUES));
|
2018-10-18 04:13:59 +02:00
|
|
|
return false;
|
2018-10-15 03:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct str_map values = str_map_make (free);
|
2018-06-24 00:40:10 +02:00
|
|
|
struct str_map response = str_map_make (free);
|
2015-03-08 05:39:57 +01:00
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
struct fcgi_nv_parser nv_parser = fcgi_nv_parser_make ();
|
2015-03-08 05:39:57 +01:00
|
|
|
nv_parser.output = &values;
|
|
|
|
|
|
|
|
fcgi_nv_parser_push (&nv_parser, parser->content.str, parser->content.len);
|
2020-10-13 02:08:53 +02:00
|
|
|
fcgi_nv_parser_free (&nv_parser);
|
2018-10-15 03:02:49 +02:00
|
|
|
const char *key = NULL;
|
2015-03-08 05:39:57 +01:00
|
|
|
|
2018-10-15 03:02:49 +02:00
|
|
|
// No real-world servers seem to actually use multiplexing
|
|
|
|
// or even issue this request, but we will implement it anyway
|
|
|
|
if (str_map_find (&values, (key = FCGI_MPXS_CONNS)))
|
|
|
|
str_map_set (&response, key, xstrdup ("1"));
|
2015-03-08 05:39:57 +01:00
|
|
|
|
2018-10-15 03:02:49 +02:00
|
|
|
// It's not clear whether FCGI_MAX_REQS means concurrently over all
|
|
|
|
// connections or over just a single connection (multiplexed), though
|
|
|
|
// supposedly it's actually per /web server/. Supply the strictest limit.
|
|
|
|
if (str_map_find (&values, (key = FCGI_MAX_REQS)))
|
|
|
|
str_map_set (&response, key,
|
|
|
|
xstrdup_printf ("%zu", N_ELEMENTS (self->requests) - 1));
|
2015-03-08 05:39:57 +01:00
|
|
|
|
2018-10-15 03:02:49 +02:00
|
|
|
// FCGI_MAX_CONNS would be basically infinity. We don't limit connections.
|
2015-03-08 05:39:57 +01:00
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
struct str content = str_make ();
|
2015-03-08 05:39:57 +01:00
|
|
|
fcgi_nv_convert (&response, &content);
|
|
|
|
fcgi_muxer_send (self, FCGI_GET_VALUES_RESULT, parser->request_id,
|
|
|
|
content.str, content.len);
|
|
|
|
str_free (&content);
|
|
|
|
|
|
|
|
str_map_free (&values);
|
|
|
|
str_map_free (&response);
|
2018-10-18 04:13:59 +02:00
|
|
|
return true;
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-18 04:13:59 +02:00
|
|
|
static bool
|
2015-03-08 05:39:57 +01:00
|
|
|
fcgi_muxer_on_begin_request
|
|
|
|
(struct fcgi_muxer *self, const struct fcgi_parser *parser)
|
|
|
|
{
|
2018-06-24 00:40:10 +02:00
|
|
|
struct msg_unpacker unpacker =
|
|
|
|
msg_unpacker_make (parser->content.str, parser->content.len);
|
2015-03-08 05:39:57 +01:00
|
|
|
|
|
|
|
uint16_t role;
|
|
|
|
uint8_t flags;
|
|
|
|
bool success = true;
|
|
|
|
success &= msg_unpacker_u16 (&unpacker, &role);
|
|
|
|
success &= msg_unpacker_u8 (&unpacker, &flags);
|
|
|
|
// Ignoring 5 reserved bytes
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
{
|
2018-10-18 04:13:59 +02:00
|
|
|
print_debug ("FastCGI: invalid %s message",
|
2015-03-08 05:39:57 +01:00
|
|
|
STRINGIFY (FCGI_BEGIN_REQUEST));
|
2018-10-18 04:13:59 +02:00
|
|
|
return false;
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-15 03:04:39 +02:00
|
|
|
struct fcgi_request *request = self->requests[parser->request_id];
|
|
|
|
if (parser->request_id == FCGI_NULL_REQUEST_ID || request)
|
|
|
|
{
|
2018-10-18 04:13:59 +02:00
|
|
|
print_debug ("FastCGI: unusable request ID in %s message",
|
|
|
|
STRINGIFY (FCGI_BEGIN_REQUEST));
|
|
|
|
return false;
|
2018-10-15 03:04:39 +02:00
|
|
|
}
|
|
|
|
|
2015-03-14 19:36:37 +01:00
|
|
|
// We can only act as a responder, reject everything else up front
|
2015-03-08 05:39:57 +01:00
|
|
|
if (role != FCGI_RESPONDER)
|
|
|
|
{
|
|
|
|
fcgi_muxer_send_end_request (self,
|
|
|
|
parser->request_id, 0, FCGI_UNKNOWN_ROLE);
|
2018-10-18 04:13:59 +02:00
|
|
|
return true;
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 02:57:08 +02:00
|
|
|
if (parser->request_id >= N_ELEMENTS (self->requests)
|
|
|
|
|| self->in_shutdown)
|
2015-03-08 05:39:57 +01:00
|
|
|
{
|
2018-10-15 03:04:39 +02:00
|
|
|
fcgi_muxer_send_end_request (self,
|
|
|
|
parser->request_id, 0, FCGI_OVERLOADED);
|
2018-10-18 04:13:59 +02:00
|
|
|
return true;
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 02:57:08 +02:00
|
|
|
request = fcgi_request_new ();
|
2015-03-08 05:39:57 +01:00
|
|
|
request->muxer = self;
|
|
|
|
request->request_id = parser->request_id;
|
|
|
|
request->flags = flags;
|
|
|
|
|
|
|
|
self->requests[parser->request_id] = request;
|
2018-10-17 02:57:08 +02:00
|
|
|
self->active_requests++;
|
2018-10-18 04:13:59 +02:00
|
|
|
return true;
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-18 04:13:59 +02:00
|
|
|
static bool
|
2015-03-08 05:39:57 +01:00
|
|
|
fcgi_muxer_on_abort_request
|
|
|
|
(struct fcgi_muxer *self, const struct fcgi_parser *parser)
|
|
|
|
{
|
|
|
|
struct fcgi_request *request = self->requests[parser->request_id];
|
2018-10-15 03:04:39 +02:00
|
|
|
if (parser->request_id == FCGI_NULL_REQUEST_ID || !request)
|
2015-03-08 05:39:57 +01:00
|
|
|
{
|
|
|
|
print_debug ("FastCGI: received %s for an unknown request",
|
|
|
|
STRINGIFY (FCGI_ABORT_REQUEST));
|
2018-10-18 04:13:59 +02:00
|
|
|
return true; // We might have just rejected it
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 01:15:12 +02:00
|
|
|
return fcgi_request_finish (request, EXIT_FAILURE);
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-18 04:13:59 +02:00
|
|
|
static bool
|
2015-03-08 05:39:57 +01:00
|
|
|
fcgi_muxer_on_params (struct fcgi_muxer *self, const struct fcgi_parser *parser)
|
|
|
|
{
|
|
|
|
struct fcgi_request *request = self->requests[parser->request_id];
|
2018-10-15 03:04:39 +02:00
|
|
|
if (parser->request_id == FCGI_NULL_REQUEST_ID || !request)
|
2015-03-08 05:39:57 +01:00
|
|
|
{
|
|
|
|
print_debug ("FastCGI: received %s for an unknown request",
|
|
|
|
STRINGIFY (FCGI_PARAMS));
|
2018-10-18 04:13:59 +02:00
|
|
|
return true; // We might have just rejected it
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-18 04:13:59 +02:00
|
|
|
// This may immediately finish and delete the request, but that's fine
|
|
|
|
return fcgi_request_push_params (request,
|
2015-03-08 05:39:57 +01:00
|
|
|
parser->content.str, parser->content.len);
|
|
|
|
}
|
|
|
|
|
2018-10-18 04:13:59 +02:00
|
|
|
static bool
|
2015-03-08 05:39:57 +01:00
|
|
|
fcgi_muxer_on_stdin (struct fcgi_muxer *self, const struct fcgi_parser *parser)
|
|
|
|
{
|
|
|
|
struct fcgi_request *request = self->requests[parser->request_id];
|
2018-10-15 03:04:39 +02:00
|
|
|
if (parser->request_id == FCGI_NULL_REQUEST_ID || !request)
|
2015-03-08 05:39:57 +01:00
|
|
|
{
|
|
|
|
print_debug ("FastCGI: received %s for an unknown request",
|
|
|
|
STRINGIFY (FCGI_STDIN));
|
2018-10-18 04:13:59 +02:00
|
|
|
return true; // We might have just rejected it
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 02:21:19 +02:00
|
|
|
// At the end of the stream, a zero-length record is received
|
2018-10-18 04:13:59 +02:00
|
|
|
return fcgi_request_push_stdin (request,
|
2015-03-08 05:39:57 +01:00
|
|
|
parser->content.str, parser->content.len);
|
|
|
|
}
|
|
|
|
|
2018-10-18 04:13:59 +02:00
|
|
|
static bool
|
2015-03-02 08:49:21 +01:00
|
|
|
fcgi_muxer_on_message (const struct fcgi_parser *parser, void *user_data)
|
|
|
|
{
|
|
|
|
struct fcgi_muxer *self = user_data;
|
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
if (parser->version != FCGI_VERSION_1)
|
|
|
|
{
|
|
|
|
print_debug ("FastCGI: unsupported version %d", parser->version);
|
2018-10-18 04:13:59 +02:00
|
|
|
return false;
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const fcgi_muxer_handler_fn handlers[] =
|
|
|
|
{
|
|
|
|
[FCGI_GET_VALUES] = fcgi_muxer_on_get_values,
|
|
|
|
[FCGI_BEGIN_REQUEST] = fcgi_muxer_on_begin_request,
|
|
|
|
[FCGI_ABORT_REQUEST] = fcgi_muxer_on_abort_request,
|
|
|
|
[FCGI_PARAMS] = fcgi_muxer_on_params,
|
|
|
|
[FCGI_STDIN] = fcgi_muxer_on_stdin,
|
|
|
|
};
|
|
|
|
|
|
|
|
fcgi_muxer_handler_fn handler;
|
|
|
|
if (parser->type >= N_ELEMENTS (handlers)
|
|
|
|
|| !(handler = handlers[parser->type]))
|
|
|
|
{
|
2018-10-12 23:07:06 +02:00
|
|
|
// Responding in this way even to application records, unspecified
|
2015-03-08 05:39:57 +01:00
|
|
|
uint8_t content[8] = { parser->type };
|
|
|
|
fcgi_muxer_send (self, FCGI_UNKNOWN_TYPE, parser->request_id,
|
|
|
|
content, sizeof content);
|
2018-10-18 04:13:59 +02:00
|
|
|
return true;
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-18 04:13:59 +02:00
|
|
|
return handler (self, parser);
|
2015-03-02 08:49:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fcgi_muxer_init (struct fcgi_muxer *self)
|
|
|
|
{
|
2018-06-24 00:40:10 +02:00
|
|
|
self->parser = fcgi_parser_make ();
|
2015-03-02 08:49:21 +01:00
|
|
|
self->parser.on_message = fcgi_muxer_on_message;
|
|
|
|
self->parser.user_data = self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fcgi_muxer_free (struct fcgi_muxer *self)
|
|
|
|
{
|
2018-10-17 02:57:08 +02:00
|
|
|
for (size_t i = 0; i < N_ELEMENTS (self->requests); i++)
|
|
|
|
{
|
|
|
|
if (!self->active_requests)
|
|
|
|
break;
|
|
|
|
if (self->requests[i])
|
|
|
|
{
|
|
|
|
fcgi_request_destroy (self->requests[i]);
|
|
|
|
self->active_requests--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 08:49:21 +01:00
|
|
|
fcgi_parser_free (&self->parser);
|
|
|
|
}
|
|
|
|
|
2018-10-18 04:13:59 +02:00
|
|
|
static bool
|
2015-03-02 08:49:21 +01:00
|
|
|
fcgi_muxer_push (struct fcgi_muxer *self, const void *data, size_t len)
|
|
|
|
{
|
2018-10-18 04:13:59 +02:00
|
|
|
return fcgi_parser_push (&self->parser, data, len);
|
2015-03-02 08:49:21 +01:00
|
|
|
}
|
|
|
|
|
2018-10-15 05:07:57 +02:00
|
|
|
/// @}
|
2015-03-09 23:32:01 +01:00
|
|
|
// --- WebSockets --------------------------------------------------------------
|
2018-10-15 05:07:57 +02:00
|
|
|
/// @defgroup WebSockets
|
|
|
|
/// @{
|
2015-03-09 23:32:01 +01:00
|
|
|
|
|
|
|
// WebSockets aren't CGI-compatible, therefore we must handle the initial HTTP
|
|
|
|
// handshake ourselves. Luckily it's not too much of a bother with http-parser.
|
|
|
|
// Typically there will be a normal HTTP server in front of us, proxying the
|
|
|
|
// requests based on the URI.
|
|
|
|
|
|
|
|
enum ws_handler_state
|
|
|
|
{
|
2015-03-23 16:47:21 +01:00
|
|
|
WS_HANDLER_CONNECTING, ///< Parsing HTTP
|
2015-03-15 04:32:04 +01:00
|
|
|
WS_HANDLER_OPEN, ///< Parsing WebSockets frames
|
2018-10-18 02:55:55 +02:00
|
|
|
WS_HANDLER_CLOSING, ///< Partial closure by us
|
|
|
|
WS_HANDLER_FLUSHING, ///< Just waiting for client EOF
|
|
|
|
WS_HANDLER_CLOSED ///< Dead, both sides closed
|
2015-03-09 23:32:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ws_handler
|
|
|
|
{
|
|
|
|
enum ws_handler_state state; ///< State
|
|
|
|
|
2015-04-10 01:40:32 +02:00
|
|
|
// HTTP handshake:
|
|
|
|
|
2015-03-09 23:32:01 +01:00
|
|
|
http_parser hp; ///< HTTP parser
|
2017-02-06 18:28:40 +01:00
|
|
|
bool have_header_value; ///< Parsing header value or field?
|
2015-03-09 23:32:01 +01:00
|
|
|
struct str field; ///< Field part buffer
|
|
|
|
struct str value; ///< Value part buffer
|
|
|
|
struct str_map headers; ///< HTTP Headers
|
|
|
|
struct str url; ///< Request URL
|
2015-04-10 01:40:32 +02:00
|
|
|
ev_timer handshake_timeout_watcher; ///< Handshake timeout watcher
|
|
|
|
|
|
|
|
// WebSocket frame protocol:
|
2015-03-09 23:32:01 +01:00
|
|
|
|
|
|
|
struct ws_parser parser; ///< Protocol frame parser
|
2015-03-14 19:36:37 +01:00
|
|
|
bool expecting_continuation; ///< For non-control traffic
|
|
|
|
|
|
|
|
enum ws_opcode message_opcode; ///< Opcode for the current message
|
|
|
|
struct str message_data; ///< Concatenated message data
|
|
|
|
|
2015-04-10 01:40:32 +02:00
|
|
|
ev_timer ping_timer; ///< Ping timer
|
|
|
|
bool received_pong; ///< Received PONG since the last PING
|
|
|
|
|
|
|
|
ev_timer close_timeout_watcher; ///< Close timeout watcher
|
|
|
|
|
|
|
|
// Configuration:
|
|
|
|
|
|
|
|
unsigned handshake_timeout; ///< How long to wait for the handshake
|
|
|
|
unsigned close_timeout; ///< How long to wait for TCP close
|
2015-03-14 19:36:37 +01:00
|
|
|
unsigned ping_interval; ///< Ping interval in seconds
|
|
|
|
uint64_t max_payload_len; ///< Maximum length of any message
|
|
|
|
|
2015-04-10 01:40:32 +02:00
|
|
|
// Event callbacks:
|
2015-03-11 00:24:20 +01:00
|
|
|
|
2015-03-23 16:47:21 +01:00
|
|
|
// TODO: void (*on_handshake) (protocols) that will allow the user
|
|
|
|
// to choose any sub-protocol, if the client has provided any.
|
2015-04-10 01:40:32 +02:00
|
|
|
// This may render "on_connected" unnecessary.
|
2018-10-18 02:55:55 +02:00
|
|
|
// Should also enable failing the handshake.
|
2015-03-23 16:47:21 +01:00
|
|
|
|
2015-04-10 01:40:32 +02:00
|
|
|
/// Called after successfuly connecting (handshake complete)
|
2018-10-15 20:38:48 +02:00
|
|
|
bool (*on_connected) (struct ws_handler *);
|
2015-03-23 16:47:21 +01:00
|
|
|
|
2015-03-09 23:32:01 +01:00
|
|
|
/// Called upon reception of a single full message
|
2018-10-15 20:38:48 +02:00
|
|
|
bool (*on_message) (struct ws_handler *,
|
2015-03-14 19:36:37 +01:00
|
|
|
enum ws_opcode type, const void *data, size_t len);
|
|
|
|
|
2015-04-10 01:40:32 +02:00
|
|
|
/// The connection is about to close. @a close_code may, or may not, be one
|
2015-03-23 16:47:21 +01:00
|
|
|
/// of enum ws_status. The @a reason is never NULL.
|
2018-10-15 20:38:48 +02:00
|
|
|
void (*on_close) (struct ws_handler *, int close_code, const char *reason);
|
2015-03-09 23:32:01 +01:00
|
|
|
|
2018-10-15 19:29:17 +02:00
|
|
|
// Virtual method callbacks:
|
2015-04-10 01:40:32 +02:00
|
|
|
|
2015-03-09 23:32:01 +01:00
|
|
|
/// Write a chunk of data to the stream
|
2018-10-15 20:38:48 +02:00
|
|
|
void (*write_cb) (struct ws_handler *, const void *data, size_t len);
|
2015-03-09 23:32:01 +01:00
|
|
|
|
2018-10-16 04:09:59 +02:00
|
|
|
/// Close the connection. If @a half_close is false, you are allowed to
|
|
|
|
/// destroy the handler directly from within the callback.
|
|
|
|
void (*close_cb) (struct ws_handler *, bool half_close);
|
2015-03-09 23:32:01 +01:00
|
|
|
};
|
|
|
|
|
2015-03-14 19:36:37 +01:00
|
|
|
static void
|
2015-03-15 04:32:04 +01:00
|
|
|
ws_handler_send_control (struct ws_handler *self,
|
|
|
|
enum ws_opcode opcode, const void *data, size_t len)
|
2015-03-14 19:36:37 +01:00
|
|
|
{
|
|
|
|
if (len > WS_MAX_CONTROL_PAYLOAD_LEN)
|
|
|
|
{
|
|
|
|
print_debug ("truncating output control frame payload"
|
|
|
|
" from %zu to %zu bytes", len, (size_t) WS_MAX_CONTROL_PAYLOAD_LEN);
|
|
|
|
len = WS_MAX_CONTROL_PAYLOAD_LEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t header[2] = { 0x80 | (opcode & 0x0F), len };
|
2018-10-15 20:38:48 +02:00
|
|
|
self->write_cb (self, header, sizeof header);
|
|
|
|
self->write_cb (self, data, len);
|
2015-03-14 19:36:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-04-10 01:40:32 +02:00
|
|
|
ws_handler_close (struct ws_handler *self,
|
|
|
|
enum ws_status close_code, const char *reason, size_t len)
|
|
|
|
{
|
2018-10-18 02:55:55 +02:00
|
|
|
hard_assert (self->state == WS_HANDLER_OPEN);
|
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
struct str payload = str_make ();
|
2015-04-10 01:40:32 +02:00
|
|
|
str_pack_u16 (&payload, close_code);
|
|
|
|
// XXX: maybe accept a null-terminated string on input? Has to be UTF-8 a/w
|
|
|
|
str_append_data (&payload, reason, len);
|
|
|
|
ws_handler_send_control (self, WS_OPCODE_CLOSE, payload.str, payload.len);
|
2018-10-18 02:55:55 +02:00
|
|
|
self->close_cb (self, true /* half_close */);
|
2015-04-10 01:40:32 +02:00
|
|
|
|
|
|
|
self->state = WS_HANDLER_CLOSING;
|
|
|
|
str_free (&payload);
|
|
|
|
}
|
|
|
|
|
2018-10-18 02:55:55 +02:00
|
|
|
static bool
|
|
|
|
ws_handler_fail_connection (struct ws_handler *self, enum ws_status close_code)
|
2015-03-14 19:36:37 +01:00
|
|
|
{
|
2018-10-18 02:55:55 +02:00
|
|
|
hard_assert (self->state == WS_HANDLER_OPEN
|
|
|
|
|| self->state == WS_HANDLER_CLOSING);
|
|
|
|
|
|
|
|
if (self->state == WS_HANDLER_OPEN)
|
|
|
|
ws_handler_close (self, close_code, NULL, 0);
|
2015-03-14 19:36:37 +01:00
|
|
|
|
2018-10-18 02:55:55 +02:00
|
|
|
self->state = WS_HANDLER_FLUSHING;
|
|
|
|
if (self->on_close)
|
|
|
|
self->on_close (self, WS_STATUS_ABNORMAL_CLOSURE, "");
|
|
|
|
|
|
|
|
ev_timer_stop (EV_DEFAULT_ &self->ping_timer);
|
|
|
|
ev_timer_set (&self->close_timeout_watcher, self->close_timeout, 0.);
|
|
|
|
ev_timer_start (EV_DEFAULT_ &self->close_timeout_watcher);
|
|
|
|
return false;
|
2015-03-14 19:36:37 +01:00
|
|
|
}
|
|
|
|
|
2015-03-15 04:32:04 +01:00
|
|
|
// TODO: add support for fragmented responses
|
|
|
|
static void
|
2018-10-18 02:55:55 +02:00
|
|
|
ws_handler_send_frame (struct ws_handler *self,
|
2015-03-15 04:32:04 +01:00
|
|
|
enum ws_opcode opcode, const void *data, size_t len)
|
|
|
|
{
|
2015-04-10 01:40:32 +02:00
|
|
|
if (!soft_assert (self->state == WS_HANDLER_OPEN))
|
|
|
|
return;
|
2015-03-23 16:47:21 +01:00
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
struct str header = str_make ();
|
2015-03-15 04:32:04 +01:00
|
|
|
str_pack_u8 (&header, 0x80 | (opcode & 0x0F));
|
|
|
|
|
|
|
|
if (len > UINT16_MAX)
|
|
|
|
{
|
|
|
|
str_pack_u8 (&header, 127);
|
|
|
|
str_pack_u64 (&header, len);
|
|
|
|
}
|
|
|
|
else if (len > 125)
|
|
|
|
{
|
|
|
|
str_pack_u8 (&header, 126);
|
|
|
|
str_pack_u16 (&header, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
str_pack_u8 (&header, len);
|
|
|
|
|
2018-10-15 20:38:48 +02:00
|
|
|
self->write_cb (self, header.str, header.len);
|
|
|
|
self->write_cb (self, data, len);
|
2015-03-15 04:32:04 +01:00
|
|
|
str_free (&header);
|
|
|
|
}
|
|
|
|
|
2015-03-14 19:36:37 +01:00
|
|
|
static bool
|
|
|
|
ws_handler_on_frame_header (void *user_data, const struct ws_parser *parser)
|
|
|
|
{
|
|
|
|
struct ws_handler *self = user_data;
|
|
|
|
|
2015-03-23 16:47:21 +01:00
|
|
|
// Note that we aren't expected to send any close frame before closing the
|
|
|
|
// connection when the frame is unmasked
|
|
|
|
|
2015-03-14 19:36:37 +01:00
|
|
|
if (parser->reserved_1 || parser->reserved_2 || parser->reserved_3
|
|
|
|
|| !parser->is_masked // client -> server payload must be masked
|
|
|
|
|| (ws_is_control_frame (parser->opcode) &&
|
|
|
|
(!parser->is_fin || parser->payload_len > WS_MAX_CONTROL_PAYLOAD_LEN))
|
|
|
|
|| (!ws_is_control_frame (parser->opcode) &&
|
2015-03-23 16:47:21 +01:00
|
|
|
(self->expecting_continuation && parser->opcode != WS_OPCODE_CONT))
|
|
|
|
|| parser->payload_len >= 0x8000000000000000ULL)
|
2018-10-18 02:55:55 +02:00
|
|
|
return ws_handler_fail_connection (self, WS_STATUS_PROTOCOL_ERROR);
|
|
|
|
|
|
|
|
if (parser->payload_len > self->max_payload_len
|
|
|
|
|| (self->expecting_continuation &&
|
|
|
|
self->message_data.len + parser->payload_len > self->max_payload_len))
|
|
|
|
return ws_handler_fail_connection (self, WS_STATUS_MESSAGE_TOO_BIG);
|
|
|
|
return true;
|
2015-03-14 19:36:37 +01:00
|
|
|
}
|
|
|
|
|
2015-04-10 01:40:32 +02:00
|
|
|
static bool
|
2018-10-18 02:55:55 +02:00
|
|
|
ws_handler_on_control_close
|
2015-04-10 01:40:32 +02:00
|
|
|
(struct ws_handler *self, const struct ws_parser *parser)
|
|
|
|
{
|
2018-10-18 02:55:55 +02:00
|
|
|
hard_assert (self->state == WS_HANDLER_OPEN
|
|
|
|
|| self->state == WS_HANDLER_CLOSING);
|
2018-06-24 00:40:10 +02:00
|
|
|
struct msg_unpacker unpacker =
|
|
|
|
msg_unpacker_make (parser->input.str, parser->payload_len);
|
2015-04-10 01:40:32 +02:00
|
|
|
|
|
|
|
char *reason = NULL;
|
|
|
|
uint16_t close_code = WS_STATUS_NO_STATUS_RECEIVED;
|
|
|
|
if (parser->payload_len >= 2)
|
|
|
|
{
|
|
|
|
(void) msg_unpacker_u16 (&unpacker, &close_code);
|
|
|
|
reason = xstrndup (parser->input.str + 2, parser->payload_len - 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
reason = xstrdup ("");
|
|
|
|
|
2018-10-18 02:55:55 +02:00
|
|
|
if (close_code < 1000 || close_code > 4999)
|
2018-10-19 01:15:12 +02:00
|
|
|
// XXX: invalid close code: maybe we should fail the connection instead,
|
|
|
|
// although the specification doesn't say anything about this case
|
2018-10-18 02:55:55 +02:00
|
|
|
close_code = WS_STATUS_PROTOCOL_ERROR;
|
|
|
|
|
2018-10-19 01:15:12 +02:00
|
|
|
// Update the now potentially different close_code (lol const)
|
|
|
|
if (parser->payload_len >= 2)
|
|
|
|
{
|
|
|
|
parser->input.str[0] = close_code >> 8;
|
|
|
|
parser->input.str[1] = close_code;
|
|
|
|
}
|
|
|
|
|
2018-10-18 02:55:55 +02:00
|
|
|
if (self->state == WS_HANDLER_OPEN)
|
2015-04-10 01:40:32 +02:00
|
|
|
{
|
|
|
|
ws_handler_send_control (self, WS_OPCODE_CLOSE,
|
|
|
|
parser->input.str, parser->payload_len);
|
2018-10-18 02:55:55 +02:00
|
|
|
|
|
|
|
self->state = WS_HANDLER_FLUSHING;
|
2015-04-10 01:40:32 +02:00
|
|
|
if (self->on_close)
|
2018-10-15 20:38:48 +02:00
|
|
|
self->on_close (self, close_code, reason);
|
2015-04-10 01:40:32 +02:00
|
|
|
}
|
2018-10-18 02:55:55 +02:00
|
|
|
else
|
2018-10-19 01:15:12 +02:00
|
|
|
// Close initiated by us, flush the write queue and close the transport
|
2018-10-18 02:55:55 +02:00
|
|
|
self->state = WS_HANDLER_FLUSHING;
|
2015-04-10 01:40:32 +02:00
|
|
|
|
|
|
|
free (reason);
|
2018-10-18 02:55:55 +02:00
|
|
|
|
|
|
|
ev_timer_stop (EV_DEFAULT_ &self->ping_timer);
|
|
|
|
ev_timer_set (&self->close_timeout_watcher, self->close_timeout, 0.);
|
|
|
|
ev_timer_start (EV_DEFAULT_ &self->close_timeout_watcher);
|
2015-04-10 01:40:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-14 19:36:37 +01:00
|
|
|
static bool
|
|
|
|
ws_handler_on_control_frame
|
|
|
|
(struct ws_handler *self, const struct ws_parser *parser)
|
|
|
|
{
|
|
|
|
switch (parser->opcode)
|
|
|
|
{
|
|
|
|
case WS_OPCODE_CLOSE:
|
2018-10-18 02:55:55 +02:00
|
|
|
return ws_handler_on_control_close (self, parser);
|
2015-03-14 19:36:37 +01:00
|
|
|
case WS_OPCODE_PING:
|
|
|
|
ws_handler_send_control (self, WS_OPCODE_PONG,
|
|
|
|
parser->input.str, parser->payload_len);
|
|
|
|
break;
|
|
|
|
case WS_OPCODE_PONG:
|
2018-10-18 02:55:55 +02:00
|
|
|
// TODO: check the payload
|
2015-03-14 19:36:37 +01:00
|
|
|
self->received_pong = true;
|
|
|
|
break;
|
|
|
|
default:
|
2015-03-23 16:47:21 +01:00
|
|
|
// Unknown control frame
|
2018-10-18 02:55:55 +02:00
|
|
|
return ws_handler_fail_connection (self, WS_STATUS_PROTOCOL_ERROR);
|
2015-03-14 19:36:37 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-09 23:32:01 +01:00
|
|
|
static bool
|
|
|
|
ws_handler_on_frame (void *user_data, const struct ws_parser *parser)
|
|
|
|
{
|
|
|
|
struct ws_handler *self = user_data;
|
2015-03-14 19:36:37 +01:00
|
|
|
if (ws_is_control_frame (parser->opcode))
|
|
|
|
return ws_handler_on_control_frame (self, parser);
|
|
|
|
if (!self->expecting_continuation)
|
|
|
|
self->message_opcode = parser->opcode;
|
|
|
|
|
|
|
|
str_append_data (&self->message_data,
|
|
|
|
parser->input.str, parser->payload_len);
|
2018-10-18 02:55:55 +02:00
|
|
|
if ((self->expecting_continuation = !parser->is_fin))
|
2015-03-14 19:36:37 +01:00
|
|
|
return true;
|
|
|
|
|
2015-03-23 16:47:21 +01:00
|
|
|
if (self->message_opcode == WS_OPCODE_TEXT
|
2018-06-24 06:02:02 +02:00
|
|
|
&& !utf8_validate (self->message_data.str, self->message_data.len))
|
2015-03-23 16:47:21 +01:00
|
|
|
{
|
2018-10-18 02:55:55 +02:00
|
|
|
return ws_handler_fail_connection
|
|
|
|
(self, WS_STATUS_INVALID_PAYLOAD_DATA);
|
2015-03-23 16:47:21 +01:00
|
|
|
}
|
|
|
|
|
2015-04-10 01:40:32 +02:00
|
|
|
bool result = true;
|
|
|
|
if (self->on_message)
|
2018-10-15 20:38:48 +02:00
|
|
|
result = self->on_message (self, self->message_opcode,
|
2015-04-10 01:40:32 +02:00
|
|
|
self->message_data.str, self->message_data.len);
|
2015-03-14 19:36:37 +01:00
|
|
|
str_reset (&self->message_data);
|
2018-10-18 02:55:55 +02:00
|
|
|
// TODO: if (!result), either replace this with a state check,
|
|
|
|
// or make sure to change the state
|
2015-03-14 19:36:37 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ws_handler_on_ping_timer (EV_P_ ev_timer *watcher, int revents)
|
|
|
|
{
|
|
|
|
(void) loop;
|
|
|
|
(void) revents;
|
|
|
|
|
|
|
|
struct ws_handler *self = watcher->data;
|
|
|
|
if (!self->received_pong)
|
2018-10-19 01:15:12 +02:00
|
|
|
ws_handler_fail_connection (self, 4000 /* private use code */);
|
2015-04-10 01:40:32 +02:00
|
|
|
else
|
|
|
|
{
|
2018-10-18 02:55:55 +02:00
|
|
|
// TODO: be an annoying server and send a nonce in the data
|
2015-04-10 01:40:32 +02:00
|
|
|
ws_handler_send_control (self, WS_OPCODE_PING, NULL, 0);
|
|
|
|
ev_timer_again (EV_A_ watcher);
|
|
|
|
}
|
|
|
|
}
|
2015-03-14 19:36:37 +01:00
|
|
|
|
2015-04-10 01:40:32 +02:00
|
|
|
static void
|
|
|
|
ws_handler_on_close_timeout (EV_P_ ev_timer *watcher, int revents)
|
|
|
|
{
|
2018-10-18 02:55:55 +02:00
|
|
|
(void) loop;
|
2018-10-15 03:28:09 +02:00
|
|
|
(void) revents;
|
2015-04-10 01:40:32 +02:00
|
|
|
struct ws_handler *self = watcher->data;
|
2018-10-15 03:28:09 +02:00
|
|
|
|
2018-10-18 02:55:55 +02:00
|
|
|
hard_assert (self->state == WS_HANDLER_OPEN
|
|
|
|
|| self->state == WS_HANDLER_CLOSING);
|
|
|
|
|
|
|
|
if (self->state == WS_HANDLER_CLOSING
|
|
|
|
&& self->on_close)
|
|
|
|
self->on_close (self, WS_STATUS_ABNORMAL_CLOSURE, "close timeout");
|
|
|
|
|
|
|
|
self->state = WS_HANDLER_CLOSED;
|
|
|
|
self->close_cb (self, false /* half_close */);
|
2015-04-10 01:40:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ws_handler_on_handshake_timeout (EV_P_ ev_timer *watcher, int revents)
|
|
|
|
{
|
2018-10-18 02:55:55 +02:00
|
|
|
(void) loop;
|
2018-10-15 03:28:09 +02:00
|
|
|
(void) revents;
|
2015-04-10 01:40:32 +02:00
|
|
|
struct ws_handler *self = watcher->data;
|
2018-10-18 02:55:55 +02:00
|
|
|
|
|
|
|
// XXX: this is a no-op, since this currently doesn't even call shutdown
|
|
|
|
// immediately but postpones it until later
|
|
|
|
self->close_cb (self, true /* half_close */);
|
|
|
|
self->state = WS_HANDLER_FLUSHING;
|
|
|
|
|
|
|
|
if (self->on_close)
|
|
|
|
self->on_close (self, WS_STATUS_ABNORMAL_CLOSURE, "handshake timeout");
|
|
|
|
|
|
|
|
self->state = WS_HANDLER_CLOSED;
|
|
|
|
self->close_cb (self, false /* half_close */);
|
2015-03-09 23:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ws_handler_init (struct ws_handler *self)
|
|
|
|
{
|
|
|
|
memset (self, 0, sizeof *self);
|
|
|
|
|
2015-04-10 01:40:32 +02:00
|
|
|
self->state = WS_HANDLER_CONNECTING;
|
|
|
|
|
2015-03-09 23:32:01 +01:00
|
|
|
http_parser_init (&self->hp, HTTP_REQUEST);
|
|
|
|
self->hp.data = self;
|
2018-06-24 00:40:10 +02:00
|
|
|
self->field = str_make ();
|
|
|
|
self->value = str_make ();
|
|
|
|
self->headers = str_map_make (free);
|
2015-03-11 00:24:20 +01:00
|
|
|
self->headers.key_xfrm = tolower_ascii_strxfrm;
|
2018-06-24 00:40:10 +02:00
|
|
|
self->url = str_make ();
|
2015-04-10 01:40:32 +02:00
|
|
|
ev_timer_init (&self->handshake_timeout_watcher,
|
|
|
|
ws_handler_on_handshake_timeout, 0., 0.);
|
|
|
|
self->handshake_timeout_watcher.data = self;
|
2015-03-09 23:32:01 +01:00
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
self->parser = ws_parser_make ();
|
2015-03-14 19:36:37 +01:00
|
|
|
self->parser.on_frame_header = ws_handler_on_frame_header;
|
2015-03-09 23:32:01 +01:00
|
|
|
self->parser.on_frame = ws_handler_on_frame;
|
2017-02-06 18:51:52 +01:00
|
|
|
self->parser.user_data = self;
|
2018-06-24 00:40:10 +02:00
|
|
|
self->message_data = str_make ();
|
2015-03-14 19:36:37 +01:00
|
|
|
|
2015-04-10 01:40:32 +02:00
|
|
|
ev_timer_init (&self->ping_timer,
|
|
|
|
ws_handler_on_ping_timer, 0., 0.);
|
|
|
|
self->ping_timer.data = self;
|
|
|
|
ev_timer_init (&self->close_timeout_watcher,
|
|
|
|
ws_handler_on_close_timeout, 0., 0.);
|
|
|
|
self->ping_timer.data = self;
|
|
|
|
// So that the first ping timer doesn't timeout the connection
|
|
|
|
self->received_pong = true;
|
|
|
|
|
|
|
|
self->handshake_timeout = self->close_timeout = self->ping_interval = 60;
|
2015-03-23 16:47:21 +01:00
|
|
|
// This is still ridiculously high. Note that the most significant bit
|
|
|
|
// must always be zero, i.e. the protocol maximum is 0x7FFF FFFF FFFF FFFF.
|
2015-03-14 19:36:37 +01:00
|
|
|
self->max_payload_len = UINT32_MAX;
|
2015-04-10 01:40:32 +02:00
|
|
|
}
|
2015-03-14 19:36:37 +01:00
|
|
|
|
2015-04-10 01:40:32 +02:00
|
|
|
/// Stop all timers, not going to use the handler anymore
|
|
|
|
static void
|
|
|
|
ws_handler_stop (struct ws_handler *self)
|
|
|
|
{
|
|
|
|
ev_timer_stop (EV_DEFAULT_ &self->handshake_timeout_watcher);
|
|
|
|
ev_timer_stop (EV_DEFAULT_ &self->ping_timer);
|
|
|
|
ev_timer_stop (EV_DEFAULT_ &self->close_timeout_watcher);
|
2015-03-09 23:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ws_handler_free (struct ws_handler *self)
|
|
|
|
{
|
2015-04-10 01:40:32 +02:00
|
|
|
ws_handler_stop (self);
|
|
|
|
|
2015-03-09 23:32:01 +01:00
|
|
|
str_free (&self->field);
|
|
|
|
str_free (&self->value);
|
|
|
|
str_map_free (&self->headers);
|
|
|
|
str_free (&self->url);
|
2015-04-10 01:40:32 +02:00
|
|
|
|
2015-03-09 23:32:01 +01:00
|
|
|
ws_parser_free (&self->parser);
|
2015-03-14 19:36:37 +01:00
|
|
|
str_free (&self->message_data);
|
2015-03-09 23:32:01 +01:00
|
|
|
}
|
|
|
|
|
2015-03-22 22:35:58 +01:00
|
|
|
static bool
|
|
|
|
ws_handler_header_field_is_a_list (const char *name)
|
|
|
|
{
|
|
|
|
// This must contain all header fields we use for anything
|
|
|
|
static const char *concatenable[] =
|
|
|
|
{ SEC_WS_PROTOCOL, SEC_WS_EXTENSIONS, "Connection", "Upgrade" };
|
|
|
|
|
|
|
|
for (size_t i = 0; i < N_ELEMENTS (concatenable); i++)
|
|
|
|
if (!strcasecmp_ascii (name, concatenable[i]))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-09 23:32:01 +01:00
|
|
|
static void
|
|
|
|
ws_handler_on_header_read (struct ws_handler *self)
|
|
|
|
{
|
2015-03-22 22:35:58 +01:00
|
|
|
// The HTTP parser unfolds values and removes preceding whitespace, but
|
|
|
|
// otherwise doesn't touch the values or the following whitespace.
|
|
|
|
|
|
|
|
// RFC 7230 states that trailing whitespace is not part of a field value
|
|
|
|
char *value = self->field.str;
|
|
|
|
size_t len = self->field.len;
|
|
|
|
while (len--)
|
|
|
|
if (value[len] == '\t' || value[len] == ' ')
|
|
|
|
value[len] = '\0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
self->field.len = len;
|
2015-03-15 04:32:04 +01:00
|
|
|
|
2015-03-22 22:35:58 +01:00
|
|
|
const char *field = self->field.str;
|
2015-03-15 04:32:04 +01:00
|
|
|
const char *current = str_map_find (&self->headers, field);
|
2015-03-22 22:35:58 +01:00
|
|
|
if (ws_handler_header_field_is_a_list (field) && current)
|
2015-03-15 04:32:04 +01:00
|
|
|
str_map_set (&self->headers, field,
|
|
|
|
xstrdup_printf ("%s, %s", current, self->value.str));
|
|
|
|
else
|
|
|
|
// If the field cannot be concatenated, just overwrite the last value.
|
|
|
|
// Maybe we should issue a warning or something.
|
|
|
|
str_map_set (&self->headers, field, xstrdup (self->value.str));
|
2015-03-09 23:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ws_handler_on_header_field (http_parser *parser, const char *at, size_t len)
|
|
|
|
{
|
|
|
|
struct ws_handler *self = parser->data;
|
2017-02-06 18:28:40 +01:00
|
|
|
if (self->have_header_value)
|
2015-03-09 23:32:01 +01:00
|
|
|
{
|
|
|
|
ws_handler_on_header_read (self);
|
|
|
|
str_reset (&self->field);
|
|
|
|
str_reset (&self->value);
|
|
|
|
}
|
|
|
|
str_append_data (&self->field, at, len);
|
2017-02-06 18:28:40 +01:00
|
|
|
self->have_header_value = false;
|
2015-03-09 23:32:01 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ws_handler_on_header_value (http_parser *parser, const char *at, size_t len)
|
|
|
|
{
|
|
|
|
struct ws_handler *self = parser->data;
|
|
|
|
str_append_data (&self->value, at, len);
|
2017-02-06 18:28:40 +01:00
|
|
|
self->have_header_value = true;
|
2015-03-09 23:32:01 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ws_handler_on_headers_complete (http_parser *parser)
|
|
|
|
{
|
2017-02-06 18:28:40 +01:00
|
|
|
struct ws_handler *self = parser->data;
|
|
|
|
if (self->have_header_value)
|
|
|
|
ws_handler_on_header_read (self);
|
|
|
|
|
2015-03-14 19:36:37 +01:00
|
|
|
// We strictly require a protocol upgrade
|
|
|
|
if (!parser->upgrade)
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
return 0;
|
2015-03-09 23:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ws_handler_on_url (http_parser *parser, const char *at, size_t len)
|
|
|
|
{
|
|
|
|
struct ws_handler *self = parser->data;
|
2020-10-13 04:46:08 +02:00
|
|
|
str_append_data (&self->url, at, len);
|
2015-03-09 23:32:01 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-15 04:32:04 +01:00
|
|
|
#define HTTP_101_SWITCHING_PROTOCOLS "101 Switching Protocols"
|
|
|
|
#define HTTP_400_BAD_REQUEST "400 Bad Request"
|
|
|
|
#define HTTP_405_METHOD_NOT_ALLOWED "405 Method Not Allowed"
|
2015-03-22 22:35:58 +01:00
|
|
|
#define HTTP_417_EXPECTATION_FAILED "407 Expectation Failed"
|
2018-10-18 02:55:55 +02:00
|
|
|
#define HTTP_426_UPGRADE_REQUIRED "426 Upgrade Required"
|
2015-03-15 04:32:04 +01:00
|
|
|
#define HTTP_505_VERSION_NOT_SUPPORTED "505 HTTP Version Not Supported"
|
|
|
|
|
|
|
|
static void
|
|
|
|
ws_handler_http_responsev (struct ws_handler *self,
|
|
|
|
const char *status, char *const *fields)
|
|
|
|
{
|
|
|
|
hard_assert (status != NULL);
|
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
struct str response = str_make ();
|
2015-03-15 04:32:04 +01:00
|
|
|
str_append_printf (&response, "HTTP/1.1 %s\r\n", status);
|
|
|
|
|
|
|
|
while (*fields)
|
|
|
|
str_append_printf (&response, "%s\r\n", *fields++);
|
|
|
|
|
2018-06-24 05:59:55 +02:00
|
|
|
time_t now = time (NULL);
|
|
|
|
struct tm ts;
|
|
|
|
gmtime_r (&now, &ts);
|
|
|
|
|
|
|
|
// See RFC 7231, 7.1.1.2. Date
|
|
|
|
const char *dow[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
|
|
|
|
const char *moy[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
|
|
|
|
str_append_printf (&response,
|
|
|
|
"Date: %s, %02d %s %04d %02d:%02d:%02d GMT\r\n",
|
|
|
|
dow[ts.tm_wday], ts.tm_mday, moy[ts.tm_mon], ts.tm_year + 1900,
|
|
|
|
ts.tm_hour, ts.tm_min, ts.tm_sec);
|
|
|
|
|
2015-03-15 04:32:04 +01:00
|
|
|
str_append (&response, "Server: "
|
|
|
|
PROGRAM_NAME "/" PROGRAM_VERSION "\r\n\r\n");
|
2018-10-15 20:38:48 +02:00
|
|
|
self->write_cb (self, response.str, response.len);
|
2015-03-15 04:32:04 +01:00
|
|
|
str_free (&response);
|
|
|
|
}
|
|
|
|
|
2018-10-18 02:55:55 +02:00
|
|
|
static bool
|
|
|
|
ws_handler_fail_handshake (struct ws_handler *self, const char *status, ...)
|
2015-03-15 04:32:04 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, status);
|
|
|
|
|
|
|
|
const char *s;
|
2018-10-18 02:55:55 +02:00
|
|
|
struct strv v = strv_make ();
|
2015-03-15 04:32:04 +01:00
|
|
|
while ((s = va_arg (ap, const char *)))
|
2017-02-05 22:44:01 +01:00
|
|
|
strv_append (&v, s);
|
2015-03-15 04:32:04 +01:00
|
|
|
|
|
|
|
va_end (ap);
|
|
|
|
ws_handler_http_responsev (self, status, v.vector);
|
2017-02-05 22:44:01 +01:00
|
|
|
strv_free (&v);
|
2018-10-18 02:55:55 +02:00
|
|
|
|
|
|
|
self->close_cb (self, true /* half_close */);
|
|
|
|
self->state = WS_HANDLER_FLUSHING;
|
|
|
|
|
|
|
|
if (self->on_close)
|
|
|
|
self->on_close (self, WS_STATUS_ABNORMAL_CLOSURE, status);
|
|
|
|
return false;
|
2015-03-15 04:32:04 +01:00
|
|
|
}
|
|
|
|
|
2018-10-18 02:55:55 +02:00
|
|
|
#define FAIL_HANDSHAKE(...) \
|
|
|
|
return ws_handler_fail_handshake (self, __VA_ARGS__, NULL)
|
2015-03-15 04:32:04 +01:00
|
|
|
|
2015-03-09 23:32:01 +01:00
|
|
|
static bool
|
|
|
|
ws_handler_finish_handshake (struct ws_handler *self)
|
|
|
|
{
|
2015-03-15 04:32:04 +01:00
|
|
|
if (self->hp.method != HTTP_GET)
|
2018-10-18 02:55:55 +02:00
|
|
|
FAIL_HANDSHAKE (HTTP_405_METHOD_NOT_ALLOWED, "Allow: GET");
|
|
|
|
|
|
|
|
// Technically, it must be /at least/ 1.1 but no other 1.x version of HTTP
|
|
|
|
// is going to happen and 2.x is entirely incompatible
|
|
|
|
// XXX: we probably shouldn't use 505 to reject the minor version but w/e
|
|
|
|
if (self->hp.http_major != 1 || self->hp.http_minor != 1)
|
|
|
|
FAIL_HANDSHAKE (HTTP_505_VERSION_NOT_SUPPORTED);
|
2015-03-09 23:32:01 +01:00
|
|
|
|
2015-03-22 22:35:58 +01:00
|
|
|
// Your expectations are way too high
|
|
|
|
if (str_map_find (&self->headers, "Expect"))
|
2018-10-18 02:55:55 +02:00
|
|
|
FAIL_HANDSHAKE (HTTP_417_EXPECTATION_FAILED);
|
2015-03-22 22:35:58 +01:00
|
|
|
|
|
|
|
// Reject URLs specifying the schema and host; we're not parsing that
|
|
|
|
// TODO: actually do parse this and let our user decide if it matches
|
2015-03-15 04:32:04 +01:00
|
|
|
struct http_parser_url url;
|
|
|
|
if (http_parser_parse_url (self->url.str, self->url.len, false, &url)
|
2015-03-22 22:35:58 +01:00
|
|
|
|| (url.field_set & (1 << UF_SCHEMA | 1 << UF_HOST | 1 << UF_PORT))
|
|
|
|
|| !str_map_find (&self->headers, "Host"))
|
2018-10-18 02:55:55 +02:00
|
|
|
FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST);
|
2015-03-09 23:32:01 +01:00
|
|
|
|
2015-03-22 22:35:58 +01:00
|
|
|
const char *connection = str_map_find (&self->headers, "Connection");
|
|
|
|
if (!connection || strcasecmp_ascii (connection, "Upgrade"))
|
2018-10-18 02:55:55 +02:00
|
|
|
FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST);
|
2015-03-22 22:35:58 +01:00
|
|
|
|
|
|
|
// Check if we can actually upgrade the protocol to WebSockets
|
|
|
|
const char *upgrade = str_map_find (&self->headers, "Upgrade");
|
|
|
|
struct http_protocol *offered_upgrades = NULL;
|
|
|
|
bool can_upgrade = false;
|
|
|
|
if (upgrade && http_parse_upgrade (upgrade, &offered_upgrades))
|
|
|
|
// Case-insensitive according to RFC 6455; neither RFC 2616 nor 7230
|
|
|
|
// say anything at all about case-sensitivity for this field
|
|
|
|
LIST_FOR_EACH (struct http_protocol, iter, offered_upgrades)
|
|
|
|
{
|
|
|
|
if (!iter->version && !strcasecmp_ascii (iter->name, "websocket"))
|
|
|
|
can_upgrade = true;
|
|
|
|
http_protocol_destroy (iter);
|
|
|
|
}
|
|
|
|
if (!can_upgrade)
|
2018-10-18 02:55:55 +02:00
|
|
|
FAIL_HANDSHAKE (HTTP_426_UPGRADE_REQUIRED,
|
|
|
|
"Upgrade: websocket", SEC_WS_VERSION ": 13");
|
2015-03-22 22:35:58 +01:00
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
// Okay, we're finally past the basic HTTP/1.1 stuff
|
2018-10-15 03:28:09 +02:00
|
|
|
const char *key = str_map_find (&self->headers, SEC_WS_KEY);
|
|
|
|
const char *version = str_map_find (&self->headers, SEC_WS_VERSION);
|
|
|
|
/*
|
|
|
|
const char *protocol = str_map_find (&self->headers, SEC_WS_PROTOCOL);
|
|
|
|
const char *extensions = str_map_find (&self->headers, SEC_WS_EXTENSIONS);
|
|
|
|
*/
|
2015-03-09 23:32:01 +01:00
|
|
|
|
2018-10-18 02:55:55 +02:00
|
|
|
if (!version)
|
|
|
|
FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST);
|
|
|
|
if (strcmp (version, "13"))
|
|
|
|
FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST, SEC_WS_VERSION ": 13");
|
2017-02-06 18:28:53 +01:00
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
struct str tmp = str_make ();
|
2018-10-18 02:55:55 +02:00
|
|
|
bool key_is_valid = key
|
|
|
|
&& base64_decode (key, false, &tmp) && tmp.len == 16;
|
2015-03-22 22:35:58 +01:00
|
|
|
str_free (&tmp);
|
|
|
|
if (!key_is_valid)
|
2018-10-18 02:55:55 +02:00
|
|
|
FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST);
|
2015-03-14 19:36:37 +01:00
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
struct strv fields = strv_make ();
|
2017-02-05 22:44:01 +01:00
|
|
|
strv_append_args (&fields,
|
2015-03-15 04:32:04 +01:00
|
|
|
"Upgrade: websocket",
|
|
|
|
"Connection: Upgrade",
|
|
|
|
NULL);
|
2015-03-09 23:32:01 +01:00
|
|
|
|
|
|
|
char *response_key = ws_encode_response_key (key);
|
2017-02-05 22:44:01 +01:00
|
|
|
strv_append_owned (&fields,
|
2015-03-15 04:32:04 +01:00
|
|
|
xstrdup_printf (SEC_WS_ACCEPT ": %s", response_key));
|
2015-03-09 23:32:01 +01:00
|
|
|
free (response_key);
|
|
|
|
|
2018-10-15 03:28:09 +02:00
|
|
|
// TODO: make it possible to choose Sec-Websocket-{Extensions,Protocol}
|
2015-03-15 04:32:04 +01:00
|
|
|
|
|
|
|
ws_handler_http_responsev (self,
|
|
|
|
HTTP_101_SWITCHING_PROTOCOLS, fields.vector);
|
|
|
|
|
2017-02-05 22:44:01 +01:00
|
|
|
strv_free (&fields);
|
2015-03-14 19:36:37 +01:00
|
|
|
|
2018-10-18 02:55:55 +02:00
|
|
|
self->state = WS_HANDLER_OPEN;
|
2015-04-10 01:40:32 +02:00
|
|
|
ev_timer_init (&self->ping_timer, ws_handler_on_ping_timer,
|
|
|
|
self->ping_interval, 0);
|
2015-03-14 19:36:37 +01:00
|
|
|
ev_timer_start (EV_DEFAULT_ &self->ping_timer);
|
2015-03-09 23:32:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-10 01:40:32 +02:00
|
|
|
/// Tells the handler that the TCP connection has been established so it can
|
|
|
|
/// timeout when the client handshake doesn't arrive soon enough
|
|
|
|
static void
|
|
|
|
ws_handler_start (struct ws_handler *self)
|
|
|
|
{
|
2018-10-18 02:55:55 +02:00
|
|
|
hard_assert (self->state == WS_HANDLER_CONNECTING);
|
|
|
|
|
2015-04-10 01:40:32 +02:00
|
|
|
ev_timer_set (&self->handshake_timeout_watcher,
|
|
|
|
self->handshake_timeout, 0.);
|
|
|
|
ev_timer_start (EV_DEFAULT_ &self->handshake_timeout_watcher);
|
|
|
|
}
|
|
|
|
|
2018-10-18 02:55:55 +02:00
|
|
|
// The client should normally never close the connection, assume that it's
|
|
|
|
// either received an EOF from our side, or that it doesn't care about our data
|
|
|
|
// anymore, having called close() already
|
2015-03-09 23:32:01 +01:00
|
|
|
static bool
|
2018-10-18 02:55:55 +02:00
|
|
|
ws_handler_push_eof (struct ws_handler *self)
|
2015-03-09 23:32:01 +01:00
|
|
|
{
|
2018-10-18 02:55:55 +02:00
|
|
|
switch (self->state)
|
2015-03-15 04:32:04 +01:00
|
|
|
{
|
2018-10-18 02:55:55 +02:00
|
|
|
case WS_HANDLER_CONNECTING:
|
2015-04-10 01:40:32 +02:00
|
|
|
ev_timer_stop (EV_DEFAULT_ &self->handshake_timeout_watcher);
|
|
|
|
|
2018-10-18 02:55:55 +02:00
|
|
|
self->state = WS_HANDLER_FLUSHING;
|
|
|
|
if (self->on_close)
|
|
|
|
self->on_close (self, WS_STATUS_ABNORMAL_CLOSURE, "unexpected EOF");
|
|
|
|
break;
|
|
|
|
case WS_HANDLER_OPEN:
|
|
|
|
ev_timer_stop (EV_DEFAULT_ &self->ping_timer);
|
|
|
|
// Fall-through
|
|
|
|
case WS_HANDLER_CLOSING:
|
2015-03-23 16:47:21 +01:00
|
|
|
self->state = WS_HANDLER_CLOSED;
|
2018-10-18 02:55:55 +02:00
|
|
|
if (self->on_close)
|
|
|
|
self->on_close (self, WS_STATUS_ABNORMAL_CLOSURE, "");
|
|
|
|
// Fall-through
|
|
|
|
case WS_HANDLER_FLUSHING:
|
|
|
|
ev_timer_stop (EV_DEFAULT_ &self->close_timeout_watcher);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
soft_assert(self->state != WS_HANDLER_CLOSED);
|
2015-03-15 04:32:04 +01:00
|
|
|
}
|
2018-10-18 02:55:55 +02:00
|
|
|
self->state = WS_HANDLER_CLOSED;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Push data to the WebSocket handler. "len == 0" means EOF.
|
|
|
|
/// You are expected to close the connection and dispose of the handler
|
|
|
|
/// when the function returns false.
|
|
|
|
static bool
|
|
|
|
ws_handler_push (struct ws_handler *self, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
if (!len)
|
|
|
|
return ws_handler_push_eof (self);
|
2015-03-09 23:32:01 +01:00
|
|
|
|
2018-10-18 02:55:55 +02:00
|
|
|
if (self->state == WS_HANDLER_FLUSHING)
|
2015-04-10 01:40:32 +02:00
|
|
|
// We're waiting for an EOF from the client, must not process data
|
|
|
|
return true;
|
2018-10-18 02:55:55 +02:00
|
|
|
|
2015-03-23 16:47:21 +01:00
|
|
|
if (self->state != WS_HANDLER_CONNECTING)
|
2018-10-18 02:55:55 +02:00
|
|
|
return soft_assert (self->state != WS_HANDLER_CLOSED)
|
|
|
|
&& ws_parser_push (&self->parser, data, len);
|
2015-03-23 16:47:21 +01:00
|
|
|
|
2015-03-09 23:32:01 +01:00
|
|
|
// The handshake hasn't been done yet, process HTTP headers
|
|
|
|
static const http_parser_settings http_settings =
|
|
|
|
{
|
|
|
|
.on_header_field = ws_handler_on_header_field,
|
|
|
|
.on_header_value = ws_handler_on_header_value,
|
|
|
|
.on_headers_complete = ws_handler_on_headers_complete,
|
|
|
|
.on_url = ws_handler_on_url,
|
|
|
|
};
|
|
|
|
|
2018-10-18 02:55:55 +02:00
|
|
|
size_t n_parsed =
|
|
|
|
http_parser_execute (&self->hp, &http_settings, data, len);
|
2015-03-09 23:32:01 +01:00
|
|
|
|
|
|
|
if (self->hp.upgrade)
|
|
|
|
{
|
2015-04-10 01:40:32 +02:00
|
|
|
ev_timer_stop (EV_DEFAULT_ &self->handshake_timeout_watcher);
|
|
|
|
|
2015-03-15 04:32:04 +01:00
|
|
|
// The handshake hasn't been finished, yet there is more data
|
|
|
|
// to be processed after the headers already
|
2015-03-09 23:32:01 +01:00
|
|
|
if (len - n_parsed)
|
2018-10-18 02:55:55 +02:00
|
|
|
FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST);
|
2015-03-09 23:32:01 +01:00
|
|
|
|
|
|
|
if (!ws_handler_finish_handshake (self))
|
|
|
|
return false;
|
2015-04-10 01:40:32 +02:00
|
|
|
if (self->on_connected)
|
2018-10-15 20:38:48 +02:00
|
|
|
return self->on_connected (self);
|
2015-03-09 23:32:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-03-14 19:36:37 +01:00
|
|
|
|
2015-03-15 04:32:04 +01:00
|
|
|
enum http_errno err = HTTP_PARSER_ERRNO (&self->hp);
|
|
|
|
if (n_parsed != len || err != HPE_OK)
|
2015-03-09 23:32:01 +01:00
|
|
|
{
|
2015-04-10 01:40:32 +02:00
|
|
|
ev_timer_stop (EV_DEFAULT_ &self->handshake_timeout_watcher);
|
|
|
|
|
2015-03-15 04:32:04 +01:00
|
|
|
if (err == HPE_CB_headers_complete)
|
|
|
|
print_debug ("WS handshake failed: %s", "missing `Upgrade' field");
|
|
|
|
else
|
|
|
|
print_debug ("WS handshake failed: %s",
|
|
|
|
http_errno_description (err));
|
2015-03-09 23:32:01 +01:00
|
|
|
|
2018-10-18 02:55:55 +02:00
|
|
|
FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST);
|
2015-03-15 04:32:04 +01:00
|
|
|
}
|
2015-03-09 23:32:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-15 05:07:57 +02:00
|
|
|
/// @}
|
2015-03-02 23:11:29 +01:00
|
|
|
// --- Server ------------------------------------------------------------------
|
|
|
|
|
2016-01-16 06:41:31 +01:00
|
|
|
static struct simple_config_item g_config_table[] =
|
2015-03-02 23:11:29 +01:00
|
|
|
{
|
|
|
|
{ "bind_host", NULL, "Address of the server" },
|
|
|
|
{ "port_fastcgi", "9000", "Port to bind for FastCGI" },
|
|
|
|
{ "port_scgi", NULL, "Port to bind for SCGI" },
|
2015-03-08 09:41:10 +01:00
|
|
|
{ "port_ws", NULL, "Port to bind for WebSockets" },
|
2015-03-11 23:56:25 +01:00
|
|
|
{ "pid_file", NULL, "Full path for the PID file" },
|
2015-03-14 19:36:37 +01:00
|
|
|
// XXX: here belongs something like a web SPA that interfaces with us
|
2015-03-08 09:41:10 +01:00
|
|
|
{ "static_root", NULL, "The root for static content" },
|
2015-03-02 23:11:29 +01:00
|
|
|
{ NULL, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct server_context
|
|
|
|
{
|
|
|
|
ev_signal sigterm_watcher; ///< Got SIGTERM
|
|
|
|
ev_signal sigint_watcher; ///< Got SIGINT
|
2015-04-10 02:44:13 +02:00
|
|
|
ev_timer quit_timeout_watcher; ///< Quit timeout watcher
|
2015-03-02 23:11:29 +01:00
|
|
|
bool quitting; ///< User requested quitting
|
|
|
|
|
|
|
|
struct listener *listeners; ///< Listeners
|
|
|
|
size_t n_listeners; ///< Number of listening sockets
|
|
|
|
|
|
|
|
struct client *clients; ///< Clients
|
|
|
|
unsigned n_clients; ///< Current number of connections
|
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
struct request_handler *handlers; ///< Request handlers
|
2015-03-02 23:11:29 +01:00
|
|
|
struct str_map config; ///< Server configuration
|
|
|
|
};
|
|
|
|
|
2015-04-10 02:44:13 +02:00
|
|
|
static void initiate_quit (struct server_context *self);
|
|
|
|
static void try_finish_quit (struct server_context *self);
|
2016-01-16 22:16:01 +01:00
|
|
|
static void on_quit_timeout (EV_P_ ev_timer *watcher, int revents);
|
2015-04-10 02:44:13 +02:00
|
|
|
static void close_listeners (struct server_context *self);
|
|
|
|
|
2015-03-02 23:11:29 +01:00
|
|
|
static void
|
|
|
|
server_context_init (struct server_context *self)
|
|
|
|
{
|
|
|
|
memset (self, 0, sizeof *self);
|
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
self->config = str_map_make (NULL);
|
2016-01-16 06:41:31 +01:00
|
|
|
simple_config_load_defaults (&self->config, g_config_table);
|
2017-02-06 17:18:24 +01:00
|
|
|
ev_timer_init (&self->quit_timeout_watcher, on_quit_timeout, 3., 0.);
|
2015-04-10 02:44:13 +02:00
|
|
|
self->quit_timeout_watcher.data = self;
|
2015-03-02 23:11:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
server_context_free (struct server_context *self)
|
|
|
|
{
|
2015-03-14 19:36:37 +01:00
|
|
|
// We really shouldn't attempt a quit without closing the clients first
|
|
|
|
soft_assert (!self->clients);
|
|
|
|
|
|
|
|
close_listeners (self);
|
|
|
|
free (self->listeners);
|
2015-03-02 23:11:29 +01:00
|
|
|
|
|
|
|
str_map_free (&self->config);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- JSON-RPC ----------------------------------------------------------------
|
2018-10-15 05:07:57 +02:00
|
|
|
/// @defgroup JSON-RPC
|
|
|
|
/// @{
|
2015-03-02 23:11:29 +01:00
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
#define JSON_RPC_ERROR_TABLE(XX) \
|
|
|
|
XX (-32700, PARSE_ERROR, "Parse error") \
|
|
|
|
XX (-32600, INVALID_REQUEST, "Invalid Request") \
|
|
|
|
XX (-32601, METHOD_NOT_FOUND, "Method not found") \
|
|
|
|
XX (-32602, INVALID_PARAMS, "Invalid params") \
|
|
|
|
XX (-32603, INTERNAL_ERROR, "Internal error")
|
|
|
|
|
|
|
|
enum json_rpc_error
|
|
|
|
{
|
|
|
|
#define XX(code, name, message) JSON_RPC_ERROR_ ## name,
|
|
|
|
JSON_RPC_ERROR_TABLE (XX)
|
|
|
|
#undef XX
|
|
|
|
JSON_RPC_ERROR_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
static json_t *
|
|
|
|
json_rpc_error (enum json_rpc_error id, json_t *data)
|
|
|
|
{
|
|
|
|
#define XX(code, name, message) { code, message },
|
|
|
|
static const struct json_rpc_error
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
const char *message;
|
|
|
|
}
|
|
|
|
errors[JSON_RPC_ERROR_COUNT] =
|
|
|
|
{
|
|
|
|
JSON_RPC_ERROR_TABLE (XX)
|
|
|
|
};
|
|
|
|
#undef XX
|
|
|
|
|
|
|
|
json_t *error = json_object ();
|
|
|
|
json_object_set_new (error, "code", json_integer (errors[id].code));
|
|
|
|
json_object_set_new (error, "message", json_string (errors[id].message));
|
2015-03-02 23:11:29 +01:00
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
if (data)
|
|
|
|
json_object_set_new (error, "data", data);
|
2015-03-02 23:11:29 +01:00
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static json_t *
|
|
|
|
json_rpc_response (json_t *id, json_t *result, json_t *error)
|
|
|
|
{
|
|
|
|
json_t *x = json_object ();
|
|
|
|
json_object_set_new (x, "jsonrpc", json_string ("2.0"));
|
|
|
|
json_object_set_new (x, "id", id ? id : json_null ());
|
|
|
|
if (result) json_object_set_new (x, "result", result);
|
|
|
|
if (error) json_object_set_new (x, "error", error);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2015-03-06 19:49:33 +01:00
|
|
|
|
2015-03-05 08:47:20 +01:00
|
|
|
static bool
|
2015-03-15 04:32:04 +01:00
|
|
|
validate_json_rpc_content_type (const char *content_type)
|
2015-03-05 08:47:20 +01:00
|
|
|
{
|
2015-03-15 04:32:04 +01:00
|
|
|
char *type = NULL;
|
|
|
|
char *subtype = NULL;
|
2015-03-05 08:47:20 +01:00
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
struct str_map parameters = str_map_make (free);
|
2015-03-15 04:32:04 +01:00
|
|
|
parameters.key_xfrm = tolower_ascii_strxfrm;
|
2015-03-05 08:47:20 +01:00
|
|
|
|
2015-03-15 04:32:04 +01:00
|
|
|
bool result = http_parse_media_type
|
|
|
|
(content_type, &type, &subtype, ¶meters);
|
|
|
|
if (!result)
|
|
|
|
goto end;
|
2015-03-05 08:47:20 +01:00
|
|
|
|
2015-03-15 04:32:04 +01:00
|
|
|
if (strcasecmp_ascii (type, "application")
|
|
|
|
|| (strcasecmp_ascii (subtype, "json") &&
|
|
|
|
strcasecmp_ascii (subtype, "json-rpc" /* obsolete */)))
|
|
|
|
result = false;
|
2015-03-05 08:47:20 +01:00
|
|
|
|
2015-03-15 04:32:04 +01:00
|
|
|
const char *charset = str_map_find (¶meters, "charset");
|
|
|
|
if (charset && strcasecmp_ascii (charset, "UTF-8"))
|
|
|
|
result = false;
|
|
|
|
|
|
|
|
// Currently ignoring all unknown parametrs
|
2015-03-05 08:47:20 +01:00
|
|
|
|
2015-03-15 04:32:04 +01:00
|
|
|
end:
|
|
|
|
free (type);
|
|
|
|
free (subtype);
|
|
|
|
str_map_free (¶meters);
|
|
|
|
return result;
|
2015-03-05 08:47:20 +01:00
|
|
|
}
|
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2020-09-01 20:55:22 +02:00
|
|
|
/// Handlers must not set the `id` field in their responses, that will be filled
|
|
|
|
/// in automatically according to whether the request is a notification or not.
|
2015-03-11 00:24:20 +01:00
|
|
|
typedef json_t *(*json_rpc_handler_fn) (struct server_context *, json_t *);
|
|
|
|
|
|
|
|
struct json_rpc_handler_info
|
|
|
|
{
|
|
|
|
const char *method_name; ///< JSON-RPC method name
|
|
|
|
json_rpc_handler_fn handler; ///< Method handler
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
json_rpc_handler_info_cmp (const void *first, const void *second)
|
|
|
|
{
|
|
|
|
return strcmp (((struct json_rpc_handler_info *) first)->method_name,
|
|
|
|
((struct json_rpc_handler_info *) second)->method_name);
|
|
|
|
}
|
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
static json_t *
|
|
|
|
json_rpc_ping (struct server_context *ctx, json_t *params)
|
|
|
|
{
|
|
|
|
(void) ctx;
|
2020-09-01 20:56:03 +02:00
|
|
|
|
|
|
|
if (params && !json_is_null (params))
|
|
|
|
return json_rpc_response (NULL, NULL,
|
|
|
|
json_rpc_error (JSON_RPC_ERROR_INVALID_PARAMS, NULL));
|
2015-03-08 05:39:57 +01:00
|
|
|
|
|
|
|
return json_rpc_response (NULL, json_string ("pong"), NULL);
|
|
|
|
}
|
|
|
|
|
2020-09-01 23:09:56 +02:00
|
|
|
static json_t *
|
|
|
|
json_rpc_date (struct server_context *ctx, json_t *params)
|
|
|
|
{
|
|
|
|
(void) ctx;
|
|
|
|
|
|
|
|
if (params && !json_is_null (params))
|
|
|
|
return json_rpc_response (NULL, NULL,
|
|
|
|
json_rpc_error (JSON_RPC_ERROR_INVALID_PARAMS, NULL));
|
|
|
|
|
|
|
|
time_t now = time (NULL);
|
|
|
|
const struct tm *tm = localtime (&now);
|
|
|
|
json_t *x = json_object ();
|
|
|
|
|
|
|
|
json_object_set_new (x, "year", json_integer (tm->tm_year + 1900));
|
|
|
|
json_object_set_new (x, "month", json_integer (tm->tm_mon + 1));
|
|
|
|
json_object_set_new (x, "day", json_integer (tm->tm_mday));
|
|
|
|
json_object_set_new (x, "hours", json_integer (tm->tm_hour));
|
|
|
|
json_object_set_new (x, "minutes", json_integer (tm->tm_min));
|
|
|
|
json_object_set_new (x, "seconds", json_integer (tm->tm_sec));
|
|
|
|
return json_rpc_response (NULL, x, NULL);
|
|
|
|
}
|
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
static json_t *
|
|
|
|
process_json_rpc_request (struct server_context *ctx, json_t *request)
|
|
|
|
{
|
2015-03-11 00:24:20 +01:00
|
|
|
// A list of all available methods; this list has to be ordered.
|
|
|
|
// Eventually it might be better to move this into a map in the context.
|
|
|
|
static struct json_rpc_handler_info handlers[] =
|
|
|
|
{
|
2020-09-01 23:09:56 +02:00
|
|
|
{ "date", json_rpc_date },
|
2015-03-11 00:24:20 +01:00
|
|
|
{ "ping", json_rpc_ping },
|
|
|
|
};
|
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
if (!json_is_object (request))
|
|
|
|
return json_rpc_response (NULL, NULL,
|
|
|
|
json_rpc_error (JSON_RPC_ERROR_INVALID_REQUEST, NULL));
|
|
|
|
|
|
|
|
json_t *v = json_object_get (request, "jsonrpc");
|
|
|
|
json_t *m = json_object_get (request, "method");
|
|
|
|
json_t *params = json_object_get (request, "params");
|
|
|
|
json_t *id = json_object_get (request, "id");
|
|
|
|
|
|
|
|
const char *version;
|
|
|
|
const char *method;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
ok &= v && (version = json_string_value (v)) && !strcmp (version, "2.0");
|
|
|
|
ok &= m && (method = json_string_value (m));
|
|
|
|
ok &= !params || json_is_array (params) || json_is_object (params);
|
|
|
|
ok &= !id || json_is_null (id) ||
|
|
|
|
json_is_string (id) || json_is_number (id);
|
|
|
|
if (!ok)
|
|
|
|
return json_rpc_response (id, NULL,
|
|
|
|
json_rpc_error (JSON_RPC_ERROR_INVALID_REQUEST, NULL));
|
|
|
|
|
2015-03-11 00:24:20 +01:00
|
|
|
struct json_rpc_handler_info key = { .method_name = method };
|
|
|
|
struct json_rpc_handler_info *handler = bsearch (&key, handlers,
|
|
|
|
N_ELEMENTS (handlers), sizeof key, json_rpc_handler_info_cmp);
|
|
|
|
if (!handler)
|
2015-03-08 05:39:57 +01:00
|
|
|
return json_rpc_response (id, NULL,
|
|
|
|
json_rpc_error (JSON_RPC_ERROR_METHOD_NOT_FOUND, NULL));
|
|
|
|
|
2015-03-11 00:24:20 +01:00
|
|
|
json_t *response = handler->handler (ctx, params);
|
2015-03-08 05:39:57 +01:00
|
|
|
if (id)
|
2020-09-01 20:55:22 +02:00
|
|
|
{
|
|
|
|
(void) json_object_set (response, "id", id);
|
2015-03-08 05:39:57 +01:00
|
|
|
return response;
|
2020-09-01 20:55:22 +02:00
|
|
|
}
|
2015-03-08 05:39:57 +01:00
|
|
|
|
|
|
|
// Notifications don't get responses
|
|
|
|
json_decref (response);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
flush_json (json_t *json, struct str *output)
|
|
|
|
{
|
|
|
|
char *utf8 = json_dumps (json, JSON_ENCODE_ANY);
|
|
|
|
str_append (output, utf8);
|
|
|
|
free (utf8);
|
|
|
|
json_decref (json);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_json_rpc (struct server_context *ctx,
|
|
|
|
const void *data, size_t len, struct str *output)
|
|
|
|
{
|
|
|
|
|
|
|
|
json_error_t e;
|
|
|
|
json_t *request;
|
|
|
|
if (!(request = json_loadb (data, len, JSON_DECODE_ANY, &e)))
|
|
|
|
{
|
|
|
|
flush_json (json_rpc_response (NULL, NULL,
|
|
|
|
json_rpc_error (JSON_RPC_ERROR_PARSE_ERROR, NULL)),
|
|
|
|
output);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_is_array (request))
|
|
|
|
{
|
|
|
|
if (!json_array_size (request))
|
|
|
|
{
|
|
|
|
flush_json (json_rpc_response (NULL, NULL,
|
|
|
|
json_rpc_error (JSON_RPC_ERROR_INVALID_REQUEST, NULL)),
|
|
|
|
output);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_t *response = json_array ();
|
|
|
|
json_t *iter;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
json_array_foreach (request, i, iter)
|
|
|
|
{
|
|
|
|
json_t *result = process_json_rpc_request (ctx, iter);
|
|
|
|
if (result)
|
|
|
|
json_array_append_new (response, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_array_size (response))
|
|
|
|
flush_json (response, output);
|
|
|
|
else
|
|
|
|
json_decref (response);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
json_t *result = process_json_rpc_request (ctx, request);
|
|
|
|
if (result)
|
|
|
|
flush_json (result, output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 05:07:57 +02:00
|
|
|
/// @}
|
2015-03-02 23:11:29 +01:00
|
|
|
// --- Requests ----------------------------------------------------------------
|
2018-10-15 05:07:57 +02:00
|
|
|
/// @defgroup Requests
|
|
|
|
/// @{
|
2015-03-02 23:11:29 +01:00
|
|
|
|
2018-10-15 19:29:17 +02:00
|
|
|
/// A generic CGI request abstraction, writing data indirectly through callbacks
|
2015-03-02 23:11:29 +01:00
|
|
|
struct request
|
|
|
|
{
|
2015-03-08 05:39:57 +01:00
|
|
|
struct server_context *ctx; ///< Server context
|
2015-03-05 08:47:20 +01:00
|
|
|
|
2018-10-15 19:29:17 +02:00
|
|
|
struct request_handler *handler; ///< Assigned request handler
|
2015-03-14 19:36:37 +01:00
|
|
|
void *handler_data; ///< User data for the handler
|
2015-03-05 08:47:20 +01:00
|
|
|
|
|
|
|
/// Callback to write some CGI response data to the output
|
2018-10-15 20:38:48 +02:00
|
|
|
void (*write_cb) (struct request *, const void *data, size_t len);
|
2015-03-05 08:47:20 +01:00
|
|
|
|
2018-10-18 04:13:59 +02:00
|
|
|
/// Callback to close the CGI response, simulates end of program execution.
|
2015-03-06 19:49:33 +01:00
|
|
|
/// CALLING THIS MAY CAUSE THE REQUEST TO BE DESTROYED.
|
2018-10-19 01:15:12 +02:00
|
|
|
void (*finish_cb) (struct request *);
|
2015-03-05 08:47:20 +01:00
|
|
|
};
|
|
|
|
|
2018-10-15 19:29:17 +02:00
|
|
|
/// An interface to detect and handle specific kinds of CGI requests.
|
|
|
|
/// The server walks through a list of them until it finds one that can serve
|
|
|
|
/// a particular request. If unsuccessful, the remote client gets a 404
|
|
|
|
/// (the default handling).
|
2015-03-05 08:47:20 +01:00
|
|
|
struct request_handler
|
|
|
|
{
|
2015-03-08 05:39:57 +01:00
|
|
|
LIST_HEADER (struct request_handler)
|
|
|
|
|
2018-10-15 19:29:17 +02:00
|
|
|
/// Install ourselves as the handler for the request, if applicable.
|
|
|
|
/// Sets @a continue_ to false if further processing should be stopped,
|
|
|
|
/// meaning the request has already been handled.
|
2015-03-14 19:36:37 +01:00
|
|
|
bool (*try_handle) (struct request *request,
|
|
|
|
struct str_map *headers, bool *continue_);
|
2015-03-05 08:47:20 +01:00
|
|
|
|
2018-10-17 02:21:19 +02:00
|
|
|
/// Handle incoming data. "len == 0" means EOF.
|
2018-10-15 19:29:17 +02:00
|
|
|
/// Returns false if there is no more processing to be done.
|
2018-10-17 23:56:11 +02:00
|
|
|
// FIXME: the EOF may or may not be delivered when request is cut short,
|
|
|
|
// we should fix FastCGI not to deliver it on CONTENT_LENGTH mismatch
|
2015-03-06 19:49:33 +01:00
|
|
|
bool (*push_cb) (struct request *request, const void *data, size_t len);
|
2015-03-05 08:47:20 +01:00
|
|
|
|
2018-10-15 19:29:17 +02:00
|
|
|
/// Destroy the handler's data stored in the request object
|
2018-10-17 03:53:07 +02:00
|
|
|
void (*finalize_cb) (struct request *request);
|
2015-03-02 23:11:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
request_init (struct request *self)
|
|
|
|
{
|
2015-03-05 08:47:20 +01:00
|
|
|
memset (self, 0, sizeof *self);
|
2015-03-02 23:11:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
request_free (struct request *self)
|
|
|
|
{
|
2015-03-06 19:49:33 +01:00
|
|
|
if (self->handler)
|
2018-10-17 03:53:07 +02:00
|
|
|
self->handler->finalize_cb (self);
|
2015-03-05 08:47:20 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 23:34:59 +02:00
|
|
|
/// Write request CGI response data, intended for use by request handlers
|
|
|
|
static void
|
|
|
|
request_write (struct request *self, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
self->write_cb (self, data, len);
|
|
|
|
}
|
|
|
|
|
2015-03-06 19:49:33 +01:00
|
|
|
/// This function is only intended to be run from asynchronous event handlers
|
|
|
|
/// such as timers, not as a direct result of starting the request or receiving
|
|
|
|
/// request data. CALLING THIS MAY CAUSE THE REQUEST TO BE DESTROYED.
|
2015-03-05 08:47:20 +01:00
|
|
|
static void
|
2015-03-06 19:49:33 +01:00
|
|
|
request_finish (struct request *self)
|
|
|
|
{
|
2018-10-19 01:15:12 +02:00
|
|
|
self->finish_cb (self);
|
2015-03-06 19:49:33 +01:00
|
|
|
}
|
|
|
|
|
2018-10-12 23:07:06 +02:00
|
|
|
/// Starts processing a request. Returns false if no further action is to be
|
|
|
|
/// done and the request should be finished.
|
2015-03-06 19:49:33 +01:00
|
|
|
static bool
|
2015-03-05 08:47:20 +01:00
|
|
|
request_start (struct request *self, struct str_map *headers)
|
|
|
|
{
|
2015-03-14 19:36:37 +01:00
|
|
|
// XXX: it feels like this should rather be two steps:
|
|
|
|
// bool (*can_handle) (request *, headers)
|
|
|
|
// ... install the handler ...
|
|
|
|
// bool (*handle) (request *)
|
|
|
|
//
|
|
|
|
// However that might cause some stuff to be done twice.
|
|
|
|
//
|
2018-10-12 23:07:06 +02:00
|
|
|
// Another way we could get rid of the continue_ argument is via adding
|
2015-03-14 19:36:37 +01:00
|
|
|
// some way of marking the request as finished from within the handler.
|
|
|
|
|
2018-10-18 07:17:06 +02:00
|
|
|
if (g_debug_mode)
|
|
|
|
{
|
|
|
|
struct str_map_iter iter = str_map_iter_make (headers);
|
|
|
|
const char *value;
|
|
|
|
while ((value = str_map_iter_next (&iter)))
|
|
|
|
print_debug ("%s: %s", iter.link->key, value);
|
|
|
|
print_debug ("--");
|
|
|
|
}
|
|
|
|
|
2015-03-14 19:36:37 +01:00
|
|
|
bool continue_ = true;
|
2015-03-08 05:39:57 +01:00
|
|
|
LIST_FOR_EACH (struct request_handler, handler, self->ctx->handlers)
|
2015-03-14 19:36:37 +01:00
|
|
|
if (handler->try_handle (self, headers, &continue_))
|
2015-03-08 05:39:57 +01:00
|
|
|
{
|
|
|
|
self->handler = handler;
|
2015-03-14 19:36:37 +01:00
|
|
|
return continue_;
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
2015-03-05 08:47:20 +01:00
|
|
|
|
|
|
|
// Unable to serve the request
|
2018-06-24 00:40:10 +02:00
|
|
|
struct str response = str_make ();
|
2015-03-08 09:41:10 +01:00
|
|
|
str_append (&response, "Status: 404 Not Found\n");
|
|
|
|
str_append (&response, "Content-Type: text/plain\n\n");
|
2018-10-17 23:34:59 +02:00
|
|
|
request_write (self, response.str, response.len);
|
2015-03-05 08:47:20 +01:00
|
|
|
str_free (&response);
|
2015-03-06 19:49:33 +01:00
|
|
|
return false;
|
2015-03-02 23:11:29 +01:00
|
|
|
}
|
2015-03-05 08:47:20 +01:00
|
|
|
|
2015-03-06 19:49:33 +01:00
|
|
|
static bool
|
2015-03-05 08:47:20 +01:00
|
|
|
request_push (struct request *self, const void *data, size_t len)
|
|
|
|
{
|
2015-03-11 00:24:20 +01:00
|
|
|
if (!soft_assert (self->handler))
|
|
|
|
// No handler, nothing to do with any data
|
|
|
|
return false;
|
2015-03-06 19:49:33 +01:00
|
|
|
|
2015-03-11 00:24:20 +01:00
|
|
|
return self->handler->push_cb (self, data, len);
|
2015-03-05 08:47:20 +01:00
|
|
|
}
|
|
|
|
|
2018-10-15 05:07:57 +02:00
|
|
|
/// @}
|
2015-03-05 08:47:20 +01:00
|
|
|
// --- Requests handlers -------------------------------------------------------
|
|
|
|
|
|
|
|
static bool
|
|
|
|
request_handler_json_rpc_try_handle
|
2015-03-14 19:36:37 +01:00
|
|
|
(struct request *request, struct str_map *headers, bool *continue_)
|
2015-03-05 08:47:20 +01:00
|
|
|
{
|
|
|
|
const char *content_type = str_map_find (headers, "CONTENT_TYPE");
|
|
|
|
const char *method = str_map_find (headers, "REQUEST_METHOD");
|
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
if (!method || strcmp (method, "POST")
|
|
|
|
|| !content_type || !validate_json_rpc_content_type (content_type))
|
2015-03-05 08:47:20 +01:00
|
|
|
return false;
|
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
struct str *buf = xcalloc (1, sizeof *buf);
|
2018-06-24 00:40:10 +02:00
|
|
|
*buf = str_make ();
|
2015-03-08 05:39:57 +01:00
|
|
|
|
|
|
|
request->handler_data = buf;
|
2015-03-14 19:36:37 +01:00
|
|
|
*continue_ = true;
|
2015-03-05 08:47:20 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-06 19:49:33 +01:00
|
|
|
static bool
|
2015-03-05 08:47:20 +01:00
|
|
|
request_handler_json_rpc_push
|
|
|
|
(struct request *request, const void *data, size_t len)
|
|
|
|
{
|
2015-03-08 05:39:57 +01:00
|
|
|
struct str *buf = request->handler_data;
|
|
|
|
if (len)
|
|
|
|
{
|
2015-03-11 00:24:20 +01:00
|
|
|
str_append_data (buf, data, len);
|
|
|
|
return true;
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
2015-03-11 00:24:20 +01:00
|
|
|
|
2018-10-12 23:07:06 +02:00
|
|
|
// TODO: check buf.len against CONTENT_LENGTH; if it's less, then the
|
2018-10-17 23:56:11 +02:00
|
|
|
// client hasn't been successful in transferring all of its data.
|
|
|
|
// See also comment on request_handler::push_cb.
|
2018-10-12 23:07:06 +02:00
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
struct str response = str_make ();
|
2015-03-11 00:24:20 +01:00
|
|
|
str_append (&response, "Status: 200 OK\n");
|
|
|
|
str_append_printf (&response, "Content-Type: %s\n\n", "application/json");
|
|
|
|
process_json_rpc (request->ctx, buf->str, buf->len, &response);
|
2018-10-17 23:34:59 +02:00
|
|
|
request_write (request, response.str, response.len);
|
2015-03-11 00:24:20 +01:00
|
|
|
str_free (&response);
|
|
|
|
return false;
|
2015-03-05 08:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-10-17 03:53:07 +02:00
|
|
|
request_handler_json_rpc_finalize (struct request *request)
|
2015-03-05 08:47:20 +01:00
|
|
|
{
|
2015-03-08 05:39:57 +01:00
|
|
|
struct str *buf = request->handler_data;
|
|
|
|
str_free (buf);
|
|
|
|
free (buf);
|
|
|
|
|
|
|
|
request->handler_data = NULL;
|
2015-03-05 08:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct request_handler g_request_handler_json_rpc =
|
|
|
|
{
|
2018-10-17 03:53:07 +02:00
|
|
|
.try_handle = request_handler_json_rpc_try_handle,
|
|
|
|
.push_cb = request_handler_json_rpc_push,
|
|
|
|
.finalize_cb = request_handler_json_rpc_finalize,
|
2015-03-05 08:47:20 +01:00
|
|
|
};
|
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2018-10-19 01:15:12 +02:00
|
|
|
/// Make a URL path canonical. The resulting path always begins with a slash,
|
|
|
|
/// and any trailing slashes are lost.
|
2015-03-11 00:24:20 +01:00
|
|
|
static char *
|
|
|
|
canonicalize_url_path (const char *path)
|
|
|
|
{
|
2018-06-24 00:40:10 +02:00
|
|
|
struct strv v = strv_make ();
|
2017-02-05 22:44:01 +01:00
|
|
|
cstr_split (path, "/", true, &v);
|
2015-03-11 00:24:20 +01:00
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
struct strv canonical = strv_make ();
|
2017-02-05 22:44:01 +01:00
|
|
|
strv_append (&canonical, "");
|
2015-03-11 00:24:20 +01:00
|
|
|
|
|
|
|
for (size_t i = 0; i < v.len; i++)
|
|
|
|
{
|
|
|
|
const char *dir = v.vector[i];
|
|
|
|
if (!strcmp (dir, "."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp (dir, ".."))
|
2017-02-05 22:44:01 +01:00
|
|
|
strv_append (&canonical, dir);
|
2015-03-23 20:12:53 +01:00
|
|
|
else if (canonical.len > 1)
|
2015-03-11 00:24:20 +01:00
|
|
|
// ".." never goes above the root
|
2017-02-05 22:44:01 +01:00
|
|
|
strv_remove (&canonical, canonical.len - 1);
|
2015-03-11 00:24:20 +01:00
|
|
|
}
|
2017-02-05 22:44:01 +01:00
|
|
|
strv_free (&v);
|
2015-03-11 00:24:20 +01:00
|
|
|
|
2017-02-05 22:44:01 +01:00
|
|
|
char *joined = strv_join (&canonical, "/");
|
|
|
|
strv_free (&canonical);
|
2015-03-11 00:24:20 +01:00
|
|
|
return joined;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
detect_magic (const void *data, size_t len)
|
|
|
|
{
|
|
|
|
magic_t cookie;
|
|
|
|
char *mime_type = NULL;
|
|
|
|
|
|
|
|
if (!(cookie = magic_open (MAGIC_MIME)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
const char *magic = NULL;
|
|
|
|
if (!magic_load (cookie, NULL)
|
|
|
|
&& (magic = magic_buffer (cookie, data, len)))
|
|
|
|
mime_type = xstrdup (magic);
|
|
|
|
else
|
|
|
|
print_debug ("MIME type detection failed: %s", magic_error (cookie));
|
|
|
|
|
|
|
|
magic_close (cookie);
|
|
|
|
return mime_type;
|
|
|
|
}
|
|
|
|
|
2015-03-08 09:41:10 +01:00
|
|
|
static bool
|
|
|
|
request_handler_static_try_handle
|
2015-03-14 19:36:37 +01:00
|
|
|
(struct request *request, struct str_map *headers, bool *continue_)
|
2015-03-08 09:41:10 +01:00
|
|
|
{
|
2015-03-14 19:36:37 +01:00
|
|
|
// Serving static files is actually quite complicated as it turns out;
|
|
|
|
// but this is only meant to serve a few tiny text files
|
|
|
|
|
2015-03-08 09:41:10 +01:00
|
|
|
struct server_context *ctx = request->ctx;
|
|
|
|
const char *root = str_map_find (&ctx->config, "static_root");
|
|
|
|
if (!root)
|
|
|
|
{
|
|
|
|
print_debug ("static document root not configured");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:17:06 +02:00
|
|
|
// TODO: implement HEAD, we don't get that for free;
|
|
|
|
// probably implies adding Content-Length
|
2015-03-08 09:41:10 +01:00
|
|
|
const char *method = str_map_find (headers, "REQUEST_METHOD");
|
|
|
|
if (!method || strcmp (method, "GET"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// TODO: look at <SCRIPT_NAME, PATH_INFO>, REQUEST_URI in the headers
|
|
|
|
const char *path_info = str_map_find (headers, "PATH_INFO");
|
2018-10-18 07:17:06 +02:00
|
|
|
if (!path_info)
|
|
|
|
path_info = str_map_find (headers, "REQUEST_URI");
|
2015-03-08 09:41:10 +01:00
|
|
|
if (!path_info)
|
|
|
|
{
|
2018-10-18 07:17:06 +02:00
|
|
|
print_debug ("neither PATH_INFO nor REQUEST_URI was defined");
|
2015-03-08 09:41:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to filter the path to stay in our root
|
|
|
|
// Being able to read /etc/passwd would be rather embarrasing
|
2015-03-11 00:24:20 +01:00
|
|
|
char *suffix = canonicalize_url_path (path_info);
|
2015-03-08 09:41:10 +01:00
|
|
|
char *path = xstrdup_printf ("%s%s", root, suffix);
|
2018-10-18 07:17:06 +02:00
|
|
|
print_debug ("trying to statically serve %s", path);
|
2015-03-08 09:41:10 +01:00
|
|
|
|
2015-03-14 19:36:37 +01:00
|
|
|
// TODO: check that this is a regular file
|
2015-03-08 09:41:10 +01:00
|
|
|
FILE *fp = fopen (path, "rb");
|
|
|
|
if (!fp)
|
|
|
|
{
|
2018-06-24 00:40:10 +02:00
|
|
|
struct str response = str_make ();
|
2015-03-08 09:41:10 +01:00
|
|
|
str_append (&response, "Status: 404 Not Found\n");
|
|
|
|
str_append (&response, "Content-Type: text/plain\n\n");
|
|
|
|
str_append_printf (&response,
|
|
|
|
"File %s was not found on this server\n", suffix);
|
2018-10-17 23:34:59 +02:00
|
|
|
request_write (request, response.str, response.len);
|
2015-03-08 09:41:10 +01:00
|
|
|
str_free (&response);
|
|
|
|
|
|
|
|
free (suffix);
|
|
|
|
free (path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
free (suffix);
|
|
|
|
free (path);
|
|
|
|
|
|
|
|
uint8_t buf[8192];
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
// Try to detect the Content-Type from the actual contents
|
|
|
|
char *mime_type = NULL;
|
|
|
|
if ((len = fread (buf, 1, sizeof buf, fp)))
|
2015-03-11 00:24:20 +01:00
|
|
|
mime_type = detect_magic (buf, len);
|
2015-03-08 09:41:10 +01:00
|
|
|
if (!mime_type)
|
|
|
|
mime_type = xstrdup ("application/octet_stream");
|
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
struct str response = str_make ();
|
2015-03-08 09:41:10 +01:00
|
|
|
str_append (&response, "Status: 200 OK\n");
|
|
|
|
str_append_printf (&response, "Content-Type: %s\n\n", mime_type);
|
2018-10-17 23:34:59 +02:00
|
|
|
request_write (request, response.str, response.len);
|
2015-03-08 09:41:10 +01:00
|
|
|
str_free (&response);
|
|
|
|
free (mime_type);
|
|
|
|
|
|
|
|
// Write the chunk we've used to help us with magic detection;
|
|
|
|
// obviously we have to do it after we've written the headers
|
|
|
|
if (len)
|
2018-10-17 23:34:59 +02:00
|
|
|
request_write (request, buf, len);
|
2015-03-08 09:41:10 +01:00
|
|
|
|
|
|
|
while ((len = fread (buf, 1, sizeof buf, fp)))
|
2018-10-17 23:34:59 +02:00
|
|
|
request_write (request, buf, len);
|
2015-03-08 09:41:10 +01:00
|
|
|
fclose (fp);
|
2015-03-14 19:36:37 +01:00
|
|
|
|
|
|
|
// TODO: this should rather not be returned all at once but in chunks;
|
|
|
|
// file read requests never return EAGAIN
|
|
|
|
// TODO: actual file data should really be returned by a callback when
|
|
|
|
// the socket is writable with nothing to be sent (pumping the entire
|
|
|
|
// file all at once won't really work if it's huge).
|
|
|
|
*continue_ = false;
|
2015-03-08 09:41:10 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
request_handler_static_push
|
|
|
|
(struct request *request, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
(void) request;
|
|
|
|
(void) data;
|
|
|
|
|
2018-10-19 01:15:12 +02:00
|
|
|
if (len == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Aborting on content; we shouldn't receive any (GET).
|
|
|
|
// In fact, we will only get here once try_handle stops dumping everything
|
|
|
|
// into the write queue at once.
|
|
|
|
print_debug ("the static file handler received data but shouldn't have");
|
|
|
|
return false;
|
2015-03-08 09:41:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-10-17 03:53:07 +02:00
|
|
|
request_handler_static_finalize (struct request *request)
|
2015-03-08 09:41:10 +01:00
|
|
|
{
|
|
|
|
(void) request;
|
2016-01-16 22:21:29 +01:00
|
|
|
// Nothing to dispose of this far
|
2015-03-08 09:41:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct request_handler g_request_handler_static =
|
|
|
|
{
|
2018-10-17 03:53:07 +02:00
|
|
|
.try_handle = request_handler_static_try_handle,
|
|
|
|
.push_cb = request_handler_static_push,
|
|
|
|
.finalize_cb = request_handler_static_finalize,
|
2015-03-08 09:41:10 +01:00
|
|
|
};
|
2015-03-02 23:11:29 +01:00
|
|
|
|
|
|
|
// --- Client communication handlers -------------------------------------------
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2018-10-15 19:29:17 +02:00
|
|
|
/// A virtual class for client connections coming either from the web server
|
|
|
|
/// or directly from the end-client, depending on the protocol in use
|
2015-03-02 08:49:21 +01:00
|
|
|
struct client
|
|
|
|
{
|
|
|
|
LIST_HEADER (struct client)
|
|
|
|
|
2018-10-17 08:40:18 +02:00
|
|
|
struct client_vtable *vtable; ///< Client behaviour
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2018-10-17 08:40:18 +02:00
|
|
|
int socket_fd; ///< The network socket
|
|
|
|
bool received_eof; ///< Whether EOF has been received yet
|
2018-10-17 23:26:11 +02:00
|
|
|
bool flushing; ///< No more data to write, send FIN
|
|
|
|
bool closing; ///< No more data to read or write
|
|
|
|
bool half_closed; ///< Conn. half-closed while flushing
|
2016-01-17 04:42:16 +01:00
|
|
|
struct write_queue write_queue; ///< Write queue
|
2018-10-17 23:26:11 +02:00
|
|
|
ev_timer close_timeout_watcher; ///< Write queue flush timer
|
2015-03-02 08:49:21 +01:00
|
|
|
|
|
|
|
ev_io read_watcher; ///< The socket can be read from
|
|
|
|
ev_io write_watcher; ///< The socket can be written to
|
2015-03-02 23:11:29 +01:00
|
|
|
};
|
|
|
|
|
2018-10-15 19:29:17 +02:00
|
|
|
/// The concrete behaviour to serve a particular client's requests
|
2016-01-16 22:21:29 +01:00
|
|
|
struct client_vtable
|
2015-03-02 23:11:29 +01:00
|
|
|
{
|
2018-10-16 04:33:33 +02:00
|
|
|
/// Process incoming data; "len == 0" means EOF.
|
2018-10-17 08:40:18 +02:00
|
|
|
/// If the method returns false, client_close() is called by the caller.
|
2018-10-16 04:33:33 +02:00
|
|
|
bool (*push) (struct client *client, const void *data, size_t len);
|
|
|
|
|
|
|
|
// TODO: optional push_error() to inform about network I/O errors
|
|
|
|
|
|
|
|
/// Attempt a graceful shutdown: make any appropriate steps before
|
|
|
|
/// the client connection times out and gets torn down by force.
|
|
|
|
/// The client is allowed to destroy itself immediately.
|
2015-04-10 02:44:13 +02:00
|
|
|
void (*shutdown) (struct client *client);
|
2015-03-11 00:24:20 +01:00
|
|
|
|
2018-10-16 04:12:37 +02:00
|
|
|
/// Do any additional cleanup for the concrete class before destruction
|
|
|
|
void (*finalize) (struct client *client);
|
2015-03-02 08:49:21 +01:00
|
|
|
};
|
|
|
|
|
2015-03-06 19:49:33 +01:00
|
|
|
static void
|
2016-01-16 22:21:29 +01:00
|
|
|
client_destroy (struct client *self)
|
2015-03-06 19:49:33 +01:00
|
|
|
{
|
2018-10-17 08:40:18 +02:00
|
|
|
// XXX: this codebase halfway pretends there could be other contexts
|
|
|
|
struct server_context *ctx = ev_userdata (EV_DEFAULT);
|
2016-01-16 22:21:29 +01:00
|
|
|
LIST_UNLINK (ctx->clients, self);
|
2015-03-06 19:49:33 +01:00
|
|
|
ctx->n_clients--;
|
|
|
|
|
|
|
|
// First uninitialize the higher-level implementation
|
2018-10-16 04:12:37 +02:00
|
|
|
self->vtable->finalize (self);
|
2015-03-06 19:49:33 +01:00
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
ev_io_stop (EV_DEFAULT_ &self->read_watcher);
|
|
|
|
ev_io_stop (EV_DEFAULT_ &self->write_watcher);
|
|
|
|
xclose (self->socket_fd);
|
2018-10-17 05:16:17 +02:00
|
|
|
write_queue_free (&self->write_queue);
|
2018-10-17 23:26:11 +02:00
|
|
|
ev_timer_stop (EV_DEFAULT_ &self->close_timeout_watcher);
|
2016-01-16 22:21:29 +01:00
|
|
|
free (self);
|
2015-04-10 02:44:13 +02:00
|
|
|
|
|
|
|
try_finish_quit (ctx);
|
2015-03-06 19:49:33 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 23:26:11 +02:00
|
|
|
static void
|
2018-10-19 01:15:12 +02:00
|
|
|
client_write_unsafe (struct client *self, void *data, size_t len)
|
2018-10-17 23:26:11 +02:00
|
|
|
{
|
|
|
|
struct write_req *req = xcalloc (1, sizeof *req);
|
2018-10-19 01:15:12 +02:00
|
|
|
req->data.iov_base = data;
|
2018-10-17 23:26:11 +02:00
|
|
|
req->data.iov_len = len;
|
|
|
|
|
|
|
|
write_queue_add (&self->write_queue, req);
|
|
|
|
ev_io_start (EV_DEFAULT_ &self->write_watcher);
|
|
|
|
}
|
|
|
|
|
2018-10-19 01:15:12 +02:00
|
|
|
static void
|
|
|
|
client_write_owned (struct client *self, void *data, size_t len)
|
|
|
|
{
|
|
|
|
if (soft_assert (!self->flushing) && len != 0)
|
|
|
|
client_write_unsafe (self, data, len);
|
|
|
|
else
|
|
|
|
free (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
client_write (struct client *self, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
if (soft_assert (!self->flushing) && len != 0)
|
|
|
|
client_write_unsafe (self, memcpy (xmalloc (len), data, len), len);
|
|
|
|
}
|
|
|
|
|
2018-10-17 23:26:11 +02:00
|
|
|
/// Half-close the connection from our side once the write_queue is flushed.
|
|
|
|
/// It is the caller's responsibility to destroy the connection upon EOF.
|
|
|
|
// XXX: or we might change on_client_readable to do it anyway, seems safe
|
|
|
|
static void
|
|
|
|
client_shutdown (struct client *self)
|
|
|
|
{
|
|
|
|
self->flushing = true;
|
|
|
|
ev_feed_event (EV_DEFAULT_ &self->write_watcher, EV_WRITE);
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:40:18 +02:00
|
|
|
/// Try to cleanly close the connection, waiting for the remote client to close
|
|
|
|
/// its own side of the connection as a sign that it has processed all the data
|
|
|
|
/// it wanted to. The client implementation will not receive any further data.
|
|
|
|
/// May directly call client_destroy().
|
|
|
|
static void
|
|
|
|
client_close (struct client *self)
|
|
|
|
{
|
|
|
|
if (self->closing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
self->closing = true;
|
2018-10-17 23:26:11 +02:00
|
|
|
ev_timer_start (EV_DEFAULT_ &self->close_timeout_watcher);
|
|
|
|
client_shutdown (self);
|
2018-10-17 08:40:18 +02:00
|
|
|
|
|
|
|
// We assume the remote client doesn't want our data if it half-closes
|
|
|
|
if (self->received_eof)
|
|
|
|
client_destroy (self);
|
|
|
|
}
|
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
static bool
|
|
|
|
client_read_loop (EV_P_ struct client *client, ev_io *watcher)
|
|
|
|
{
|
|
|
|
char buf[8192];
|
2018-10-17 08:40:18 +02:00
|
|
|
ssize_t n_read;
|
|
|
|
again:
|
|
|
|
while ((n_read = recv (watcher->fd, buf, sizeof buf, 0)) >= 0)
|
2016-01-16 22:21:29 +01:00
|
|
|
{
|
2018-10-17 08:40:18 +02:00
|
|
|
if (!n_read)
|
2016-01-16 22:21:29 +01:00
|
|
|
{
|
2018-10-17 08:40:18 +02:00
|
|
|
// Don't deliver the EOF condition repeatedly
|
|
|
|
ev_io_stop (EV_A_ watcher);
|
|
|
|
client->received_eof = true;
|
2016-01-16 22:21:29 +01:00
|
|
|
}
|
2018-10-17 08:40:18 +02:00
|
|
|
if (!client->closing
|
|
|
|
&& !client->vtable->push (client, buf, n_read))
|
|
|
|
{
|
|
|
|
client_close (client);
|
2016-01-16 22:21:29 +01:00
|
|
|
return false;
|
2018-10-17 08:40:18 +02:00
|
|
|
}
|
|
|
|
if (!n_read)
|
|
|
|
return true;
|
2016-01-16 22:21:29 +01:00
|
|
|
}
|
2018-10-17 08:40:18 +02:00
|
|
|
if (errno == EINTR)
|
|
|
|
goto again;
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return true;
|
2016-01-16 22:21:29 +01:00
|
|
|
|
2018-10-17 08:40:18 +02:00
|
|
|
client_destroy (client);
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-16 22:21:29 +01:00
|
|
|
|
2018-10-17 08:40:18 +02:00
|
|
|
static void
|
|
|
|
on_client_readable (EV_P_ ev_io *watcher, int revents)
|
|
|
|
{
|
|
|
|
struct client *client = watcher->data;
|
|
|
|
(void) revents;
|
|
|
|
|
|
|
|
if (client_read_loop (EV_A_ client, watcher)
|
|
|
|
&& client->closing && client->received_eof)
|
|
|
|
client_destroy (client);
|
2016-01-16 22:21:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-10-17 08:40:18 +02:00
|
|
|
on_client_writable (EV_P_ ev_io *watcher, int revents)
|
2016-01-16 22:21:29 +01:00
|
|
|
{
|
|
|
|
struct client *client = watcher->data;
|
2018-10-17 08:40:18 +02:00
|
|
|
(void) loop;
|
|
|
|
(void) revents;
|
|
|
|
|
|
|
|
// TODO: some sort of "on_buffers_flushed" callback for streaming huge
|
|
|
|
// chunks of external (or generated) data. That will need to be
|
|
|
|
// forwarded to "struct request_handler".
|
|
|
|
if (!flush_queue (&client->write_queue, watcher->fd))
|
|
|
|
{
|
|
|
|
client_destroy (client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!write_queue_is_empty (&client->write_queue))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ev_io_stop (EV_A_ watcher);
|
2018-10-17 23:26:11 +02:00
|
|
|
if (client->flushing && !client->half_closed)
|
2018-10-17 08:40:18 +02:00
|
|
|
{
|
|
|
|
if (!shutdown (client->socket_fd, SHUT_WR))
|
|
|
|
client->half_closed = true;
|
|
|
|
else
|
|
|
|
client_destroy (client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_client_timeout (EV_P_ ev_timer *watcher, int revents)
|
|
|
|
{
|
|
|
|
(void) loop;
|
|
|
|
(void) revents;
|
|
|
|
|
|
|
|
client_destroy (watcher->data);
|
2016-01-16 22:21:29 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 05:16:17 +02:00
|
|
|
/// Create a new instance of a subclass with the given size.
|
|
|
|
/// The superclass is assumed to be the first member of the structure.
|
|
|
|
static void *
|
|
|
|
client_new (EV_P_ size_t size, int sock_fd)
|
2016-01-16 22:21:29 +01:00
|
|
|
{
|
|
|
|
struct server_context *ctx = ev_userdata (loop);
|
2018-10-17 05:16:17 +02:00
|
|
|
struct client *self = xcalloc (1, size);
|
2016-01-16 22:21:29 +01:00
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
self->write_queue = write_queue_make ();
|
2018-10-17 23:26:11 +02:00
|
|
|
ev_timer_init (&self->close_timeout_watcher, on_client_timeout, 5., 0.);
|
|
|
|
self->close_timeout_watcher.data = self;
|
2016-01-16 22:21:29 +01:00
|
|
|
|
|
|
|
set_blocking (sock_fd, false);
|
|
|
|
self->socket_fd = sock_fd;
|
|
|
|
|
2018-10-17 08:40:18 +02:00
|
|
|
ev_io_init (&self->read_watcher, on_client_readable, sock_fd, EV_READ);
|
|
|
|
ev_io_init (&self->write_watcher, on_client_writable, sock_fd, EV_WRITE);
|
2016-01-16 22:21:29 +01:00
|
|
|
self->read_watcher.data = self;
|
|
|
|
self->write_watcher.data = self;
|
|
|
|
|
|
|
|
// We're only interested in reading as the write queue is empty now
|
|
|
|
ev_io_start (EV_A_ &self->read_watcher);
|
|
|
|
|
|
|
|
LIST_PREPEND (ctx->clients, self);
|
|
|
|
ctx->n_clients++;
|
2018-10-17 05:16:17 +02:00
|
|
|
return self;
|
2016-01-16 22:21:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- FastCGI client handler --------------------------------------------------
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2015-03-02 23:11:29 +01:00
|
|
|
struct client_fcgi
|
2015-03-02 08:49:21 +01:00
|
|
|
{
|
2016-01-16 22:21:29 +01:00
|
|
|
struct client client; ///< Parent class
|
2015-03-08 05:39:57 +01:00
|
|
|
struct fcgi_muxer muxer; ///< FastCGI de/multiplexer
|
|
|
|
};
|
|
|
|
|
|
|
|
struct client_fcgi_request
|
|
|
|
{
|
|
|
|
struct fcgi_request *fcgi_request; ///< FastCGI request
|
|
|
|
struct request request; ///< Request
|
2015-03-02 08:49:21 +01:00
|
|
|
};
|
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2015-03-08 05:39:57 +01:00
|
|
|
static void
|
2018-10-15 20:38:48 +02:00
|
|
|
client_fcgi_request_write_cb (struct request *req, const void *data, size_t len)
|
2015-03-08 05:39:57 +01:00
|
|
|
{
|
2018-10-15 20:38:48 +02:00
|
|
|
FIND_CONTAINER (self, req, struct client_fcgi_request, request);
|
|
|
|
fcgi_request_write (self->fcgi_request, data, len);
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 01:15:12 +02:00
|
|
|
// XXX: it should be possible to pass a specific status code but we'd have to
|
|
|
|
// allow it in multiple places over this code base, notably request_push()
|
2015-03-08 05:39:57 +01:00
|
|
|
static void
|
2018-10-19 01:15:12 +02:00
|
|
|
client_fcgi_request_finish_cb (struct request *req)
|
2015-03-08 05:39:57 +01:00
|
|
|
{
|
2018-10-15 20:38:48 +02:00
|
|
|
FIND_CONTAINER (self, req, struct client_fcgi_request, request);
|
2018-10-17 08:40:18 +02:00
|
|
|
struct fcgi_muxer *muxer = self->fcgi_request->muxer;
|
|
|
|
// No more data to send, terminate the substream/request,
|
|
|
|
// and also the transport if the client didn't specifically ask to keep it
|
2018-10-19 01:15:12 +02:00
|
|
|
if (!fcgi_request_finish (self->fcgi_request, EXIT_SUCCESS))
|
2018-10-17 08:40:18 +02:00
|
|
|
muxer->close_cb (muxer);
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2018-10-17 02:57:08 +02:00
|
|
|
static bool
|
2018-10-17 08:40:18 +02:00
|
|
|
client_fcgi_request_start (struct fcgi_request *fcgi_request)
|
2015-03-08 05:39:57 +01:00
|
|
|
{
|
2018-10-17 02:57:08 +02:00
|
|
|
struct client_fcgi_request *request =
|
|
|
|
fcgi_request->handler_data = xcalloc (1, sizeof *request);
|
2015-03-08 05:39:57 +01:00
|
|
|
request->fcgi_request = fcgi_request;
|
|
|
|
request_init (&request->request);
|
2018-10-19 01:15:12 +02:00
|
|
|
request->request.ctx = ev_userdata (EV_DEFAULT);
|
|
|
|
request->request.write_cb = client_fcgi_request_write_cb;
|
|
|
|
request->request.finish_cb = client_fcgi_request_finish_cb;
|
2018-10-17 02:57:08 +02:00
|
|
|
|
|
|
|
return request_start (&request->request, &fcgi_request->headers);
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 08:40:18 +02:00
|
|
|
static bool
|
2018-10-17 02:57:08 +02:00
|
|
|
client_fcgi_request_push
|
|
|
|
(struct fcgi_request *req, const void *data, size_t len)
|
2015-03-08 05:39:57 +01:00
|
|
|
{
|
2018-10-17 02:57:08 +02:00
|
|
|
struct client_fcgi_request *request = req->handler_data;
|
2018-10-17 08:40:18 +02:00
|
|
|
return request_push (&request->request, data, len)
|
2018-10-19 01:15:12 +02:00
|
|
|
|| fcgi_request_finish (req, EXIT_SUCCESS);
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-10-17 03:53:07 +02:00
|
|
|
client_fcgi_request_finalize (struct fcgi_request *req)
|
2015-03-08 05:39:57 +01:00
|
|
|
{
|
2018-10-17 02:57:08 +02:00
|
|
|
struct client_fcgi_request *request = req->handler_data;
|
2015-03-08 05:39:57 +01:00
|
|
|
request_free (&request->request);
|
2016-01-16 22:21:29 +01:00
|
|
|
free (request);
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2015-03-08 05:39:57 +01:00
|
|
|
|
|
|
|
static void
|
2018-10-19 01:15:12 +02:00
|
|
|
client_fcgi_write_cb (struct fcgi_muxer *mux, void *data, size_t len)
|
2015-03-08 05:39:57 +01:00
|
|
|
{
|
2018-10-15 20:38:48 +02:00
|
|
|
FIND_CONTAINER (self, mux, struct client_fcgi, muxer);
|
2018-10-19 01:15:12 +02:00
|
|
|
client_write_owned (&self->client, data, len);
|
2015-03-08 05:39:57 +01:00
|
|
|
}
|
|
|
|
|
2015-03-02 23:11:29 +01:00
|
|
|
static void
|
2018-10-15 20:38:48 +02:00
|
|
|
client_fcgi_close_cb (struct fcgi_muxer *mux)
|
2015-03-02 08:49:21 +01:00
|
|
|
{
|
2018-10-15 20:38:48 +02:00
|
|
|
FIND_CONTAINER (self, mux, struct client_fcgi, muxer);
|
2018-10-17 08:40:18 +02:00
|
|
|
client_close (&self->client);
|
2015-03-02 23:11:29 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2018-10-16 04:33:33 +02:00
|
|
|
static bool
|
|
|
|
client_fcgi_push (struct client *client, const void *data, size_t len)
|
|
|
|
{
|
2018-10-17 02:57:08 +02:00
|
|
|
FIND_CONTAINER (self, client, struct client_fcgi, client);
|
2018-10-18 04:13:59 +02:00
|
|
|
return fcgi_muxer_push (&self->muxer, data, len);
|
2018-10-16 04:33:33 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 02:44:13 +02:00
|
|
|
static void
|
|
|
|
client_fcgi_shutdown (struct client *client)
|
|
|
|
{
|
2018-10-17 02:57:08 +02:00
|
|
|
FIND_CONTAINER (self, client, struct client_fcgi, client);
|
|
|
|
self->muxer.in_shutdown = true;
|
2015-04-10 02:44:13 +02:00
|
|
|
|
2018-10-17 02:57:08 +02:00
|
|
|
// TODO: respond with FCGI_END_REQUEST: FCGI_REQUEST_COMPLETE to everything?
|
|
|
|
// The FastCGI specification isn't very clear about what we should do.
|
2015-04-10 02:44:13 +02:00
|
|
|
}
|
|
|
|
|
2015-03-02 23:11:29 +01:00
|
|
|
static void
|
2018-10-16 04:12:37 +02:00
|
|
|
client_fcgi_finalize (struct client *client)
|
2015-03-02 08:49:21 +01:00
|
|
|
{
|
2018-10-17 02:57:08 +02:00
|
|
|
FIND_CONTAINER (self, client, struct client_fcgi, client);
|
2015-03-08 05:39:57 +01:00
|
|
|
fcgi_muxer_free (&self->muxer);
|
2015-03-02 23:11:29 +01:00
|
|
|
}
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
static struct client_vtable client_fcgi_vtable =
|
2015-03-02 23:11:29 +01:00
|
|
|
{
|
2018-10-16 04:33:33 +02:00
|
|
|
.push = client_fcgi_push,
|
2015-04-10 02:44:13 +02:00
|
|
|
.shutdown = client_fcgi_shutdown,
|
2018-10-16 04:12:37 +02:00
|
|
|
.finalize = client_fcgi_finalize,
|
2015-03-02 23:11:29 +01:00
|
|
|
};
|
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
static struct client *
|
|
|
|
client_fcgi_create (EV_P_ int sock_fd)
|
|
|
|
{
|
2018-10-17 05:16:17 +02:00
|
|
|
struct client_fcgi *self = client_new (EV_A_ sizeof *self, sock_fd);
|
2016-01-16 22:21:29 +01:00
|
|
|
self->client.vtable = &client_fcgi_vtable;
|
|
|
|
|
|
|
|
fcgi_muxer_init (&self->muxer);
|
2018-10-17 03:53:07 +02:00
|
|
|
self->muxer.write_cb = client_fcgi_write_cb;
|
|
|
|
self->muxer.close_cb = client_fcgi_close_cb;
|
|
|
|
self->muxer.request_start_cb = client_fcgi_request_start;
|
|
|
|
self->muxer.request_push_cb = client_fcgi_request_push;
|
|
|
|
self->muxer.request_finalize_cb = client_fcgi_request_finalize;
|
2016-01-16 22:21:29 +01:00
|
|
|
return &self->client;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- SCGI client handler -----------------------------------------------------
|
2015-03-02 23:11:29 +01:00
|
|
|
|
|
|
|
struct client_scgi
|
|
|
|
{
|
2016-01-16 22:21:29 +01:00
|
|
|
struct client client; ///< Parent class
|
2015-03-02 23:11:29 +01:00
|
|
|
struct scgi_parser parser; ///< SCGI stream parser
|
2015-03-06 19:49:33 +01:00
|
|
|
struct request request; ///< Request (only one per connection)
|
2018-10-17 23:56:11 +02:00
|
|
|
unsigned long remaining_content; ///< Length of input data to be seen
|
2015-03-02 08:49:21 +01:00
|
|
|
};
|
|
|
|
|
2015-03-05 08:47:20 +01:00
|
|
|
static void
|
2018-10-15 20:38:48 +02:00
|
|
|
client_scgi_write_cb (struct request *req, const void *data, size_t len)
|
2015-03-05 08:47:20 +01:00
|
|
|
{
|
2018-10-15 20:38:48 +02:00
|
|
|
FIND_CONTAINER (self, req, struct client_scgi, request);
|
2016-01-16 22:21:29 +01:00
|
|
|
client_write (&self->client, data, len);
|
2015-03-05 08:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-10-19 01:15:12 +02:00
|
|
|
client_scgi_finish_cb (struct request *req)
|
2015-03-05 08:47:20 +01:00
|
|
|
{
|
2018-10-15 20:38:48 +02:00
|
|
|
FIND_CONTAINER (self, req, struct client_scgi, request);
|
2018-10-17 08:40:18 +02:00
|
|
|
client_close (&self->client);
|
2015-03-05 08:47:20 +01:00
|
|
|
}
|
|
|
|
|
2015-03-06 19:49:33 +01:00
|
|
|
static bool
|
2015-03-05 08:47:20 +01:00
|
|
|
client_scgi_on_headers_read (void *user_data)
|
|
|
|
{
|
2016-01-16 22:21:29 +01:00
|
|
|
struct client_scgi *self = user_data;
|
2018-10-17 23:56:11 +02:00
|
|
|
const char *cl = str_map_find (&self->parser.headers, "CONTENT_LENGTH");
|
|
|
|
if (!cl || !xstrtoul (&self->remaining_content, cl, 10))
|
|
|
|
{
|
|
|
|
print_debug ("SCGI request with invalid or missing CONTENT_LENGTH");
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-06 19:49:33 +01:00
|
|
|
return request_start (&self->request, &self->parser.headers);
|
2015-03-05 08:47:20 +01:00
|
|
|
}
|
|
|
|
|
2015-03-06 19:49:33 +01:00
|
|
|
static bool
|
2015-03-05 08:47:20 +01:00
|
|
|
client_scgi_on_content (void *user_data, const void *data, size_t len)
|
|
|
|
{
|
2016-01-16 22:21:29 +01:00
|
|
|
struct client_scgi *self = user_data;
|
2018-10-17 23:56:11 +02:00
|
|
|
if (len > self->remaining_content)
|
|
|
|
{
|
|
|
|
print_debug ("SCGI request got more data than CONTENT_LENGTH");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// We're in a slight disagreement with the specification since
|
|
|
|
// this tries to write output before it has read all the input
|
|
|
|
if (!request_push (&self->request, data, len))
|
|
|
|
return false;
|
2015-03-05 08:47:20 +01:00
|
|
|
|
2018-10-17 23:56:11 +02:00
|
|
|
// Signalise end of input to the request handler
|
|
|
|
return (self->remaining_content -= len) != 0
|
|
|
|
|| request_push (&self->request, NULL, 0);
|
2015-03-05 08:47:20 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2015-03-02 23:11:29 +01:00
|
|
|
static bool
|
2015-03-06 19:49:33 +01:00
|
|
|
client_scgi_push (struct client *client, const void *data, size_t len)
|
2015-03-02 23:11:29 +01:00
|
|
|
{
|
2016-01-16 22:21:29 +01:00
|
|
|
struct client_scgi *self = (struct client_scgi *) client;
|
2015-03-02 23:11:29 +01:00
|
|
|
struct error *e = NULL;
|
|
|
|
if (scgi_parser_push (&self->parser, data, len, &e))
|
|
|
|
return true;
|
|
|
|
|
2015-03-06 19:49:33 +01:00
|
|
|
if (e != NULL)
|
|
|
|
{
|
|
|
|
print_debug ("SCGI parser failed: %s", e->message);
|
|
|
|
error_free (e);
|
|
|
|
}
|
2015-03-02 23:11:29 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-16 04:33:33 +02:00
|
|
|
static void
|
|
|
|
client_scgi_finalize (struct client *client)
|
|
|
|
{
|
|
|
|
struct client_scgi *self = (struct client_scgi *) client;
|
|
|
|
request_free (&self->request);
|
|
|
|
scgi_parser_free (&self->parser);
|
|
|
|
}
|
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
static struct client_vtable client_scgi_vtable =
|
2015-03-02 23:11:29 +01:00
|
|
|
{
|
2018-10-16 04:12:37 +02:00
|
|
|
.push = client_scgi_push,
|
2018-10-16 04:33:33 +02:00
|
|
|
.finalize = client_scgi_finalize,
|
2015-03-02 23:11:29 +01:00
|
|
|
};
|
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
static struct client *
|
|
|
|
client_scgi_create (EV_P_ int sock_fd)
|
|
|
|
{
|
2018-10-17 05:16:17 +02:00
|
|
|
struct client_scgi *self = client_new (EV_A_ sizeof *self, sock_fd);
|
2016-01-16 22:21:29 +01:00
|
|
|
self->client.vtable = &client_scgi_vtable;
|
|
|
|
|
|
|
|
request_init (&self->request);
|
2018-10-18 06:41:12 +02:00
|
|
|
self->request.ctx = ev_userdata (EV_DEFAULT);
|
2016-01-16 22:21:29 +01:00
|
|
|
self->request.write_cb = client_scgi_write_cb;
|
2018-10-19 01:15:12 +02:00
|
|
|
self->request.finish_cb = client_scgi_finish_cb;
|
2016-01-16 22:21:29 +01:00
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
self->parser = scgi_parser_make ();
|
2016-01-16 22:21:29 +01:00
|
|
|
self->parser.on_headers_read = client_scgi_on_headers_read;
|
|
|
|
self->parser.on_content = client_scgi_on_content;
|
|
|
|
self->parser.user_data = self;
|
|
|
|
return &self->client;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- WebSockets client handler -----------------------------------------------
|
2015-03-08 09:41:10 +01:00
|
|
|
|
2015-03-09 23:32:01 +01:00
|
|
|
struct client_ws
|
|
|
|
{
|
2016-01-16 22:21:29 +01:00
|
|
|
struct client client; ///< Parent class
|
2015-03-09 23:32:01 +01:00
|
|
|
struct ws_handler handler; ///< WebSockets connection handler
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
2018-10-15 20:38:48 +02:00
|
|
|
client_ws_on_message (struct ws_handler *handler,
|
2015-03-14 19:36:37 +01:00
|
|
|
enum ws_opcode type, const void *data, size_t len)
|
2015-03-09 23:32:01 +01:00
|
|
|
{
|
2018-10-15 20:38:48 +02:00
|
|
|
FIND_CONTAINER (self, handler, struct client_ws, handler);
|
2015-03-15 04:32:04 +01:00
|
|
|
if (type != WS_OPCODE_TEXT)
|
|
|
|
{
|
2018-10-18 02:55:55 +02:00
|
|
|
return ws_handler_fail_connection
|
|
|
|
(&self->handler, WS_STATUS_UNSUPPORTED_DATA);
|
2015-03-15 04:32:04 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 08:40:18 +02:00
|
|
|
struct server_context *ctx = ev_userdata (EV_DEFAULT);
|
2018-06-24 00:40:10 +02:00
|
|
|
struct str response = str_make ();
|
2018-10-17 08:40:18 +02:00
|
|
|
process_json_rpc (ctx, data, len, &response);
|
2017-02-06 20:48:14 +01:00
|
|
|
if (response.len)
|
2018-10-18 02:55:55 +02:00
|
|
|
ws_handler_send_frame (&self->handler,
|
2017-02-06 20:48:14 +01:00
|
|
|
WS_OPCODE_TEXT, response.str, response.len);
|
2015-03-14 19:36:37 +01:00
|
|
|
str_free (&response);
|
2015-03-09 23:32:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-10 01:40:32 +02:00
|
|
|
static void
|
2018-10-15 20:38:48 +02:00
|
|
|
client_ws_write_cb (struct ws_handler *handler, const void *data, size_t len)
|
2015-04-10 01:40:32 +02:00
|
|
|
{
|
2018-10-15 20:38:48 +02:00
|
|
|
FIND_CONTAINER (self, handler, struct client_ws, handler);
|
|
|
|
client_write (&self->client, data, len);
|
2015-04-10 01:40:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-10-16 04:09:59 +02:00
|
|
|
client_ws_close_cb (struct ws_handler *handler, bool half_close)
|
2015-04-10 01:40:32 +02:00
|
|
|
{
|
2018-10-15 20:38:48 +02:00
|
|
|
FIND_CONTAINER (self, handler, struct client_ws, handler);
|
2018-10-17 23:26:11 +02:00
|
|
|
(half_close ? client_shutdown : client_destroy) (&self->client);
|
2015-04-10 01:40:32 +02:00
|
|
|
}
|
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2015-03-08 09:41:10 +01:00
|
|
|
|
2018-10-16 04:33:33 +02:00
|
|
|
static bool
|
|
|
|
client_ws_push (struct client *client, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
FIND_CONTAINER (self, client, struct client_ws, client);
|
2018-10-18 02:55:55 +02:00
|
|
|
// client_close() will correctly destroy the client on EOF
|
2018-10-16 04:33:33 +02:00
|
|
|
return ws_handler_push (&self->handler, data, len);
|
|
|
|
}
|
|
|
|
|
2015-04-10 02:44:13 +02:00
|
|
|
static void
|
|
|
|
client_ws_shutdown (struct client *client)
|
|
|
|
{
|
2018-10-16 04:05:42 +02:00
|
|
|
FIND_CONTAINER (self, client, struct client_ws, client);
|
2018-10-16 04:09:59 +02:00
|
|
|
if (self->handler.state == WS_HANDLER_CONNECTING)
|
2018-10-18 02:55:55 +02:00
|
|
|
// No on_close, no problem
|
|
|
|
client_destroy (&self->client);
|
2018-10-16 04:09:59 +02:00
|
|
|
else if (self->handler.state == WS_HANDLER_OPEN)
|
|
|
|
ws_handler_close (&self->handler, WS_STATUS_GOING_AWAY, NULL, 0);
|
2015-04-10 02:44:13 +02:00
|
|
|
}
|
|
|
|
|
2015-03-08 09:41:10 +01:00
|
|
|
static void
|
2018-10-16 04:12:37 +02:00
|
|
|
client_ws_finalize (struct client *client)
|
2015-03-08 09:41:10 +01:00
|
|
|
{
|
2018-10-16 04:05:42 +02:00
|
|
|
FIND_CONTAINER (self, client, struct client_ws, client);
|
2015-03-09 23:32:01 +01:00
|
|
|
ws_handler_free (&self->handler);
|
2015-03-08 09:41:10 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
static struct client_vtable client_ws_vtable =
|
2015-03-08 09:41:10 +01:00
|
|
|
{
|
2018-10-16 04:33:33 +02:00
|
|
|
.push = client_ws_push,
|
2015-04-10 02:44:13 +02:00
|
|
|
.shutdown = client_ws_shutdown,
|
2018-10-16 04:12:37 +02:00
|
|
|
.finalize = client_ws_finalize,
|
2015-03-08 09:41:10 +01:00
|
|
|
};
|
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
static struct client *
|
|
|
|
client_ws_create (EV_P_ int sock_fd)
|
|
|
|
{
|
2018-10-17 05:16:17 +02:00
|
|
|
struct client_ws *self = client_new (EV_A_ sizeof *self, sock_fd);
|
2016-01-16 22:21:29 +01:00
|
|
|
self->client.vtable = &client_ws_vtable;
|
|
|
|
|
|
|
|
ws_handler_init (&self->handler);
|
|
|
|
self->handler.on_message = client_ws_on_message;
|
|
|
|
self->handler.write_cb = client_ws_write_cb;
|
|
|
|
self->handler.close_cb = client_ws_close_cb;
|
|
|
|
|
|
|
|
// One mebibyte seems to be a reasonable value
|
|
|
|
self->handler.max_payload_len = 1 << 10;
|
2018-10-16 00:18:13 +02:00
|
|
|
|
|
|
|
ws_handler_start (&self->handler);
|
2016-01-16 22:21:29 +01:00
|
|
|
return &self->client;
|
|
|
|
}
|
|
|
|
|
2015-03-02 23:11:29 +01:00
|
|
|
// --- Basic server stuff ------------------------------------------------------
|
|
|
|
|
2016-01-16 22:21:29 +01:00
|
|
|
typedef struct client *(*client_create_fn) (EV_P_ int sock_fd);
|
|
|
|
|
2015-03-02 23:11:29 +01:00
|
|
|
struct listener
|
|
|
|
{
|
|
|
|
int fd; ///< Listening socket FD
|
|
|
|
ev_io watcher; ///< New connection available
|
2016-01-16 22:21:29 +01:00
|
|
|
client_create_fn create; ///< Client constructor
|
2015-03-02 23:11:29 +01:00
|
|
|
};
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2015-03-14 19:36:37 +01:00
|
|
|
static void
|
|
|
|
close_listeners (struct server_context *self)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < self->n_listeners; i++)
|
|
|
|
{
|
|
|
|
struct listener *listener = &self->listeners[i];
|
|
|
|
if (listener->fd == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ev_io_stop (EV_DEFAULT_ &listener->watcher);
|
|
|
|
xclose (listener->fd);
|
|
|
|
listener->fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 02:44:13 +02:00
|
|
|
static void
|
|
|
|
try_finish_quit (struct server_context *self)
|
|
|
|
{
|
|
|
|
if (!self->quitting || self->clients)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ev_timer_stop (EV_DEFAULT_ &self->quit_timeout_watcher);
|
|
|
|
ev_break (EV_DEFAULT_ EVBREAK_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-16 22:16:01 +01:00
|
|
|
on_quit_timeout (EV_P_ ev_timer *watcher, int revents)
|
2015-04-10 02:44:13 +02:00
|
|
|
{
|
|
|
|
struct server_context *self = watcher->data;
|
|
|
|
(void) loop;
|
|
|
|
(void) revents;
|
|
|
|
|
|
|
|
LIST_FOR_EACH (struct client, iter, self->clients)
|
2016-01-16 22:21:29 +01:00
|
|
|
client_destroy (iter);
|
2015-04-10 02:44:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
initiate_quit (struct server_context *self)
|
|
|
|
{
|
2017-02-06 17:18:24 +01:00
|
|
|
self->quitting = true;
|
2015-04-10 02:44:13 +02:00
|
|
|
close_listeners (self);
|
2017-02-06 17:18:24 +01:00
|
|
|
|
|
|
|
// Wait a little while for all clients to clean up, if necessary
|
2015-04-10 02:44:13 +02:00
|
|
|
LIST_FOR_EACH (struct client, iter, self->clients)
|
2016-01-16 22:21:29 +01:00
|
|
|
if (iter->vtable->shutdown)
|
|
|
|
iter->vtable->shutdown (iter);
|
2017-02-06 17:18:24 +01:00
|
|
|
ev_timer_start (EV_DEFAULT_ &self->quit_timeout_watcher);
|
|
|
|
try_finish_quit (self);
|
2015-04-10 02:44:13 +02:00
|
|
|
}
|
|
|
|
|
2015-03-10 20:48:25 +01:00
|
|
|
static void
|
|
|
|
on_client_available (EV_P_ ev_io *watcher, int revents)
|
|
|
|
{
|
2015-04-10 02:44:13 +02:00
|
|
|
struct server_context *ctx = ev_userdata (loop);
|
2015-03-10 20:48:25 +01:00
|
|
|
struct listener *listener = watcher->data;
|
|
|
|
(void) revents;
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2015-03-10 20:48:25 +01:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int sock_fd = accept (watcher->fd, NULL, NULL);
|
|
|
|
if (sock_fd != -1)
|
2016-01-16 22:21:29 +01:00
|
|
|
listener->create (EV_A_ sock_fd);
|
2015-03-10 20:48:25 +01:00
|
|
|
else if (errno == EAGAIN)
|
|
|
|
return;
|
2018-10-12 19:59:17 +02:00
|
|
|
else if (errno != EINTR && errno != EMFILE
|
|
|
|
&& errno != ECONNRESET && errno != ECONNABORTED)
|
2015-03-10 20:48:25 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2015-03-10 20:48:25 +01:00
|
|
|
// Stop accepting connections to prevent busy looping
|
|
|
|
ev_io_stop (EV_A_ watcher);
|
2015-03-02 23:11:29 +01:00
|
|
|
|
2015-03-10 20:48:25 +01:00
|
|
|
print_fatal ("%s: %s", "accept", strerror (errno));
|
2015-04-10 02:44:13 +02:00
|
|
|
initiate_quit (ctx);
|
2015-03-02 08:49:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Application setup -------------------------------------------------------
|
|
|
|
|
|
|
|
/// This function handles values that require validation before their first use,
|
|
|
|
/// or some kind of a transformation (such as conversion to an integer) needs
|
|
|
|
/// to be done before they can be used directly.
|
|
|
|
static bool
|
|
|
|
parse_config (struct server_context *ctx, struct error **e)
|
|
|
|
{
|
|
|
|
(void) ctx;
|
|
|
|
(void) e;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-11 00:24:20 +01:00
|
|
|
listener_bind (struct addrinfo *gai_iter)
|
2015-03-02 08:49:21 +01:00
|
|
|
{
|
|
|
|
int fd = socket (gai_iter->ai_family,
|
|
|
|
gai_iter->ai_socktype, gai_iter->ai_protocol);
|
|
|
|
if (fd == -1)
|
|
|
|
return -1;
|
|
|
|
set_cloexec (fd);
|
|
|
|
|
|
|
|
int yes = 1;
|
|
|
|
soft_assert (setsockopt (fd, SOL_SOCKET, SO_KEEPALIVE,
|
|
|
|
&yes, sizeof yes) != -1);
|
|
|
|
soft_assert (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
&yes, sizeof yes) != -1);
|
|
|
|
|
|
|
|
char host[NI_MAXHOST], port[NI_MAXSERV];
|
|
|
|
host[0] = port[0] = '\0';
|
|
|
|
int err = getnameinfo (gai_iter->ai_addr, gai_iter->ai_addrlen,
|
|
|
|
host, sizeof host, port, sizeof port,
|
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
if (err)
|
|
|
|
print_debug ("%s: %s", "getnameinfo", gai_strerror (err));
|
|
|
|
|
|
|
|
char *address = format_host_port_pair (host, port);
|
|
|
|
if (bind (fd, gai_iter->ai_addr, gai_iter->ai_addrlen))
|
|
|
|
print_error ("bind to %s failed: %s", address, strerror (errno));
|
|
|
|
else if (listen (fd, 16 /* arbitrary number */))
|
|
|
|
print_error ("listen on %s failed: %s", address, strerror (errno));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print_status ("listening on %s", address);
|
|
|
|
free (address);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
free (address);
|
|
|
|
xclose (fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-03-02 23:11:29 +01:00
|
|
|
listener_add (struct server_context *ctx, const char *host, const char *port,
|
2016-01-16 22:21:29 +01:00
|
|
|
const struct addrinfo *gai_hints, client_create_fn create)
|
2015-03-02 08:49:21 +01:00
|
|
|
{
|
|
|
|
struct addrinfo *gai_result, *gai_iter;
|
|
|
|
int err = getaddrinfo (host, port, gai_hints, &gai_result);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
char *address = format_host_port_pair (host, port);
|
|
|
|
print_error ("bind to %s failed: %s: %s",
|
|
|
|
address, "getaddrinfo", gai_strerror (err));
|
|
|
|
free (address);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
for (gai_iter = gai_result; gai_iter; gai_iter = gai_iter->ai_next)
|
|
|
|
{
|
2015-03-11 00:24:20 +01:00
|
|
|
if ((fd = listener_bind (gai_iter)) == -1)
|
2015-03-02 08:49:21 +01:00
|
|
|
continue;
|
|
|
|
set_blocking (fd, false);
|
|
|
|
|
|
|
|
struct listener *listener = &ctx->listeners[ctx->n_listeners++];
|
2015-03-02 23:11:29 +01:00
|
|
|
ev_io_init (&listener->watcher, on_client_available, fd, EV_READ);
|
2015-03-02 08:49:21 +01:00
|
|
|
ev_io_start (EV_DEFAULT_ &listener->watcher);
|
2015-03-02 23:11:29 +01:00
|
|
|
listener->watcher.data = listener;
|
2016-01-16 22:21:29 +01:00
|
|
|
listener->create = create;
|
2017-06-20 14:01:23 +02:00
|
|
|
listener->fd = fd;
|
2015-03-02 08:49:21 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
freeaddrinfo (gai_result);
|
|
|
|
}
|
|
|
|
|
2015-03-11 00:24:20 +01:00
|
|
|
static void
|
|
|
|
get_ports_from_config (struct server_context *ctx,
|
2017-02-05 22:44:01 +01:00
|
|
|
const char *key, struct strv *out)
|
2015-03-11 00:24:20 +01:00
|
|
|
{
|
|
|
|
const char *ports;
|
|
|
|
if ((ports = str_map_find (&ctx->config, key)))
|
2017-02-05 22:44:01 +01:00
|
|
|
cstr_split (ports, ",", true, out);
|
2015-03-11 00:24:20 +01:00
|
|
|
}
|
|
|
|
|
2015-03-02 08:49:21 +01:00
|
|
|
static bool
|
|
|
|
setup_listen_fds (struct server_context *ctx, struct error **e)
|
|
|
|
{
|
2015-03-10 20:48:25 +01:00
|
|
|
static const struct addrinfo gai_hints =
|
|
|
|
{
|
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
.ai_flags = AI_PASSIVE,
|
|
|
|
};
|
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
struct strv ports_fcgi = strv_make ();
|
|
|
|
struct strv ports_scgi = strv_make ();
|
|
|
|
struct strv ports_ws = strv_make ();
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2015-03-11 00:24:20 +01:00
|
|
|
get_ports_from_config (ctx, "port_fastcgi", &ports_fcgi);
|
|
|
|
get_ports_from_config (ctx, "port_scgi", &ports_scgi);
|
|
|
|
get_ports_from_config (ctx, "port_ws", &ports_ws);
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2015-03-11 00:24:20 +01:00
|
|
|
const char *bind_host = str_map_find (&ctx->config, "bind_host");
|
2015-03-10 20:48:25 +01:00
|
|
|
size_t n_ports = ports_fcgi.len + ports_scgi.len + ports_ws.len;
|
2015-03-02 08:49:21 +01:00
|
|
|
ctx->listeners = xcalloc (n_ports, sizeof *ctx->listeners);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ports_fcgi.len; i++)
|
2015-03-02 23:11:29 +01:00
|
|
|
listener_add (ctx, bind_host, ports_fcgi.vector[i],
|
2016-01-16 22:21:29 +01:00
|
|
|
&gai_hints, client_fcgi_create);
|
2015-03-02 08:49:21 +01:00
|
|
|
for (size_t i = 0; i < ports_scgi.len; i++)
|
2015-03-02 23:11:29 +01:00
|
|
|
listener_add (ctx, bind_host, ports_scgi.vector[i],
|
2016-01-16 22:21:29 +01:00
|
|
|
&gai_hints, client_scgi_create);
|
2015-03-08 09:41:10 +01:00
|
|
|
for (size_t i = 0; i < ports_ws.len; i++)
|
|
|
|
listener_add (ctx, bind_host, ports_ws.vector[i],
|
2016-01-16 22:21:29 +01:00
|
|
|
&gai_hints, client_ws_create);
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2017-02-05 22:44:01 +01:00
|
|
|
strv_free (&ports_fcgi);
|
|
|
|
strv_free (&ports_scgi);
|
|
|
|
strv_free (&ports_ws);
|
2015-03-02 08:49:21 +01:00
|
|
|
|
|
|
|
if (!ctx->n_listeners)
|
|
|
|
{
|
|
|
|
error_set (e, "%s: %s",
|
|
|
|
"network setup failed", "no ports to listen on");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-16 22:16:25 +01:00
|
|
|
static bool
|
|
|
|
app_lock_pid_file (struct server_context *ctx, struct error **e)
|
|
|
|
{
|
|
|
|
const char *path = str_map_find (&ctx->config, "pid_file");
|
|
|
|
if (!path)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
char *resolved = resolve_filename (path, resolve_relative_runtime_filename);
|
|
|
|
bool result = lock_pid_file (resolved, e) != -1;
|
|
|
|
free (resolved);
|
|
|
|
return result;
|
2015-03-11 23:56:25 +01:00
|
|
|
}
|
|
|
|
|
2015-03-23 20:12:53 +01:00
|
|
|
// --- Tests -------------------------------------------------------------------
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_misc (void)
|
|
|
|
{
|
|
|
|
soft_assert ( validate_json_rpc_content_type
|
|
|
|
("application/JSON; charset=\"utf-8\""));
|
|
|
|
soft_assert (!validate_json_rpc_content_type
|
|
|
|
("text/html; charset=\"utf-8\""));
|
|
|
|
|
|
|
|
char *canon = canonicalize_url_path ("///../../../etc/./passwd");
|
|
|
|
soft_assert (!strcmp (canon, "/etc/passwd"));
|
|
|
|
free (canon);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
test_main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct test test;
|
|
|
|
test_init (&test, argc, argv);
|
|
|
|
|
|
|
|
test_add_simple (&test, "/misc", NULL, test_misc);
|
|
|
|
|
|
|
|
// TODO: write more tests
|
2015-03-29 03:14:20 +02:00
|
|
|
// TODO: test the server handler (happy path)
|
2015-03-23 20:12:53 +01:00
|
|
|
|
|
|
|
return test_run (&test);
|
|
|
|
}
|
|
|
|
|
2015-03-02 08:49:21 +01:00
|
|
|
// --- Main program ------------------------------------------------------------
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_termination_signal (EV_P_ ev_signal *handle, int revents)
|
|
|
|
{
|
|
|
|
struct server_context *ctx = ev_userdata (loop);
|
|
|
|
(void) handle;
|
|
|
|
(void) revents;
|
|
|
|
|
2018-10-17 06:08:11 +02:00
|
|
|
if (ctx->quitting)
|
|
|
|
{
|
|
|
|
// Double C-c from the terminal accelerates the process
|
|
|
|
LIST_FOR_EACH (struct client, iter, ctx->clients)
|
|
|
|
client_destroy (iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
initiate_quit (ctx);
|
2016-01-16 22:21:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup_signal_handlers (struct server_context *ctx)
|
|
|
|
{
|
|
|
|
ev_signal_init (&ctx->sigterm_watcher, on_termination_signal, SIGTERM);
|
|
|
|
ev_signal_start (EV_DEFAULT_ &ctx->sigterm_watcher);
|
|
|
|
|
|
|
|
ev_signal_init (&ctx->sigint_watcher, on_termination_signal, SIGINT);
|
|
|
|
ev_signal_start (EV_DEFAULT_ &ctx->sigint_watcher);
|
|
|
|
|
|
|
|
(void) signal (SIGPIPE, SIG_IGN);
|
2015-03-02 08:49:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-16 22:16:25 +01:00
|
|
|
daemonize (struct server_context *ctx)
|
2015-03-02 08:49:21 +01:00
|
|
|
{
|
|
|
|
print_status ("daemonizing...");
|
|
|
|
|
|
|
|
if (chdir ("/"))
|
|
|
|
exit_fatal ("%s: %s", "chdir", strerror (errno));
|
|
|
|
|
2016-01-16 22:16:25 +01:00
|
|
|
// Because of systemd, we need to exit the parent process _after_ writing
|
|
|
|
// a PID file, otherwise our grandchild would receive a SIGTERM
|
|
|
|
int sync_pipe[2];
|
|
|
|
if (pipe (sync_pipe))
|
|
|
|
exit_fatal ("%s: %s", "pipe", strerror (errno));
|
|
|
|
|
2015-03-02 08:49:21 +01:00
|
|
|
pid_t pid;
|
|
|
|
if ((pid = fork ()) < 0)
|
|
|
|
exit_fatal ("%s: %s", "fork", strerror (errno));
|
|
|
|
else if (pid)
|
2016-01-16 22:16:25 +01:00
|
|
|
{
|
|
|
|
// Wait until all write ends of the pipe are closed, which can mean
|
|
|
|
// either success or failure, we don't need to care
|
|
|
|
xclose (sync_pipe[PIPE_WRITE]);
|
|
|
|
|
|
|
|
char dummy;
|
|
|
|
if (read (sync_pipe[PIPE_READ], &dummy, 1) < 0)
|
|
|
|
exit_fatal ("%s: %s", "read", strerror (errno));
|
|
|
|
|
2015-03-02 08:49:21 +01:00
|
|
|
exit (EXIT_SUCCESS);
|
2016-01-16 22:16:25 +01:00
|
|
|
}
|
2015-03-02 08:49:21 +01:00
|
|
|
|
|
|
|
setsid ();
|
|
|
|
signal (SIGHUP, SIG_IGN);
|
|
|
|
|
|
|
|
if ((pid = fork ()) < 0)
|
|
|
|
exit_fatal ("%s: %s", "fork", strerror (errno));
|
|
|
|
else if (pid)
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
|
|
|
|
openlog (PROGRAM_NAME, LOG_NDELAY | LOG_NOWAIT | LOG_PID, 0);
|
|
|
|
g_log_message_real = log_message_syslog;
|
|
|
|
|
2016-01-16 22:16:25 +01:00
|
|
|
// Write the PID file (if so configured) and get rid of the pipe, so that
|
|
|
|
// the read() in our grandparent finally returns zero (no write ends)
|
|
|
|
struct error *e = NULL;
|
|
|
|
if (!app_lock_pid_file (ctx, &e))
|
|
|
|
exit_fatal ("%s", e->message);
|
|
|
|
|
|
|
|
xclose (sync_pipe[PIPE_READ]);
|
|
|
|
xclose (sync_pipe[PIPE_WRITE]);
|
|
|
|
|
2015-03-11 23:56:25 +01:00
|
|
|
// XXX: we may close our own descriptors this way, crippling ourselves;
|
|
|
|
// there is no real guarantee that we will start with all three
|
|
|
|
// descriptors open. In theory we could try to enumerate the descriptors
|
|
|
|
// at the start of main().
|
2015-03-02 08:49:21 +01:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
xclose (i);
|
|
|
|
|
|
|
|
int tty = open ("/dev/null", O_RDWR);
|
|
|
|
if (tty != 0 || dup (0) != 1 || dup (0) != 2)
|
|
|
|
exit_fatal ("failed to reopen FD's: %s", strerror (errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
parse_program_arguments (int argc, char **argv)
|
|
|
|
{
|
|
|
|
static const struct opt opts[] =
|
|
|
|
{
|
2015-03-23 20:12:53 +01:00
|
|
|
{ 't', "test", NULL, 0, "self-test" },
|
2015-03-02 08:49:21 +01:00
|
|
|
{ 'd', "debug", NULL, 0, "run in debug mode" },
|
|
|
|
{ 'h', "help", NULL, 0, "display this help and exit" },
|
|
|
|
{ 'V', "version", NULL, 0, "output version information and exit" },
|
|
|
|
{ 'w', "write-default-cfg", "FILENAME",
|
|
|
|
OPT_OPTIONAL_ARG | OPT_LONG_ONLY,
|
|
|
|
"write a default configuration file and exit" },
|
|
|
|
{ 0, NULL, NULL, 0, NULL }
|
|
|
|
};
|
|
|
|
|
2018-06-24 00:40:10 +02:00
|
|
|
struct opt_handler oh =
|
|
|
|
opt_handler_make (argc, argv, opts, NULL, "JSON-RPC 2.0 demo server.");
|
2015-03-02 08:49:21 +01:00
|
|
|
|
|
|
|
int c;
|
|
|
|
while ((c = opt_handler_get (&oh)) != -1)
|
|
|
|
switch (c)
|
|
|
|
{
|
2015-03-23 20:12:53 +01:00
|
|
|
case 't':
|
|
|
|
test_main (argc, argv);
|
|
|
|
exit (EXIT_SUCCESS);
|
2015-03-02 08:49:21 +01:00
|
|
|
case 'd':
|
|
|
|
g_debug_mode = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
opt_handler_usage (&oh, stdout);
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
case 'V':
|
|
|
|
printf (PROGRAM_NAME " " PROGRAM_VERSION "\n");
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
case 'w':
|
2016-01-16 06:41:31 +01:00
|
|
|
call_simple_config_write_default (optarg, g_config_table);
|
2015-03-02 08:49:21 +01:00
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
default:
|
|
|
|
print_error ("wrong options");
|
|
|
|
opt_handler_usage (&oh, stderr);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc)
|
|
|
|
{
|
|
|
|
opt_handler_usage (&oh, stderr);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
opt_handler_free (&oh);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
parse_program_arguments (argc, argv);
|
|
|
|
|
|
|
|
print_status (PROGRAM_NAME " " PROGRAM_VERSION " starting");
|
|
|
|
|
|
|
|
struct server_context ctx;
|
|
|
|
server_context_init (&ctx);
|
|
|
|
|
|
|
|
struct error *e = NULL;
|
2016-01-16 06:41:31 +01:00
|
|
|
if (!simple_config_update_from_file (&ctx.config, &e))
|
2015-03-02 08:49:21 +01:00
|
|
|
{
|
|
|
|
print_error ("error loading configuration: %s", e->message);
|
|
|
|
error_free (e);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ev_loop *loop;
|
|
|
|
if (!(loop = EV_DEFAULT))
|
|
|
|
exit_fatal ("libev initialization failed");
|
|
|
|
|
|
|
|
ev_set_userdata (loop, &ctx);
|
2016-01-16 22:21:29 +01:00
|
|
|
setup_signal_handlers (&ctx);
|
2015-03-02 08:49:21 +01:00
|
|
|
|
2015-03-08 09:41:10 +01:00
|
|
|
LIST_PREPEND (ctx.handlers, &g_request_handler_static);
|
2015-03-08 05:39:57 +01:00
|
|
|
LIST_PREPEND (ctx.handlers, &g_request_handler_json_rpc);
|
|
|
|
|
2015-03-02 08:49:21 +01:00
|
|
|
if (!parse_config (&ctx, &e)
|
|
|
|
|| !setup_listen_fds (&ctx, &e))
|
|
|
|
{
|
|
|
|
print_error ("%s", e->message);
|
|
|
|
error_free (e);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_debug_mode)
|
2016-01-16 22:16:25 +01:00
|
|
|
daemonize (&ctx);
|
|
|
|
else if (!app_lock_pid_file (&ctx, &e))
|
|
|
|
exit_fatal ("%s", e->message);
|
2015-03-02 08:49:21 +01:00
|
|
|
|
|
|
|
ev_run (loop, 0);
|
|
|
|
ev_loop_destroy (loop);
|
|
|
|
|
|
|
|
server_context_free (&ctx);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|