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

56 this->host(hostFieldValue);
57 }
web::http::client::SocketContext * socketContext
Definition Request.h:169
std::string hostFieldValue
Definition Request.h:142
Request & host(const std::string &hostFieldValue)
Definition Request.cpp:119

◆ Request() [2/3]

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

◆ Request() [3/3]

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

Definition at line 59 of file Request.cpp.

60 : hostFieldValue(request.hostFieldValue) // NOLINT
61 , method(std::move(request.method))
62 , url(std::move(request.url))
63 , httpMajor(request.httpMajor)
64 , httpMinor(request.httpMinor)
65 , queries(std::move(request.queries))
66 , headers(std::move(request.headers))
67 , cookies(std::move(request.cookies))
68 , trailer(std::move(request.trailer))
69 , requestCommands(std::move(request.requestCommands))
70 , transferEncoding(request.transferEncoding)
71 , contentLength(request.contentLength)
72 , contentLengthSent(request.contentLengthSent)
73 , connectionState(request.connectionState)
74 , onResponseReceived(std::move(request.onResponseReceived))
75 , onResponseParseError(std::move(request.onResponseParseError))
76 , masterRequest(request.masterRequest) // NOLINT
77 , socketContext(request.socketContext) {
78 request.init();
79 }
CiStringMap< std::string > headers
Definition Request.h:150
TransferEncoding transferEncoding
Definition Request.h:157
ConnectionState connectionState
Definition Request.h:162
std::size_t contentLengthSent
Definition Request.h:160
CiStringMap< std::string > cookies
Definition Request.h:151
CiStringMap< std::string > trailer
Definition Request.h:152
std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> onResponseReceived
Definition Request.h:164
std::function< void(const std::shared_ptr< Request > &request, const std::string &message)> onResponseParseError
Definition Request.h:165
CiStringMap< std::string > queries
Definition Request.h:149
std::size_t contentLength
Definition Request.h:159
std::list< RequestCommand * > requestCommands
Definition Request.h:155
std::weak_ptr< Request > masterRequest
Definition Request.h:167

References httpMajor, httpMinor, init(), and Request().

Referenced by Request().

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

◆ ~Request()

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

Definition at line 81 of file Request.cpp.

81 {
82 for (const RequestCommand* requestCommand : requestCommands) {
83 delete requestCommand;
84 }
85
86 if (!masterRequest.expired() && Sink::isStreaming()) {
88 }
89 }

Member Function Documentation

◆ append()

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

Definition at line 125 of file Request.cpp.

125 {
126 const std::map<std::string, std::string>::iterator it = headers.find(field);
127
128 if (it != headers.end()) {
129 set(field, it->second.append(", ").append(value));
130 } else {
131 set(field, value);
132 }
133
134 return *this;
135 }
Request & set(const std::string &field, const std::string &value, bool overwrite=true)
Definition Request.cpp:137

◆ cookie() [1/2]

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

Definition at line 203 of file Request.cpp.

203 {
204 for (const auto& [name, value] : cookies) {
205 cookie(name, value);
206 }
207
208 return *this;
209 }
Request & cookie(const std::string &name, const std::string &value)
Definition Request.cpp:197

◆ cookie() [2/2]

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

Definition at line 197 of file Request.cpp.

197 {
198 cookies.insert({name, value});
199
200 return *this;
201 }

◆ deliverResponse()

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

Definition at line 558 of file Request.cpp.

558 {
559 onResponseReceived(request, response);
560 }

◆ deliverResponseParseError()

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

Definition at line 562 of file Request.cpp.

562 {
563 onResponseParseError(request, message);
564 }

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

395 {
396 bool queued = true;
397
398 if (!masterRequest.expired()) {
401
402 sendHeader();
403
404 requestCommands.push_back(new commands::EndCommand());
405
407 } else {
408 queued = false;
409 }
410
411 return queued;
412 }

References requestPrepared(), and sendHeader().

Here is the call graph for this function:

◆ executeEnd()

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

Definition at line 501 of file Request.cpp.

501 { // NOLINT
502 return true;
503 }

◆ executeSendFile()

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

Definition at line 439 of file Request.cpp.

