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
 
Requestoperator= (Request &)=delete
 
Requestoperator= (Request &&) noexcept=delete
 
void setMasterRequest (const std::shared_ptr< MasterRequest > &masterRequest)
 
std::shared_ptr< MasterRequestgetMasterRequest () const
 
SocketContextgetSocketContext () const
 
Requesthost (const std::string &hostFieldValue)
 
Requestappend (const std::string &field, const std::string &value)
 
Requestset (const std::string &field, const std::string &value, bool overwrite=true)
 
Requestset (const std::map< std::string, std::string > &headers, bool overwrite=true)
 
Requesttype (const std::string &type)
 
Requestcookie (const std::string &name, const std::string &value)
 
Requestcookie (const std::map< std::string, std::string > &cookies)
 
Requestquery (const std::string &key, const std::string &value)
 
RequestsetTrailer (const std::string &field, const std::string &value, bool overwrite=true)
 
std::string header (const std::string &field) const
 
const CiStringMap< std::string > & getQueries () const
 
const CiStringMap< std::string > & getHeaders () const
 
const CiStringMap< std::string > & getTrailer () const
 
const CiStringMap< std::string > & getCookies () const
 

Public Attributes

std::string hostFieldValue
 
std::string method = "GET"
 
std::string url = "/"
 
int httpMajor = 1
 
int httpMinor = 1
 
std::size_t count {0}
 

Protected Member Functions

 Request (Request &&request) noexcept
 
void upgrade (const std::shared_ptr< Response > &response, const std::function< void(const std::string &)> &status)
 

Protected Attributes

CiStringMap< std::string > queries
 
CiStringMap< std::string > headers
 
CiStringMap< std::string > cookies
 
CiStringMap< std::string > trailer
 
std::size_t contentLength = 0
 
std::weak_ptr< MasterRequestmasterRequest
 
web::http::client::SocketContextsocketContext
 
TransferEncoding transferEncoding = TransferEncoding::HTTP10
 
ConnectionState connectionState = ConnectionState::Default
 

Friends

class MasterRequest
 

Detailed Description

Definition at line 86 of file Request.h.

Constructor & Destructor Documentation

◆ Request() [1/3]

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

Definition at line 77 of file Request.cpp.

81 set("X-Powered-By", "snode.c");
82 }
Request & set(const std::string &field, const std::string &value, bool overwrite=true)
Definition Request.cpp:136
web::http::client::SocketContext * socketContext
Definition Request.h:139
std::string hostFieldValue
Definition Request.h:120
Request & host(const std::string &hostFieldValue)
Definition Request.cpp:118

References host(), hostFieldValue, set(), and socketContext.

Referenced by web::http::client::MasterRequest::MasterRequest().

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

◆ Request() [2/3]

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

◆ Request() [3/3]

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

Definition at line 84 of file Request.cpp.

85 : hostFieldValue(request.hostFieldValue) // NOLINT
86 , method(std::move(request.method))
87 , url(std::move(request.url))
88 , httpMajor(request.httpMajor)
89 , httpMinor(request.httpMinor)
90 , count(request.count)
91 , queries(std::move(request.queries))
92 , headers(std::move(request.headers))
93 , cookies(std::move(request.cookies))
94 , trailer(std::move(request.trailer))
95 , contentLength(request.contentLength)
96 , masterRequest(request.masterRequest) // NOLINT
97 , socketContext(request.socketContext)
98 , transferEncoding(request.transferEncoding)
99 , connectionState(request.connectionState) {
100 request.count++;
101
103 set("X-Powered-By", "snode.c");
104 }
CiStringMap< std::string > headers
Definition Request.h:132
TransferEncoding transferEncoding
Definition Request.h:141
ConnectionState connectionState
Definition Request.h:142
CiStringMap< std::string > cookies
Definition Request.h:133
CiStringMap< std::string > trailer
Definition Request.h:134
std::weak_ptr< MasterRequest > masterRequest
Definition Request.h:138
CiStringMap< std::string > queries
Definition Request.h:131
std::size_t contentLength
Definition Request.h:136

References connectionState, contentLength, cookies, count, headers, host(), hostFieldValue, httpMajor, httpMinor, masterRequest, method, queries, set(), socketContext, trailer, transferEncoding, and url.

Referenced by web::http::client::MasterRequest::MasterRequest().

Here is the call graph for this function:
Here is the caller 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 124 of file Request.cpp.

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

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

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

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

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

References cookies.

Referenced by cookie().

Here is the caller graph for this function:

◆ getCookies()

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

Definition at line 253 of file Request.cpp.

253 {
254 return cookies;
255 }

References cookies.

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

Here is the caller graph for this function:

◆ getHeaders()

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

Definition at line 245 of file Request.cpp.

245 {
246 return headers;
247 }

References headers.

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

Here is the caller graph for this function:

◆ getMasterRequest()

std::shared_ptr< MasterRequest > web::http::client::Request::getMasterRequest ( ) const

Definition at line 110 of file Request.cpp.

110 {
111 return masterRequest.lock();
112 }

References masterRequest.

◆ getQueries()

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

Definition at line 241 of file Request.cpp.

241 {
242 return queries;
243 }

References queries.

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

Here is the caller graph for this function:

◆ getSocketContext()

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

Definition at line 114 of file Request.cpp.

114 {
115 return socketContext;
116 }

References socketContext.

Referenced by web::http::client::MasterRequest::executeUpgrade(), main(), web::http::client::MasterRequest::requestEventSource(), and web::http::client::MasterRequest::upgrade().

Here is the caller graph for this function:

◆ getTrailer()

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

