SNode.C
Loading...
Searching...
No Matches
web::http::client::MasterRequest Class Reference

#include <Request.h>

Inheritance diagram for web::http::client::MasterRequest:
Collaboration diagram for web::http::client::MasterRequest:

Public Member Functions

 MasterRequest (SocketContext *socketContext, const std::string &hostFieldValue)
 
 MasterRequest (MasterRequest &)=delete
 
 MasterRequest (MasterRequest &&) noexcept
 
MasterRequestoperator= (MasterRequest &)=delete
 
MasterRequestoperator= (MasterRequest &&) noexcept=delete
 
 ~MasterRequest () override
 
void init ()
 
bool send (const char *chunk, std::size_t chunkLen, const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &onResponseReceived, const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &onResponseParseError)
 
bool send (const std::string &chunk, const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &onResponseReceived, const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &onResponseParseError)
 
bool upgrade (const std::string &url, const std::string &protocols, const std::function< void(bool)> &onUpgradeInitiate, const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &, bool)> &onResponseReceived, const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &onResponseParseError)
 
bool sendFile (const std::string &file, const std::function< void(int errnum)> &onStatus, const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &onResponseReceived, const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &onResponseParseError)
 
MasterRequestsendHeader ()
 
MasterRequestsendFragment (const char *chunk, std::size_t chunkLen)
 
MasterRequestsendFragment (const std::string &data)
 
bool end (const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &onResponseReceived, const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &onResponseParseError)
 
bool requestEventSource (const std::string &url, const std::function< std::size_t()> &onServerSentEvent, const std::function< void()> &onOpen, const std::function< void()> &onError)
 
- Public Member Functions inherited from web::http::client::Request
 Request (SocketContext *socketContext, const std::string &host)
 
 Request (Request &)=delete
 
Requestoperator= (Request &)=delete
 
Requestoperator= (Request &&) noexcept=delete
 
void setMasterRequest (const std::shared_ptr< MasterRequest > &masterRequest)
 
std::shared_ptr< MasterRequestgetMasterRequest () const
 
SocketContextgetSocketContext () const
 
Requesthost (const std::string &hostFieldValue)
 
Requestappend (const std::string &field, const std::string &value)
 
Requestset (const std::string &field, const std::string &value, bool overwrite=true)
 
Requestset (const std::map< std::string, std::string > &headers, bool overwrite=true)
 
Requesttype (const std::string &type)
 
Requestcookie (const std::string &name, const std::string &value)
 
Requestcookie (const std::map< std::string, std::string > &cookies)
 
Requestquery (const std::string &key, const std::string &value)
 
RequestsetTrailer (const std::string &field, const std::string &value, bool overwrite=true)
 
std::string header (const std::string &field) const
 
const CiStringMap< std::string > & getQueries () const
 
const CiStringMap< std::string > & getHeaders () const
 
const CiStringMap< std::string > & getTrailer () const
 
const CiStringMap< std::string > & getCookies () const
 
- Public Member Functions inherited from core::pipe::Sink
 Sink (Sink &)=delete
 
Sinkoperator= (Sink &)=delete
 

Private Member Functions

bool initiate (const std::shared_ptr< MasterRequest > &request)
 
bool executeSendFile (const std::string &file, const std::function< void(int)> &onStatus)
 
bool executeUpgrade (const std::string &url, const std::string &protocols, const std::function< void(bool)> &onStatus)
 
bool executeSendHeader ()
 
bool executeSendFragment (const char *chunk, std::size_t chunkLen)
 
bool executeEnd ()
 
bool executeSse ()
 
void requestPrepared (const std::shared_ptr< MasterRequest > &request)
 
void requestDelivered ()
 
void deliverResponse (const std::shared_ptr< MasterRequest > &request, const std::shared_ptr< Response > &response)
 
void deliverResponseParseError (const std::shared_ptr< MasterRequest > &request, const std::string &message)
 
void onSourceConnect (core::pipe::Source *source) override
 
void onSourceData (const char *chunk, std::size_t chunkLen) override
 
void onSourceEof () override
 
void onSourceError (int errnum) override
 

Private Attributes

std::list< RequestCommand * > requestCommands
 
std::size_t contentLengthSent = 0
 
std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> onResponseReceived
 
std::function< void(const std::shared_ptr< Request > &, const std::string &message)> onResponseParseError
 

Friends

class commands::SendFileCommand
 
class commands::SendFragmentCommand
 
class commands::SendHeaderCommand
 
class commands::UpgradeCommand
 
