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

#include <Request.h>

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

Public Member Functions

 Request (SocketContext *socketContext, const std::string &host)
 
 Request (Request &)=delete
 
 Request (Request &&) noexcept
 
Requestoperator= (Request &)=delete
 
Requestoperator= (Request &&) noexcept=delete
 
 ~Request () override
 
void setMasterRequest (const std::shared_ptr< Request > &masterRequest)
 
virtual void init ()
 
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)
 
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=responseParseError)
 
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=responseParseError)
 
bool upgrade (const std::string &url, const std::string &protocols, const std::function< void(const std::shared_ptr< Request > &, 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=responseParseError)
 
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=responseParseError)
 
RequestsendHeader ()
 
RequestsendFragment (const char *chunk, std::size_t chunkLen)
 
RequestsendFragment (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=responseParseError)
 
std::string header (const std::string &field)
 
const CiStringMap< std::string > & getQueries () const
 
const CiStringMap< std::string > & getHeaders () const
 
const CiStringMap< std::string > & getCookies () const
 
SocketContextgetSocketContext () const
 
- Public Member Functions inherited from core::pipe::Sink
 Sink (Sink &)=delete
 
Sinkoperator= (Sink &)=delete
 

Static Public Member Functions

static void responseParseError (const std::shared_ptr< Request > &request, const std::string &message)
 

Public Attributes

std::string hostFieldValue
 
std::string method = "GET"
 
std::string url = "/"
 
int httpMajor = 1
 
int httpMinor = 1
 

Protected Attributes

CiStringMap< std::string > queries
 
CiStringMap< std::string > headers
 
CiStringMap< std::string > cookies
 
CiStringMap< std::string > trailer
 

Private Member Functions

bool initiate (const std::shared_ptr< Request > &request)
 
void upgrade (const std::shared_ptr< Response > &response, const std::function< void(const std::string &)> &status)
 
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 executeEnd ()
 
bool executeSendHeader ()
 
bool executeSendFragment (const char *chunk, std::size_t chunkLen)
 
void requestPrepared ()
 
void requestDelivered ()
 
void deliverResponse (const std::shared_ptr< Request > &request, const std::shared_ptr< Response > &response)
 
void deliverResponseParseError (const std::shared_ptr< Request > &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
 
TransferEncoding transferEncoding = TransferEncoding::HTTP10
 
std::size_t contentLength = 0
 
std::size_t contentLengthSent = 0
 
ConnectionState connectionState = ConnectionState::Default
 
std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> onResponseReceived
 
std::function< void(const std::shared_ptr< Request > &request, const std::string &message)> onResponseParseError
 
std::weak_ptr< RequestmasterRequest
 
web::http::client::SocketContextsocketContext
 

Friends

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

Additional Inherited Members

- Protected Member Functions inherited from core::pipe::Sink
 Sink ()=default
 
 Sink (Sink &&) noexcept=default
 
Sinkoperator= (Sink &&) noexcept=default
 
virtual ~Sink ()
 
bool isStreaming ()
 
void stop ()
 

Detailed Description

Definition at line 76 of file Request.h.

Constructor & Destructor Documentation

◆ Request() [1/3]

web::http::client::Request::Request ( SocketContext socketContext,
const std::string &  host 
)
explicit

Definition at line 75 of file Request.cpp.

78 this->host(hostFieldValue);
79 }
web::http::client::SocketContext * socketContext
Definition Request.h:193
std::string hostFieldValue
Definition Request.h:166
Request & host(const std::string &hostFieldValue)
Definition Request.cpp:141

References host(), hostFieldValue, and socketContext.

Here is the call graph for this function:

◆ Request() [2/3]

web::http::client::Request::Request ( Request )
explicitdelete

◆ Request() [3/3]

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

Definition at line 81 of file Request.cpp.