439 {
440 bool atomar = true;
441
442 std::string absolutFileName = file;
443
444 if (std::filesystem::exists(absolutFileName)) {
445 std::error_code ec;
446 absolutFileName = std::filesystem::canonical(absolutFileName);
447
448 if (std::filesystem::is_regular_file(absolutFileName, ec) && !ec) {
449 core::file::FileReader::open(absolutFileName)->pipe(this, [this, &atomar, &absolutFileName, &onStatus](int errnum) {
450 errno = errnum;
451 onStatus(errnum);
452
453 if (errnum == 0) {
454 if (httpMajor == 1) {
455 atomar = false;
456
457 set("Content-Type", web::http::MimeTypes::contentType(absolutFileName), false);
458 set("Last-Modified", httputils::file_mod_http_date(absolutFileName), false);
459 if (httpMinor == 1 && contentLength == 0) {
460 set("Transfer-Encoding", "chunked");
461 } else {
462 set("Content-Length", std::to_string(std::filesystem::file_size(absolutFileName) + contentLength));
463 }
464
466 }
467 }
468 });
469 } else {
470 errno = EINVAL;
471 onStatus(errno);
472 }
473 } else {
474 errno = ENOENT;
475 onStatus(errno);
476 }
477
478 return atomar;
479 }
static FileReader * open(const std::string &path)
void pipe(Sink *sink, const std::function< void(int)> &callback)
Definition Source.cpp:38
static std::string contentType(const std::string &file)
std::string file_mod_http_date(const std::string &filePath)

◆ executeSendFragment()

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

Definition at line 537 of file Request.cpp.

537 {
539 socketContext->sendToPeer(to_hex_str(chunkLen).append("\r\n"));
540 }
541
542 socketContext->sendToPeer(chunk, chunkLen);
543 contentLengthSent += chunkLen;
544
546 socketContext->sendToPeer("\r\n");
547 contentLength += chunkLen;
548 }
549
550 return true;
551 }
void sendToPeer(const char *chunk, std::size_t chunkLen) const final
Request & append(const std::string &field, const std::string &value)
Definition Request.cpp:125
#define to_hex_str(int_val)
Definition Request.cpp:49

◆ executeSendHeader()

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

Definition at line 505 of file Request.cpp.

505 {
506 const std::string httpVersion = "HTTP/" + std::to_string(httpMajor) + "." + std::to_string(httpMinor);
507
508 std::string queryString;
509 if (!queries.empty()) {
510 queryString += "?";
511 for (auto& [key, value] : queries) {
512 queryString += httputils::url_encode(key) + "=" + httputils::url_encode(value) + "&";
513 }
514 queryString.pop_back();
515 }
516
517 socketContext->sendToPeer(method + " " + url + queryString + " " + httpVersion + "\r\n");
518 socketContext->sendToPeer("Date: " + httputils::to_http_date() + "\r\n");
519
520 if (!headers.contains("Transfer-Encoding") && contentLength > 0) {
521 set("Content-Length", std::to_string(contentLength));
522 }
523
524 for (const auto& [field, value] : headers) {
525 socketContext->sendToPeer(std::string(field).append(":").append(value).append("\r\n"));
526 }
527
528 for (const auto& [name, value] : cookies) {
529 socketContext->sendToPeer(std::string("Cookie:").append(name).append("=").append(value).append("\r\n"));
530 }
531
532 socketContext->sendToPeer("\r\n");
533
534 return true;
535 }
std::string to_http_date(struct tm *tm)
std::string url_encode(const std::string &text)

Referenced by executeUpgrade().

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

481 {
482 this->url = url;
483
484 set("Connection", "Upgrade", true);
485 set("Upgrade", protocols, true);
486
487 web::http::client::SocketContextUpgradeFactory* socketContextUpgradeFactory =
489
490 if (socketContextUpgradeFactory != nullptr) {
491 socketContextUpgradeFactory->checkRefCount();
492
494 } else {
496 }
497
498 return true;
499 }
SocketContextUpgradeFactory * select(const std::string &protocols, Request &req)

References executeSendHeader().

Here is the call graph for this function:

◆ getCookies()

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

Definition at line 628 of file Request.cpp.

628 {
629 return cookies;
630 }