class commands::EndCommand
 
class commands::SseCommand
 
class SocketContext
 

Additional Inherited Members

- Public Attributes inherited from web::http::client::Request
std::string hostFieldValue
 
std::string method = "GET"
 
std::string url = "/"
 
int httpMajor = 1
 
int httpMinor = 1
 
std::size_t count {0}
 
- Protected Member Functions inherited from web::http::client::Request
 Request (Request &&request) noexcept
 
void upgrade (const std::shared_ptr< Response > &response, const std::function< void(const std::string &)> &status)
 
- Protected Member Functions inherited from core::pipe::Sink
 Sink ()=default
 
 Sink (Sink &&) noexcept=default
 
Sinkoperator= (Sink &&) noexcept=default
 
virtual ~Sink ()
 
bool isStreaming ()
 
void stop ()
 
- Protected Attributes inherited from web::http::client::Request
CiStringMap< std::string > queries
 
CiStringMap< std::string > headers
 
CiStringMap< std::string > cookies
 
CiStringMap< std::string > trailer
 
std::size_t contentLength = 0
 
std::weak_ptr< MasterRequestmasterRequest
 
web::http::client::SocketContextsocketContext
 
TransferEncoding transferEncoding = TransferEncoding::HTTP10
 
ConnectionState connectionState = ConnectionState::Default
 

Detailed Description

Definition at line 147 of file Request.h.

Constructor & Destructor Documentation

◆ MasterRequest() [1/3]

web::http::client::MasterRequest::MasterRequest ( SocketContext socketContext,
const std::string &  hostFieldValue 
)

Definition at line 308 of file Request.cpp.

310 this->init();
311 }
Request(SocketContext *socketContext, const std::string &host)
Definition Request.cpp:77
web::http::client::SocketContext * socketContext
Definition Request.h:139
Request & host(const std::string &hostFieldValue)
Definition Request.cpp:118

References init(), and web::http::client::Request::Request().

Here is the call graph for this function:

◆ MasterRequest() [2/3]

web::http::client::MasterRequest::MasterRequest ( MasterRequest )
explicitdelete

◆ MasterRequest() [3/3]

web::http::client::MasterRequest::MasterRequest ( MasterRequest &&  request)
explicitnoexcept

Definition at line 313 of file Request.cpp.

314 : Request(std::move(request))
315 , requestCommands(std::move(request.requestCommands))
316 , contentLengthSent(request.contentLengthSent)
317 , onResponseReceived(std::move(request.onResponseReceived))
318 , onResponseParseError(std::move(request.onResponseParseError)) {
319 request.init();
320 }
std::function< void(const std::shared_ptr< Request > &, const std::string &message)> onResponseParseError
Definition Request.h:223
std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> onResponseReceived
Definition Request.h:222
std::list< RequestCommand * > requestCommands
Definition Request.h:218

References contentLengthSent, init(), onResponseParseError, onResponseReceived, web::http::client::Request::Request(), and requestCommands.

Here is the call graph for this function:

◆ ~MasterRequest()

web::http::client::MasterRequest::~MasterRequest ( )
override

Definition at line 322 of file Request.cpp.

322 {
323 for (const RequestCommand* requestCommand : requestCommands) {
324 delete requestCommand;
325 }
326
327 if (!masterRequest.expired() && Sink::isStreaming()) {
329 }
330 }
std::weak_ptr< MasterRequest > masterRequest
Definition Request.h:138

References core::pipe::Sink::isStreaming(), web::http::client::Request::masterRequest, requestCommands, web::http::client::Request::socketContext, and core::socket::stream::SocketContext::streamEof().

Here is the call graph for this function:

Member Function Documentation

◆ deliverResponse()

void web::http::client::MasterRequest::deliverResponse ( const std::shared_ptr< MasterRequest > &  request,
const std::shared_ptr< Response > &  response 
)
private

Definition at line 716 of file Request.cpp.

716 {
717 onResponseReceived(request, response);
718 }

References onResponseReceived.

Referenced by web::http::client::SocketContext::deliverResponse().

Here is the caller graph for this function:

◆ deliverResponseParseError()

void web::http::client::MasterRequest::deliverResponseParseError ( const std::shared_ptr< MasterRequest > &  request,
const std::string &  message 
)
private

Definition at line 720 of file Request.cpp.

720 {
721 onResponseParseError(request, message);
722 }

References onResponseParseError.

Referenced by web::http::client::SocketContext::deliverResponseParseError().

Here is the caller graph for this function:

◆ end()

