Skip to content

Commit

Permalink
-wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kamchatka-volcano committed Jul 14, 2024
1 parent f37bfa0 commit 1749147
Show file tree
Hide file tree
Showing 32 changed files with 447 additions and 278 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ set(ASYNCGI_FCGI_RESPONDER_OBJECT_LIB ON)
SealLake_Bundle(
NAME asyncgi_fcgi_responder
GIT_REPOSITORY https://github.com/kamchatka-volcano/fcgi_responder.git
GIT_TAG v1.7.0
GIT_TAG dev
TEXT_REPLACEMENTS
"namespace fcgi" "namespace asyncgi::fcgi"
)
Expand All @@ -43,7 +43,7 @@ set(ASYNCGI_HOT_TEACUP_OBJECT_LIB ON)
SealLake_Bundle(
NAME asyncgi_hot_teacup
GIT_REPOSITORY https://github.com/kamchatka-volcano/hot_teacup.git
GIT_TAG v3.3.0
GIT_TAG dev
WILDCARDS
include/hot_teacup/*.h
DESTINATION include/asyncgi/http
Expand Down Expand Up @@ -77,6 +77,8 @@ set(SRC
src/response.cpp
src/routeresponsecontextaccessor.cpp
src/serviceholder.cpp
src/fastcgirequest.cpp
src/fastcgiresponse.cpp
)

SealLake_StaticLibrary(
Expand Down
2 changes: 1 addition & 1 deletion examples/example_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main()
9088,
#endif
http::Request{http::RequestMethod::Get, "/"},
[&io](const std::optional<http::ResponseView>& response)
[&io](std::optional<http::Response> response)
{
if (response)
std::cout << response->body() << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions examples/example_client_in_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace http = asyncgi::http;

struct RequestPage{
void operator()(const asyncgi::Request&, asyncgi::Responder& responder)
void operator()(const http::Request&, asyncgi::Responder& responder)
{
// making request to FastCgi application listening on /tmp/fcgi.sock and showing the received response
auto client = asyncgi::Client{responder};
Expand All @@ -15,7 +15,7 @@ struct RequestPage{
9088,
#endif
http::Request{http::RequestMethod::Get, "/"},
[responder](const std::optional<http::ResponseView>& reqResponse) mutable
[responder](std::optional<http::Response> reqResponse) mutable
{
if (reqResponse)
responder.send(std::string{reqResponse->body()});
Expand Down
16 changes: 8 additions & 8 deletions examples/example_guestbook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ struct RouteContext {

template<>
struct asyncgi::config::RouteMatcher<AccessRole, RouteContext> {
bool operator()(AccessRole value, const asyncgi::Request&, const RouteContext& context) const
bool operator()(AccessRole value, const http::Request&, const RouteContext& context) const
{
return value == context.role;
}
};

std::optional<http::Response> authorizeAdmin(const asyncgi::Request& request, RouteContext& context)
std::optional<http::Response> authorizeAdmin(const http::Request& request, RouteContext& context)
{
if (request.cookie("admin_id") == "ADMIN_SECRET")
context.role = AccessRole::Admin;

return std::nullopt;
}

http::Response showLoginPage(const asyncgi::Request&)
http::Response showLoginPage(const http::Request&)
{
return {R"(
<head><link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css"></head>
Expand All @@ -44,15 +44,15 @@ http::Response showLoginPage(const asyncgi::Request&)
</form>)"};
}

http::Response loginAdmin(const asyncgi::Request& request)
http::Response loginAdmin(const http::Request& request)
{
if (request.formField("login") == "admin" && request.formField("passwd") == "12345")
return {http::Redirect{"/"}, {http::Cookie("admin_id", "ADMIN_SECRET")}};
else
return http::Redirect{"/login"};
}

http::Response logoutAdmin(const asyncgi::Request&)
http::Response logoutAdmin(const http::Request&)
{
return {http::Redirect{"/"}, {http::Cookie("admin_id", "")}};
}
Expand Down Expand Up @@ -117,7 +117,7 @@ std::string makeLinksDiv(AccessRole role)

auto showGuestBookPage(GuestBookState& state)
{
return [&state](const asyncgi::Request& request, RouteContext& context) -> http::Response
return [&state](const http::Request& request, RouteContext& context) -> http::Response
{
auto page = R"(<head><link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css"></head>
<div style="display:flex; flex-direction: row; justify-content: flex-end">%LINKS%</div>
Expand Down Expand Up @@ -159,7 +159,7 @@ auto showGuestBookPage(GuestBookState& state)

auto addMessage(GuestBookState& state)
{
return [&state](const asyncgi::Request& request) -> http::Response
return [&state](const http::Request& request) -> http::Response
{
if (std::all_of(
request.formField("msg").begin(),
Expand All @@ -182,7 +182,7 @@ auto addMessage(GuestBookState& state)

auto removeMessage(GuestBookState& state)
{
return [&state](int index, const asyncgi::Request&) -> http::Response
return [&state](int index, const http::Request&) -> http::Response
{
state.removeMessage(index);
return http::Redirect{"/"};
Expand Down
2 changes: 1 addition & 1 deletion examples/example_hello_world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ int main()
auto router = asyncgi::Router{io};
router.route("/", http::RequestMethod::Get)
.process(
[](const asyncgi::Request&)
[](const http::Request&)
{
return http::Response{"Hello world"};
});
Expand Down
2 changes: 1 addition & 1 deletion examples/example_request_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace http = asyncgi::http;

http::Response guestBookPage(const asyncgi::Request& request)
http::Response guestBookPage(const http::Request& request)
{
if (request.path() == "/")
return {R"(
Expand Down
2 changes: 1 addition & 1 deletion examples/example_response_dispatching_asio_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace asio = boost::asio;
namespace http = asyncgi::http;

struct DelayedPage {
void operator()(const asyncgi::Request&, asyncgi::Responder& responder)
void operator()(const http::Request&, asyncgi::Responder& responder)
{
auto disp = asyncgi::AsioDispatcher{responder};
disp.postTask(
Expand Down
2 changes: 1 addition & 1 deletion examples/example_response_wait_future.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using namespace asyncgi;

struct DelayedPage{
void operator()(const asyncgi::Request&, asyncgi::Responder& responder)
void operator()(const http::Request&, asyncgi::Responder& responder)
{
auto timer = asyncgi::Timer{responder};
timer.waitFuture(
Expand Down
12 changes: 6 additions & 6 deletions examples/example_route_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct RouteContext {
};

struct AdminAuthorizer {
std::optional<http::Response> operator()(const asyncgi::Request& request, RouteContext& context)
std::optional<http::Response> operator()(const http::Request& request, RouteContext& context)
{
if (request.cookie("admin_id") == "ADMIN_SECRET")
context.role = AccessRole::Admin;
Expand All @@ -25,7 +25,7 @@ struct AdminAuthorizer {
};

struct LoginPage {
http::Response operator()(const asyncgi::Request&, RouteContext& context)
http::Response operator()(const http::Request&, RouteContext& context)
{
if (context.role == AccessRole::Guest)
return {R"(
Expand All @@ -43,7 +43,7 @@ struct LoginPage {
};

struct LoginPageAuthorize {
http::Response operator()(const asyncgi::Request& request, RouteContext& context)
http::Response operator()(const http::Request& request, RouteContext& context)
{
if (context.role == AccessRole::Guest) {
if (request.formField("login") == "admin" && request.formField("passwd") == "12345")
Expand All @@ -62,12 +62,12 @@ int main()
auto router = asyncgi::Router<RouteContext>{io};
router.route(asyncgi::rx{".*"}).process<AdminAuthorizer>();
router.route("/").process(
[](const asyncgi::Request&, asyncgi::Responder& response, RouteContext& context)
[](const http::Request&, RouteContext& context) -> http::Response
{
if (context.role == AccessRole::Admin)
response.send("<p>Hello admin</p>");
return {"<p>Hello admin</p>"};
else
response.send(R"(<p>Hello guest</p><p><a href="/login">login</a>)");
return {R"(<p>Hello guest</p><p><a href="/login">login</a>)"};
});

router.route("/login", http::RequestMethod::Get).process<LoginPage>();
Expand Down
10 changes: 5 additions & 5 deletions examples/example_route_matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct RouteContext {
};

struct AdminAuthorizer {
std::optional<http::Response> operator()(const asyncgi::Request& request, RouteContext& context)
std::optional<http::Response> operator()(const http::Request& request, RouteContext& context)
{
if (request.cookie("admin_id") == "ADMIN_SECRET")
context.role = AccessRole::Admin;
Expand All @@ -25,7 +25,7 @@ struct AdminAuthorizer {
};

struct LoginPage {
http::Response operator()(const asyncgi::Request&)
http::Response operator()(const http::Request&)
{
return {R"(
<html>
Expand All @@ -40,7 +40,7 @@ struct LoginPage {
};

struct LoginPageAuthorize {
http::Response operator()(const asyncgi::Request& request)
http::Response operator()(const http::Request& request)
{
if (request.formField("login") == "admin" && request.formField("passwd") == "12345")
return {http::Redirect{"/"}, {asyncgi::http::Cookie("admin_id", "ADMIN_SECRET")}};
Expand All @@ -51,7 +51,7 @@ struct LoginPageAuthorize {

template<>
struct asyncgi::config::RouteMatcher<AccessRole, RouteContext> {
bool operator()(AccessRole value, const asyncgi::Request&, const RouteContext& context) const
bool operator()(AccessRole value, const http::Request&, const RouteContext& context) const
{
return value == context.role;
}
Expand All @@ -63,7 +63,7 @@ int main()
auto router = asyncgi::Router<RouteContext>{io};
router.route(asyncgi::rx{".*"}).process<AdminAuthorizer>();
router.route("/").process(
[](const asyncgi::Request&, RouteContext& context) -> http::Response
[](const http::Request&, RouteContext& context) -> http::Response
{
if (context.role == AccessRole::Admin)
return {"<p>Hello admin</p>"};
Expand Down
6 changes: 3 additions & 3 deletions examples/example_route_params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class GuestBookPage {
{
}

http::Response operator()(const asyncgi::Request&)
http::Response operator()(const http::Request&)
{
auto messages = state_->messages();
auto page = "<h1>Guest book</h1>"s;
Expand Down Expand Up @@ -74,7 +74,7 @@ class GuestBookAddMessage {
{
}

http::Response operator()(const asyncgi::Request& request)
http::Response operator()(const http::Request& request)
{
state_->addMessage(std::string{request.formField("msg")});
return http::Redirect{"/"};
Expand All @@ -91,7 +91,7 @@ class GuestBookRemoveMessage {
{
}

http::Response operator()(int index, const asyncgi::Request&)
http::Response operator()(int index, const http::Request&)
{
state_->removeMessage(index);
return http::Redirect{"/"};
Expand Down
6 changes: 3 additions & 3 deletions examples/example_route_params_user_defined_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class GuestBookPage {
{
}

http::Response operator()(const asyncgi::Request&)
http::Response operator()(const http::Request&)
{
auto messages = state_->messages();
auto page = "<h1>Guest book</h1>"s;
Expand Down Expand Up @@ -86,7 +86,7 @@ class GuestBookAddMessage {
{
}

http::Response operator()(const asyncgi::Request& request)
http::Response operator()(const http::Request& request)
{
state_->addMessage(std::string{request.formField("msg")});
return http::Redirect{"/"};
Expand All @@ -103,7 +103,7 @@ class GuestBookRemoveMessage {
{
}

http::Response operator()(MessageNumber msgNumber, const asyncgi::Request&)
http::Response operator()(MessageNumber msgNumber, const http::Request&)
{
state_->removeMessage(msgNumber.value);
return http::Redirect{"/"};
Expand Down
4 changes: 2 additions & 2 deletions examples/example_router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class GuestBookPage {
{
}

http::Response operator()(const asyncgi::Request&)
http::Response operator()(const http::Request&)
{
auto messages = state_->messages();
auto page = "<h1>Guest book</h1>"s;
Expand Down Expand Up @@ -60,7 +60,7 @@ class GuestBookAddMessage {
{
}

http::Response operator()(const asyncgi::Request& request)
http::Response operator()(const http::Request& request)
{
state_->addMessage(std::string{request.formField("msg")});
return http::Redirect{"/"};
Expand Down
2 changes: 1 addition & 1 deletion examples/example_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct Greeter{
{
}

http::Response operator()(const asyncgi::Request&)
http::Response operator()(const http::Request&)
{
return "Hello world\n(alive for " + std::to_string(*secondsCounter_) + " seconds)";
}
Expand Down
8 changes: 5 additions & 3 deletions include/asyncgi/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#include "errors.h"
#include "types.h"
#include "detail/serviceholder.h"
#include "fastcgi/request.h"
#include "fastcgi/response.h"
#include "http/request.h"
#include "http/response_view.h"
#include "http/response.h"
#include <filesystem>
#include <functional>
#include <memory>
Expand Down Expand Up @@ -33,7 +35,7 @@ class Client {
void makeRequest(
const std::filesystem::path& socketPath,
const http::Request& request,
const std::function<void(std::optional<http::ResponseView>)>& responseHandler,
const std::function<void(std::optional<http::Response>)>& responseHandler,
std::chrono::milliseconds timeout = std::chrono::seconds{3});

void makeRequest(
Expand All @@ -46,7 +48,7 @@ class Client {
std::string_view ipAddress,
uint16_t port,
const http::Request& request,
const std::function<void(std::optional<http::ResponseView>)>& responseHandler,
const std::function<void(std::optional<http::Response>)>& responseHandler,
std::chrono::milliseconds timeout = std::chrono::seconds{3});

void disconnect();
Expand Down
Loading

0 comments on commit 1749147

Please sign in to comment.