82 : hostFieldValue(request.hostFieldValue) // NOLINT
83 , method(std::move(request.method))
84 , url(std::move(request.url))
85 , httpMajor(request.httpMajor)
86 , httpMinor(request.httpMinor)
87 , queries(std::move(request.queries))
88 , headers(std::move(request.headers))
89 , cookies(std::move(request.cookies))
90 , trailer(std::move(request.trailer))
91 , requestCommands(std::move(request.requestCommands))
92 , transferEncoding(request.transferEncoding)
93 , contentLength(request.contentLength)
94 , contentLengthSent(request.contentLengthSent)
95 , connectionState(request.connectionState)
96 , onResponseReceived(std::move(request.onResponseReceived))
97 , onResponseParseError(std::move(request.onResponseParseError))
98 , masterRequest(request.masterRequest) // NOLINT
99 , socketContext(request.socketContext) {
100 request.init();
101 }
CiStringMap< std::string > headers
Definition Request.h:174
TransferEncoding transferEncoding
Definition Request.h:181
ConnectionState connectionState
Definition Request.h:186
std::size_t contentLengthSent
Definition Request.h:184
CiStringMap< std::string > cookies
Definition Request.h:175
CiStringMap< std::string > trailer
Definition Request.h:176
std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> onResponseReceived
Definition Request.h:188
std::function< void(const std::shared_ptr< Request > &request, const std::string &message)> onResponseParseError
Definition Request.h:189
CiStringMap< std::string > queries
Definition Request.h:173
std::size_t contentLength
Definition Request.h:183
std::list< RequestCommand * > requestCommands
Definition Request.h:179
std::weak_ptr< Request > masterRequest
Definition Request.h:191

References connectionState, contentLength, contentLengthSent, cookies, headers, hostFieldValue, httpMajor, httpMinor, init(), masterRequest, method, onResponseParseError, onResponseReceived, queries, requestCommands, socketContext, trailer, transferEncoding, and url.

Here is the call graph for this function:

◆ ~Request()

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

Definition at line 103 of file Request.cpp.

103 {
104 for (const RequestCommand* requestCommand : requestCommands) {
105 delete requestCommand;
106 }
107
108 if (!masterRequest.expired() && Sink::isStreaming()) {
110 }
111 }

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

Here is the call graph for this function:

Member Function Documentation

◆ append()

Request & web::http::client::Request::append ( const std::string &  field,
const std::string &  value 
)

Definition at line 147 of file Request.cpp.

147 {
148 const std::map<std::string, std::string>::iterator it = headers.find(field);
149
150 if (it != headers.end()) {
151 set(field, it->second.append(", ").append(value));
152 } else {
153 set(field, value);
154 }
155
156 return *this;
157 }
Request & set(const std::string &field, const std::string &value, bool overwrite=true)
Definition Request.cpp:159

References headers, and set().

Here is the call graph for this function:

◆ cookie() [1/2]

Request & web::http::client::Request::cookie ( const std::map< std::string, std::string > &  cookies)

Definition at line 225 of file Request.cpp.

225 {
226 for (const auto& [name, value] : cookies) {
227 cookie(name, value);
228 }
229
230 return *this;
231 }
Request & cookie(const std::string &name, const std::string &value)
Definition Request.cpp:219

References cookie().

Here is the call graph for this function:

◆ cookie() [2/2]

Request & web::http::client::Request::cookie ( const std::string &  name,
const std::string &  value 
)

Definition at line 219 of file Request.cpp.

219 {
220 cookies.insert({name, value});
221
222 return *this;
223 }

References cookies.

Referenced by cookie().

Here is the caller graph for this function:

◆ deliverResponse()

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

Definition at line 620 of file Request.cpp.

620 {
621 onResponseReceived(request, response);
622 }

References onResponseReceived.

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

Here is the caller graph for this function:

◆ deliverResponseParseError()

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

Definition at line 624 of file Request.cpp.

624 {
625 onResponseParseError(request, message);
626 }

References onResponseParseError.

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

Here is the caller graph for this function:

◆ end()

bool web::http::client::Request::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 = responseParseError 
)

Definition at line 431 of file Request.cpp.

432 {
433 bool queued = true;
434
435 if (!masterRequest.expired()) {
438
439 sendHeader();
440
441 requestCommands.push_back(new commands::EndCommand());
442
444 } else {
445 queued = false;
446 }
447
448 return queued;
449 }

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

Here is the call graph for this function:

◆ executeEnd()

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

Definition at line 563 of file Request.cpp.

563 { // NOLINT
564 return true;
565 }

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

Here is the caller graph for this function:

◆ executeSendFile()

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

Definition at line 476 of file Request.cpp.