◆ getHeaders()

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

Definition at line 624 of file Request.cpp.

624 {
625 return headers;
626 }

◆ getQueries()

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

Definition at line 620 of file Request.cpp.

620 {
621 return queries;
622 }

◆ getSocketContext()

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

Definition at line 632 of file Request.cpp.

632 {
633 return socketContext;
634 }

◆ header()

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

Definition at line 616 of file Request.cpp.

616 {
617 return headers.contains(field) ? headers[field] : "";
618 }

◆ host()

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

Definition at line 119 of file Request.cpp.

119 {
120 set("Host", hostFieldValue);
121
122 return *this;
123 }

◆ init()

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

Definition at line 95 of file Request.cpp.

95 {
96 method = "GET";
97 url = "/";
98 httpMajor = 1;
99 httpMinor = 1;
100 queries.clear();
101 headers.clear();
102 cookies.clear();
103 trailer.clear();
104 for (const RequestCommand* requestCommand : requestCommands) {
105 delete requestCommand;
106 }
107 requestCommands.clear();
109 contentLength = 0;
112 onResponseReceived = nullptr;
113 onResponseParseError = nullptr;
114
115 this->host(hostFieldValue);
116 set("X-Powered-By", "snode.c");
117 }

References httpMajor, and httpMinor.

Referenced by Request(), and requestPrepared().

Here is the caller graph for this function:

◆ initiate()

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

Definition at line 414 of file Request.cpp.

414 {
415 bool error = false;
416 bool atomar = true;
417
418 for (RequestCommand* requestCommand : requestCommands) {
419 if (!error) {
420 const bool atomarCommand = requestCommand->execute(this);
421 if (atomar) {
422 atomar = atomarCommand;
423 }
424
425 error = requestCommand->getError();
426 }
427
428 delete requestCommand;
429 }
430 requestCommands.clear();
431
432 if (atomar && (!error || contentLengthSent != 0)) {
434 }
435
436 return !error || contentLengthSent != 0;
437 }

References requestDelivered().

Here is the call graph for this function:

◆ onSourceConnect()

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

Implements core::pipe::Sink.

Definition at line 583 of file Request.cpp.

583 {
584 if (!masterRequest.expired()) {
586 source->start();
587 }
588 } else {
589 source->stop();
590 }
591 }
Source * source
Definition Sink.h:68
virtual void stop()=0
virtual void start()=0
bool streamToPeer(core::pipe::Source *source) const

References core::pipe::Source::start(), and core::pipe::Source::stop().

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

593 {
594 executeSendFragment(chunk, chunkLen);
595 }
bool executeSendFragment(const char *chunk, std::size_t chunkLen)
Definition Request.cpp:537

◆ onSourceEof()

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

Implements core::pipe::Sink.

Definition at line 597 of file Request.cpp.

597 {
598 if (!masterRequest.expired()) {
600
602 }
603 }

References requestDelivered().

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

605 {
606 errno = errnum;
607
608 if (!masterRequest.expired()) {
611
613 }
614 }

References requestDelivered().

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

211 {
212 queries.insert({key, value});
213
214 return *this;
215 }

◆ requestDelivered()

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

Definition at line 566 of file Request.cpp.

566 {
567 if (!masterRequest.expired()) {
569 executeSendFragment("", 0); // For transfer encoding chunked. Terminate the chunk sequence.
570
571 if (!trailer.empty()) {
572 for (auto& [field, value] : trailer) {
573 socketContext->sendToPeer(std::string(field).append(":").append(value).append("\r\n"));
574 }
575 socketContext->sendToPeer("\r\n");
576 }
577 }
578
580 }
581 }
void requestDelivered(Request &&request, bool success)

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

Here is the caller graph for this function:

◆ requestPrepared()

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

Definition at line 553 of file Request.cpp.

553 {
554 socketContext->requestPrepared(std::move(*this));
555 init();
556 }
void requestPrepared(Request &&request)

References init().

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

236 {
237 LOG(WARNING) << request->getSocketContext()->getSocketConnection()->getConnectionName()
238 << " HTTP: Response parse error: " << request->method << " " << request->url << " "
239 << "HTTP/" << request->httpMajor << "." << request->httpMinor << ": " << message;
240 }

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