bool web::http::client::MasterRequest::end ( const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &  onResponseReceived,
const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &  onResponseParseError 
)

Definition at line 524 of file Request.cpp.

525 {
526 bool queued = true;
527
528 if (!masterRequest.expired()) {
529 const std::shared_ptr<MasterRequest> newRequest = std::make_shared<MasterRequest>(std::move(*this));
530
531 newRequest->sendHeader();
532
533 newRequest->requestCommands.push_back(new commands::EndCommand(onResponseReceived, onResponseParseError));
534
535 requestPrepared(newRequest);
536 } else {
537 queued = false;
538 }
539
540 return queued;
541 }
void requestPrepared(const std::shared_ptr< MasterRequest > &request)
Definition Request.cpp:712

References web::http::client::commands::EndCommand::EndCommand(), web::http::client::Request::masterRequest, requestCommands, requestPrepared(), and sendHeader().

Here is the call graph for this function:

◆ executeEnd()

bool web::http::client::MasterRequest::executeEnd ( )
private

Definition at line 656 of file Request.cpp.

656 {
657 return true;
658 }

Referenced by web::http::client::commands::EndCommand::execute(), and executeSendFile().

Here is the caller graph for this function:

◆ executeSendFile()

bool web::http::client::MasterRequest::executeSendFile ( const std::string &  file,
const std::function< void(int)> &  onStatus 
)
private

Definition at line 567 of file Request.cpp.

567 {
568 bool atomar = true;
569
570 std::string absolutFileName = file;
571
572 if (std::filesystem::exists(absolutFileName)) {
573 std::error_code ec;
574 absolutFileName = std::filesystem::canonical(absolutFileName);
575
576 if (std::filesystem::is_regular_file(absolutFileName, ec) && !ec) {
577 core::file::FileReader::open(absolutFileName, [this, &absolutFileName, &onStatus, &atomar](int fd) {
578 onStatus(errno);
579
580 if (fd >= 0) {
581 if (httpMajor == 1) {
582 atomar = false;
583
584 set("Content-Type", web::http::MimeTypes::contentType(absolutFileName), false);
585 set("Last-Modified", httputils::file_mod_http_date(absolutFileName), false);
586 if (httpMinor == 1 && contentLength == 0) {
587 set("Transfer-Encoding", "chunked");
588 } else {
589 set("Content-Length", std::to_string(std::filesystem::file_size(absolutFileName) + contentLength));
590 }
591
593 }
594 } else {
595 executeEnd();
596 }
597 })->pipe(this);
598 } else {
599 errno = EINVAL;
600 onStatus(errno);
601 }
602 } else {
603 errno = ENOENT;
604 onStatus(errno);
605 }
606
607 return atomar;
608 }
static FileReader * open(const std::string &path, const std::function< void(int)> &callback)
void pipe(Source *source)
Definition Sink.cpp:58
static std::string contentType(const std::string &file)
Request & set(const std::string &field, const std::string &value, bool overwrite=true)
Definition Request.cpp:136
std::size_t contentLength
Definition Request.h:136
std::string file_mod_http_date(const std::string &filePath)

References web::http::client::Request::contentLength, web::http::MimeTypes::contentType(), executeEnd(), executeSendHeader(), httputils::file_mod_http_date(), web::http::client::Request::httpMajor, web::http::client::Request::httpMinor, core::file::FileReader::open(), core::pipe::Source::pipe(), and web::http::client::Request::set().

Referenced by web::http::client::commands::SendFileCommand::execute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ executeSendFragment()

bool web::http::client::MasterRequest::executeSendFragment ( const char *  chunk,
std::size_t  chunkLen 
)
private

Definition at line 696 of file Request.cpp.

696 {
698 socketContext->sendToPeer(to_hex_str(chunkLen).append("\r\n"));
699 }
700
701 socketContext->sendToPeer(chunk, chunkLen);
702 contentLengthSent += chunkLen;
703
705 socketContext->sendToPeer("\r\n");
706 contentLength += chunkLen;
707 }
708
709 return true;
710 }
void sendToPeer(const char *chunk, std::size_t chunkLen) const final
TransferEncoding transferEncoding
Definition Request.h:141
Request & append(const std::string &field, const std::string &value)
Definition Request.cpp:124
#define to_hex_str(int_val)
Definition Request.cpp:73

References web::http::Chunked, web::http::client::Request::contentLength, contentLengthSent, core::socket::stream::SocketContext::sendToPeer(), core::socket::SocketContext::sendToPeer(), web::http::client::Request::socketContext, and web::http::client::Request::transferEncoding.