476 {
477 bool atomar = true;
478
479 std::string absolutFileName = file;
480
481 if (std::filesystem::exists(absolutFileName)) {
482 std::error_code ec;
483 absolutFileName = std::filesystem::canonical(absolutFileName);
484
485 if (std::filesystem::is_regular_file(absolutFileName, ec) && !ec) {
486 core::file::FileReader::open(absolutFileName)->pipe(this, [this, &atomar, &absolutFileName, &onStatus](int errnum) {
487 errno = errnum;
488 onStatus(errnum);
489
490 if (errnum == 0) {
491 if (httpMajor == 1) {
492 atomar = false;
493
494 set("Content-Type", web::http::MimeTypes::contentType(absolutFileName), false);
495 set("Last-Modified", httputils::file_mod_http_date(absolutFileName), false);
496 if (httpMinor == 1 && contentLength == 0) {
497 set("Transfer-Encoding", "chunked");
498 } else {
499 set("Content-Length", std::to_string(std::filesystem::file_size(absolutFileName) + contentLength));
500 }
501
503 }
504 }
505 });
506 } else {
507 errno = EINVAL;
508 onStatus(errno);
509 }
510 } else {
511 errno = ENOENT;
512 onStatus(errno);
513 }
514
515 return atomar;
516 }
static FileReader * open(const std::string &path)
void pipe(Sink *sink, const std::function< void(int)> &callback)
Definition Source.cpp:60
static std::string contentType(const std::string &file)
std::string file_mod_http_date(const std::string &filePath)

References contentLength, web::http::MimeTypes::contentType(), executeSendHeader(), httputils::file_mod_http_date(), httpMajor, httpMinor, core::file::FileReader::open(), core::pipe::Source::pipe(), and 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::Request::executeSendFragment ( const char *  chunk,
std::size_t  chunkLen 
)
private

Definition at line 599 of file Request.cpp.

599 {
601 socketContext->sendToPeer(to_hex_str(chunkLen).append("\r\n"));
602 }
603
604 socketContext->sendToPeer(chunk, chunkLen);
605 contentLengthSent += chunkLen;
606
608 socketContext->sendToPeer("\r\n");
609 contentLength += chunkLen;
610 }
611
612 return true;
613 }
void sendToPeer(const char *chunk, std::size_t chunkLen) const final
Request & append(const std::string &field, const std::string &value)
Definition Request.cpp:147
#define to_hex_str(int_val)
Definition Request.cpp:71

References web::http::Chunked, contentLength, contentLengthSent, core::socket::stream::SocketContext::sendToPeer(), core::socket::SocketContext::sendToPeer(), socketContext, and 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::Request::executeSendHeader ( )
private

Definition at line 567 of file Request.cpp.

567 {
568 const std::string httpVersion = "HTTP/" + std::to_string(httpMajor) + "." + std::to_string(httpMinor);
569
570 std::string queryString;
571 if (!queries.empty()) {
572 queryString += "?";
573 for (auto& [key, value] : queries) {
574 queryString += httputils::url_encode(key) + "=" + httputils::url_encode(value) + "&";
575 }
576 queryString.pop_back();
577 }
578
579 socketContext->sendToPeer(method + " " + url + queryString + " " + httpVersion + "\r\n");
580 socketContext->sendToPeer("Date: " + httputils::to_http_date() + "\r\n");
581
582 if (!headers.contains("Transfer-Encoding") && contentLength > 0) {
583 set("Content-Length", std::to_string(contentLength));
584 }
585
586 for (const auto& [field, value] : headers) {
587 socketContext->sendToPeer(std::string(field).append(":").append(value).append("\r\n"));
588 }
589
590 for (const auto& [name, value] : cookies) {
591 socketContext->sendToPeer(std::string("Cookie:").append(name).append("=").append(value).append("\r\n"));
592 }
593
594 socketContext->sendToPeer("\r\n");
595
596 return true;
597 }
std::string to_http_date(struct tm *tm)
std::string url_encode(const std::string &text)

References contentLength, cookies, headers, httpMajor, httpMinor, method, queries, core::socket::SocketContext::sendToPeer(), set(), socketContext, httputils::to_http_date(), 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:

◆ executeUpgrade()

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

Definition at line 518 of file Request.cpp.