245 {
246 bool queued = true;
247
248 if (!masterRequest.expired()) {
251
252 if (chunkLen > 0) {
253 set("Content-Type", "application/octet-stream", false);
254 }
255
256 sendHeader();
257 sendFragment(chunk, chunkLen);
258
259 requestCommands.push_back(new commands::EndCommand());
260
262 } else {
263 queued = false;
264 }
265
266 return queued;
267 }
Request & sendFragment(const char *chunk, std::size_t chunkLen)
Definition Request.cpp:380

References requestPrepared(), and sendHeader().

Here is the call 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 269 of file Request.cpp.

271 {
272 if (!chunk.empty()) {
273 set("Content-Type", "text/html; charset=utf-8", false);
274 }
275
276 return send(chunk.data(), chunk.size(), onResponseReceived, onResponseParseError);
277 }
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:242

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

355 {
356 bool queued = true;
357
358 if (!masterRequest.expired()) {
361
362 requestCommands.push_back(new commands::SendFileCommand(file, onStatus));
363
365 } else {
366 queued = false;
367 }
368
369 return queued;
370 }

References requestPrepared().

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

380 {
381 if (!masterRequest.expired()) {
382 contentLength += chunkLen;
383
384 requestCommands.push_back(new commands::SendFragmentCommand(chunk, chunkLen));
385 }
386
387 return *this;
388 }

◆ sendFragment() [2/2]

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

Definition at line 390 of file Request.cpp.

390 {
391 return sendFragment(data.data(), data.size());
392 }

◆ sendHeader()

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

Definition at line 372 of file Request.cpp.

372 {
373 if (!masterRequest.expired()) {
374 requestCommands.push_back(new commands::SendHeaderCommand());
375 }
376
377 return *this;
378 }

Referenced by end(), and send().

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

183 {
184 for (const auto& [field, value] : headers) {
185 set(field, value, overwrite);
186 }
187
188 return *this;
189 }

◆ set() [2/2]

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

Definition at line 137 of file Request.cpp.

137 {
138 if (!value.empty()) {
139 if (overwrite) {
140 headers.insert_or_assign(field, value);
141 } else {
142 headers.insert({field, value});
143 }
144
145 if (web::http::ciEquals(field, "Connection")) {
146 if (web::http::ciContains(headers[field], "close")) {
148 } else if (web::http::ciContains(headers[field], "keep-alive")) {
150 }
151 } else if (web::http::ciEquals(field, "Content-Length")) {
152 contentLength = std::stoul(value);
154 headers.erase("Transfer-Encoding");
155 } else if (web::http::ciEquals(field, "Transfer-Encoding")) {
156 if (web::http::ciContains(headers[field], "chunked")) {
158 headers.erase("Content-Length");
159 }
160 if (web::http::ciContains(headers[field], "compressed")) {
161 }
162 if (web::http::ciContains(headers[field], "deflate")) {
163 }
164 if (web::http::ciContains(headers[field], "gzip")) {
165 }
166 } else if (web::http::ciEquals(field, "Content-Encoding")) {
167 if (web::http::ciContains(headers[field], "compressed")) {
168 }
169 if (web::http::ciContains(headers[field], "deflate")) {
170 }
171 if (web::http::ciContains(headers[field], "gzip")) {
172 }
173 if (web::http::ciContains(headers[field], "br")) {
174 }
175 }
176 } else {
177 headers.erase(field);
178 }
179
180 return *this;
181 }
bool ciEquals(const std::string &str1, const std::string &str2)
bool ciContains(const std::string &str1, const std::string &str2)

◆ setMasterRequest()

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

Definition at line 91 of file Request.cpp.

91 {
92 this->masterRequest = masterRequest;
93 }

◆ setTrailer()

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

Definition at line 217 of file Request.cpp.

217 {
218 if (!value.empty()) {
219 if (overwrite) {
220 trailer.insert_or_assign(field, value);
221 } else {
222 trailer.insert({field, value});
223 }
224 if (!headers.contains("Trailer")) {
225 set("Trailer", field);
226 } else {
227 headers["Trailer"].append("," + field);
228 }
229 } else {
230 trailer.erase(field);
231 }
232
233 return *this;
234 }

