Bence Balint 3 vuotta sitten
vanhempi
commit
621a41ca95
3 muutettua tiedostoa jossa 30 lisäystä ja 26 poistoa
  1. 13 13
      include/kbf/web_service.h
  2. 10 8
      src/http/common.cpp
  3. 7 5
      src/web_service.cpp

+ 13 - 13
include/kbf/web_service.h

@@ -63,8 +63,8 @@ namespace kbf {
             }
 
             auto *arg = new HandlerArg{*this, *controller};
-            server.route({kbf::http::Method::GET, controller->path, handleGet, arg, handler});
-            server.route({kbf::http::Method::POST, controller->path, handlePost, arg, handler});
+            server.route({http::Method::GET, controller->path, handleGet, arg, handler});
+            server.route({http::Method::POST, controller->path, handlePost, arg, handler});
         }
 
         class Middleware;
@@ -117,8 +117,8 @@ namespace kbf {
              * @param request
              * @return HTTP response
              */
-            virtual kbf::http::Response get(const kbf::http::Request &request) {
-                return kbf::http::Response("method not allowed", 405);
+            virtual http::Response get(const http::Request &request) {
+                return http::Response("method not allowed", 405);
             }
 
             /**
@@ -127,8 +127,8 @@ namespace kbf {
              * @param request
              * @return HTTP response
              */
-            virtual kbf::http::Response post(const kbf::http::Request &request) {
-                return kbf::http::Response("method not allowed", 405);
+            virtual http::Response post(const http::Request &request) {
+                return http::Response("method not allowed", 405);
             }
 
             /**
@@ -136,10 +136,10 @@ namespace kbf {
              *
              * @param response
              */
-            virtual void onResponseSent(const kbf::http::Response &response) {}
+            virtual void onResponseSent(const http::Response &response) {}
 
         private:
-            static void responseSentHandler(const kbf::http::Response &response, void *data);
+            static void responseSentHandler(const http::Response &response, void *data);
         };
 
         /**
@@ -165,7 +165,7 @@ namespace kbf {
         };
 
     private:
-        kbf::http::Server server;
+        http::Server server;
 
         bool running = false;
         int  port;
@@ -175,16 +175,16 @@ namespace kbf {
             Controller &controller;
         };
 
-        typedef kbf::http::Response (kbf::WebService::Controller::*MethodFunction)(const kbf::http::Request &request);
+        typedef http::Response (WebService::Controller::*MethodFunction)(const http::Request &request);
 
         MethodFunction currentMethod           = nullptr;
         Controller     *currentController      = nullptr;
 
-        static kbf::http::Response handleGet(const kbf::http::Request &request, void *data);
+        static http::Response handleGet(const http::Request &request, void *data);
 
-        static kbf::http::Response handlePost(const kbf::http::Request &request, void *data);
+        static http::Response handlePost(const http::Request &request, void *data);
 
-        kbf::http::Response startPipeline(const kbf::http::Request &request, HandlerArg *arg);
+        http::Response startPipeline(const http::Request &request, HandlerArg *arg);
 
         std::vector<Middleware *> middlewares;
         std::optional<std::vector<Middleware *>::iterator>

+ 10 - 8
src/http/common.cpp

@@ -9,6 +9,8 @@
 #include "kbf/macros.h"
 
 using namespace kbf;
+using http::Request;
+using http::Response;
 using std::string;
 using std::map;
 using std::vector;
@@ -27,7 +29,7 @@ void http::HTTPObject::parseToMap(map<string, string> &target, const string &buf
     }
 }
 
-http::Request::Request(httpd_req_t *httpdRequest) {
+Request::Request(httpd_req_t *httpdRequest) {
     this->httpdRequest = httpdRequest;
     method = static_cast<Method>(httpdRequest->method);
     uri    = httpdRequest->uri;
@@ -39,7 +41,7 @@ http::Request::Request(httpd_req_t *httpdRequest) {
     readBody();
 }
 
-string http::Request::readHeader(const string &header) const {
+string Request::readHeader(const string &header) const {
     auto len = httpd_req_get_hdr_value_len(httpdRequest, header.c_str());
     if (len == 0) {
         ESP_LOGD(TAG, "  header not found: %s", header.c_str());
@@ -51,11 +53,11 @@ string http::Request::readHeader(const string &header) const {
     return buffer;
 }
 
-void http::Request::readAndStoreHeader(const string &header) {
+void Request::readAndStoreHeader(const string &header) {
     headers[header] = readHeader(header);
 }
 
-void http::Request::readQuery() {
+void Request::readQuery() {
     auto queryLen = httpd_req_get_url_query_len(httpdRequest);
     char buffer[queryLen + 1];
 
@@ -70,7 +72,7 @@ void http::Request::readQuery() {
     parseToMap(query, buffer);
 }
 
-void http::Request::readBody() {
+void Request::readBody() {
     if (httpdRequest->content_len == 0) {
         return;
     }
@@ -101,19 +103,19 @@ void http::Request::readBody() {
     }
 }
 
-http::Response::Response(string body, int status, string contentType) :
+Response::Response(string body, int status, string contentType) :
         HTTPObject(std::move(body)),
         status(status),
         contentType(std::move(contentType)) {
 }
 
-http::Response::Response(const char *body, int status, string contentType) :
+Response::Response(const char *body, int status, string contentType) :
         HTTPObject(body),
         status(status),
         contentType(std::move(contentType)) {
 }
 
-http::Response::Response(const nlohmann::json &json, int status, string contentType) :
+Response::Response(const nlohmann::json &json, int status, string contentType) :
         HTTPObject(json.dump()),
         status(status),
         contentType(std::move(contentType)) {

+ 7 - 5
src/web_service.cpp

@@ -5,6 +5,8 @@
 #include <utility>
 
 using namespace kbf;
+using http::Request;
+using http::Response;
 
 WebService::WebService(int port) : server(), port(port) {
     ESP_LOGI(TAG, "%s(%d)", __func__, this->port);
@@ -40,7 +42,7 @@ void WebService::stop() {
     running = false;
 }
 
-http::Response WebService::startPipeline(const http::Request &request, HandlerArg *arg) {
+Response WebService::startPipeline(const Request &request, HandlerArg *arg) {
     ESP_LOGD(TAG, "%s()", __func__);
 
     currentController = &arg->controller;
@@ -53,7 +55,7 @@ http::Response WebService::startPipeline(const http::Request &request, HandlerAr
     return next(request);
 }
 
-kbf::http::Response WebService::handleGet(const http::Request &request, void *data) {
+Response WebService::handleGet(const Request &request, void *data) {
     ESP_LOGD(TAG, "%s()", __func__);
 
     auto arg = static_cast<HandlerArg *>(data);
@@ -62,7 +64,7 @@ kbf::http::Response WebService::handleGet(const http::Request &request, void *da
     return arg->webService.startPipeline(request, arg);
 }
 
-kbf::http::Response WebService::handlePost(const http::Request &request, void *data) {
+Response WebService::handlePost(const Request &request, void *data) {
     ESP_LOGD(TAG, "%s()", __func__);
 
     auto arg = static_cast<HandlerArg *>(data);
@@ -73,14 +75,14 @@ kbf::http::Response WebService::handlePost(const http::Request &request, void *d
 
 WebService::Controller::Controller(std::string path) : path(std::move(path)) {}
 
-void WebService::Controller::responseSentHandler(const http::Response &response, void *data) {
+void WebService::Controller::responseSentHandler(const Response &response, void *data) {
     ESP_LOGD(TAG, "%s()", __func__);
 
     auto arg = static_cast<HandlerArg *>(data);
     arg->controller.onResponseSent(response);
 }
 
-http::Response WebService::next(const http::Request &request) { // noqa
+Response WebService::next(const Request &request) { // noqa
     ESP_LOGD(TAG, "%s()", __func__);
 
     if (middlewareIt == std::nullopt) {