518 {
519 const std::string connectionName = this->getSocketContext()->getSocketConnection()->getConnectionName();
520 this->url = url;
521
522 set("Connection", "Upgrade", true);
523 set("Upgrade", protocols, true);
524
525 web::http::client::SocketContextUpgradeFactory* socketContextUpgradeFactory =
527
528 if (socketContextUpgradeFactory != nullptr) {
529 LOG(DEBUG) << connectionName << " HTTP: "
530 << "SocketContextUpgradeFactory create success: " << socketContextUpgradeFactory->name();
531 LOG(DEBUG) << connectionName << " HTTP: Initiating upgrade: " << method << " " << url
532 << " HTTP/" + std::to_string(httpMajor) + "." + std::to_string(httpMinor);
533
534 } else {
535 LOG(DEBUG) << connectionName << " HTTP: "
536 << "SocketContextUpgradeFactory create failed: " << protocols;
537 LOG(DEBUG) << connectionName << " HTTP: Not initiating upgrade " << method << " " << url
538 << " HTTP/" + std::to_string(httpMajor) + "." + std::to_string(httpMinor);
539 }
540
541 LOG(DEBUG) << connectionName << " HTTP: Upgrade request:\n"
543 url,
544 "HTTP/" + std::to_string(httpMajor) + "." + std::to_string(httpMinor),
545 queries,
546 headers,
547 cookies,
548 std::vector<char>());
549
550 onStatus(socketContextUpgradeFactory != nullptr);
551
552 if (socketContextUpgradeFactory != nullptr) {
554
555 socketContextUpgradeFactory->checkRefCount();
556 } else {
558 }
559
560 return true;
561 }
const std::string & getConnectionName() const
SocketConnection * getSocketConnection() const
SocketContext * getSocketContext() const
Definition Request.cpp:694
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 > &cookies, const std::vector< char > &body)

References web::http::client::SocketContextUpgradeFactory::checkRefCount(), core::socket::stream::SocketContext::close(), cookies, executeSendHeader(), core::socket::stream::SocketConnection::getConnectionName(), core::socket::stream::SocketContext::getSocketConnection(), getSocketContext(), headers, httpMajor, httpMinor, web::http::client::SocketContextUpgradeFactorySelector::instance(), method, web::http::SocketContextUpgradeFactory< RequestT, ResponseT >::name(), queries, web::http::client::SocketContextUpgradeFactorySelector::select(), set(), socketContext, httputils::toString(), and 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:

◆ getCookies()

const web::http::CiStringMap< std::string > & web::http::client::Request::getCookies ( ) const

Definition at line 690 of file Request.cpp.

690 {
691 return cookies;
692 }

References cookies.

◆ getHeaders()

const web::http::CiStringMap< std::string > & web::http::client::Request::getHeaders ( ) const

Definition at line 686 of file Request.cpp.

686 {
687 return headers;
688 }

References headers.

◆ getQueries()

const web::http::CiStringMap< std::string > & web::http::client::Request::getQueries ( ) const

Definition at line 682 of file Request.cpp.

682 {
683 return queries;
684 }

References queries.

◆ getSocketContext()

SocketContext * web::http::client::Request::getSocketContext ( ) const

Definition at line 694 of file Request.cpp.

694 {
695 return socketContext;
696 }

References socketContext.

Referenced by executeUpgrade(), main(), responseParseError(), and upgrade().

Here is the caller graph for this function:

◆ header()

std::string web::http::client::Request::header ( const std::string &  field)

Definition at line 678 of file Request.cpp.

678 {
679 return headers.contains(field) ? headers[field] : "";
680 }

References headers.

Referenced by web::websocket::client::SocketContextUpgradeFactory::create(), main(), web::http::client::SocketContext::requestPrepared(), upgrade(), and upgrade().

Here is the caller graph for this function:

◆ host()

Request & web::http::client::Request::host ( const std::string &  hostFieldValue)

Definition at line 141 of file Request.cpp.

141 {
142 set("Host", hostFieldValue);
143
144 return *this;
145 }

References set().

Referenced by init(), and Request().

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

◆ init()

void web::http::client::Request::init ( )
virtual

Definition at line 117 of file Request.cpp.

117 {
118 method = "GET";
119 url = "/";
120 httpMajor = 1;
121 httpMinor = 1;
122 queries.clear();
123 headers.clear();
124 cookies.clear();
125 trailer.clear();
126 for (const RequestCommand* requestCommand : requestCommands) {
127 delete requestCommand;
128 }
129 requestCommands.clear();
131 contentLength = 0;
134 onResponseReceived = nullptr;
135 onResponseParseError = nullptr;
136
137 this->host(hostFieldValue);
138 set("X-Powered-By", "snode.c");
139 }

References connectionState, contentLength, contentLengthSent, cookies, web::http::Default, headers, host(), hostFieldValue, web::http::HTTP10, httpMajor, httpMinor, method, onResponseParseError, onResponseReceived, queries, requestCommands, set(), trailer, transferEncoding, and url.

Referenced by Request(), and requestPrepared().

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