◆ type()

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

Definition at line 191 of file Request.cpp.

191 {
192 headers.insert({"Content-Type", type});
193
194 return *this;
195 }
Request & type(const std::string &type)
Definition Request.cpp:191

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

299 {
300 std::string name;
301
302 if (!masterRequest.expired()) {
303 if (response != nullptr) {
304 if (web::http::ciContains(response->get("connection"), "Upgrade")) {
305 web::http::client::SocketContextUpgradeFactory* socketContextUpgradeFactory =
307
308 if (socketContextUpgradeFactory != nullptr) {
309 name = socketContextUpgradeFactory->name();
310
312 << " HTTP upgrade: SocketContextUpgradeFactory created successful: " << name;
313
314 core::socket::stream::SocketContext* socketContextUpgrade =
315 socketContextUpgradeFactory->create(socketContext->getSocketConnection());
316
317 if (socketContextUpgrade != nullptr) {
319 << " HTTP upgrade: SocketContextUpgrade created successful: " << name;
320
321 socketContext->switchSocketContext(socketContextUpgrade);
322 } else {
324 << " HTTP upgrade: Create SocketContextUpgrade failed: " << name;
325
327 }
328 } else {
330 << " HTTP upgrade: SocketContextUpgradeFactory not supported by server: " << header("upgrade");
331
333 }
334 } else {
336 << " HTTP upgrade: Not any protocol supported by server: " << header("upgrade");
337
339 }
340 } else {
341 LOG(ERROR) << socketContext->getSocketConnection()->getConnectionName() << " HTTP upgrade: Response has gone away";
342
344 }
345 } else {
346 LOG(ERROR) << socketContext->getSocketConnection()->getConnectionName() << " HTTP upgrade: SocketContext has gone away";
347 }
348
349 status(name);
350 }
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:616

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

282 {
283 bool success = true;
284
285 if (!masterRequest.expired()) {
288
289 requestCommands.push_back(new commands::UpgradeCommand(url, protocols));
290
292 } else {
293 success = false;
294 }
295
296 return success;
297 }

References requestPrepared().

Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ commands::EndCommand

friend class commands::EndCommand
friend

Definition at line 114 of file Request.h.

◆ commands::SendFileCommand

friend class commands::SendFileCommand
friend

Definition at line 110 of file Request.h.

◆ commands::SendFragmentCommand

friend class commands::SendFragmentCommand
friend

Definition at line 111 of file Request.h.

◆ commands::SendHeaderCommand

friend class commands::SendHeaderCommand
friend

Definition at line 112 of file Request.h.

◆ commands::UpgradeCommand

friend class commands::UpgradeCommand
friend

Definition at line 113 of file Request.h.

◆ SocketContext

friend class SocketContext
friend

Definition at line 171 of file Request.h.

Member Data Documentation

◆ connectionState

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

Definition at line 162 of file Request.h.

◆ contentLength

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

Definition at line 159 of file Request.h.

◆ contentLengthSent

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

Definition at line 160 of file Request.h.

◆ cookies

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

Definition at line 151 of file Request.h.

◆ headers

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

Definition at line 150 of file Request.h.

◆ hostFieldValue

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

Definition at line 142 of file Request.h.

◆ httpMajor

int web::http::client::Request::httpMajor = 1

◆ httpMinor

int web::http::client::Request::httpMinor = 1

◆ masterRequest

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

Definition at line 167 of file Request.h.

◆ method

std::string web::http::client::Request::method = "GET"

Definition at line 143 of file Request.h.

◆ onResponseParseError

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

Definition at line 165 of file Request.h.

◆ onResponseReceived

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

Definition at line 164 of file Request.h.

◆ queries

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

Definition at line 149 of file Request.h.

◆ requestCommands

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

Definition at line 155 of file Request.h.

◆ socketContext

web::http::client::SocketContext* web::http::client::Request::socketContext
private

Definition at line 169 of file Request.h.

◆ trailer

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

Definition at line 152 of file Request.h.

◆ transferEncoding

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

Definition at line 157 of file Request.h.

◆ url

std::string web::http::client::Request::url = "/"

Definition at line 144 of file Request.h.


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