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 (web::http::client::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 > &, const std::shared_ptr< Response > &)> &onResponseReceived, const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &onResponseParseError=responseParseError)
 
void upgrade (const std::shared_ptr< Response > &response, const std::function< void(const std::string &)> &status)
 
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
 
web::http::client::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 ()
 
bool executeSendFile (const std::string &file, const std::function< void(int)> &onStatus)
 
bool executeUpgrade (const std::string &url, const std::string &protocols)
 
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 ( web::http::client::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:191
std::string hostFieldValue
Definition Request.h:164
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:172
TransferEncoding transferEncoding
Definition Request.h:179
ConnectionState connectionState
Definition Request.h:184
std::size_t contentLengthSent
Definition Request.h:182
CiStringMap< std::string > cookies
Definition Request.h:173
CiStringMap< std::string > trailer
Definition Request.h:174
std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> onResponseReceived
Definition Request.h:186
std::function< void(const std::shared_ptr< Request > &request, const std::string &message)> onResponseParseError
Definition Request.h:187
CiStringMap< std::string > queries
Definition Request.h:171
std::size_t contentLength
Definition Request.h:181
std::list< RequestCommand * > requestCommands
Definition Request.h:177
std::weak_ptr< Request > masterRequest
Definition Request.h:189

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 580 of file Request.cpp.

580 {
581 onResponseReceived(request, response);
582 }

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 584 of file Request.cpp.

584 {
585 onResponseParseError(request, message);
586 }

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 416 of file Request.cpp.

417 {
418 bool queued = true;
419
420 if (!masterRequest.expired()) {
423
424 sendHeader();
425
426 requestCommands.push_back(new commands::EndCommand());
427
429 } else {
430 queued = false;
431 }
432
433 return queued;
434 }

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 523 of file Request.cpp.

523 { // NOLINT
524 return true;
525 }

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 461 of file Request.cpp.

461 {
462 bool atomar = true;
463
464 std::string absolutFileName = file;
465
466 if (std::filesystem::exists(absolutFileName)) {
467 std::error_code ec;
468 absolutFileName = std::filesystem::canonical(absolutFileName);
469
470 if (std::filesystem::is_regular_file(absolutFileName, ec) && !ec) {
471 core::file::FileReader::open(absolutFileName)->pipe(this, [this, &atomar, &absolutFileName, &onStatus](int errnum) {
472 errno = errnum;
473 onStatus(errnum);
474
475 if (errnum == 0) {
476 if (httpMajor == 1) {
477 atomar = false;
478
479 set("Content-Type", web::http::MimeTypes::contentType(absolutFileName), false);
480 set("Last-Modified", httputils::file_mod_http_date(absolutFileName), false);
481 if (httpMinor == 1 && contentLength == 0) {
482 set("Transfer-Encoding", "chunked");
483 } else {
484 set("Content-Length", std::to_string(std::filesystem::file_size(absolutFileName) + contentLength));
485 }
486
488 }
489 }
490 });
491 } else {
492 errno = EINVAL;
493 onStatus(errno);
494 }
495 } else {
496 errno = ENOENT;
497 onStatus(errno);
498 }
499
500 return atomar;
501 }
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 559 of file Request.cpp.

559 {
561 socketContext->sendToPeer(to_hex_str(chunkLen).append("\r\n"));
562 }
563
564 socketContext->sendToPeer(chunk, chunkLen);
565 contentLengthSent += chunkLen;
566
568 socketContext->sendToPeer("\r\n");
569 contentLength += chunkLen;
570 }
571
572 return true;
573 }
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 527 of file Request.cpp.

527 {
528 const std::string httpVersion = "HTTP/" + std::to_string(httpMajor) + "." + std::to_string(httpMinor);
529
530 std::string queryString;
531 if (!queries.empty()) {
532 queryString += "?";
533 for (auto& [key, value] : queries) {
534 queryString += httputils::url_encode(key) + "=" + httputils::url_encode(value) + "&";
535 }
536 queryString.pop_back();
537 }
538
539 socketContext->sendToPeer(method + " " + url + queryString + " " + httpVersion + "\r\n");
540 socketContext->sendToPeer("Date: " + httputils::to_http_date() + "\r\n");
541
542 if (!headers.contains("Transfer-Encoding") && contentLength > 0) {
543 set("Content-Length", std::to_string(contentLength));
544 }
545
546 for (const auto& [field, value] : headers) {
547 socketContext->sendToPeer(std::string(field).append(":").append(value).append("\r\n"));
548 }
549
550 for (const auto& [name, value] : cookies) {
551 socketContext->sendToPeer(std::string("Cookie:").append(name).append("=").append(value).append("\r\n"));
552 }
553
554 socketContext->sendToPeer("\r\n");
555
556 return true;
557 }
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 
)
private