◆ initiate()

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

Definition at line 451 of file Request.cpp.

451 {
452 bool error = false;
453 bool atomar = true;
454
455 for (RequestCommand* requestCommand : requestCommands) {
456 if (!error) {
457 const bool atomarCommand = requestCommand->execute(request);
458 if (atomar) {
459 atomar = atomarCommand;
460 }
461
462 error = requestCommand->getError();
463 }
464
465 delete requestCommand;
466 }
467 requestCommands.clear();
468
469 if (atomar && (!error || contentLengthSent != 0)) {
471 }
472
473 return !error || contentLengthSent != 0;
474 }

References contentLengthSent, web::http::client::RequestCommand::execute(), web::http::client::RequestCommand::getError(), 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::Request::onSourceConnect ( core::pipe::Source source)
overrideprivatevirtual

Implements core::pipe::Sink.

Definition at line 645 of file Request.cpp.

645 {
646 if (!masterRequest.expired()) {
648 source->start();
649 }
650 } else {
651 source->stop();
652 }
653 }
Source * source
Definition Sink.h:90
virtual void stop()=0
virtual void start()=0
bool streamToPeer(core::pipe::Source *source) const

References masterRequest, 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::Request::onSourceData ( const char *  chunk,
std::size_t  chunkLen 
)
overrideprivatevirtual

Implements core::pipe::Sink.

Definition at line 655 of file Request.cpp.

655 {
656 executeSendFragment(chunk, chunkLen);
657 }
bool executeSendFragment(const char *chunk, std::size_t chunkLen)
Definition Request.cpp:599

References executeSendFragment().

Here is the call graph for this function:

◆ onSourceEof()

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

Implements core::pipe::Sink.

Definition at line 659 of file Request.cpp.

659 {
660 if (!masterRequest.expired()) {
662
664 }
665 }

References masterRequest, requestDelivered(), socketContext, and core::socket::stream::SocketContext::streamEof().

Here is the call graph for this function:

◆ onSourceError()

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

Implements core::pipe::Sink.

Definition at line 667 of file Request.cpp.

667 {
668 errno = errnum;
669
670 if (!masterRequest.expired()) {
673
675 }
676 }

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

Here is the call graph for this function:

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ query()

Request & web::http::client::Request::query ( const std::string &  key,
const std::string &  value 
)

Definition at line 233 of file Request.cpp.

233 {
234 queries.insert({key, value});
235
236 return *this;
237 }

References queries.

◆ requestDelivered()

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

Definition at line 628 of file Request.cpp.

628 {
629 if (!masterRequest.expired()) {
631 executeSendFragment("", 0); // For transfer encoding chunked. Terminate the chunk sequence.
632
633 if (!trailer.empty()) {
634 for (auto& [field, value] : trailer) {
635 socketContext->sendToPeer(std::string(field).append(":").append(value).append("\r\n"));
636 }
637 socketContext->sendToPeer("\r\n");
638 }
639 }
640
642 }
643 }
void requestDelivered(Request &&request, bool success)

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

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

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

◆ requestPrepared()

void web::http::client::Request::requestPrepared ( )
private

Definition at line 615 of file Request.cpp.

615 {
616 socketContext->requestPrepared(std::move(*this));
617 init();
618 }
void requestPrepared(Request &&request)

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

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

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

◆ responseParseError()

void web::http::client::Request::responseParseError ( const std::shared_ptr< Request > &  request,
const std::string &  message 
)
static

Definition at line 258 of file Request.cpp.

258 {
259 LOG(WARNING) << request->getSocketContext()->getSocketConnection()->getConnectionName()
260 << " HTTP: Response parse error: " << request->method << " " << request->url << " "
261 << "HTTP/" << request->httpMajor << "." << request->httpMinor << ": " << message;
262 }

References core::socket::stream::SocketConnection::getConnectionName(), core::socket::stream::SocketContext::getSocketConnection(), getSocketContext(), httpMajor, httpMinor, method, and url.

Here is the call graph for this function:

◆ send() [1/2]

bool web::http::client::Request::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 = responseParseError 
)

Definition at line 264 of file Request.cpp.

267 {
268 bool queued = true;
269
270 if (!masterRequest.expired()) {
273
274 if (chunkLen > 0) {
275 set("Content-Type", "application/octet-stream", false);
276 }
277
278 sendHeader();
279 sendFragment(chunk, chunkLen);
280
281 requestCommands.push_back(new commands::EndCommand());
282
284 } else {
285 queued = false;
286 }
287
288 return queued;
289 }
Request & sendFragment(const char *chunk, std::size_t chunkLen)
Definition Request.cpp:417