Referenced by web::http::client::commands::SendFragmentCommand::execute(), onSourceData(), and requestDelivered().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ executeSendHeader()

bool web::http::client::MasterRequest::executeSendHeader ( )
private

Definition at line 664 of file Request.cpp.

664 {
665 const std::string httpVersion = "HTTP/" + std::to_string(httpMajor) + "." + std::to_string(httpMinor);
666
667 std::string queryString;
668 if (!queries.empty()) {
669 queryString += "?";
670 for (auto& [key, value] : queries) {
671 queryString += httputils::url_encode(key) + "=" + httputils::url_encode(value) + "&";
672 }
673 queryString.pop_back();
674 }
675
676 socketContext->sendToPeer(method + " " + url + queryString + " " + httpVersion + "\r\n");
677 socketContext->sendToPeer("Date: " + httputils::to_http_date() + "\r\n");
678
679 if (!headers.contains("Transfer-Encoding") && contentLength > 0) {
680 set("Content-Length", std::to_string(contentLength));
681 }
682
683 for (const auto& [field, value] : headers) {
684 socketContext->sendToPeer(std::string(field).append(":").append(value).append("\r\n"));
685 }
686
687 for (const auto& [name, value] : cookies) {
688 socketContext->sendToPeer(std::string("Cookie:").append(name).append("=").append(value).append("\r\n"));
689 }
690
691 socketContext->sendToPeer("\r\n");
692
693 return true;
694 }
CiStringMap< std::string > headers
Definition Request.h:132
CiStringMap< std::string > cookies
Definition Request.h:133
CiStringMap< std::string > queries
Definition Request.h:131
std::string to_http_date(struct tm *tm)
std::string url_encode(const std::string &text)

References web::http::client::Request::contentLength, web::http::client::Request::cookies, web::http::client::Request::headers, web::http::client::Request::httpMajor, web::http::client::Request::httpMinor, web::http::client::Request::method, web::http::client::Request::queries, core::socket::SocketContext::sendToPeer(), web::http::client::Request::set(), web::http::client::Request::socketContext, httputils::to_http_date(), web::http::client::Request::url, and httputils::url_encode().

Referenced by web::http::client::commands::SendHeaderCommand::execute(), executeSendFile(), and executeUpgrade().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ executeSse()

bool web::http::client::MasterRequest::executeSse ( )
private

Definition at line 660 of file Request.cpp.

660 {
661 return true;
662 }

Referenced by web::http::client::commands::SseCommand::execute().

Here is the caller graph for this function:

◆ executeUpgrade()

bool web::http::client::MasterRequest::executeUpgrade ( const std::string &  url,
const std::string &  protocols,
const std::function< void(bool)> &  onStatus 
)
private

Definition at line 610 of file Request.cpp.

610 {
611 const std::string connectionName = this->getSocketContext()->getSocketConnection()->getConnectionName();
612 this->url = url;
613
614 set("Connection", "Upgrade", true);
615 set("Upgrade", protocols, true);
616
617 web::http::client::SocketContextUpgradeFactory* socketContextUpgradeFactory =
619
620 if (socketContextUpgradeFactory != nullptr) {
621 LOG(DEBUG) << connectionName << " HTTP: "
622 << "SocketContextUpgradeFactory create success: " << socketContextUpgradeFactory->name();
623 LOG(DEBUG) << connectionName << " HTTP: Initiating upgrade: " << method << " " << url
624 << " HTTP/" + std::to_string(httpMajor) + "." + std::to_string(httpMinor);
625
626 } else {
627 LOG(DEBUG) << connectionName << " HTTP: "
628 << "SocketContextUpgradeFactory create failed: " << protocols;
629 LOG(DEBUG) << connectionName << " HTTP: Not initiating upgrade " << method << " " << url
630 << " HTTP/" + std::to_string(httpMajor) + "." + std::to_string(httpMinor);
631 }
632
633 LOG(DEBUG) << connectionName << " HTTP: Upgrade request:\n"
635 url,
636 "HTTP/" + std::to_string(httpMajor) + "." + std::to_string(httpMinor),
637 queries,
638 headers,
639 trailer,
640 cookies,
641 std::vector<char>());
642
643 onStatus(socketContextUpgradeFactory != nullptr);
644
645 if (socketContextUpgradeFactory != nullptr) {
647
648 socketContextUpgradeFactory->checkRefCount();
649 } else {
651 }
652
653 return true;
654 }
const std::string & getConnectionName() const
SocketConnection * getSocketConnection() const
CiStringMap< std::string > trailer
Definition Request.h:134
SocketContext * getSocketContext() const
Definition Request.cpp:114
SocketContextUpgradeFactory * select(const std::string &protocols, Request &req)
std::string toString(const std::string &method, const std::string &url, const std::string &version, const web::http::CiStringMap< std::string > &queries, const web::http::CiStringMap< std::string > &header, const web::http::CiStringMap< std::string > &trailer, const web::http::CiStringMap< std::string > &cookies, const std::vector< char > &body)

