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

Store auth inside request.

Kishore Nallan2925b2e8d182
diff --git a/include/http_data.h b/include/http_data.h
index 1084ebe3..3b6c08cc 100644
--- a/include/http_data.h
+++ b/include/http_data.h
@@ -210,6 +210,7 @@ struct http_req {
uint64_t route_hash;
std::map<std::string, std::string> params;
std::vector<nlohmann::json> embedded_params_vec;
+ std::string api_auth_key;
bool first_chunk_aggregate;
std::atomic<bool> last_chunk_aggregate;
@@ -247,10 +248,10 @@ struct http_req {
}
http_req(h2o_req_t* _req, const std::string & http_method, const std::string & path_without_query, uint64_t route_hash,
- const std::map<std::string, std::string>& params,
- std::vector<nlohmann::json>& embedded_params_vec, const std::string& body, const std::string& client_ip):
+ const std::map<std::string, std::string>& params, std::vector<nlohmann::json>& embedded_params_vec,
+ const std::string& api_auth_key, const std::string& body, const std::string& client_ip):
_req(_req), http_method(http_method), path_without_query(path_without_query), route_hash(route_hash),
- params(params), embedded_params_vec(embedded_params_vec),
+ params(params), embedded_params_vec(embedded_params_vec), api_auth_key(api_auth_key),
first_chunk_aggregate(true), last_chunk_aggregate(false),
chunk_len(0), body(body), body_index(0), data(nullptr), ready(false),
log_index(0), is_diposed(false), client_ip(client_ip) {
diff --git a/include/http_server.h b/include/http_server.h
index 0b976cc2..ba4421ac 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -22,7 +22,6 @@ class HttpServer;
struct h2o_custom_req_handler_t {
h2o_handler_t super;
HttpServer* http_server;
- std::string api_auth_key_sent;
};
struct h2o_custom_generator_t {
diff --git a/src/http_server.cpp b/src/http_server.cpp
index 0689c939..c6c53d36 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -278,7 +278,6 @@ void HttpServer::on_res_generator_dispose(void *self) {
}
// without this, warning about memory allocated by std::string leaking happens
- std::string().swap(custom_generator->h2o_handler->api_auth_key_sent);
delete custom_generator;
}
@@ -398,14 +397,13 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {
// Extract auth key from header. If that does not exist, look for a GET parameter.
ssize_t auth_header_cursor = h2o_find_header_by_str(&req->headers, http_req::AUTH_HEADER, strlen(http_req::AUTH_HEADER), -1);
+ std::string api_auth_key_sent;
if(auth_header_cursor != -1) {
h2o_iovec_t & slot = req->headers.entries[auth_header_cursor].value;
- const std::string api_auth_key_sent = std::string(slot.base, slot.len);
- // NOTE: directly using `h2o_handler->api_auth_key_sent` without an intermediate string causes memory errors
- h2o_handler->api_auth_key_sent = api_auth_key_sent;
+ api_auth_key_sent = std::string(slot.base, slot.len);
} else if(query_map.count(http_req::AUTH_HEADER) != 0) {
- h2o_handler->api_auth_key_sent = query_map[http_req::AUTH_HEADER];
+ api_auth_key_sent = query_map[http_req::AUTH_HEADER];
}
route_path *rpath = nullptr;
@@ -460,7 +458,7 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {
// multi_search needs to be handled later because the API key could be part of request body and
// the whole request body might not be available right now.
bool authenticated = h2o_handler->http_server->auth_handler(query_map, embedded_params_vec, body, *rpath,
- h2o_handler->api_auth_key_sent);
+ api_auth_key_sent);
if(!authenticated) {
std::string message = std::string("{\"message\": \"Forbidden - a valid `") + http_req::AUTH_HEADER +
"` header must be sent.\"}";
@@ -469,8 +467,8 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {
}
std::shared_ptr<http_req> request = std::make_shared<http_req>(req, rpath->http_method, path_without_query,
- route_hash, query_map, embedded_params_vec, body,
- client_ip);
+ route_hash, query_map, embedded_params_vec,
+ api_auth_key_sent, body, client_ip);
// add custom generator with a dispose function for cleaning up resources
h2o_custom_generator_t* custom_gen = new h2o_custom_generator_t;
@@ -656,7 +654,7 @@ int HttpServer::process_request(const std::shared_ptr<http_req>& request, const
if(root_resource == "multi_search") {
// We can authenticate only when the full request body is available
bool authenticated = handler->http_server->auth_handler(request->params, request->embedded_params_vec,
- request->body, *rpath, handler->api_auth_key_sent);
+ request->body, *rpath, request->api_auth_key);
if(!authenticated) {
std::string message = std::string("{\"message\": \"Forbidden - a valid `") + http_req::AUTH_HEADER +
"` header must be sent.\"}";