References web::http::client::commands::EndCommand::EndCommand(), masterRequest, onResponseParseError, onResponseReceived, requestCommands, requestPrepared(), sendFragment(), sendHeader(), and 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::Request::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 = responseParseError 
)

Definition at line 291 of file Request.cpp.

293 {
294 if (!chunk.empty()) {
295 set("Content-Type", "text/html; charset=utf-8", false);
296 }
297
298 return send(chunk.data(), chunk.size(), onResponseReceived, onResponseParseError);
299 }
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=responseParseError)
Definition Request.cpp:264

References send(), and 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::Request::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 = responseParseError 
)

Definition at line 389 of file Request.cpp.

392 {
393 bool queued = false;
394
395 if (!masterRequest.expired()) {
398
399 requestCommands.push_back(new commands::SendFileCommand(file, onStatus));
400
402
403 queued = true;
404 }
405
406 return queued;
407 }

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

Here is the call graph for this function:

◆ sendFragment() [1/2]

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

Definition at line 417 of file Request.cpp.

417 {
418 if (!masterRequest.expired()) {
419 contentLength += chunkLen;
420
421 requestCommands.push_back(new commands::SendFragmentCommand(chunk, chunkLen));
422 }
423
424 return *this;
425 }

References contentLength, 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]

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

Definition at line 427 of file Request.cpp.

427 {
428 return sendFragment(data.data(), data.size());
429 }

References sendFragment().

Here is the call graph for this function:

◆ sendHeader()

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

Definition at line 409 of file Request.cpp.

409 {
410 if (!masterRequest.expired()) {
411 requestCommands.push_back(new commands::SendHeaderCommand());
412 }
413
414 return *this;
415 }

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

Referenced by end(), and send().

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

◆ set() [1/2]

Request & web::http::client::Request::set ( const std::map< std::string, std::string > &  headers,
bool  overwrite = true 
)

Definition at line 205 of file Request.cpp.

205 {
206 for (const auto& [field, value] : headers) {
207 set(field, value, overwrite);
208 }
209
210 return *this;
211 }

References set().

Here is the call graph for this function:

◆ set() [2/2]

Request & web::http::client::Request::set ( const std::string &  field,
const std::string &  value,
bool  overwrite = true 
)

Definition at line 159 of file Request.cpp.

159 {
160 if (!value.empty()) {
161 if (overwrite) {
162 headers.insert_or_assign(field, value);
163 } else {
164 headers.insert({field, value});
165 }
166
167 if (web::http::ciEquals(field, "Connection")) {
168 if (web::http::ciContains(headers[field], "close")) {
170 } else if (web::http::ciContains(headers[field], "keep-alive")) {
172 }
173 } else if (web::http::ciEquals(field, "Content-Length")) {
174 contentLength = std::stoul(value);
176 headers.erase("Transfer-Encoding");
177 } else if (web::http::ciEquals(field, "Transfer-Encoding")) {
178 if (web::http::ciContains(headers[field], "chunked")) {
180 headers.erase("Content-Length");
181 }
182 if (web::http::ciContains(headers[field], "compressed")) {
183 }
184 if (web::http::ciContains(headers[field], "deflate")) {
185 }
186 if (web::http::ciContains(headers[field], "gzip")) {
187 }
188 } else if (web::http::ciEquals(field, "Content-Encoding")) {
189 if (web::http::ciContains(headers[field], "compressed")) {
190 }
191 if (web::http::ciContains(headers[field], "deflate")) {
192 }
193 if (web::http::ciContains(headers[field], "gzip")) {
194 }
195 if (web::http::ciContains(headers[field], "br")) {
196 }
197 }
198 } else {
199 headers.erase(field);
200 }
201
202 return *this;
203 }
bool ciEquals(const std::string &str1, const std::string &str2)
bool ciContains(const std::string &str1, const std::string &str2)

References web::http::Chunked, web::http::ciContains(), web::http::ciEquals(), web::http::Close, connectionState, contentLength, headers, web::http::Identity, web::http::Keep, and transferEncoding.

Referenced by append(), executeSendFile(), executeSendHeader(), executeUpgrade(), host(), init(), main(), web::websocket::client::SocketContextUpgradeFactory::prepare(), send(), send(), set(), and setTrailer().

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

