From a92ac7b0bdf638800eaea6749760f770f23fd703 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 25 Aug 2024 17:28:09 +0200 Subject: [PATCH] was/{Glue,MGlue}: merge common code into class WasStockRequest --- src/was/Glue.cxx | 135 +------------------------------------------ src/was/MGlue.cxx | 135 +------------------------------------------ src/was/SRequest.cxx | 76 ++++++++++++++++++++++++ src/was/SRequest.hxx | 85 +++++++++++++++++++++++++++ src/was/meson.build | 1 + 5 files changed, 168 insertions(+), 264 deletions(-) create mode 100644 src/was/SRequest.cxx create mode 100644 src/was/SRequest.hxx diff --git a/src/was/Glue.cxx b/src/was/Glue.cxx index 7f4611df3..095214a2a 100644 --- a/src/was/Glue.cxx +++ b/src/was/Glue.cxx @@ -4,73 +4,17 @@ #include "Glue.hxx" #include "Stock.hxx" -#include "SConnection.hxx" -#include "SLease.hxx" -#include "Client.hxx" +#include "SRequest.hxx" #include "was/async/Socket.hxx" -#include "http/PendingRequest.hxx" -#include "http/ResponseHandler.hxx" -#include "stock/GetHandler.hxx" -#include "stock/Stock.hxx" -#include "stock/Item.hxx" #include "pool/pool.hxx" -#include "pool/LeakDetector.hxx" -#include "stopwatch.hxx" -#include "util/Cancellable.hxx" #include "util/StringCompare.hxx" #include #include -class WasRequest final : StockGetHandler, Cancellable, HttpResponseHandler, PoolLeakDetector { - struct pool &pool; - - StopwatchPtr stopwatch; - - const char *const site_name; - - const char *const remote_host; - - PendingHttpRequest pending_request; - const char *script_name; - const char *path_info; - const char *query_string; - - std::span parameters; - - WasMetricsHandler *const metrics_handler; - HttpResponseHandler &handler; - CancellablePointer cancel_ptr; - +class WasRequest final : WasStockRequest { public: - WasRequest(struct pool &_pool, - StopwatchPtr &&_stopwatch, - const char *_site_name, - const char *_remote_host, - HttpMethod _method, const char *_uri, - const char *_script_name, const char *_path_info, - const char *_query_string, - StringMap &&_headers, - UnusedIstreamPtr _body, - std::span _parameters, - WasMetricsHandler *_metrics_handler, - HttpResponseHandler &_handler) - :PoolLeakDetector(_pool), - pool(_pool), - stopwatch(std::move(_stopwatch)), - site_name(_site_name), - remote_host(_remote_host), - pending_request(_pool, _method, _uri, - std::move(_headers), std::move(_body)), - script_name(_script_name), - path_info(_path_info), query_string(_query_string), - parameters(_parameters), - metrics_handler(_metrics_handler), - handler(_handler) {} - - void Destroy() noexcept { - DeleteFromPool(pool, this); - } + using WasStockRequest::WasStockRequest; void Start(WasStock &was_stock, const ChildOptions &options, const char *action, std::span args, @@ -84,81 +28,8 @@ class WasRequest final : StockGetHandler, Cancellable, HttpResponseHandler, Pool parallelism, disposable, *this, cancel_ptr); } - -private: - /* virtual methods from class StockGetHandler */ - void OnStockItemReady(StockItem &item) noexcept override; - void OnStockItemError(std::exception_ptr ep) noexcept override; - - /* virtual methods from class HttpResponseHandler */ - void OnHttpResponse(HttpStatus status, StringMap &&headers, - UnusedIstreamPtr body) noexcept override; - void OnHttpError(std::exception_ptr error) noexcept override; - - /* virtual methods from class Cancellable */ - void Cancel() noexcept override { - auto c = std::move(cancel_ptr); - Destroy(); - c.Cancel(); - } }; -/* - * stock callback - * - */ - -void -WasRequest::OnStockItemReady(StockItem &item) noexcept -{ - auto &connection = static_cast(item); - connection.SetSite(site_name); - connection.SetUri(pending_request.uri); - - const auto &process = connection.GetSocket(); - auto &lease = *NewFromPool(pool, connection); - - was_client_request(pool, item.GetStock().GetEventLoop(), - std::move(stopwatch), - process.control, - process.input, process.output, - lease, - remote_host, - pending_request.method, pending_request.uri, - script_name, path_info, - query_string, - pending_request.headers, - std::move(pending_request.body), - parameters, - metrics_handler, - *this, cancel_ptr); -} - -void -WasRequest::OnStockItemError(std::exception_ptr ep) noexcept -{ - auto &_handler = handler; - Destroy(); - _handler.InvokeError(ep); -} - -void -WasRequest::OnHttpResponse(HttpStatus status, StringMap &&_headers, - UnusedIstreamPtr _body) noexcept -{ - auto &_handler = handler; - Destroy(); - _handler.InvokeResponse(status, std::move(_headers), std::move(_body)); -} - -void -WasRequest::OnHttpError(std::exception_ptr error) noexcept -{ - auto &_handler = handler; - Destroy(); - _handler.InvokeError(std::move(error)); -} - /* * constructor * diff --git a/src/was/MGlue.cxx b/src/was/MGlue.cxx index 95559469d..e564050c9 100644 --- a/src/was/MGlue.cxx +++ b/src/was/MGlue.cxx @@ -5,77 +5,21 @@ #include "MGlue.hxx" #include "MStock.hxx" #include "RStock.hxx" -#include "SConnection.hxx" -#include "SLease.hxx" -#include "Client.hxx" +#include "SRequest.hxx" #include "was/async/Socket.hxx" -#include "http/PendingRequest.hxx" -#include "http/ResponseHandler.hxx" -#include "stock/GetHandler.hxx" -#include "stock/Stock.hxx" -#include "stock/Item.hxx" #include "pool/pool.hxx" -#include "pool/LeakDetector.hxx" #include "stopwatch.hxx" #include "net/SocketAddress.hxx" #include "net/FormatAddress.hxx" -#include "util/Cancellable.hxx" #include "util/StringCompare.hxx" #include "AllocatorPtr.hxx" #include -class MultiWasRequest final - : StockGetHandler, Cancellable, HttpResponseHandler, PoolLeakDetector +class MultiWasRequest final : WasStockRequest { - struct pool &pool; - - StopwatchPtr stopwatch; - - const char *const site_name; - - const char *const remote_host; - - PendingHttpRequest pending_request; - const char *script_name; - const char *path_info; - const char *query_string; - - std::span parameters; - - WasMetricsHandler *const metrics_handler; - HttpResponseHandler &handler; - CancellablePointer cancel_ptr; - public: - MultiWasRequest(struct pool &_pool, - StopwatchPtr &&_stopwatch, - const char *_site_name, - const char *_remote_host, - HttpMethod _method, const char *_uri, - const char *_script_name, const char *_path_info, - const char *_query_string, - StringMap &&_headers, - UnusedIstreamPtr _body, - std::span _parameters, - WasMetricsHandler *_metrics_handler, - HttpResponseHandler &_handler) noexcept - :PoolLeakDetector(_pool), - pool(_pool), - stopwatch(std::move(_stopwatch)), - site_name(_site_name), - remote_host(_remote_host), - pending_request(_pool, _method, _uri, - std::move(_headers), std::move(_body)), - script_name(_script_name), - path_info(_path_info), query_string(_query_string), - parameters(_parameters), - metrics_handler(_metrics_handler), - handler(_handler) {} - - void Destroy() noexcept { - this->~MultiWasRequest(); - } + using WasStockRequest::WasStockRequest; void Start(MultiWasStock &stock, const ChildOptions &options, const char *action, std::span args, @@ -96,81 +40,8 @@ class MultiWasRequest final stock.Get(pool, address, parallelism, concurrency, *this, cancel_ptr); } - -private: - /* virtual methods from class StockGetHandler */ - void OnStockItemReady(StockItem &item) noexcept override; - void OnStockItemError(std::exception_ptr ep) noexcept override; - - /* virtual methods from class HttpResponseHandler */ - void OnHttpResponse(HttpStatus status, StringMap &&headers, - UnusedIstreamPtr body) noexcept override; - void OnHttpError(std::exception_ptr error) noexcept override; - - /* virtual methods from class Cancellable */ - void Cancel() noexcept override { - auto c = std::move(cancel_ptr); - Destroy(); - c.Cancel(); - } }; -/* - * stock callback - * - */ - -void -MultiWasRequest::OnStockItemReady(StockItem &item) noexcept -{ - auto &connection = static_cast(item); - connection.SetSite(site_name); - connection.SetUri(pending_request.uri); - - const auto &socket = connection.GetSocket(); - auto &lease = *NewFromPool(pool, connection); - - was_client_request(pool, item.GetStock().GetEventLoop(), - std::move(stopwatch), - socket.control, - socket.input, socket.output, - lease, - remote_host, - pending_request.method, pending_request.uri, - script_name, path_info, - query_string, - pending_request.headers, - std::move(pending_request.body), - parameters, - metrics_handler, - *this, cancel_ptr); -} - -void -MultiWasRequest::OnStockItemError(std::exception_ptr ep) noexcept -{ - auto &_handler = handler; - Destroy(); - _handler.InvokeError(ep); -} - -void -MultiWasRequest::OnHttpResponse(HttpStatus status, StringMap &&_headers, - UnusedIstreamPtr _body) noexcept -{ - auto &_handler = handler; - Destroy(); - _handler.InvokeResponse(status, std::move(_headers), std::move(_body)); -} - -void -MultiWasRequest::OnHttpError(std::exception_ptr error) noexcept -{ - auto &_handler = handler; - Destroy(); - _handler.InvokeError(std::move(error)); -} - /* * constructor * diff --git a/src/was/SRequest.cxx b/src/was/SRequest.cxx new file mode 100644 index 000000000..311f5dc97 --- /dev/null +++ b/src/was/SRequest.cxx @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: BSD-2-Clause +// Copyright CM4all GmbH +// author: Max Kellermann + +#include "SRequest.hxx" +#include "SConnection.hxx" +#include "SLease.hxx" +#include "Client.hxx" +#include "pool/pool.hxx" +#include "stock/Item.hxx" +#include "stock/Stock.hxx" + +inline void +WasStockRequest::Destroy() noexcept +{ + DeleteFromPool(pool, this); +} + +void +WasStockRequest::OnStockItemReady(StockItem &item) noexcept +{ + auto &connection = static_cast(item); + connection.SetSite(site_name); + connection.SetUri(pending_request.uri); + + const auto &process = connection.GetSocket(); + auto &lease = *NewFromPool(pool, connection); + + was_client_request(pool, item.GetStock().GetEventLoop(), + std::move(stopwatch), + process.control, + process.input, process.output, + lease, + remote_host, + pending_request.method, pending_request.uri, + script_name, path_info, + query_string, + pending_request.headers, + std::move(pending_request.body), + parameters, + metrics_handler, + *this, cancel_ptr); +} + +void +WasStockRequest::OnStockItemError(std::exception_ptr ep) noexcept +{ + auto &_handler = handler; + Destroy(); + _handler.InvokeError(ep); +} + +void +WasStockRequest::OnHttpResponse(HttpStatus status, StringMap &&_headers, + UnusedIstreamPtr _body) noexcept +{ + auto &_handler = handler; + Destroy(); + _handler.InvokeResponse(status, std::move(_headers), std::move(_body)); +} + +void +WasStockRequest::OnHttpError(std::exception_ptr error) noexcept +{ + auto &_handler = handler; + Destroy(); + _handler.InvokeError(std::move(error)); +} + +void +WasStockRequest::Cancel() noexcept +{ + auto c = std::move(cancel_ptr); + Destroy(); + c.Cancel(); +} diff --git a/src/was/SRequest.hxx b/src/was/SRequest.hxx new file mode 100644 index 000000000..77282c537 --- /dev/null +++ b/src/was/SRequest.hxx @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: BSD-2-Clause +// Copyright CM4all GmbH +// author: Max Kellermann + +#pragma once + +#include "pool/LeakDetector.hxx" +#include "stock/GetHandler.hxx" +#include "http/PendingRequest.hxx" +#include "http/ResponseHandler.hxx" +#include "util/Cancellable.hxx" +#include "stopwatch.hxx" + +class WasMetricsHandler; + +class WasStockRequest + : protected StockGetHandler, HttpResponseHandler, + protected Cancellable, + PoolLeakDetector +{ +protected: + struct pool &pool; + +public: + StopwatchPtr stopwatch; + + const char *const site_name; + + const char *const remote_host; + + PendingHttpRequest pending_request; + const char *const script_name; + const char *const path_info; + const char *const query_string; + + const std::span parameters; + + WasMetricsHandler *const metrics_handler; + HttpResponseHandler &handler; + CancellablePointer cancel_ptr; + +public: + WasStockRequest(struct pool &_pool, + StopwatchPtr &&_stopwatch, + const char *_site_name, + const char *_remote_host, + HttpMethod _method, const char *_uri, + const char *_script_name, const char *_path_info, + const char *_query_string, + StringMap &&_headers, + UnusedIstreamPtr _body, + std::span _parameters, + WasMetricsHandler *_metrics_handler, + HttpResponseHandler &_handler) + :PoolLeakDetector(_pool), + pool(_pool), + stopwatch(std::move(_stopwatch)), + site_name(_site_name), + remote_host(_remote_host), + pending_request(_pool, _method, _uri, + std::move(_headers), std::move(_body)), + script_name(_script_name), + path_info(_path_info), query_string(_query_string), + parameters(_parameters), + metrics_handler(_metrics_handler), + handler(_handler) {} + + virtual ~WasStockRequest() noexcept = default; + +protected: + void Destroy() noexcept; + +private: + /* virtual methods from class StockGetHandler */ + void OnStockItemReady(StockItem &item) noexcept final; + void OnStockItemError(std::exception_ptr ep) noexcept final; + + /* virtual methods from class HttpResponseHandler */ + void OnHttpResponse(HttpStatus status, StringMap &&headers, + UnusedIstreamPtr body) noexcept final; + void OnHttpError(std::exception_ptr error) noexcept final; + + /* virtual methods from class Cancellable */ + void Cancel() noexcept final; +}; diff --git a/src/was/meson.build b/src/was/meson.build index 288e83eb5..89c8de601 100644 --- a/src/was/meson.build +++ b/src/was/meson.build @@ -52,6 +52,7 @@ was_stock = static_library( 'SConnection.cxx', 'Stock.cxx', 'SLease.cxx', + 'SRequest.cxx', 'Glue.cxx', 'MStock.cxx', 'MGlue.cxx',