Definition at line 503 of file Request.cpp.

503 {
504 this->url = url;
505
506 set("Connection", "Upgrade", true);
507 set("Upgrade", protocols, true);
508
509 web::http::client::SocketContextUpgradeFactory* socketContextUpgradeFactory =
511
512 if (socketContextUpgradeFactory != nullptr) {
513 socketContextUpgradeFactory->checkRefCount();
514
516 } else {
518 }
519
520 return true;
521 }
SocketContextUpgradeFactory * select(const std::string &protocols, Request &req)

References web::http::client::SocketContextUpgradeFactory::checkRefCount(), core::socket::stream::SocketContext::close(), executeSendHeader(), web::http::client::SocketContextUpgradeFactorySelector::instance(), web::http::client::SocketContextUpgradeFactorySelector::select(), set(), socketContext, 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 650 of file Request.cpp.

650 {
651 return cookies;
652 }

References cookies.

◆ getHeaders()

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

Definition at line 646 of file Request.cpp.

646 {
647 return headers;
648 }

References headers.

◆ getQueries()

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

Definition at line 642 of file Request.cpp.

642 {
643 return queries;
644 }

References queries.

◆ getSocketContext()

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

Definition at line 654 of file Request.cpp.

654 {
655 return socketContext;
656 }

References socketContext.

Referenced by responseParseError().

Here is the caller graph for this function:

◆ header()

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

Definition at line 638 of file Request.cpp.

638 {
639 return headers.contains(field) ? headers[field] : "";
640 }

References headers.

Referenced by web::websocket::client::SocketContextUpgradeFactory::create(), main(), web::http::client::SocketContext::requestPrepared(), 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 ( )
private

Definition at line 436 of file Request.cpp.

436 {
437 bool error = false;
438 bool atomar = true;
439
440 for (RequestCommand* requestCommand : requestCommands) {
441 if (!error) {
442 const bool atomarCommand = requestCommand->execute(this);
443 if (atomar) {
444 atomar = atomarCommand;
445 }
446
447 error = requestCommand->getError();
448 }
449
450 delete requestCommand;
451 }
452 requestCommands.clear();
453
454 if (atomar && (!error || contentLengthSent != 0)) {
456 }
457
458 return !error || contentLengthSent != 0;
459 }

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 605 of file Request.cpp.

605 {
606 if (!masterRequest.expired()) {
608 source->start();
609 }
610 } else {
611 source->stop();
612 }
613 }
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 615 of file Request.cpp.

615 {
616 executeSendFragment(chunk, chunkLen);
617 }
bool executeSendFragment(const char *chunk, std::size_t chunkLen)
Definition Request.cpp:559

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 619 of file Request.cpp.

619 {
620 if (!masterRequest.expired()) {
622
624 }
625 }

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 627 of file Request.cpp.

627 {
628 errno = errnum;
629
630 if (!masterRequest.expired()) {
633
635 }
636 }

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 588 of file Request.cpp.

588 {
589 if (!masterRequest.expired()) {
591 executeSendFragment("", 0); // For transfer encoding chunked. Terminate the chunk sequence.
592
593 if (!trailer.empty()) {
594 for (auto& [field, value] : trailer) {
595 socketContext->sendToPeer(std::string(field).append(":").append(value).append("\r\n"));
596 }
597 socketContext->sendToPeer("\r\n");
598 }
599 }
600
602 }
603 }
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 575 of file Request.cpp.

575 {
576 socketContext->requestPrepared(std::move(*this));
577 init();
578 }
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:402

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 374 of file Request.cpp.

377 {
378 bool queued = true;
379
380 if (!masterRequest.expired()) {
383
384 requestCommands.push_back(new commands::SendFileCommand(file, onStatus));
385
387 } else {
388 queued = false;
389 }
390
391 return queued;
392 }

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 402 of file Request.cpp.