References web::http::client::SocketContextUpgradeFactory::checkRefCount(), core::socket::stream::SocketContext::close(), web::http::client::Request::cookies, executeSendHeader(), core::socket::stream::SocketConnection::getConnectionName(), core::socket::stream::SocketContext::getSocketConnection(), web::http::client::Request::getSocketContext(), web::http::client::Request::headers, web::http::client::Request::httpMajor, web::http::client::Request::httpMinor, web::http::client::SocketContextUpgradeFactorySelector::instance(), web::http::client::Request::method, web::http::SocketContextUpgradeFactory< RequestT, ResponseT >::name(), web::http::client::Request::queries, web::http::client::SocketContextUpgradeFactorySelector::select(), web::http::client::Request::set(), web::http::client::Request::socketContext, httputils::toString(), web::http::client::Request::trailer, and web::http::client::Request::url.

Referenced by web::http::client::commands::UpgradeCommand::execute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init()

◆ initiate()

bool web::http::client::MasterRequest::initiate ( const std::shared_ptr< MasterRequest > &  request)
private

Definition at line 543 of file Request.cpp.

543 {
544 bool error = false;
545 bool atomar = true;
546
547 for (RequestCommand* requestCommand : requestCommands) {
548 if (!error) {
549 this->onResponseParseError = requestCommand->onResponseParseError;
550 this->onResponseReceived = requestCommand->onResponseReceived;
551
552 atomar = requestCommand->execute(request);
553 error = requestCommand->getError();
554 }
555
556 delete requestCommand;
557 }
558 requestCommands.clear();
559
560 if (atomar && !error) {
562 }
563
564 return !error;
565 }

References web::http::client::RequestCommand::execute(), web::http::client::RequestCommand::getError(), onResponseParseError, web::http::client::RequestCommand::onResponseParseError, onResponseReceived, web::http::client::RequestCommand::onResponseReceived, requestCommands, and requestDelivered().

Referenced by web::http::client::SocketContext::initiateRequest().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ onSourceConnect()

void web::http::client::MasterRequest::onSourceConnect ( core::pipe::Source source)
overrideprivatevirtual

Implements core::pipe::Sink.

Definition at line 741 of file Request.cpp.

741 {
742 if (!masterRequest.expired()) {
744 source->start();
745 }
746 } else {
747 source->stop();
748 }
749 }
Source * source
Definition Sink.h:90
virtual void stop()=0
virtual void start()=0
bool streamToPeer(core::pipe::Source *source) const

References web::http::client::Request::masterRequest, web::http::client::Request::socketContext, core::pipe::Source::start(), core::pipe::Source::stop(), and core::socket::stream::SocketContext::streamToPeer().

Here is the call graph for this function:

◆ onSourceData()

void web::http::client::MasterRequest::onSourceData ( const char *  chunk,
std::size_t  chunkLen 
)
overrideprivatevirtual

Implements core::pipe::Sink.

Definition at line 751 of file Request.cpp.

751 {
752 executeSendFragment(chunk, chunkLen);
753 }
bool executeSendFragment(const char *chunk, std::size_t chunkLen)
Definition Request.cpp:696

References executeSendFragment().

Here is the call graph for this function:

◆ onSourceEof()

void web::http::client::MasterRequest::onSourceEof ( )
overrideprivatevirtual

Implements core::pipe::Sink.

Definition at line 755 of file Request.cpp.

755 {
756 if (!masterRequest.expired()) {
758
760 }
761 }

References web::http::client::Request::masterRequest, requestDelivered(), web::http::client::Request::socketContext, and core::socket::stream::SocketContext::streamEof().

Here is the call graph for this function:

◆ onSourceError()

void web::http::client::MasterRequest::onSourceError ( int  errnum)
overrideprivatevirtual

Implements core::pipe::Sink.

Definition at line 763 of file Request.cpp.

763 {
764 errno = errnum;
765
766 if (!masterRequest.expired()) {
769
771 }
772 }

