diff --git a/include/asyncgi/request.h b/include/asyncgi/request.h index c05842f..37e8de9 100755 --- a/include/asyncgi/request.h +++ b/include/asyncgi/request.h @@ -1,7 +1,7 @@ #ifndef ASYNCGI_REQUEST_H #define ASYNCGI_REQUEST_H -#include "detail/external/sfun/member.h" +#include "detail/external/sfun/interface.h" #include "detail/lazyinitialized.h" #include "http/cookie_view.h" #include "http/query_view.h" @@ -12,9 +12,13 @@ #include #include #include +#include #include namespace asyncgi { +namespace detail { +struct ConnectionAccessToken; +} namespace fcgi { class Request; @@ -54,15 +58,20 @@ class Request { const std::vector>& fcgiParams() const; const std::string& fcgiStdIn() const; - explicit Request(const fcgi::Request& fcgiRequest); + //Can be useful for testing, otherwise you don't need to instantiate Request objects by yourself + explicit Request(std::vector> fcgiParams, std::string fcgiStdIn); private: const fcgi::Request& fcgiRequest() const; const http::RequestView& httpRequest() const; private: - sfun::member fcgiRequest_; + std::variant> fcgiRequest_; detail::LazyInitialized httpRequest_; + +public: + //Constructor for library internal usage + explicit Request(const fcgi::Request& fcgiRequest, sfun::access_permission); }; } // namespace asyncgi diff --git a/src/connection.cpp b/src/connection.cpp index 38ab8e0..ff77ed2 100755 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -118,12 +118,19 @@ void Connection::disconnect() close(); } +struct ConnectionAccessToken { + static sfun::access_token create() + { + return {}; + } +}; + template void Connection::processRequest(fcgi::Request&& fcgiRequest, fcgi::Response&& fcgiResponse) { fcgiRequest_ = std::move(fcgiRequest); responseSender_.emplace(std::move(fcgiResponse)); - const auto request = Request{*fcgiRequest_}; + const auto request = Request{*fcgiRequest_, ConnectionAccessToken::create()}; responseContext_ = std::make_shared(*responseSender_, timerProvider_, client_, asioDispatcher_); try { requestProcessor_(request, responseContext_); diff --git a/src/request.cpp b/src/request.cpp index d9383db..f713fd5 100644 --- a/src/request.cpp +++ b/src/request.cpp @@ -4,33 +4,44 @@ namespace asyncgi { -Request::Request(const fcgi::Request& request) - : fcgiRequest_{request} - , httpRequest_{[this] - { - return http::RequestView{ - fcgiRequest().hasParam("REQUEST_METHOD") ? fcgiRequest().param("REQUEST_METHOD") - : std::string_view{}, - fcgiRequest().hasParam("REMOTE_ADDR") ? fcgiRequest().param("REMOTE_ADDR") - : std::string_view{}, - fcgiRequest().hasParam("HTTP_HOST") ? fcgiRequest().param("HTTP_HOST") - : std::string_view{}, - fcgiRequest().hasParam("REQUEST_URI") ? fcgiRequest().param("REQUEST_URI") - : std::string_view{}, - fcgiRequest().hasParam("QUERY_STRING") ? fcgiRequest().param("QUERY_STRING") - : std::string_view{}, - fcgiRequest().hasParam("HTTP_COOKIE") ? fcgiRequest().param("HTTP_COOKIE") - : std::string_view{}, - fcgiRequest().hasParam("CONTENT_TYPE") ? fcgiRequest().param("CONTENT_TYPE") - : std::string_view{}, - fcgiRequest().stdIn()}; - }} +namespace { +auto makeHttpRequestViewFactory(const fcgi::Request& fcgiRequest) +{ + return [&fcgiRequest] + { + return http::RequestView{ + fcgiRequest.hasParam("REQUEST_METHOD") ? fcgiRequest.param("REQUEST_METHOD") : std::string_view{}, + fcgiRequest.hasParam("REMOTE_ADDR") ? fcgiRequest.param("REMOTE_ADDR") : std::string_view{}, + fcgiRequest.hasParam("HTTP_HOST") ? fcgiRequest.param("HTTP_HOST") : std::string_view{}, + fcgiRequest.hasParam("REQUEST_URI") ? fcgiRequest.param("REQUEST_URI") : std::string_view{}, + fcgiRequest.hasParam("QUERY_STRING") ? fcgiRequest.param("QUERY_STRING") : std::string_view{}, + fcgiRequest.hasParam("HTTP_COOKIE") ? fcgiRequest.param("HTTP_COOKIE") : std::string_view{}, + fcgiRequest.hasParam("CONTENT_TYPE") ? fcgiRequest.param("CONTENT_TYPE") : std::string_view{}, + fcgiRequest.stdIn()}; + }; +} +} //namespace + +Request::Request(const fcgi::Request& request, sfun::access_permission) + : fcgiRequest_{&request} + , httpRequest_{makeHttpRequestViewFactory(fcgiRequest())} +{ +} + +Request::Request(std::vector> fcgiParams, std::string fcgiStdIn) + : fcgiRequest_{std::make_unique(std::move(fcgiParams), std::move(fcgiStdIn))} + , httpRequest_{makeHttpRequestViewFactory(fcgiRequest())} { } const fcgi::Request& Request::fcgiRequest() const { - return fcgiRequest_; + return std::visit( + [](const auto& fcgiPtr) -> const fcgi::Request& + { + return *fcgiPtr; + }, + fcgiRequest_); } const http::RequestView& Request::httpRequest() const