Definition at line 249 of file Request.cpp.

249 {
250 return trailer;
251 }

References trailer.

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

Here is the caller graph for this function:

◆ header()

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

Definition at line 235 of file Request.cpp.

235 {
236 auto fieldElement = headers.find(field);
237
238 return fieldElement != headers.end() ? fieldElement->second : "";
239 }

References headers.

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

Here is the caller graph for this function:

◆ host()

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

Definition at line 118 of file Request.cpp.

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

References set().

Referenced by Request(), and Request().

Here is the call graph for this function:
Here is the caller 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 210 of file Request.cpp.

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

References queries.

◆ set() [1/2]

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

Definition at line 182 of file Request.cpp.

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

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

136 {
137 if (!value.empty()) {
138 if (overwrite) {
139 headers.insert_or_assign(field, value);
140 } else {
141 headers.insert({field, value});
142 }
143
144 if (web::http::ciEquals(field, "Connection")) {
145 if (web::http::ciContains(headers[field], "close")) {
147 } else if (web::http::ciContains(headers[field], "keep-alive")) {
149 }
150 } else if (web::http::ciEquals(field, "Content-Length")) {
151 contentLength = std::stoul(value);
153 headers.erase("Transfer-Encoding");
154 } else if (web::http::ciEquals(field, "Transfer-Encoding")) {
155 if (web::http::ciContains(headers[field], "chunked")) {
157 headers.erase("Content-Length");
158 }
159 if (web::http::ciContains(headers[field], "compressed")) {
160 }
161 if (web::http::ciContains(headers[field], "deflate")) {
162 }
163 if (web::http::ciContains(headers[field], "gzip")) {
164 }
165 } else if (web::http::ciEquals(field, "Content-Encoding")) {
166 if (web::http::ciContains(headers[field], "compressed")) {
167 }
168 if (web::http::ciContains(headers[field], "deflate")) {
169 }
170 if (web::http::ciContains(headers[field], "gzip")) {
171 }
172 if (web::http::ciContains(headers[field], "br")) {
173 }
174 }
175 } else {
176 headers.erase(field);
177 }
178
179 return *this;
180 }
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(), web::http::client::MasterRequest::executeSendFile(), web::http::client::MasterRequest::executeSendHeader(), web::http::client::MasterRequest::executeUpgrade(), host(), main(), web::websocket::client::SocketContextUpgradeFactory::prepare(), Request(), Request(), web::http::client::MasterRequest::requestEventSource(), web::http::client::MasterRequest::send(), web::http::client::MasterRequest::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< MasterRequest > &  masterRequest)

Definition at line 106 of file Request.cpp.

106 {
108 }

References masterRequest.

◆ setTrailer()

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

Definition at line 216 of file Request.cpp.

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

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

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

References headers.

Referenced by main().

Here is the caller graph for this function:

◆ upgrade()

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

Definition at line 257 of file Request.cpp.

257 {
258 const std::string connectionName = socketContext->getSocketConnection()->getConnectionName();
259
260 std::string name;
261
262 if (!masterRequest.expired()) {
263 if (response != nullptr) {
264 if (web::http::ciContains(response->get("connection"), "Upgrade")) {
265 SocketContextUpgradeFactory* socketContextUpgradeFactory =
267
268 if (socketContextUpgradeFactory != nullptr) {
269 name = socketContextUpgradeFactory->name();
270
271 LOG(DEBUG) << connectionName << " HTTP upgrade: SocketContextUpgradeFactory create success for: " << name;
272
273 core::socket::stream::SocketContext* socketContextUpgrade =
274 socketContextUpgradeFactory->create(socketContext->getSocketConnection());
275
276 if (socketContextUpgrade != nullptr) {
277 LOG(DEBUG) << connectionName << " HTTP upgrade: SocketContextUpgrade create success for: " << name;
278
279 socketContext->switchSocketContext(socketContextUpgrade);
280 } else {
281 LOG(DEBUG) << connectionName << " HTTP upgrade: SocketContextUpgrade create failed for: " << name;
282
284 }
285 } else {
286 LOG(DEBUG) << connectionName
287 << " HTTP upgrade: SocketContextUpgradeFactory not supported by server: " << header("upgrade");
288
290 }
291 } else {
292 LOG(DEBUG) << connectionName << " HTTP upgrade: No upgrade requested";
293
295 }
296 } else {
297 LOG(ERROR) << connectionName << " HTTP upgrade: Response has gone away";
298
300 }
301 } else {
302 LOG(ERROR) << connectionName << " HTTP upgrade: Unexpected disconnect";
303 }
304
305 status(name);
306 }
const std::string & getConnectionName() const
SocketConnection * getSocketConnection() const
void switchSocketContext(SocketContext *newSocketContext)
std::string header(const std::string &field) const
Definition Request.cpp:235
SocketContextUpgradeFactory * select(const std::string &protocols, Request &req)

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 web::http::client::MasterRequest::upgrade().

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

Friends And Related Symbol Documentation

◆ MasterRequest

friend class MasterRequest
friend

Definition at line 144 of file Request.h.

Member Data Documentation

◆ connectionState

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

Definition at line 142 of file Request.h.

Referenced by web::http::client::MasterRequest::init(), Request(), and set().

◆ contentLength

◆ cookies

◆ count

◆ headers

◆ hostFieldValue

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

Definition at line 120 of file Request.h.

Referenced by Request(), and Request().

◆ httpMajor

◆ httpMinor

◆ masterRequest

◆ method

◆ queries

◆ socketContext

◆ trailer

◆ transferEncoding

◆ url


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