References core::socket::stream::SocketContext::close(), web::http::client::Request::masterRequest, requestDelivered(), web::http::client::Request::socketContext, and core::socket::stream::SocketContext::streamEof().

Here is the call graph for this function:

◆ operator=() [1/2]

MasterRequest & web::http::client::MasterRequest::operator= ( MasterRequest &&  )
deletenoexcept

◆ operator=() [2/2]

MasterRequest & web::http::client::MasterRequest::operator= ( MasterRequest )
delete

◆ requestDelivered()

void web::http::client::MasterRequest::requestDelivered ( )
private

Definition at line 724 of file Request.cpp.

724 {
725 if (!masterRequest.expired()) {
727 executeSendFragment("", 0); // For transfer encoding chunked. Terminate the chunk sequence.
728
729 if (!trailer.empty()) {
730 for (auto& [field, value] : trailer) {
731 socketContext->sendToPeer(std::string(field).append(":").append(value).append("\r\n"));
732 }
733 socketContext->sendToPeer("\r\n");
734 }
735 }
736
738 }
739 }

References web::http::Chunked, web::http::client::Request::contentLength, contentLengthSent, executeSendFragment(), web::http::client::Request::masterRequest, web::http::client::SocketContext::requestDelivered(), core::socket::SocketContext::sendToPeer(), web::http::client::Request::socketContext, web::http::client::Request::trailer, and web::http::client::Request::transferEncoding.

Referenced by initiate(), onSourceEof(), and onSourceError().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ requestEventSource()

bool web::http::client::MasterRequest::requestEventSource ( const std::string &  url,
const std::function< std::size_t()> &  onServerSentEvent,
const std::function< void()> &  onOpen,
const std::function< void()> &  onError 
)

Definition at line 435 of file Request.cpp.

438 {
439 if (!masterRequest.expired()) {
440 const std::shared_ptr<MasterRequest> newRequest = std::make_shared<MasterRequest>(std::move(*this));
441
442 newRequest->url = url;
443 newRequest->httpMajor = 1;
444 newRequest->httpMinor = 1;
445
446 newRequest->set("Connection", "keep-alive", true);
447 newRequest->set("Accept", "text/event-stream", true);
448 newRequest->set("Cache-Control", "no-cache", true);
449
450 newRequest->sendHeader();
451 newRequest->requestCommands.push_back(new commands::SseCommand(
452 [masterRequest = this->masterRequest, onServerSentEvent, onOpen, onError](const std::shared_ptr<Request>& request,
453 const std::shared_ptr<Response>& response) {
454 if (!masterRequest.expired()) {
455 if (web::http::ciContains(response->headers["Content-Type"], "text/event-stream") &&
456 web::http::ciContains(request->header("Accept"), "text/event-stream")) {
457 masterRequest.lock()->getSocketContext()->setSseEventReceiver(onServerSentEvent);
458
459 const std::string connectionName = request->getSocketContext()->getSocketConnection()->getConnectionName();
460 onOpen();
461 } else {
462 masterRequest.lock()->getSocketContext()->shutdownWrite(true);
463 onError();
464 }
465 }
466 },
467 [masterRequest = this->masterRequest](const std::shared_ptr<Request>& request, const std::string& status) {
468 if (!masterRequest.expired()) {
469 LOG(DEBUG) << request->getSocketContext()->getSocketConnection()->getConnectionName()
470 << " error in response: " << status;
471 masterRequest.lock()->getSocketContext()->shutdownWrite(true);
472 }
473 }));
474
475 requestPrepared(newRequest);
476 }
477
478 return !masterRequest.expired();
479 }

References web::http::ciContains(), core::socket::stream::SocketConnection::getConnectionName(), core::socket::stream::SocketContext::getSocketConnection(), web::http::client::Request::getSocketContext(), web::http::client::Request::header(), web::http::client::Response::headers, web::http::client::Request::httpMajor, web::http::client::Request::httpMinor, web::http::client::Request::masterRequest, requestCommands, requestPrepared(), sendHeader(), web::http::client::Request::set(), web::http::client::SocketContext::setSseEventReceiver(), core::socket::stream::SocketContext::shutdownWrite(), web::http::client::commands::SseCommand::SseCommand(), and web::http::client::Request::url.

Here is the call graph for this function:

◆ requestPrepared()

void web::http::client::MasterRequest::requestPrepared ( const std::shared_ptr< MasterRequest > &  request)
private

Definition at line 712 of file Request.cpp.

