From: LeonardoBizzoni Date: Thu, 15 Aug 2024 18:21:20 +0000 (+0200) Subject: Fixed query inside path + added method in route identifier X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=75810f59cb1f4627d070cb69e7369e8c5e38857f;p=http-lib Fixed query inside path + added method in route identifier --- diff --git a/src/listener.cpp b/src/listener.cpp index 8c801b7..022d033 100644 --- a/src/listener.cpp +++ b/src/listener.cpp @@ -5,6 +5,7 @@ #include #include +#include #include "const_definitions.h" #include "error.h" @@ -53,7 +54,9 @@ namespace http { if (maybe_req.has_value()) { try { - resp = this->routes.at(maybe_req.value().query)(maybe_req.value()); + std::stringstream ss; + ss << maybe_req.value().method << " " << maybe_req.value().path; + resp = this->routes.at(ss.str())(maybe_req.value()); } catch (...) { } } diff --git a/src/request.cpp b/src/request.cpp index 4758d51..9493653 100644 --- a/src/request.cpp +++ b/src/request.cpp @@ -12,8 +12,9 @@ std::ostream &operator<<(std::ostream &os, const http::Request &req) { os << "Request { domain: " << std::quoted(req.domain_name) << ", port: " << req.port - << ", query: " << std::quoted(req.query) << ", method: " << req.method - << ", version: " << (int)req.version.major << "." << (int)req.version.minor << ", headers: [ "; + << ", path: " << std::quoted(req.path) << ", query: " << std::quoted(req.query) + << ", method: " << req.method << ", version: " << (int)req.version.major << "." + << (int)req.version.minor << ", headers: [ "; for (const auto &header : req.optheaders) { os << header.first << " = " << header.second << ","; @@ -49,11 +50,15 @@ namespace http { } std::string_view query(*word_iter++); - std::cout << "\t" << query << std::endl; if (query.empty() || !query.starts_with('/')) { return Err(Error::InvalidQueryRequest); } else { - req.query = query; + auto query_view = query | std::views::split('?'); + auto query_view_iter = query_view.begin(); + + req.path = std::string((*query_view_iter).begin(), (*query_view_iter).end()); + query_view_iter++; + req.query = std::string((*query_view_iter).begin(), (*query_view_iter).end()); } std::string_view version(*word_iter); diff --git a/src/request.h b/src/request.h index 913abc8..3a4bd4d 100644 --- a/src/request.h +++ b/src/request.h @@ -18,8 +18,9 @@ namespace http { public: uint16_t port = 80; std::string_view domain_name; - std::string query = "/"; - std::string body = ""; + std::string path = "/"; + std::string query; + std::string body; Method method; http_version version = {.major = 1, .minor = 1};