Skip to content

Commit

Permalink
-hide request constructor using fcgi::Request object, added a public …
Browse files Browse the repository at this point in the history
…constructor taking fcgi parameters map and input using standard types
  • Loading branch information
kamchatka-volcano committed Jul 9, 2024
1 parent f37bfa0 commit c29c251
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
15 changes: 12 additions & 3 deletions include/asyncgi/request.h
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -12,9 +12,13 @@
#include <memory>
#include <optional>
#include <string>
#include <variant>
#include <vector>

namespace asyncgi {
namespace detail {
struct ConnectionAccessToken;
}

namespace fcgi {
class Request;
Expand Down Expand Up @@ -54,15 +58,20 @@ class Request {
const std::vector<std::pair<std::string, std::string>>& 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<std::pair<std::string, std::string>> fcgiParams, std::string fcgiStdIn);

private:
const fcgi::Request& fcgiRequest() const;
const http::RequestView& httpRequest() const;

private:
sfun::member<const fcgi::Request&> fcgiRequest_;
std::variant<const fcgi::Request*, std::unique_ptr<const fcgi::Request>> fcgiRequest_;
detail::LazyInitialized<http::RequestView> httpRequest_;

public:
//Constructor for library internal usage
explicit Request(const fcgi::Request& fcgiRequest, sfun::access_permission<detail::ConnectionAccessToken>);
};

} // namespace asyncgi
Expand Down
9 changes: 8 additions & 1 deletion src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,19 @@ void Connection<TProtocol>::disconnect()
close();
}

struct ConnectionAccessToken {
static sfun::access_token<ConnectionAccessToken> create()
{
return {};
}
};

template<typename TProtocol>
void Connection<TProtocol>::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<ResponseContext>(*responseSender_, timerProvider_, client_, asioDispatcher_);
try {
requestProcessor_(request, responseContext_);
Expand Down
55 changes: 33 additions & 22 deletions src/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<detail::ConnectionAccessToken>)
: fcgiRequest_{&request}
, httpRequest_{makeHttpRequestViewFactory(fcgiRequest())}
{
}

Request::Request(std::vector<std::pair<std::string, std::string>> fcgiParams, std::string fcgiStdIn)
: fcgiRequest_{std::make_unique<fcgi::Request>(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
Expand Down

0 comments on commit c29c251

Please sign in to comment.