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
Send 401 rather than 403 when API key is incorrect.
Kishore Nallan005b637f74b7
diff --git a/include/http_server.h b/include/http_server.hindex e5ae5855..0b0386a7 100644--- a/include/http_server.h+++ b/include/http_server.h@@ -33,6 +33,11 @@ struct http_res {body = "{\"message\": \"" + message + "\"}";}+ void send_401(const std::string & message) {+ status_code = 400;+ body = "{\"message\": \"" + message + "\"}";+ }+void send_403() {status_code = 403;body = "{\"message\": \"Forbidden\"}";@@ -48,6 +53,11 @@ struct http_res {body = "{\"message\": \"" + message + "\"}";}+ void send_422(const std::string & message) {+ status_code = 422;+ body = "{\"message\": \"" + message + "\"}";+ }+void send_500(const std::string & res_body) {status_code = 500;body = res_body;@@ -101,7 +111,7 @@ private:static int catch_all_handler(h2o_handler_t *self, h2o_req_t *req);- static int send_403_forbidden(h2o_req_t *req);+ static int send_401_unauthorized(h2o_req_t *req);public:HttpServer(std::string listen_address, uint32_t listen_port);@@ -118,5 +128,5 @@ public:int run();- static constexpr const char* AUTH_HEADER = "x-api-key";+ static constexpr const char* AUTH_HEADER = "x-typesense-api-key";};\ No newline at end of filediff --git a/src/http_server.cpp b/src/http_server.cppindex 98f426b9..0487b118 100644--- a/src/http_server.cpp+++ b/src/http_server.cpp@@ -85,6 +85,7 @@ const char* HttpServer::get_status_reason(uint32_t status_code) {case 200: return "OK";case 201: return "Created";case 400: return "Bad Request";+ case 401: return "Unauthorized";case 403: return "Forbidden";case 404: return "Not Found";case 409: return "Conflict";@@ -170,14 +171,14 @@ int HttpServer::catch_all_handler(h2o_handler_t *self, h2o_req_t *req) {if(auth_header_cursor == -1) {// requires authentication, but API Key is not present in the headers- return send_403_forbidden(req);+ return send_401_unauthorized(req);} else {// api key is found, let's validateh2o_iovec_t & slot = req->headers.entries[auth_header_cursor].value;std::string auth_key_from_header = std::string(slot.base, slot.len);if(!collectionManager.auth_key_matches(auth_key_from_header)) {- return send_403_forbidden(req);+ return send_401_unauthorized(req);}}}@@ -207,11 +208,11 @@ int HttpServer::catch_all_handler(h2o_handler_t *self, h2o_req_t *req) {return 0;}-int HttpServer::send_403_forbidden(h2o_req_t *req) {+int HttpServer::send_401_unauthorized(h2o_req_t *req) {h2o_generator_t generator = {NULL, NULL};std::string res_body = std::string("{\"message\": \"Forbidden - ") + AUTH_HEADER + " header is invalid or not present.\"}";h2o_iovec_t body = h2o_strdup(&req->pool, res_body.c_str(), SIZE_MAX);- req->res.status = 403;+ req->res.status = 401;req->res.reason = get_status_reason(req->res.status);h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, H2O_STRLIT("application/json; charset=utf-8"));h2o_start_response(req, &generator);