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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Refactor http req handler: prepare for deferred request body access.
kishorenc6e77a9792e14
diff --git a/include/auth_manager.h b/include/auth_manager.hindex 13262931..501ae9dd 100644--- a/include/auth_manager.h+++ b/include/auth_manager.h@@ -131,7 +131,7 @@ public:const std::string& req_api_key,const std::string& action,const std::string& collection,- std::map<std::string, std::string> & params+ std::map<std::string, std::string>& params);Option<std::string> params_from_scoped_key(diff --git a/include/collection_manager.h b/include/collection_manager.hindex c35c937b..dc8c475a 100644--- a/include/collection_manager.h+++ b/include/collection_manager.h@@ -73,7 +73,7 @@ public:void add_to_collections(Collection* collection);bool auth_key_matches(const std::string& auth_key_sent, const std::string& action,- const std::string& collection, std::map<std::string, std::string> & params);+ const std::string& collection, std::map<std::string, std::string>& params);Option<Collection*> create_collection(const std::string name, const std::vector<field> & fields,const std::string & default_sorting_field,diff --git a/include/core_api.h b/include/core_api.hindex 3fef072a..59e2d1dd 100644--- a/include/core_api.h+++ b/include/core_api.h@@ -2,7 +2,8 @@#include "http_server.h"-bool handle_authentication(http_req& req, const route_path& rpath, const std::string& auth_key);+bool handle_authentication(std::map<std::string, std::string>& req_params, const route_path& rpath,+ const std::string& auth_key);// Collectionsdiff --git a/include/http_data.h b/include/http_data.hindex cd306204..8986a50f 100644--- a/include/http_data.h+++ b/include/http_data.h@@ -165,12 +165,12 @@ struct route_path {std::string http_method;std::vector<std::string> path_parts;bool (*handler)(http_req &, http_res &);- bool async;+ bool async_res;std::string action;route_path(const std::string &httpMethod, const std::vector<std::string> &pathParts,bool (*handler)(http_req &, http_res &), bool async) :- http_method(httpMethod), path_parts(pathParts), handler(handler), async(async) {+ http_method(httpMethod), path_parts(pathParts), handler(handler), async_res(async) {action = _get_action();}diff --git a/include/http_server.h b/include/http_server.hindex c9e2f312..d26037e7 100644--- a/include/http_server.h+++ b/include/http_server.h@@ -61,7 +61,8 @@ private:bool cors_enabled;- bool (*auth_handler)(http_req& req, const route_path& rpath, const std::string& auth_key);+ bool (*auth_handler)(std::map<std::string, std::string>& params, const route_path& rpath,+ const std::string& auth_key);static void on_accept(h2o_socket_t *listener, const char *err);@@ -95,8 +96,8 @@ public:uint64_t node_state() const;- void set_auth_handler(bool (*handler)(http_req & req, const route_path & rpath,- const std::string & auth_key));+ void set_auth_handler(bool (*handler)(std::map<std::string, std::string>& params, const route_path & rpath,+ const std::string & auth_key));void get(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async = false);diff --git a/src/auth_manager.cpp b/src/auth_manager.cppindex 1f985773..3389207a 100644--- a/src/auth_manager.cpp+++ b/src/auth_manager.cpp@@ -123,7 +123,7 @@ Option<bool> AuthManager::init(Store *store) {}bool AuthManager::authenticate(const std::string& req_api_key, const std::string& action,- const std::string& collection, std::map<std::string, std::string> & params) {+ const std::string& collection, std::map<std::string, std::string>& params) {if(req_api_key.size() > KEY_LEN) {// scoped API key: validate and if valid, extract paramsdiff --git a/src/collection_manager.cpp b/src/collection_manager.cppindex 33a27878..8f27f93e 100644--- a/src/collection_manager.cpp+++ b/src/collection_manager.cpp@@ -213,7 +213,7 @@ void CollectionManager::dispose() {bool CollectionManager::auth_key_matches(const std::string& auth_key_sent,const std::string& action,const std::string& collection,- std::map<std::string, std::string> & params) {+ std::map<std::string, std::string>& params) {if(auth_key_sent.empty()) {return false;}diff --git a/src/core_api.cpp b/src/core_api.cppindex 22956bcf..92dde5f5 100644--- a/src/core_api.cpp+++ b/src/core_api.cpp@@ -9,13 +9,14 @@#include "system_metrics.h"#include "logger.h"-bool handle_authentication(http_req& req, const route_path& rpath, const std::string& auth_key) {+bool handle_authentication(std::map<std::string, std::string>& req_params, const route_path& rpath,+ const std::string& auth_key) {CollectionManager & collectionManager = CollectionManager::get_instance();std::string collection = "*";- if(req.params.count("collection") != 0) {- collection = req.params.at("collection");+ if(req_params.count("collection") != 0) {+ collection = req_params.at("collection");}if(rpath.handler == get_health) {@@ -23,7 +24,7 @@ bool handle_authentication(http_req& req, const route_path& rpath, const std::streturn true;}- return collectionManager.auth_key_matches(auth_key, rpath.action, collection, req.params);+ return collectionManager.auth_key_matches(auth_key, rpath.action, collection, req_params);}bool get_collections(http_req & req, http_res & res) {@@ -988,7 +989,7 @@ bool async_write_request(void *data) {bool route_found = server->get_route(index_arg->req->route_hash, &found_rpath);if(route_found) {found_rpath->handler(*index_arg->req, *index_arg->res);- async_call = found_rpath->async;+ async_call = found_rpath->async_res;} else {index_arg->res->set_404();}diff --git a/src/http_server.cpp b/src/http_server.cppindex e5f7b521..a9eb97cf 100644--- a/src/http_server.cpp+++ b/src/http_server.cpp@@ -332,7 +332,6 @@ int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {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 = "";@@ -348,55 +347,53 @@ int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {route_path *rpath = nullptr;uint64_t route_hash = self->http_server->find_route(path_parts, http_method, &rpath);- if(route_hash != static_cast<uint64_t>(ROUTE_CODES::NOT_FOUND)) {- // iterate and extract path params- for(size_t i = 0; i < rpath->path_parts.size(); i++) {- const std::string & path_part = rpath->path_parts[i];- if(path_part[0] == ':') {- query_map.emplace(path_part.substr(1), path_parts[i]);- }- }-- http_req* request = new http_req(req, http_method, route_hash, query_map, req_body);- http_res* response = new http_res();-- bool authenticated = self->http_server->auth_handler(*request, *rpath, api_auth_key_sent);- if(!authenticated) {- std::string message = std::string("{\"message\": \"Forbidden - a valid `") + AUTH_HEADER +- "` header must be sent.\"}";-- delete request;- delete response;+ if(route_hash == static_cast<uint64_t>(ROUTE_CODES::NOT_FOUND)) {+ std::string message = "{ \"message\": \"Not Found\"}";+ return send_response(req, 404, message);+ }- return send_response(req, 401, message);+ // iterate and extract path params+ for(size_t i = 0; i < rpath->path_parts.size(); i++) {+ const std::string & path_part = rpath->path_parts[i];+ if(path_part[0] == ':') {+ query_map.emplace(path_part.substr(1), path_parts[i]);}+ }- // routes match and is an authenticated request- // do any additional pre-request middleware operations here- if(rpath->action == "keys:create") {- // we enrich incoming request with a random API key here so that leader and replicas will use the same key- request->metadata = StringUtils::randstring(AuthManager::KEY_LEN);- }+ bool authenticated = self->http_server->auth_handler(query_map, *rpath, api_auth_key_sent);+ if(!authenticated) {+ std::string message = std::string("{\"message\": \"Forbidden - a valid `") + AUTH_HEADER ++ "` header must be sent.\"}";+ return send_response(req, 401, message);+ }- // for writes, we defer to replication_state- if(http_method == "POST" || http_method == "PUT" || http_method == "DELETE") {- self->http_server->get_replication_state()->write(request, response);- return 0;- }+ const std::string & req_body = std::string(req->entity.base, req->entity.len);- (rpath->handler)(*request, *response);+ http_req* request = new http_req(req, http_method, route_hash, query_map, req_body);+ http_res* response = new http_res();- if(!rpath->async) {- // If a handler is marked async, it's assumed that it's responsible for sending the response itself- // later in an async fashion by calling into the main http thread via a message- self->http_server->send_response(request, response);- }+ // routes match and is an authenticated request+ // do any additional pre-request middleware operations here+ if(rpath->action == "keys:create") {+ // we enrich incoming request with a random API key here so that leader and replicas will use the same key+ request->metadata = StringUtils::randstring(AuthManager::KEY_LEN);+ }+ // for writes, we defer to replication_state+ if(http_method == "POST" || http_method == "PUT" || http_method == "DELETE") {+ self->http_server->get_replication_state()->write(request, response);return 0;}- std::string message = "{ \"message\": \"Not Found\"}";- return send_response(req, 404, message);+ (rpath->handler)(*request, *response);++ if(!rpath->async_res) {+ // If a handler is marked as async res, it's responsible for sending the response itself in an async fashion+ // otherwise, we send the response on behalf of the handler+ self->http_server->send_response(request, response);+ }++ return 0;}void HttpServer::send_message(const std::string & type, void* data) {@@ -430,7 +427,7 @@ int HttpServer::send_response(h2o_req_t *req, int status_code, const std::stringreturn 0;}-void HttpServer::set_auth_handler(bool (*handler)(http_req& req, const route_path& rpath,+void HttpServer::set_auth_handler(bool (*handler)(std::map<std::string, std::string>& params, const route_path& rpath,const std::string& auth_key)) {auth_handler = handler;}