712 {
714 }
void requestPrepared(const std::shared_ptr< MasterRequest > &request)

References web::http::client::SocketContext::requestPrepared(), and web::http::client::Request::socketContext.

Referenced by end(), requestEventSource(), send(), sendFile(), and upgrade().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ send() [1/2]

bool web::http::client::MasterRequest::send ( const char *  chunk,
std::size_t  chunkLen,
const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &  onResponseReceived,
const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &  onResponseParseError 
)

Definition at line 349 of file Request.cpp.

352 {
353 bool queued = true;
354
355 if (!masterRequest.expired()) {
356 const std::shared_ptr<MasterRequest> newRequest = std::make_shared<MasterRequest>(std::move(*this));
357
358 if (chunkLen > 0) {
359 newRequest->set("Content-Type", "application/octet-stream", false);
360 }
361
362 newRequest->sendHeader();
363 newRequest->sendFragment(chunk, chunkLen);
364
365 newRequest->requestCommands.push_back(new commands::EndCommand(onResponseReceived, onResponseParseError));
366
367 newRequest->requestPrepared(newRequest);
368
369 } else {
370 queued = false;
371 }
372
373 return queued;
374 }

References web::http::client::commands::EndCommand::EndCommand(), web::http::client::Request::masterRequest, requestCommands, requestPrepared(), sendFragment(), sendHeader(), and web::http::client::Request::set().

Referenced by send().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ send() [2/2]

bool web::http::client::MasterRequest::send ( const std::string &  chunk,
const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &  onResponseReceived,
const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &  onResponseParseError 
)

Definition at line 377 of file Request.cpp.

379 {
380 if (!chunk.empty()) {
381 set("Content-Type", "text/html; charset=utf-8", false);
382 }
383
384 return send(chunk.data(), chunk.size(), onResponseReceived, onResponseParseError);
385 }
bool send(const char *chunk, std::size_t chunkLen, const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &onResponseReceived, const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &onResponseParseError)
Definition Request.cpp:349

References send(), and web::http::client::Request::set().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendFile()

bool web::http::client::MasterRequest::sendFile ( const std::string &  file,
const std::function< void(int errnum)> &  onStatus,
const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &  onResponseReceived,
const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &  onResponseParseError 
)

Definition at line 481 of file Request.cpp.

485 {
486 bool queued = false;
487
488 if (!masterRequest.expired()) {
489 const std::shared_ptr<MasterRequest> newRequest = std::make_shared<MasterRequest>(std::move(*this));
490
491 newRequest->requestCommands.push_back(new commands::SendFileCommand(file, onStatus, onResponseReceived, onResponseParseError));
492
493 requestPrepared(newRequest);
494
495 queued = true;
496 }
497
498 return queued;
499 }

References web::http::client::Request::masterRequest, requestCommands, requestPrepared(), and web::http::client::commands::SendFileCommand::SendFileCommand().

Here is the call graph for this function:

◆ sendFragment() [1/2]

MasterRequest & web::http::client::MasterRequest::sendFragment ( const char *  chunk,
std::size_t  chunkLen 
)

Definition at line 509 of file Request.cpp.

509 {
510 if (!masterRequest.expired()) {
511 contentLength += chunkLen;
512
513 requestCommands.push_back(new commands::SendFragmentCommand(chunk, chunkLen));
514 }
515
516 return *this;
517 }

References web::http::client::Request::contentLength, web::http::client::Request::masterRequest, requestCommands, and web::http::client::commands::SendFragmentCommand::SendFragmentCommand().

Referenced by send(), and sendFragment().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendFragment() [2/2]

MasterRequest & web::http::client::MasterRequest::sendFragment ( const std::string &  data)

Definition at line 519 of file Request.cpp.

519 {
520 return sendFragment(data.data(), data.size());
521 }
MasterRequest & sendFragment(const char *chunk, std::size_t chunkLen)
Definition Request.cpp:509

References sendFragment().

Here is the call graph for this function:

◆ sendHeader()

MasterRequest & web::http::client::MasterRequest::sendHeader ( )

Definition at line 501 of file Request.cpp.

501 {
502 if (!masterRequest.expired()) {
503 requestCommands.push_back(new commands::SendHeaderCommand());
504 }
505
506 return *this;
507 }

References web::http::client::Request::masterRequest, requestCommands, and web::http::client::commands::SendHeaderCommand::SendHeaderCommand().

Referenced by end(), requestEventSource(), and send().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ upgrade()

