Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Update H2O.

kishorenc5bb3e062daa1
diff --git a/cmake/H2O.cmake b/cmake/H2O.cmake
index 5f371ffb..8b50819f 100644
--- a/cmake/H2O.cmake
+++ b/cmake/H2O.cmake
@@ -1,6 +1,6 @@
# Download and build H2O
-set(H2O_VERSION 2.2.6)
+set(H2O_VERSION 2.3.0-beta2)
set(H2O_NAME h2o-${H2O_VERSION})
set(H2O_TAR_PATH ${DEP_ROOT_DIR}/${H2O_NAME}.tar.gz)
@@ -18,7 +18,7 @@ if(NOT EXISTS ${DEP_ROOT_DIR}/${H2O_NAME}/build/h2o)
message("Configuring ${H2O_NAME}...")
file(MAKE_DIRECTORY ${DEP_ROOT_DIR}/${H2O_NAME}/build)
execute_process(COMMAND ${CMAKE_COMMAND}
- "-DWITH_BUNDLED_SSL=off"
+ "-DCMAKE_FIND_LIBRARY_SUFFIXES=.a"
"-DWITH_MRUBY=off"
"-H${DEP_ROOT_DIR}/${H2O_NAME}"
"-B${DEP_ROOT_DIR}/${H2O_NAME}/build"
diff --git a/include/http_data.h b/include/http_data.h
index b69fa815..3a917956 100644
--- a/include/http_data.h
+++ b/include/http_data.h
@@ -275,7 +275,7 @@ struct http_message_dispatcher {
h2o_multithread_unregister_receiver(message_queue, message_receiver);
h2o_multithread_destroy_queue(message_queue);
- free(message_queue);
+
delete message_receiver;
}
diff --git a/include/http_server.h b/include/http_server.h
index 650999e3..c9e2f312 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -18,13 +18,13 @@ extern "C" {
class ReplicationState;
class HttpServer;
-struct h2o_custom_timeout_entry_t {
- h2o_timeout_entry_t timeout_entry{};
+struct h2o_custom_timer_t {
+ h2o_timer_t timer{};
HttpServer *server;
- h2o_custom_timeout_entry_t(): server(nullptr) {}
+ h2o_custom_timer_t(): server(nullptr) {}
- explicit h2o_custom_timeout_entry_t(HttpServer *server): server(server) {
+ explicit h2o_custom_timer_t(HttpServer *server): server(server) {
}
};
@@ -38,8 +38,8 @@ private:
h2o_hostconf_t *hostconf;
h2o_socket_t* listener_socket;
- h2o_timeout_t ssl_refresh_timeout;
- h2o_custom_timeout_entry_t custom_timeout_entry;
+ static const uint64_t SSL_REFRESH_INTERVAL_MS = 8 * 60 * 60 * 1000;
+ h2o_custom_timer_t ssl_refresh_timer;
http_message_dispatcher* message_dispatcher;
@@ -67,7 +67,7 @@ private:
int setup_ssl(const char *cert_file, const char *key_file);
- static void on_ssl_refresh_timeout(h2o_timeout_entry_t *entry);
+ static void on_ssl_refresh_timeout(h2o_timer_t *entry);
int create_listener();
@@ -121,7 +121,7 @@ public:
void stop();
- void clear_timeouts(const std::vector<h2o_timeout_t*> & timeouts, bool trigger_callback = true);
+ void clear_timeouts(const std::vector<h2o_timer_t*> & timers, bool trigger_callback = true);
static bool on_stop_server(void *data);
diff --git a/src/http_server.cpp b/src/http_server.cpp
index b8937c26..37b6aae0 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -32,7 +32,7 @@ HttpServer::HttpServer(const std::string & version, const std::string & listen_a
message_dispatcher = new http_message_dispatcher;
message_dispatcher->init(ctx.loop);
- ssl_refresh_timeout.timeout = 0; // used during destructor
+ ssl_refresh_timer.timer.expire_at = 0; // used during destructor
}
void HttpServer::on_accept(h2o_socket_t *listener, const char *err) {
@@ -50,12 +50,12 @@ void HttpServer::on_accept(h2o_socket_t *listener, const char *err) {
h2o_accept(http_server->accept_ctx, sock);
}
-void HttpServer::on_ssl_refresh_timeout(h2o_timeout_entry_t *entry) {
- h2o_custom_timeout_entry_t* custom_timeout_entry = reinterpret_cast<h2o_custom_timeout_entry_t*>(entry);
+void HttpServer::on_ssl_refresh_timeout(h2o_timer_t *entry) {
+ h2o_custom_timer_t* custom_timer = reinterpret_cast<h2o_custom_timer_t*>(entry);
LOG(INFO) << "Refreshing SSL certs from disk.";
- HttpServer *hs = custom_timeout_entry->server;
+ HttpServer *hs = custom_timer->server;
SSL_CTX *ssl_ctx = hs->accept_ctx->ssl_ctx;
if (SSL_CTX_use_certificate_chain_file(ssl_ctx, hs->ssl_cert_path.c_str()) != 1) {
@@ -65,19 +65,19 @@ void HttpServer::on_ssl_refresh_timeout(h2o_timeout_entry_t *entry) {
}
// link the timer for the next cycle
- h2o_timeout_link(
+ h2o_timer_link(
hs->ctx.loop,
- &hs->ssl_refresh_timeout,
- &custom_timeout_entry->timeout_entry
+ SSL_REFRESH_INTERVAL_MS,
+ &hs->ssl_refresh_timer.timer
);
}
int HttpServer::setup_ssl(const char *cert_file, const char *key_file) {
// Set up a timer to refresh SSL config from disk. Also, initializing upfront so that destructor works
- h2o_timeout_init(ctx.loop, &ssl_refresh_timeout, 8*60*60*1000); // every 8 hours
- custom_timeout_entry = h2o_custom_timeout_entry_t(this);
- custom_timeout_entry.timeout_entry.cb = on_ssl_refresh_timeout;
- h2o_timeout_link(ctx.loop, &ssl_refresh_timeout, &custom_timeout_entry.timeout_entry);
+ ssl_refresh_timer = h2o_custom_timer_t(this);
+ ssl_refresh_timer.timer.expire_at = 10000000;
+ h2o_timer_init(&ssl_refresh_timer.timer, on_ssl_refresh_timeout); // every 8 hours
+ h2o_timer_link(ctx.loop, SSL_REFRESH_INTERVAL_MS, &ssl_refresh_timer.timer);
accept_ctx->ssl_ctx = SSL_CTX_new(SSLv23_server_method());
@@ -131,7 +131,7 @@ int HttpServer::create_listener() {
}
}
- ctx.globalconf->server_name = h2o_strdup(NULL, "", SIZE_MAX);
+ ctx.globalconf->server_name = h2o_strdup(nullptr, "", SIZE_MAX);
accept_ctx->ctx = &ctx;
accept_ctx->hosts = config.hosts;
@@ -183,19 +183,23 @@ std::string HttpServer::get_version() {
return version;
}
-void HttpServer::clear_timeouts(const std::vector<h2o_timeout_t*> & timeouts, bool trigger_callback) {
- for(h2o_timeout_t* timeout: timeouts) {
- while (!h2o_linklist_is_empty(&timeout->_entries)) {
- h2o_timeout_entry_t *entry = H2O_STRUCT_FROM_MEMBER(h2o_timeout_entry_t, _link, timeout->_entries.next);
- h2o_linklist_unlink(&entry->_link);
- entry->registered_at = 0;
+void HttpServer::clear_timeouts(const std::vector<h2o_timer_t*> & timers, bool trigger_callback) {
+ for(h2o_timer_t* timer: timers) {
+ h2o_timer_unlink(timer);
+ /*while (!h2o_linklist_is_empty(&timer->_link)) {
+ h2o_timer_t *entry = H2O_STRUCT_FROM_MEMBER(h2o_timer_t, _link, timer->_link.next);
+ if(entry == nullptr) {
+ continue;
+ }
if(trigger_callback) {
entry->cb(entry);
}
- h2o_timeout__do_post_callback(ctx.loop);
- }
+ //entry->expire_at = 0;
+ h2o_linklist_unlink(&entry->_link);
+ h2o_timer_unlink(timer);
+ }*/
}
}
@@ -289,44 +293,15 @@ int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {
StringUtils::split(path, path_with_query_parts, "?");
const std::string & path_without_query = path_with_query_parts[0];
- // Except for health check, wait for replicating state to be ready before allowing requests
- // Follower or leader must have started AND data must also have been loaded
- if(path_without_query != "/health" && !self->http_server->get_replication_state()->is_ready()) {
- std::string message = "{ \"message\": \"Not Ready\"}";
- return send_response(req, 503, message);
- }
-
- std::vector<std::string> path_parts;
- StringUtils::split(path_without_query, path_parts, "/");
-
- h2o_iovec_t query = req->query_at != SIZE_MAX ?
- h2o_iovec_init(req->path.base + req->query_at, req->path.len - req->query_at) :
- h2o_iovec_init(H2O_STRLIT(""));
-
- std::string query_str(query.base, query.len);
- std::map<std::string, std::string> query_map = parse_query(query_str);
- const std::string & req_body = std::string(req->entity.base, req->entity.len);
-
- // Extract auth key from header. If that does not exist, look for a GET parameter.
- std::string api_auth_key_sent = "";
-
- ssize_t auth_header_cursor = h2o_find_header_by_str(&req->headers, AUTH_HEADER, strlen(AUTH_HEADER), -1);
- if(auth_header_cursor != -1) {
- h2o_iovec_t & slot = req->headers.entries[auth_header_cursor].value;
- api_auth_key_sent = std::string(slot.base, slot.len);
- } else if(query_map.count(AUTH_HEADER) != 0) {
- api_auth_key_sent = query_map[AUTH_HEADER];
- }
-
// Handle CORS
if(self->http_server->cors_enabled) {
h2o_add_header_by_str(&req->pool, &req->res.headers, H2O_STRLIT("access-control-allow-origin"),
0, NULL, H2O_STRLIT("*"));
-
+
if(http_method == "OPTIONS") {
// locate request access control headers
const char* ACL_REQ_HEADERS = "access-control-request-headers";
- ssize_t acl_header_cursor = h2o_find_header_by_str(&req->headers, ACL_REQ_HEADERS,
+ ssize_t acl_header_cursor = h2o_find_header_by_str(&req->headers, ACL_REQ_HEADERS,
strlen(ACL_REQ_HEADERS), -1);
if(acl_header_cursor != -1) {
@@ -354,6 +329,35 @@ int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {
}
}
+ // Except for health check, wait for replicating state to be ready before allowing requests
+ // Follower or leader must have started AND data must also have been loaded
+ if(path_without_query != "/health" && !self->http_server->get_replication_state()->is_ready()) {
+ std::string message = "{ \"message\": \"Not Ready\"}";
+ return send_response(req, 503, message);
+ }
+
+ std::vector<std::string> path_parts;
+ StringUtils::split(path_without_query, path_parts, "/");
+
+ h2o_iovec_t query = req->query_at != SIZE_MAX ?
+ h2o_iovec_init(req->path.base + req->query_at, req->path.len - req->query_at) :
+ h2o_iovec_init(H2O_STRLIT(""));
+
+ std::string query_str(query.base, query.len);
+ std::map<std::string, std::string> query_map = parse_query(query_str);
+ const std::string & req_body = std::string(req->entity.base, req->entity.len);
+
+ // Extract auth key from header. If that does not exist, look for a GET parameter.
+ std::string api_auth_key_sent = "";
+
+ ssize_t auth_header_cursor = h2o_find_header_by_str(&req->headers, AUTH_HEADER, strlen(AUTH_HEADER), -1);
+ if(auth_header_cursor != -1) {
+ h2o_iovec_t & slot = req->headers.entries[auth_header_cursor].value;
+ api_auth_key_sent = std::string(slot.base, slot.len);
+ } else if(query_map.count(AUTH_HEADER) != 0) {
+ api_auth_key_sent = query_map[AUTH_HEADER];
+ }
+
route_path *rpath = nullptr;
uint64_t route_hash = self->http_server->find_route(path_parts, http_method, &rpath);
@@ -373,6 +377,10 @@ int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {
if(!authenticated) {
std::string message = std::string("{\"message\": \"Forbidden - a valid `") + AUTH_HEADER +
"` header must be sent.\"}";
+
+ delete request;
+ delete response;
+
return send_response(req, 401, message);
}
@@ -409,7 +417,7 @@ void HttpServer::send_response(http_req* request, const http_res* response) {
h2o_iovec_t body = h2o_strdup(&req->pool, response->body.c_str(), SIZE_MAX);
req->res.status = response->status_code;
req->res.reason = http_res::get_status_reason(response->status_code);
- h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL, H2O_STRLIT("application/json; charset=utf-8"));
+ h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, nullptr, H2O_STRLIT("application/json; charset=utf-8"));
h2o_start_response(req, &generator);
h2o_send(req, &body, 1, H2O_SEND_STATE_FINAL);
@@ -418,11 +426,11 @@ void HttpServer::send_response(http_req* request, const http_res* response) {
}
int HttpServer::send_response(h2o_req_t *req, int status_code, const std::string & message) {
- h2o_generator_t generator = {NULL, NULL};
+ h2o_generator_t generator = {nullptr, nullptr};
h2o_iovec_t body = h2o_strdup(&req->pool, message.c_str(), SIZE_MAX);
req->res.status = status_code;
req->res.reason = http_res::get_status_reason(req->res.status);
- h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL, H2O_STRLIT("application/json; charset=utf-8"));
+ h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, nullptr, H2O_STRLIT("application/json; charset=utf-8"));
h2o_start_response(req, &generator);
h2o_send(req, &body, 1, H2O_SEND_STATE_FINAL);
return 0;
@@ -469,7 +477,7 @@ HttpServer::~HttpServer() {
delete message_dispatcher;
// remove all timeouts defined in: https://github.com/h2o/h2o/blob/v2.2.2/lib/core/context.c#L142
- std::vector<h2o_timeout_t*> timeouts = {
+ /*std::vector<h2o_timeout_t*> timeouts = {
&ctx.zero_timeout,
&ctx.one_sec_timeout,
&ctx.hundred_ms_timeout,
@@ -481,20 +489,18 @@ HttpServer::~HttpServer() {
};
clear_timeouts(timeouts);
+ */
- if(ssl_refresh_timeout.timeout != 0) {
+ if(ssl_refresh_timer.timer.expire_at != 0) {
// avoid callback since it recreates timeout
- clear_timeouts({&ssl_refresh_timeout}, false);
- h2o_timeout_dispose(ctx.loop, &ssl_refresh_timeout);
+ clear_timeouts({&ssl_refresh_timer.timer}, false);
}
h2o_context_dispose(&ctx);
- if(h2o_lookup_token(ctx.globalconf->server_name.base, ctx.globalconf->server_name.len) != nullptr) {
- free(ctx.globalconf->server_name.base);
- }
+ free(ctx.globalconf->server_name.base);
+ ctx.globalconf->server_name.base = nullptr;
- free(ctx.queue);
h2o_evloop_destroy(ctx.loop);
h2o_config_dispose(&config);