]> git.leonardobizzoni.com Git - http-lib/commitdiff
Fixed query inside path + added method in route identifier
authorLeonardoBizzoni <leo2002714@gmail.com>
Thu, 15 Aug 2024 18:21:20 +0000 (20:21 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Thu, 15 Aug 2024 18:21:20 +0000 (20:21 +0200)
src/listener.cpp
src/request.cpp
src/request.h

index 8c801b7344a246bea28e78fc6542cf953e05a1a2..022d0336336b3f4eefc5c61092653d46fa5b6569 100644 (file)
@@ -5,6 +5,7 @@
 #include <unistd.h>
 
 #include <cstdio>
+#include <sstream>
 
 #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 (...) {
          }
        }
index 4758d519a3d0dc69dcc466c14e2e3baf54d3a3ee..94936532a6cbe570bf0272f4dd79abde58e0b8ca 100644 (file)
@@ -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);
index 913abc8da3a46cde4e8ec57dc1b22ce4510af705..3a4bd4d2c408458cfda010b794813590f09ac711 100644 (file)
@@ -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};