402 {
403 if (!masterRequest.expired()) {
404 contentLength += chunkLen;
405
406 requestCommands.push_back(new commands::SendFragmentCommand(chunk, chunkLen));
407 }
408
409 return *this;
410 }

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 412 of file Request.cpp.

412 {
413 return sendFragment(data.data(), data.size());
414 }

References sendFragment().

Here is the call graph for this function:

◆ sendHeader()

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

Definition at line 394 of file Request.cpp.

394 {
395 if (!masterRequest.expired()) {
396 requestCommands.push_back(new commands::SendHeaderCommand());
397 }
398
399 return *this;
400 }

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 
)

Definition at line 321 of file Request.cpp.

321 {
322 std::string name;
323
324 if (!masterRequest.expired()) {
325 if (response != nullptr) {
326 if (web::http::ciContains(response->get("connection"), "Upgrade")) {
327 web::http::client::SocketContextUpgradeFactory* socketContextUpgradeFactory =
329
330 if (socketContextUpgradeFactory != nullptr) {
331 name = socketContextUpgradeFactory->name();
332
334 << " HTTP upgrade: SocketContextUpgradeFactory created successful: " << name;
335
336 core::socket::stream::SocketContext* socketContextUpgrade =
337 socketContextUpgradeFactory->create(socketContext->getSocketConnection());
338
339 if (socketContextUpgrade != nullptr) {
341 << " HTTP upgrade: SocketContextUpgrade created successful: " << name;
342
343 socketContext->switchSocketContext(socketContextUpgrade);
344 } else {
346 << " HTTP upgrade: Create SocketContextUpgrade failed: " << name;
347
349 }
350 } else {
352 << " HTTP upgrade: SocketContextUpgradeFactory not supported by server: " << header("upgrade");
353
355 }
356 } else {
358 << " HTTP upgrade: Not any protocol supported by server: " << header("upgrade");
359
361 }
362 } else {
363 LOG(ERROR) << socketContext->getSocketConnection()->getConnectionName() << " HTTP upgrade: Response has gone away";
364
366 }
367 } else {
368 LOG(ERROR) << socketContext->getSocketConnection()->getConnectionName() << " HTTP upgrade: SocketContext has gone away";
369 }
370
371 status(name);
372 }
const std::string & getConnectionName() const
SocketConnection * getSocketConnection() const
virtual void switchSocketContext(SocketContext *newSocketContext)
virtual SocketContextUpgrade< Request, Response > * create(core::socket::stream::SocketConnection *socketConnection, Request *request, Response *response)=0
std::string header(const std::string &field)
Definition Request.cpp:638

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 main().

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 > &, const std::shared_ptr< Response > &)> &  onResponseReceived,
const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &  onResponseParseError = responseParseError 
)

Definition at line 301 of file Request.cpp.

304 {
305 bool success = true;
306
307 if (!masterRequest.expired()) {
310
311 requestCommands.push_back(new commands::UpgradeCommand(url, protocols));
312
314 } else {
315 success = false;
316 }
317
318 return success;
319 }

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

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 136 of file Request.h.

◆ commands::SendFileCommand

friend class commands::SendFileCommand
friend

Definition at line 132 of file Request.h.

◆ commands::SendFragmentCommand

friend class commands::SendFragmentCommand
friend

Definition at line 133 of file Request.h.

◆ commands::SendHeaderCommand

friend class commands::SendHeaderCommand
friend

Definition at line 134 of file Request.h.

◆ commands::UpgradeCommand

friend class commands::UpgradeCommand
friend

Definition at line 135 of file Request.h.

◆ SocketContext

friend class SocketContext
friend

Definition at line 193 of file Request.h.

Member Data Documentation

◆ connectionState

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

Definition at line 184 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 182 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 173 of file Request.h.

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

◆ headers

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

Definition at line 172 of file Request.h.

Referenced by append(), executeSendHeader(), getHeaders(), header(), init(), Request(), set(), setTrailer(), and type().

◆ hostFieldValue

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

Definition at line 164 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 187 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 186 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 171 of file Request.h.

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

◆ requestCommands

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

Definition at line 177 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 174 of file Request.h.

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

◆ transferEncoding

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

Definition at line 179 of file Request.h.

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

◆ url


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