◆ setMasterRequest()

void web::http::client::Request::setMasterRequest ( const std::shared_ptr< Request > &  masterRequest)

Definition at line 113 of file Request.cpp.

113 {
115 }

References masterRequest.

◆ setTrailer()

Request & web::http::client::Request::setTrailer ( const std::string &  field,
const std::string &  value,
bool  overwrite = true 
)

Definition at line 239 of file Request.cpp.

239 {
240 if (!value.empty()) {
241 if (overwrite) {
242 trailer.insert_or_assign(field, value);
243 } else {
244 trailer.insert({field, value});
245 }
246 if (!headers.contains("Trailer")) {
247 set("Trailer", field);
248 } else {
249 headers["Trailer"].append("," + field);
250 }
251 } else {
252 trailer.erase(field);
253 }
254
255 return *this;
256 }

References headers, set(), and trailer.

Here is the call graph for this function:

◆ type()

Request & web::http::client::Request::type ( const std::string &  type)

Definition at line 213 of file Request.cpp.

213 {
214 headers.insert({"Content-Type", type});
215
216 return *this;
217 }
Request & type(const std::string &type)
Definition Request.cpp:213

References headers.

Referenced by main().

Here is the caller graph for this function:

◆ upgrade() [1/2]

void web::http::client::Request::upgrade ( const std::shared_ptr< Response > &  response,
const std::function< void(const std::string &)> &  status 
)
private

Definition at line 338 of file Request.cpp.

338 {
339 const std::string connectionName = socketContext->getSocketConnection()->getConnectionName();
340
341 std::string name;
342
343 if (!masterRequest.expired()) {
344 if (response != nullptr) {
345 if (web::http::ciContains(response->get("connection"), "Upgrade")) {
346 SocketContextUpgradeFactory* socketContextUpgradeFactory =
348
349 if (socketContextUpgradeFactory != nullptr) {
350 name = socketContextUpgradeFactory->name();
351
352 LOG(DEBUG) << connectionName << " HTTP upgrade: SocketContextUpgradeFactory create success for: " << name;
353
354 core::socket::stream::SocketContext* socketContextUpgrade =
355 socketContextUpgradeFactory->create(socketContext->getSocketConnection());
356
357 if (socketContextUpgrade != nullptr) {
358 LOG(DEBUG) << connectionName << " HTTP upgrade: SocketContextUpgrade create success for: " << name;
359
360 socketContext->switchSocketContext(socketContextUpgrade);
361 } else {
362 LOG(DEBUG) << connectionName << " HTTP upgrade: SocketContextUpgrade create failed for: " << name;
363
365 }
366 } else {
367 LOG(DEBUG) << connectionName
368 << " HTTP upgrade: SocketContextUpgradeFactory not supported by server: " << header("upgrade");
369
371 }
372 } else {
373 LOG(DEBUG) << connectionName << " HTTP upgrade: No upgrade requested";
374
376 }
377 } else {
378 LOG(ERROR) << connectionName << " HTTP upgrade: Response has gone away";
379
381 }
382 } else {
383 LOG(ERROR) << connectionName << " HTTP upgrade: Unexpected disconnect";
384 }
385
386 status(name);
387 }
virtual void switchSocketContext(SocketContext *newSocketContext)
std::string header(const std::string &field)
Definition Request.cpp:678

References web::http::ciContains(), core::socket::stream::SocketContext::close(), web::http::SocketContextUpgradeFactory< RequestT, ResponseT >::create(), web::http::client::Response::get(), core::socket::stream::SocketConnection::getConnectionName(), core::socket::stream::SocketContext::getSocketConnection(), header(), web::http::client::SocketContextUpgradeFactorySelector::instance(), masterRequest, web::http::SocketContextUpgradeFactory< RequestT, ResponseT >::name(), web::http::client::SocketContextUpgradeFactorySelector::select(), socketContext, and core::socket::stream::SocketContext::switchSocketContext().

Referenced by upgrade().

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

◆ upgrade() [2/2]

