4#include "Stroika/Frameworks/StroikaPreComp.h"
9#include "Stroika/Foundation/Characters/FloatConversion.h"
11#include "Stroika/Foundation/Characters/String2Int.h"
13#include "Stroika/Foundation/Containers/Common.h"
14#include "Stroika/Foundation/DataExchange/BadFormatException.h"
20#include "Stroika/Foundation/Execution/Throw.h"
23#include "Stroika/Foundation/IO/Network/HTTP/ClientErrorException.h"
24#include "Stroika/Foundation/IO/Network/HTTP/Headers.h"
26#include "Stroika/Foundation/IO/Network/HTTP/Methods.h"
30#include "Connection.h"
38using namespace Stroika::Foundation::Memory;
39using namespace Stroika::Foundation::Traversal;
40using namespace Stroika::Foundation::Time;
42using namespace Stroika::Frameworks;
58 const Headers& defaultResponseHeaders,
const optional<bool> autoComputeETagResponse)
59 :
Message{
Request{socketStream}, Response{socket, socketStream, defaultResponseHeaders}, socket.GetPeerAddress ()}
60 , fMsgHeaderInTextStream{HTTP::MessageStartTextInputStreamBinaryAdapter::New (rwRequest ().GetInputStream ())}
62 if (autoComputeETagResponse) {
63 this->rwResponse ().autoComputeETag = *autoComputeETagResponse;
67Connection::MyMessage_::ReadHeadersResult Connection::MyMessage_::ReadHeaders (
68#
if qStroika_Framework_WebServer_Connection_DetailedMessagingLog
69 const function<
void (
const String&)>& logMsg
73#if qStroika_Framework_WebServer_Connection_DetailedMessagingLog
74 logMsg (
"Starting ReadHeaders_"sv);
80 if (not fMsgHeaderInTextStream.AssureHeaderSectionAvailable ()) {
81#if qStroika_Framework_WebServer_Connection_DetailedMessagingLog
82 logMsg (
"got fMsgHeaderInTextStream.AssureHeaderSectionAvailable INCOMPLETE"sv);
84 return ReadHeadersResult::eIncompleteButMoreMayBeAvailable;
90 Request& updatableRequest = this->rwRequest ();
93 String line = fMsgHeaderInTextStream.ReadLine ();
95#if qStroika_Framework_WebServer_Connection_DetailedMessagingLog
96 logMsg (
"got EOF from src stream reading headers(incomplete)"sv);
98 return ReadHeadersResult::eIncompleteDeadEnd;
101 if (tokens.size () < 3) {
102 DbgTrace (
"tokens={}, line='{}', fMsgHeaderInTextStream={}"_f, tokens, line, fMsgHeaderInTextStream.ToString ());
107 if (tokens[1].empty ()) {
109 DbgTrace (
"tokens={}, line='{}'"_f, tokens, line);
113 if (updatableRequest.
httpMethod ().empty ()) {
115 DbgTrace (
"tokens={}, line='{}'"_f, tokens, line);
121 static const String kCRLF_{
"\r\n"sv};
122 String line = fMsgHeaderInTextStream.ReadLine ();
123 if (line == kCRLF_ or line.empty ()) {
128 size_t i = line.
find (
':');
129 if (i == string::npos) {
136 updatableRequest.
rwHeaders ().Add (hdr, value);
139#if qStroika_Framework_WebServer_Connection_DetailedMessagingLog
140 logMsg (
"ReadHeaders completed normally"sv);
142 return ReadHeadersResult::eCompleteGood;
164#if qStroika_Framework_WebServer_Connection_TrackExtraStats
165 if (fMostRecentMessage) {
166 sb <<
", mostRecentMessage: " << *fMostRecentMessage;
168 if (fHandlingThread) {
170 sb <<
", handlingThread: " << fHandlingThread;
173 sb <<
", thread: " << fHandlingThread;
193 const optional<Headers>& defaultGETResponseHeaders,
const optional<bool> autoComputeETagResponse)
194 :
Connection{s, Options{.fInterceptorChain = interceptorChain,
195 .fDefaultResponseHeaders = defaultResponseHeaders,
196 .fDefaultGETResponseHeaders = defaultGETResponseHeaders,
197 .fAutoComputeETagResponse = autoComputeETagResponse}}
202 :
socket{[qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]] const auto* property) -> ConnectionOrientedStreamSocket::Ptr {
204 AssertExternallySynchronizedMutex::ReadContext declareContext{*thisObj};
205 return thisObj->fSocket_;
207 ,
request{[qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]]
const auto* property) ->
const Request& {
209 AssertExternallySynchronizedMutex::ReadContext declareContext{*thisObj};
210 return thisObj->fMessage_->request ();
212 ,
response{[qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]]
const auto* property) ->
const Response& {
214 AssertExternallySynchronizedMutex::ReadContext declareContext{*thisObj};
215 return thisObj->fMessage_->response ();
217 ,
rwResponse{[qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]]
const auto* property) -> Response& {
219 AssertExternallySynchronizedMutex::WriteContext declareContext{*thisObj};
220 return thisObj->fMessage_->rwResponse ();
222 ,
stats{[qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]]
const auto* property) -> Stats {
227#if qStroika_Framework_WebServer_Connection_TrackExtraStats
228 Stats2Capture_ statsCapturedDuringMessageProcessing = thisObj->fExtraStats_.
load ();
231 .fSocketID = uniqueID,
232 .fCreatedAt = createdAt,
233#if qStroika_Framework_WebServer_Connection_TrackExtraStats
234 .fMostRecentMessage = statsCapturedDuringMessageProcessing.fMessageStart
236 statsCapturedDuringMessageProcessing.fMessageCompleted}
237 : optional<Range<TimePointSeconds>>{},
238 .fHandlingThread = statsCapturedDuringMessageProcessing.fHandlingThread,
239 .fRemotePeerAddress = statsCapturedDuringMessageProcessing.fPeer,
240 .fRequestWebMethod = statsCapturedDuringMessageProcessing.fWebMethod,
241 .fRequestURI = statsCapturedDuringMessageProcessing.fRequestURI,
247 [qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]]
const auto* property) -> optional<HTTP::KeepAlive> {
249 AssertExternallySynchronizedMutex::ReadContext declareContext{*thisObj};
250 return thisObj->fRemaining_;
252 [qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]]
auto* property,
const auto&
remainingConnectionLimits) {
254 AssertExternallySynchronizedMutex::WriteContext declareContext{*thisObj};
257 , fInterceptorChain_{options.fInterceptorChain}
258 , fDefaultResponseHeaders_{options.fDefaultResponseHeaders}
259 , fDefaultGETResponseHeaders_{options.fDefaultGETResponseHeaders}
260 , fAutoComputeETagResponse_{options.fAutoComputeETagResponse}
261 , fSupportedCompressionEncodings_{options.fSupportedCompressionEncodings}
263 , fConnectionStartedAt_{Time::GetTickCount ()}
265 Require (s !=
nullptr);
266#if USE_NOISY_TRACE_IN_THIS_MODULE_
267 DbgTrace (
"Created connection for socket {}"_f, s);
269 fSocketStream_ = SocketStream::New (fSocket_);
270#if qStroika_Framework_WebServer_Connection_DetailedMessagingLog
272 String socketName =
"{}-{}"_f((
long)DateTime::Now ().As<time_t> (), (
int)s.GetNativeSocket ());
273 fSocketStream_ = Streams::LoggingInputOutputStream<byte>::New (
279 Streams::TextToBinary::Writer::Format::eUTF8WithoutBOM);
284Connection::~Connection ()
286#if USE_NOISY_TRACE_IN_THIS_MODULE_
287 DbgTrace (
"Destroying connection for socket {}, message={}"_f, fSocket_,
static_cast<const void*
> (fMessage_.get ()));
289 AssertExternallySynchronizedMutex::WriteContext declareContext{*
this};
290#if qStroika_Framework_WebServer_Connection_DetailedMessagingLog
291 WriteLogConnectionMsg_ (L
"DestroyingConnection");
293 if (fMessage_ !=
nullptr) {
294 if (not fMessage_->response ().responseCompleted ()) {
295 IgnoreExceptionsForCall (fMessage_->rwResponse ().Abort ());
297 Require (fMessage_->response ().responseCompleted ());
310 DbgTrace (
"Exception ignored closing socket: {}"_f, current_exception ());
316 AssertExternallySynchronizedMutex::WriteContext declareContext{*
this};
318#if USE_NOISY_TRACE_IN_THIS_MODULE_
321 fMessage_ = make_unique<MyMessage_> (fSocket_, fSocketStream_, fDefaultResponseHeaders_, fAutoComputeETagResponse_);
322#if qStroika_Foundation_Debug_AssertExternallySynchronizedMutex_Enabled
323 fMessage_->SetAssertExternallySynchronizedMutexContext (GetSharedContext ());
326 auto readHeaders = [&] () -> optional<ReadAndProcessResult> {
327 switch (fMessage_->ReadHeaders (
328#
if qStroika_Framework_WebServer_Connection_DetailedMessagingLog
329 [
this] (
const String& i) ->
void { WriteLogConnectionMsg_ (i); }
332 case MyMessage_::eIncompleteDeadEnd: {
333 DbgTrace (
"ReadHeaders failed - typically because the client closed the connection before we could handle it (e.g. "
334 "in web browser hitting refresh button fast)."_f);
337 case MyMessage_::eIncompleteButMoreMayBeAvailable: {
338 DbgTrace (
"ReadHeaders failed - incomplete header (most likely a DOS attack)."_f);
339 return ReadAndProcessResult::eTryAgainLater;
341 case MyMessage_::eCompleteGood: {
351 if (
auto r = readHeaders ()) {
356#if qStroika_Foundation_Debug_AssertionsChecked
357 [[maybe_unused]]
auto&& cleanup =
Finally ([&] ()
noexcept { Ensure (fMessage_->response ().responseCompleted ()); });
360#if qStroika_Framework_WebServer_Connection_TrackExtraStats
361 [[maybe_unused]]
auto&& cleanup2 =
362 Finally ([&] ()
noexcept { fExtraStats_.
rwget ().rwref ().fMessageCompleted = Time::GetTickCount (); });
363 fExtraStats_.
store (Stats2Capture_{.fMessageStart = Time::GetTickCount (),
365 .fWebMethod = fMessage_->request ().httpMethod (),
366 .fRequestURI = fMessage_->request ().url (),
367 .fHandlingThread = std::this_thread::get_id ()});
370 auto applyDefaultsToResponseHeaders = [&] () ->
void {
371 if (fDefaultGETResponseHeaders_ and fMessage_->request ().httpMethod () == HTTP::Methods::kGet) {
372 fMessage_->rwResponse ().rwHeaders () += *fDefaultGETResponseHeaders_;
375 fMessage_->rwResponse ().rwHeaders ().date = DateTime::Now ();
380 if (optional<HTTP::ContentEncodings> acceptEncoding = fMessage_->request ().headers ().acceptEncoding) {
381 optional<HTTP::ContentEncodings> oBodyEncoding = fMessage_->rwResponse ().bodyEncoding ();
383 fMessage_->rwResponse ().bodyEncoding = [&] () {
385 auto bc = *oBodyEncoding;
386 bc += contentEncoding2Add;
390 return HTTP::ContentEncodings{contentEncoding2Add};
394 bool needBodyEncoding = not oBodyEncoding.has_value ();
397 if (needBodyEncoding and acceptEncoding->Contains (ce) and
398 (fSupportedCompressionEncodings_ == nullopt or fSupportedCompressionEncodings_->Contains (ce))) {
400 needBodyEncoding =
false;
403 if constexpr (DataExchange::Compression::Deflate::kSupported) {
406 if constexpr (DataExchange::Compression::GZip::kSupported) {
409 if constexpr (DataExchange::Compression::ZStd::kSupported) {
415 if (
auto requestedINoneMatch = this->
request ().headers ().ifNoneMatch ()) {
416 if (this->
response ().autoComputeETag ()) {
417 this->
rwResponse ().automaticTransferChunkSize =
418 Response::kNoChunkedTransfer;
422 applyDefaultsToResponseHeaders ();
427 auto applyKeepAliveLogic = [&] () ->
bool {
428 bool thisMessageKeepAlive = fMessage_->request ().keepAliveRequested;
429 if (thisMessageKeepAlive) {
433 if (
auto keepAliveValue = fMessage_->request ().headers ().keepAlive ()) {
438 if (oRemaining->fMessages) {
439 if (oRemaining->fMessages == 0u) {
440 thisMessageKeepAlive =
false;
443 oRemaining->fMessages = *oRemaining->fMessages - 1u;
446 if (oRemaining->fTimeout) {
447 if (fConnectionStartedAt_ + *oRemaining->fTimeout < Time::GetTickCount ()) {
448 thisMessageKeepAlive =
false;
455 this->
rwResponse ().rwHeaders ().connection = thisMessageKeepAlive ? Headers::eKeepAlive : Headers::eClose;
456 return thisMessageKeepAlive;
458 bool thisMessageKeepAlive = applyKeepAliveLogic ();
464 auto invokeInterceptorChain = [&] () {
465#if USE_NOISY_TRACE_IN_THIS_MODULE_
468#if qStroika_Framework_WebServer_Connection_DetailedMessagingLog
469 WriteLogConnectionMsg_ (
"Handing request {} to interceptor chain"_f(
request ()));
475#if USE_NOISY_TRACE_IN_THIS_MODULE_
476 DbgTrace (
"Interceptor-Chain caught exception handling message: {}"_f, current_exception ());
478#if qStroika_Framework_WebServer_Connection_DetailedMessagingLog
479 WriteLogConnectionMsg_ (
"Interceptor-Chain caught exception handling message: {}"_f(current_exception ()));
483 invokeInterceptorChain ();
485 auto assureRequestFullyRead = [&] () {
486 if (thisMessageKeepAlive) {
499 if (
request ().headers ().contentLength ()) {
500#if qStroika_Framework_WebServer_Connection_DetailedMessagingLog
501 WriteLogConnectionMsg_ (L
"msg is keepalive, and have content length, so making sure we read all of request body");
503#if USE_NOISY_TRACE_IN_THIS_MODULE_
507 (void)fMessage_->rwRequest ().GetBody ();
511 assureRequestFullyRead ();
517 auto completeResponse = [&] () {
519 if (
auto requestedINoneMatch = this->
request ().headers ().ifNoneMatch ()) {
520 if (
auto actualETag = this->
response ().headers ().ETag ()) {
521 bool ctm = this->
response ().chunkedTransferMode ();
523 DbgTrace (
"Warning - disregarding ifNoneMatch request (though it matched) - cuz in chunked transfer mode"_f);
525 if (requestedINoneMatch->fETags.Contains (*actualETag) and not ctm) {
526 DbgTrace (
"Updating OK response to NotModified (due to ETag match)"_f);
527 this->
rwResponse ().status = HTTP::StatusCodes::kNotModified;
533 thisMessageKeepAlive =
false;
535#if qStroika_Framework_WebServer_Connection_DetailedMessagingLog
536 WriteLogConnectionMsg_ (L
"Did GetResponse ().End ()");
541 return thisMessageKeepAlive ? eTryAgainLater : eClose;
544 DbgTrace (
"ReadAndProcessMessage Exception caught ({}), so returning ReadAndProcessResult::eClose"_f, current_exception ());
546 return Connection::ReadAndProcessResult::eClose;
550#if qStroika_Framework_WebServer_Connection_DetailedMessagingLog
551void Connection::WriteLogConnectionMsg_ (
const String& msg)
const
553 String useMsg = DateTime::Now ().Format () +
" -- "sv + msg.
Trim ();
554 fLogConnectionState_.WriteLn (useMsg);
560 AssertExternallySynchronizedMutex::ReadContext declareContext{*
this};
563 sb <<
"Socket: "sv << fSocket_;
564 if (not abbreviatedOutput) {
565 sb <<
", Message: "sv << fMessage_;
566 sb <<
", Remaining: "sv << fRemaining_;
568 sb <<
", Connection-Started-At: "sv << fConnectionStartedAt_;
#define AssertNotReached()
time_point< RealtimeClock, DurationSeconds > TimePointSeconds
TimePointSeconds is a simpler approach to chrono::time_point, which doesn't require using templates e...
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
nonvirtual size_t length() const noexcept
nonvirtual String SubString(SZ from) const
nonvirtual String Trim(bool(*shouldBeTrimmed)(Character)=Character::IsWhitespace) const
nonvirtual Containers::Sequence< String > Tokenize() const
nonvirtual size_t find(Character c, size_t startAt=0) const
A generalization of a vector: a container whose elements are keyed by the natural numbers.
nonvirtual WritableReference rwget()
get a read-write smart pointer to the underlying Synchronized<> object, holding the full lock the who...
nonvirtual void store(const T &v)
nonvirtual T load() const
nonvirtual optional< IO::Network::SocketAddress > GetPeerAddress() const
nonvirtual void Close() const
ClientErrorException is to capture exceptions caused by a bad (e.g ill-formed) request.
Common::Property< String > httpMethod
typically HTTP::Methods::kGet
Common::Property< Headers & > rwHeaders
Common::Property< URI > url
Common::Property< String > httpVersion
nonvirtual PlatformNativeHandle GetNativeSocket() const
static URI ParseRelative(const String &rawRelativeURL)
A Connection object represents the state (and socket) for an ongoing, active, HTTP Connection,...
Common::Property< optional< HTTP::KeepAlive > > remainingConnectionLimits
const Common::ReadOnlyProperty< Stats > stats
retrieve stats about this connection, like threads used, start/end times. NB: INTERNALLY SYNCRONIZED
const Common::ReadOnlyProperty< ConnectionOrientedStreamSocket::Ptr > socket
nonvirtual ReadAndProcessResult ReadAndProcessMessage() noexcept
const Common::ReadOnlyProperty< const Request & > request
Common::ReadOnlyProperty< Response & > rwResponse
const Common::ReadOnlyProperty< const Response & > response
nonvirtual String ToString(bool abbreviatedOutput=true) const
nonvirtual void HandleMessage(Message &m) const
this represents a HTTP request object for the WebServer module
CONTAINER::value_type * End(CONTAINER &c)
For a contiguous container (such as a vector or basic_string) - find the pointer to the end of the co...
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
auto Finally(FUNCTION &&f) -> Private_::FinallySentry< FUNCTION >
filesystem::path GetTemporary()
constexpr bool IsOK(Status s)
several status codes considered OK, so check if it is among them
Ptr New(const Streams::OutputStream::Ptr< byte > &src, const Characters::CodeCvt<> &char2OutputConverter)
Content coding values indicate an encoding transformation that has been or can be applied to an entit...
static const ContentEncoding kDeflate
static const ContentEncoding kZStd
probably fastest/best, but NYI in Stroika as of 2024-06-20
static const ContentEncoding kGZip
optional< URI > fRequestURI
last requested URI (always relative uri)
nonvirtual String ToString() const
Socket::PlatformNativeHandle fSocketID
optional< SocketAddress > fRemotePeerAddress
the address of the client which is talking to the server
TimePointSeconds fCreatedAt
optional< String > fRequestWebMethod
last request