From 8a3241d5c46e8b8fac382f606147285e314f1f6b Mon Sep 17 00:00:00 2001 From: Přemysl Janouch
Date: Sun, 24 Jun 2018 00:40:10 +0200 Subject: Bump liberty --- .gitmodules | 2 +- demo-json-rpc-server.c | 106 ++++++++++++++++++++----------------------------- liberty | 2 +- 3 files changed, 44 insertions(+), 66 deletions(-) (limited to 'liberty') diff --git a/.gitmodules b/.gitmodules index c8d5acf..1a41faf 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "liberty"] path = liberty - url = git://github.com/pjanouch/liberty.git + url = https://git.janouch.name/p/liberty.git [submodule "http-parser"] path = http-parser url = https://github.com/joyent/http-parser.git diff --git a/demo-json-rpc-server.c b/demo-json-rpc-server.c index 10c808e..0cfb13b 100644 --- a/demo-json-rpc-server.c +++ b/demo-json-rpc-server.c @@ -144,8 +144,7 @@ fcgi_muxer_send (struct fcgi_muxer *self, { hard_assert (len <= UINT16_MAX); - struct str message; - str_init (&message); + struct str message = str_make (); str_pack_u8 (&message, FCGI_VERSION_1); str_pack_u8 (&message, type); @@ -177,10 +176,9 @@ fcgi_request_init (struct fcgi_request *self) { memset (self, 0, sizeof *self); - str_map_init (&self->headers); - self->headers.free = free; + self->headers = str_map_make (free); - fcgi_nv_parser_init (&self->hdr_parser); + self->hdr_parser = fcgi_nv_parser_make (); self->hdr_parser.output = &self->headers; } @@ -275,17 +273,15 @@ static void fcgi_muxer_on_get_values (struct fcgi_muxer *self, const struct fcgi_parser *parser) { - struct str_map values; str_map_init (&values); values.free = free; - struct str_map response; str_map_init (&response); response.free = free; + struct str_map values = str_map_make (free); + struct str_map response = str_map_make (free); - struct fcgi_nv_parser nv_parser; - fcgi_nv_parser_init (&nv_parser); + struct fcgi_nv_parser nv_parser = fcgi_nv_parser_make (); nv_parser.output = &values; fcgi_nv_parser_push (&nv_parser, parser->content.str, parser->content.len); - struct str_map_iter iter; - str_map_iter_init (&iter, &values); + struct str_map_iter iter = str_map_iter_make (&values); while (str_map_iter_next (&iter)) { const char *key = iter.link->key; @@ -297,8 +293,7 @@ fcgi_muxer_on_get_values str_map_set (&response, key, xstrdup ("1")); } - struct str content; - str_init (&content); + struct str content = str_make (); fcgi_nv_convert (&response, &content); fcgi_muxer_send (self, FCGI_GET_VALUES_RESULT, parser->request_id, content.str, content.len); @@ -312,8 +307,8 @@ static void fcgi_muxer_on_begin_request (struct fcgi_muxer *self, const struct fcgi_parser *parser) { - struct msg_unpacker unpacker; - msg_unpacker_init (&unpacker, parser->content.str, parser->content.len); + struct msg_unpacker unpacker = + msg_unpacker_make (parser->content.str, parser->content.len); uint16_t role; uint8_t flags; @@ -435,7 +430,7 @@ fcgi_muxer_on_message (const struct fcgi_parser *parser, void *user_data) static void fcgi_muxer_init (struct fcgi_muxer *self) { - fcgi_parser_init (&self->parser); + self->parser = fcgi_parser_make (); self->parser.on_message = fcgi_muxer_on_message; self->parser.user_data = self; } @@ -554,8 +549,7 @@ static void ws_handler_close (struct ws_handler *self, enum ws_status close_code, const char *reason, size_t len) { - struct str payload; - str_init (&payload); + struct str payload = str_make (); 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); @@ -590,8 +584,7 @@ ws_handler_send (struct ws_handler *self, if (!soft_assert (self->state == WS_HANDLER_OPEN)) return; - struct str header; - str_init (&header); + struct str header = str_make (); str_pack_u8 (&header, 0x80 | (opcode & 0x0F)); if (len > UINT16_MAX) @@ -639,8 +632,8 @@ static bool ws_handler_on_protocol_close (struct ws_handler *self, const struct ws_parser *parser) { - struct msg_unpacker unpacker; - msg_unpacker_init (&unpacker, parser->input.str, parser->payload_len); + struct msg_unpacker unpacker = + msg_unpacker_make (parser->input.str, parser->payload_len); char *reason = NULL; uint16_t close_code = WS_STATUS_NO_STATUS_RECEIVED; @@ -775,21 +768,20 @@ ws_handler_init (struct ws_handler *self) http_parser_init (&self->hp, HTTP_REQUEST); self->hp.data = self; - str_init (&self->field); - str_init (&self->value); - str_map_init (&self->headers); - self->headers.free = free; + self->field = str_make (); + self->value = str_make (); + self->headers = str_map_make (free); self->headers.key_xfrm = tolower_ascii_strxfrm; - str_init (&self->url); + self->url = str_make (); ev_timer_init (&self->handshake_timeout_watcher, ws_handler_on_handshake_timeout, 0., 0.); self->handshake_timeout_watcher.data = self; - ws_parser_init (&self->parser); + self->parser = ws_parser_make (); self->parser.on_frame_header = ws_handler_on_frame_header; self->parser.on_frame = ws_handler_on_frame; self->parser.user_data = self; - str_init (&self->message_data); + self->message_data = str_make (); ev_timer_init (&self->ping_timer, ws_handler_on_ping_timer, 0., 0.); @@ -927,8 +919,7 @@ ws_handler_http_responsev (struct ws_handler *self, { hard_assert (status != NULL); - struct str response; - str_init (&response); + struct str response = str_make (); str_append_printf (&response, "HTTP/1.1 %s\r\n", status); while (*fields) @@ -943,8 +934,7 @@ ws_handler_http_responsev (struct ws_handler *self, static void ws_handler_http_response (struct ws_handler *self, const char *status, ...) { - struct strv v; - strv_init (&v); + struct strv v = strv_make (); va_list ap; va_start (ap, status); @@ -1015,8 +1005,7 @@ ws_handler_finish_handshake (struct ws_handler *self) if (!key) FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST, NULL); - struct str tmp; - str_init (&tmp); + struct str tmp = str_make (); bool key_is_valid = base64_decode (key, false, &tmp) && tmp.len == 16; str_free (&tmp); if (!key_is_valid) @@ -1027,9 +1016,7 @@ ws_handler_finish_handshake (struct ws_handler *self) if (strcmp (version, "13")) FAIL_HANDSHAKE (HTTP_400_BAD_REQUEST, SEC_WS_VERSION ": 13", NULL); - struct strv fields; - strv_init (&fields); - + struct strv fields = strv_make (); strv_append_args (&fields, "Upgrade: websocket", "Connection: Upgrade", @@ -1181,7 +1168,7 @@ server_context_init (struct server_context *self) { memset (self, 0, sizeof *self); - str_map_init (&self->config); + self->config = str_map_make (NULL); simple_config_load_defaults (&self->config, g_config_table); ev_timer_init (&self->quit_timeout_watcher, on_quit_timeout, 3., 0.); self->quit_timeout_watcher.data = self; @@ -1260,9 +1247,7 @@ validate_json_rpc_content_type (const char *content_type) char *type = NULL; char *subtype = NULL; - struct str_map parameters; - str_map_init (¶meters); - parameters.free = free; + struct str_map parameters = str_map_make (free); parameters.key_xfrm = tolower_ascii_strxfrm; bool result = http_parse_media_type @@ -1503,8 +1488,7 @@ request_start (struct request *self, struct str_map *headers) } // Unable to serve the request - struct str response; - str_init (&response); + struct str response = str_make (); str_append (&response, "Status: 404 Not Found\n"); str_append (&response, "Content-Type: text/plain\n\n"); self->write_cb (self->user_data, response.str, response.len); @@ -1536,7 +1520,7 @@ request_handler_json_rpc_try_handle return false; struct str *buf = xcalloc (1, sizeof *buf); - str_init (buf); + *buf = str_make (); request->handler_data = buf; *continue_ = true; @@ -1554,8 +1538,7 @@ request_handler_json_rpc_push return true; } - struct str response; - str_init (&response); + struct str response = str_make (); 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); @@ -1587,12 +1570,10 @@ static char * canonicalize_url_path (const char *path) { // XXX: this strips any slashes at the end - struct strv v; - strv_init (&v); + struct strv v = strv_make (); cstr_split (path, "/", true, &v); - struct strv canonical; - strv_init (&canonical); + struct strv canonical = strv_make (); // So that the joined path always begins with a slash strv_append (&canonical, ""); @@ -1672,8 +1653,7 @@ request_handler_static_try_handle FILE *fp = fopen (path, "rb"); if (!fp) { - struct str response; - str_init (&response); + struct str response = str_make (); str_append (&response, "Status: 404 Not Found\n"); str_append (&response, "Content-Type: text/plain\n\n"); str_append_printf (&response, @@ -1699,8 +1679,7 @@ request_handler_static_try_handle if (!mime_type) mime_type = xstrdup ("application/octet_stream"); - struct str response; - str_init (&response); + struct str response = str_make (); str_append (&response, "Status: 200 OK\n"); str_append_printf (&response, "Content-Type: %s\n\n", mime_type); request->write_cb (request->user_data, response.str, response.len); @@ -1878,7 +1857,7 @@ client_init (EV_P_ struct client *self, int sock_fd) memset (self, 0, sizeof *self); self->ctx = ctx; - write_queue_init (&self->write_queue); + self->write_queue = write_queue_make (); set_blocking (sock_fd, false); self->socket_fd = sock_fd; @@ -2119,7 +2098,7 @@ client_scgi_create (EV_P_ int sock_fd) self->request.close_cb = client_scgi_close_cb; self->request.user_data = self; - scgi_parser_init (&self->parser); + self->parser = scgi_parser_make (); self->parser.on_headers_read = client_scgi_on_headers_read; self->parser.on_content = client_scgi_on_content; self->parser.user_data = self; @@ -2145,8 +2124,7 @@ client_ws_on_message (void *user_data, return false; } - struct str response; - str_init (&response); + struct str response = str_make (); process_json_rpc (self->client.ctx, data, len, &response); if (response.len) ws_handler_send (&self->handler, @@ -2410,9 +2388,9 @@ setup_listen_fds (struct server_context *ctx, struct error **e) .ai_flags = AI_PASSIVE, }; - struct strv ports_fcgi; strv_init (&ports_fcgi); - struct strv ports_scgi; strv_init (&ports_scgi); - struct strv ports_ws; strv_init (&ports_ws); + struct strv ports_fcgi = strv_make (); + struct strv ports_scgi = strv_make (); + struct strv ports_ws = strv_make (); get_ports_from_config (ctx, "port_fastcgi", &ports_fcgi); get_ports_from_config (ctx, "port_scgi", &ports_scgi); @@ -2588,8 +2566,8 @@ parse_program_arguments (int argc, char **argv) { 0, NULL, NULL, 0, NULL } }; - struct opt_handler oh; - opt_handler_init (&oh, argc, argv, opts, NULL, "JSON-RPC 2.0 demo server."); + struct opt_handler oh = + opt_handler_make (argc, argv, opts, NULL, "JSON-RPC 2.0 demo server."); int c; while ((c = opt_handler_get (&oh)) != -1) diff --git a/liberty b/liberty index 1dcd259..8ffe20c 160000 --- a/liberty +++ b/liberty @@ -1 +1 @@ -Subproject commit 1dcd259d0506b9e2de3715bdf07144b22f57903a +Subproject commit 8ffe20c0e83b52db1344fe91f57236be4c4cb504 -- cgit v1.2.3-70-g09d2 From 329fc9b88fa419ef2c3a32ccc4414dec223bd69d Mon Sep 17 00:00:00 2001 From: Přemysl Janouch
Date: Thu, 11 Oct 2018 21:03:34 +0200 Subject: Bump liberty Eliminates some fall-through warnings. --- liberty | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'liberty') diff --git a/liberty b/liberty index 8ffe20c..9494e8e 160000 --- a/liberty +++ b/liberty @@ -1 +1 @@ -Subproject commit 8ffe20c0e83b52db1344fe91f57236be4c4cb504 +Subproject commit 9494e8e2affcd08b791d3ecf68985a8a3a310e55 -- cgit v1.2.3-70-g09d2 From d883f4cc71a1f9db5cc4cec2bc60df6cba1f8833 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch
Date: Thu, 18 Oct 2018 04:13:59 +0200 Subject: Finish the FastCGI backend Bump liberty, also fixing SCGI. --- demo-json-rpc-server.c | 154 ++++++++++++++++++++++++------------------------- liberty | 2 +- 2 files changed, 75 insertions(+), 81 deletions(-) (limited to 'liberty') diff --git a/demo-json-rpc-server.c b/demo-json-rpc-server.c index 60a7173..50e203f 100644 --- a/demo-json-rpc-server.c +++ b/demo-json-rpc-server.c @@ -194,6 +194,8 @@ fcgi_request_new (void) self->hdr_parser = fcgi_nv_parser_make (); self->hdr_parser.output = &self->headers; + + self->output_buffer = str_make (); return self; } @@ -208,40 +210,6 @@ fcgi_request_destroy (struct fcgi_request *self) free (self); } -static void -fcgi_request_push_params - (struct fcgi_request *self, const void *data, size_t len) -{ - if (self->state != FCGI_REQUEST_PARAMS) - { - // TODO: probably reject the request - return; - } - - if (len) - fcgi_nv_parser_push (&self->hdr_parser, data, len); - else - { - // TODO: probably check the state of the header parser - // TODO: request_start() can return false, end the request here? - (void) self->muxer->request_start_cb (self); - self->state = FCGI_REQUEST_STDIN; - } -} - -static void -fcgi_request_push_stdin - (struct fcgi_request *self, const void *data, size_t len) -{ - if (self->state != FCGI_REQUEST_STDIN) - { - // TODO: probably reject the request - return; - } - - self->muxer->request_push_cb (self, data, len); -} - static void fcgi_request_flush (struct fcgi_request *self) { @@ -294,36 +262,62 @@ fcgi_request_finish (struct fcgi_request *self) self->muxer->requests[self->request_id] = NULL; fcgi_request_destroy (self); - // TODO: tear down (shut down) the connection. This is called from: - // - // 1. client_fcgi_request_push <- request_push_cb - // <- fcgi_request_push_stdin <- fcgi_muxer_on_stdin - // <- fcgi_muxer_on_message <- fcgi_parser_push <- fcgi_muxer_push - // <- client_fcgi_push <- client_read_loop - // => in this case no close_cb may be called - // -> need to pass a false boolean aaall the way up, - // then client_fcgi_finalize eventually cleans up the rest - // - // 2. client_fcgi_request_close_cb <- request_finish - // => our direct caller must call fcgi_muxer::close_cb - // -> not very nice to delegate it there return !should_close; } +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)) + return fcgi_request_finish (self); + } + 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); +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -typedef void (*fcgi_muxer_handler_fn) +typedef bool (*fcgi_muxer_handler_fn) (struct fcgi_muxer *, const struct fcgi_parser *); -static void +static bool fcgi_muxer_on_get_values (struct fcgi_muxer *self, const struct fcgi_parser *parser) { if (parser->request_id != FCGI_NULL_REQUEST_ID) { - print_debug ("FastCGI: ignoring invalid %s message", + print_debug ("FastCGI: invalid %s message", STRINGIFY (FCGI_GET_VALUES)); - return; + return false; } struct str_map values = str_map_make (free); @@ -357,9 +351,10 @@ fcgi_muxer_on_get_values str_map_free (&values); str_map_free (&response); + return true; } -static void +static bool fcgi_muxer_on_begin_request (struct fcgi_muxer *self, const struct fcgi_parser *parser) { @@ -375,16 +370,17 @@ fcgi_muxer_on_begin_request if (!success) { - print_debug ("FastCGI: ignoring invalid %s message", + print_debug ("FastCGI: invalid %s message", STRINGIFY (FCGI_BEGIN_REQUEST)); - return; + return false; } struct fcgi_request *request = self->requests[parser->request_id]; if (parser->request_id == FCGI_NULL_REQUEST_ID || request) { - // TODO: fail - return; + print_debug ("FastCGI: unusable request ID in %s message", + STRINGIFY (FCGI_BEGIN_REQUEST)); + return false; } // We can only act as a responder, reject everything else up front @@ -392,7 +388,7 @@ fcgi_muxer_on_begin_request { fcgi_muxer_send_end_request (self, parser->request_id, 0, FCGI_UNKNOWN_ROLE); - return; + return true; } if (parser->request_id >= N_ELEMENTS (self->requests) @@ -400,7 +396,7 @@ fcgi_muxer_on_begin_request { fcgi_muxer_send_end_request (self, parser->request_id, 0, FCGI_OVERLOADED); - return; + return true; } request = fcgi_request_new (); @@ -410,9 +406,10 @@ fcgi_muxer_on_begin_request self->requests[parser->request_id] = request; self->active_requests++; + return true; } -static void +static bool fcgi_muxer_on_abort_request (struct fcgi_muxer *self, const struct fcgi_parser *parser) { @@ -421,15 +418,13 @@ fcgi_muxer_on_abort_request { print_debug ("FastCGI: received %s for an unknown request", STRINGIFY (FCGI_ABORT_REQUEST)); - return; + return true; // We might have just rejected it } - // TODO: abort the request: let it somehow produce FCGI_END_REQUEST, - // make sure to send an stdout EOF record - // TODO: and if that was not a FCGI_KEEP_CONN request, close the transport + return fcgi_request_finish (request); } -static void +static bool fcgi_muxer_on_params (struct fcgi_muxer *self, const struct fcgi_parser *parser) { struct fcgi_request *request = self->requests[parser->request_id]; @@ -437,14 +432,15 @@ fcgi_muxer_on_params (struct fcgi_muxer *self, const struct fcgi_parser *parser) { print_debug ("FastCGI: received %s for an unknown request", STRINGIFY (FCGI_PARAMS)); - return; + return true; // We might have just rejected it } - fcgi_request_push_params (request, + // This may immediately finish and delete the request, but that's fine + return fcgi_request_push_params (request, parser->content.str, parser->content.len); } -static void +static bool fcgi_muxer_on_stdin (struct fcgi_muxer *self, const struct fcgi_parser *parser) { struct fcgi_request *request = self->requests[parser->request_id]; @@ -452,15 +448,15 @@ fcgi_muxer_on_stdin (struct fcgi_muxer *self, const struct fcgi_parser *parser) { print_debug ("FastCGI: received %s for an unknown request", STRINGIFY (FCGI_STDIN)); - return; + return true; // We might have just rejected it } // At the end of the stream, a zero-length record is received - fcgi_request_push_stdin (request, + return fcgi_request_push_stdin (request, parser->content.str, parser->content.len); } -static void +static bool fcgi_muxer_on_message (const struct fcgi_parser *parser, void *user_data) { struct fcgi_muxer *self = user_data; @@ -468,8 +464,7 @@ fcgi_muxer_on_message (const struct fcgi_parser *parser, void *user_data) if (parser->version != FCGI_VERSION_1) { print_debug ("FastCGI: unsupported version %d", parser->version); - // TODO: also return false to stop processing on protocol error? - return; + return false; } static const fcgi_muxer_handler_fn handlers[] = @@ -489,10 +484,10 @@ fcgi_muxer_on_message (const struct fcgi_parser *parser, void *user_data) uint8_t content[8] = { parser->type }; fcgi_muxer_send (self, FCGI_UNKNOWN_TYPE, parser->request_id, content, sizeof content); - return; + return true; } - handler (self, parser); + return handler (self, parser); } static void @@ -520,10 +515,10 @@ fcgi_muxer_free (struct fcgi_muxer *self) fcgi_parser_free (&self->parser); } -static void +static bool fcgi_muxer_push (struct fcgi_muxer *self, const void *data, size_t len) { - fcgi_parser_push (&self->parser, data, len); + return fcgi_parser_push (&self->parser, data, len); } /// @} @@ -1574,7 +1569,7 @@ struct request /// Callback to write some CGI response data to the output void (*write_cb) (struct request *, const void *data, size_t len); - /// Callback to close the connection. + /// Callback to close the CGI response, simulates end of program execution. /// CALLING THIS MAY CAUSE THE REQUEST TO BE DESTROYED. void (*close_cb) (struct request *); }; @@ -1967,7 +1962,7 @@ client_destroy (struct client *self) static void client_write (struct client *self, const void *data, size_t len) { - if (!soft_assert (!self->flushing)) + if (!soft_assert (!self->flushing) || len == 0) return; struct write_req *req = xcalloc (1, sizeof *req); @@ -2206,8 +2201,7 @@ static bool client_fcgi_push (struct client *client, const void *data, size_t len) { FIND_CONTAINER (self, client, struct client_fcgi, client); - fcgi_muxer_push (&self->muxer, data, len); - return true; + return fcgi_muxer_push (&self->muxer, data, len); } static void diff --git a/liberty b/liberty index 9494e8e..bca7167 160000 --- a/liberty +++ b/liberty @@ -1 +1 @@ -Subproject commit 9494e8e2affcd08b791d3ecf68985a8a3a310e55 +Subproject commit bca7167d037d857448cb18243425d7c61de3bdd5 -- cgit v1.2.3-70-g09d2