bool web::http::client::Request::upgrade ( const std::string &  url,
const std::string &  protocols,
const std::function< void(const std::shared_ptr< Request > &, 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 = responseParseError 
)

Definition at line 302 of file Request.cpp.

306 {
307 if (!masterRequest.expired()) {
308 this->url = url;
309
311
312 this->onResponseReceived = [onResponseReceived](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
313 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
314
315 LOG(DEBUG) << connectionName << " HTTP: Response to upgrade request: " << req->method << " " << req->url << " " << "HTTP/"
316 << req->httpMajor << "." << req->httpMinor << "\n"
317 << httputils::toString(res->httpVersion, res->statusCode, res->reason, res->headers, res->cookies, res->body);
318
319 req->upgrade(res, [req, res, connectionName, &onResponseReceived](const std::string& name) {
320 LOG(DEBUG) << connectionName << " HTTP: Upgrade bootstrap " << (!name.empty() ? "success" : "failed");
321 LOG(DEBUG) << " Protocol selected: " << name;
322 LOG(DEBUG) << " requested: " << req->header("upgrade");
323 LOG(DEBUG) << " Subprotocol selected: " << res->get("Sec-WebSocket-Protocol");
324 LOG(DEBUG) << " requested: " << req->header("Sec-WebSocket-Protocol");
325
326 onResponseReceived(req, res, !name.empty());
327 });
328 };
329
330 requestCommands.push_back(new commands::UpgradeCommand(url, protocols, onUpgradeInitiate));
331
333 }
334
335 return !masterRequest.expired();
336 }

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(), getSocketContext(), header(), web::http::client::Response::headers, httpMajor, httpMinor, web::http::client::Response::httpVersion, masterRequest, method, onResponseParseError, onResponseReceived, web::http::client::Response::reason, requestCommands, requestPrepared(), web::http::client::Response::statusCode, httputils::toString(), upgrade(), web::http::client::commands::UpgradeCommand::UpgradeCommand(), and 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 138 of file Request.h.

◆ commands::SendFileCommand

friend class commands::SendFileCommand
friend

Definition at line 134 of file Request.h.

◆ commands::SendFragmentCommand

friend class commands::SendFragmentCommand
friend

Definition at line 135 of file Request.h.

◆ commands::SendHeaderCommand

friend class commands::SendHeaderCommand
friend

Definition at line 136 of file Request.h.

◆ commands::UpgradeCommand

friend class commands::UpgradeCommand
friend

Definition at line 137 of file Request.h.

◆ SocketContext

friend class SocketContext
friend

Definition at line 195 of file Request.h.

Member Data Documentation

◆ connectionState

ConnectionState web::http::client::Request::connectionState = ConnectionState::Default
private

Definition at line 186 of file Request.h.

Referenced by init(), Request(), and set().

◆ contentLength

std::size_t web::http::client::Request::contentLength = 0
private

◆ contentLengthSent

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

Definition at line 184 of file Request.h.

Referenced by executeSendFragment(), init(), initiate(), Request(), and requestDelivered().

◆ cookies

CiStringMap<std::string> web::http::client::Request::cookies
protected

Definition at line 175 of file Request.h.

Referenced by cookie(), executeSendHeader(), executeUpgrade(), getCookies(), init(), and Request().

◆ headers

CiStringMap<std::string> web::http::client::Request::headers
protected

◆ hostFieldValue

std::string web::http::client::Request::hostFieldValue

Definition at line 166 of file Request.h.

Referenced by init(), Request(), and Request().

◆ httpMajor

◆ httpMinor

◆ masterRequest

std::weak_ptr<Request> web::http::client::Request::masterRequest
private

◆ method

◆ onResponseParseError

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

Definition at line 189 of file Request.h.

Referenced by deliverResponseParseError(), end(), init(), Request(), send(), sendFile(), and upgrade().

◆ onResponseReceived

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

Definition at line 188 of file Request.h.

Referenced by deliverResponse(), end(), init(), Request(), send(), sendFile(), and upgrade().

◆ queries

CiStringMap<std::string> web::http::client::Request::queries
protected

Definition at line 173 of file Request.h.

Referenced by executeSendHeader(), executeUpgrade(), getQueries(), init(), query(), and Request().

◆ requestCommands

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

Definition at line 179 of file Request.h.

Referenced by end(), init(), initiate(), Request(), send(), sendFile(), sendFragment(), sendHeader(), upgrade(), and ~Request().

◆ socketContext

◆ trailer

CiStringMap<std::string> web::http::client::Request::trailer
protected

Definition at line 176 of file Request.h.

Referenced by init(), Request(), requestDelivered(), and setTrailer().

◆ transferEncoding

TransferEncoding web::http::client::Request::transferEncoding = TransferEncoding::HTTP10
private

Definition at line 181 of file Request.h.

Referenced by executeSendFragment(), init(), Request(), requestDelivered(), and set().

◆ url


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