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

Fix a route ordering issue introduced by using unordered map.

kishorenc3b3d9cfb0173
diff --git a/include/http_server.h b/include/http_server.h
index 6e39074c..125c9f66 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -34,7 +34,7 @@ private:
std::string version;
- std::unordered_map<uint32_t, route_path> routes;
+ std::vector<std::pair<uint32_t, route_path>> routes;
const std::string listen_address;
@@ -93,7 +93,7 @@ public:
int find_route(const std::vector<std::string> & path_parts, const std::string & http_method,
route_path** found_rpath);
- void get_route(size_t index, route_path** found_rpath);
+ bool get_route(uint64_t hash, route_path** found_rpath);
int run(ReplicationState* replication_state);
diff --git a/src/core_api.cpp b/src/core_api.cpp
index ae10cae4..f7173c22 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -619,8 +619,12 @@ bool async_write_request(void *data) {
} else if(index_arg->req->route_hash != static_cast<int>(ROUTE_CODES::ALREADY_HANDLED)) {
// call the underlying http handler
route_path* found_rpath = nullptr;
- server->get_route(index_arg->req->route_hash, &found_rpath);
- found_rpath->handler(*index_arg->req, *index_arg->res);
+ 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);
+ } else {
+ index_arg->res->send_404();
+ }
}
if(index_arg->req->_req != nullptr) {
diff --git a/src/http_server.cpp b/src/http_server.cpp
index dc2c4f06..84664df3 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -396,28 +396,28 @@ void HttpServer::get(const std::string & path, bool (*handler)(http_req &, http_
std::vector<std::string> path_parts;
StringUtils::split(path, path_parts, "/");
route_path rpath = {"GET", path_parts, handler, async};
- routes.emplace(rpath.route_hash(), rpath);
+ routes.emplace_back(rpath.route_hash(), rpath);
}
void HttpServer::post(const std::string & path, bool (*handler)(http_req &, http_res &), bool async) {
std::vector<std::string> path_parts;
StringUtils::split(path, path_parts, "/");
route_path rpath = {"POST", path_parts, handler, async};
- routes.emplace(rpath.route_hash(), rpath);
+ routes.emplace_back(rpath.route_hash(), rpath);
}
void HttpServer::put(const std::string & path, bool (*handler)(http_req &, http_res &), bool async) {
std::vector<std::string> path_parts;
StringUtils::split(path, path_parts, "/");
route_path rpath = {"PUT", path_parts, handler, async};
- routes.emplace(rpath.route_hash(), rpath);
+ routes.emplace_back(rpath.route_hash(), rpath);
}
void HttpServer::del(const std::string & path, bool (*handler)(http_req &, http_res &), bool async) {
std::vector<std::string> path_parts;
StringUtils::split(path, path_parts, "/");
route_path rpath = {"DELETE", path_parts, handler, async};
- routes.emplace(rpath.route_hash(), rpath);
+ routes.emplace_back(rpath.route_hash(), rpath);
}
void HttpServer::on(const std::string & message, bool (*handler)(void*)) {
@@ -460,8 +460,13 @@ ReplicationState* HttpServer::get_replication_state() const {
return replication_state;
}
-void HttpServer::get_route(size_t index, route_path** found_rpath) {
- if(routes.count(index) > 0) {
- *found_rpath = &routes[index];
+bool HttpServer::get_route(uint64_t hash, route_path** found_rpath) {
+ for (auto& hash_route : routes) {
+ if(hash_route.first == hash) {
+ *found_rpath = &hash_route.second;
+ return true;
+ }
}
+
+ return false;
}