bool web::http::client::MasterRequest::upgrade ( const std::string &  url,
const std::string &  protocols,
const std::function< void(bool)> &  onUpgradeInitiate,
const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &, bool)> &  onResponseReceived,
const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &  onResponseParseError 
)

Definition at line 387 of file Request.cpp.

392 {
393 if (!masterRequest.expired()) {
394 const std::shared_ptr<MasterRequest> newRequest = std::make_shared<MasterRequest>(std::move(*this));
395
396 newRequest->url = url;
397
398 newRequest->requestCommands.push_back(new commands::UpgradeCommand(
399 url,
400 protocols,
401 onUpgradeInitiate,
402 [onResponseReceived](const std::shared_ptr<Request>& request, const std::shared_ptr<Response>& response) {
403 if (request != nullptr) {
404 const std::string connectionName = request->getSocketContext()->getSocketConnection()->getConnectionName();
405
406 LOG(DEBUG) << connectionName << " HTTP upgrade: Response to upgrade request: " << request->method << " "
407 << request->url << " "
408 << "HTTP/" << request->httpMajor << "." << request->httpMinor << "\n"
409 << httputils::toString(response->httpVersion,
410 response->statusCode,
411 response->reason,
412 response->headers,
413 response->cookies,
414 response->body);
415
416 request->upgrade(response, [request, response, connectionName, &onResponseReceived](const std::string& name) {
417 LOG(DEBUG) << connectionName << " HTTP upgrade: bootstrap " << (!name.empty() ? "success" : "failed");
418 LOG(DEBUG) << " Protocol selected: " << name;
419 LOG(DEBUG) << " requested: " << request->header("upgrade");
420 LOG(DEBUG) << " Subprotocol selected: " << response->get("Sec-WebSocket-Protocol");
421 LOG(DEBUG) << " requested: " << request->header("Sec-WebSocket-Protocol");
422
423 onResponseReceived(request, response, !name.empty());
424 });
425 }
426 },
428
429 requestPrepared(newRequest);
430 }
431
432 return !masterRequest.expired();
433 }

References web::http::client::Response::body, web::http::client::Response::cookies, web::http::client::Response::get(), core::socket::stream::SocketConnection::getConnectionName(), core::socket::stream::SocketContext::getSocketConnection(), web::http::client::Request::getSocketContext(), web::http::client::Request::header(), web::http::client::Response::headers, web::http::client::Request::httpMajor, web::http::client::Request::httpMinor, web::http::client::Response::httpVersion, web::http::client::Request::masterRequest, web::http::client::Request::method, web::http::client::Response::reason, requestCommands, requestPrepared(), web::http::client::Response::statusCode, httputils::toString(), web::http::client::Request::upgrade(), web::http::client::commands::UpgradeCommand::UpgradeCommand(), and web::http::client::Request::url.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ commands::EndCommand

friend class commands::EndCommand
friend

Definition at line 197 of file Request.h.

◆ commands::SendFileCommand

friend class commands::SendFileCommand
friend

Definition at line 193 of file Request.h.

◆ commands::SendFragmentCommand

friend class commands::SendFragmentCommand
friend

Definition at line 194 of file Request.h.

◆ commands::SendHeaderCommand

friend class commands::SendHeaderCommand
friend

Definition at line 195 of file Request.h.

◆ commands::SseCommand

friend class commands::SseCommand
friend

Definition at line 198 of file Request.h.

◆ commands::UpgradeCommand

friend class commands::UpgradeCommand
friend

Definition at line 196 of file Request.h.

◆ SocketContext

friend class SocketContext
friend

Definition at line 225 of file Request.h.

Member Data Documentation

◆ contentLengthSent

std::size_t web::http::client::MasterRequest::contentLengthSent = 0
private

Definition at line 220 of file Request.h.

Referenced by executeSendFragment(), init(), MasterRequest(), and requestDelivered().

◆ onResponseParseError

std::function<void(const std::shared_ptr<Request>&, const std::string& message)> web::http::client::MasterRequest::onResponseParseError
private

Definition at line 223 of file Request.h.

Referenced by deliverResponseParseError(), initiate(), and MasterRequest().

◆ onResponseReceived

std::function<void(const std::shared_ptr<Request>&, const std::shared_ptr<Response>&)> web::http::client::MasterRequest::onResponseReceived
private

Definition at line 222 of file Request.h.

Referenced by deliverResponse(), initiate(), and MasterRequest().

◆ requestCommands

std::list<RequestCommand*> web::http::client::MasterRequest::requestCommands
private

The documentation for this class was generated from the following files: