SNode.C
Loading...
Searching...
No Matches
Request.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025, 2026
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "web/http/client/Request.h"
43
44#include "core/file/FileReader.h"
45#include "core/socket/stream/SocketConnection.h"
46#include "web/http/MimeTypes.h"
47#include "web/http/TransferEncoding.h"
48#include "web/http/client/SocketContext.h"
49#include "web/http/client/SocketContextUpgradeFactorySelector.h"
50
51//
52
53#include "commands/EndCommand.h"
54#include "commands/SendFileCommand.h"
55#include "commands/SendFragmentCommand.h"
56#include "commands/SendHeaderCommand.h"
57#include "commands/SseCommand.h"
58#include "commands/UpgradeCommand.h"
59
60#ifndef DOXYGEN_SHOULD_SKIP_THIS
61
62#include "log/Logger.h"
63#include "web/http/http_utils.h"
64
65#include <cerrno>
66#include <filesystem>
67#include <sstream>
68#include <system_error>
69#include <utility>
70
71#endif /* DOXYGEN_SHOULD_SKIP_THIS */
72
73#define to_hex_str(int_val) (static_cast<std::ostringstream const&>(std::ostringstream() << std::uppercase << std::hex << int_val)).str()
74
75namespace web::http::client {
76
77 Request::Request(const std::string& connectionName, const std::string& hostFieldValue)
78 : hostFieldValue(hostFieldValue)
79 , connectionName(connectionName) {
80 host(hostFieldValue);
81 set("X-Powered-By", "SNode.C");
82 }
83
84 Request::Request(Request&& request) noexcept
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))
99 request.count++;
100
102 set("X-Powered-By", "SNode.C");
103 }
104
105 std::string Request::getConnectionName() const {
106 return connectionName;
107 }
108
109 void MasterRequest::setMasterRequest(const std::shared_ptr<MasterRequest>& masterRequest) {
110 this->masterRequest = masterRequest;
111 }
112
113 std::shared_ptr<MasterRequest> MasterRequest::getMasterRequest() const {
114 return masterRequest.lock();
115 }
116
120
122 return socketContext != nullptr;
123 }
124
125 Request& Request::host(const std::string& hostFieldValue) {
126 set("Host", hostFieldValue, true);
127
128 return *this;
129 }
130
131 Request& Request::append(const std::string& field, const std::string& value) {
132 const std::map<std::string, std::string>::iterator it = headers.find(field);
133
134 if (it != headers.end()) {
135 set(field, it->second.append(", ").append(value));
136 } else {
137 set(field, value);
138 }
139
140 return *this;
141 }
142
143 Request& Request::set(const std::string& field, const std::string& value, bool overwrite) {
144 if (!value.empty()) {
145 if (overwrite) {
146 headers.insert_or_assign(field, value);
147 } else {
148 headers.insert({field, value});
149 }
150
151 if (web::http::ciEquals(field, "Connection")) {
152 if (web::http::ciContains(headers[field], "close")) {
154 } else if (web::http::ciContains(headers[field], "keep-alive")) {
156 }
157 } else if (web::http::ciEquals(field, "Content-Length")) {
158 contentLength = std::stoul(value);
160 headers.erase("Transfer-Encoding");
161 } else if (web::http::ciEquals(field, "Transfer-Encoding")) {
162 if (web::http::ciContains(headers[field], "chunked")) {
164 headers.erase("Content-Length");
165 }
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 } else if (web::http::ciEquals(field, "Content-Encoding")) {
173 if (web::http::ciContains(headers[field], "compressed")) {
174 }
175 if (web::http::ciContains(headers[field], "deflate")) {
176 }
177 if (web::http::ciContains(headers[field], "gzip")) {
178 }
179 if (web::http::ciContains(headers[field], "br")) {
180 }
181 }
182 } else {
183 headers.erase(field);
184 }
185
186 return *this;
187 }
188
189 Request& Request::set(const std::map<std::string, std::string>& headers, bool overwrite) {
190 for (const auto& [field, value] : headers) {
191 set(field, value, overwrite);
192 }
193
194 return *this;
195 }
196
197 Request& Request::type(const std::string& type) {
198 headers.insert({"Content-Type", type});
199
200 return *this;
201 }
202
203 Request& Request::cookie(const std::string& name, const std::string& value) {
204 cookies.insert({name, value});
205
206 return *this;
207 }
208
209 Request& Request::cookie(const std::map<std::string, std::string>& cookies) {
210 for (const auto& [name, value] : cookies) {
211 cookie(name, value);
212 }
213
214 return *this;
215 }
216
217 Request& Request::query(const std::string& key, const std::string& value) {
218 queries.insert({key, value});
219
220 return *this;
221 }
222
223 Request& Request::setTrailer(const std::string& field, const std::string& value, bool overwrite) {
224 if (!value.empty()) {
225 if (overwrite) {
226 trailer.insert_or_assign(field, value);
227 } else {
228 trailer.insert({field, value});
229 }
230 if (!headers.contains("Trailer")) {
231 set("Trailer", field);
232 } else {
233 headers["Trailer"].append("," + field);
234 }
235 } else {
236 trailer.erase(field);
237 }
238
239 return *this;
240 }
241
242 std::string Request::header(const std::string& field) const {
243 auto fieldElement = headers.find(field);
244
245 return fieldElement != headers.end() ? fieldElement->second : "";
246 }
247
248 const std::map<std::string, std::string>& Request::getQueries() const {
249 return queries;
250 }
251
252 const CiStringMap<std::string>& Request::getHeaders() const {
253 return headers;
254 }
255
256 const CiStringMap<std::string>& Request::getTrailer() const {
257 return trailer;
258 }
259
260 const CiStringMap<std::string>& Request::getCookies() const {
261 return cookies;
262 }
263
264 MasterRequest::MasterRequest(SocketContext* socketContext, const std::string& host)
266 , socketContext(socketContext) {
267 this->init();
268 }
269
271 : Request(std::move(request))
272 , requestCommands(std::move(request.requestCommands))
274 , onResponseReceived(std::move(request.onResponseReceived))
277 , masterRequest(request.masterRequest) { // NOLINT
278 request.init();
279 }
280
282 for (const RequestCommand* requestCommand : requestCommands) {
283 delete requestCommand;
284 }
285
288 }
289 }
290
292 method = "GET";
293 url = "/";
294 httpMajor = 1;
295 httpMinor = 1;
296 queries.clear();
297 headers.clear();
298 cookies.clear();
299 trailer.clear();
300 requestCommands.clear();
302 contentLength = 0;
305 }
306
308 stop();
309 socketContext = nullptr;
310 }
311
312 bool
313 MasterRequest::send(const char* chunk,
314 std::size_t chunkLen,
315 const std::function<void(const std::shared_ptr<Request>&, const std::shared_ptr<Response>&)>& onResponseReceived,
316 const std::function<void(const std::shared_ptr<Request>&, const std::string&)>& onResponseParseError) {
317 bool queued = true;
318
319 if (isConnected()) {
320 const std::shared_ptr<MasterRequest> newRequest = std::make_shared<MasterRequest>(std::move(*this));
321
322 if (chunkLen > 0) {
323 newRequest->set("Content-Type", "application/octet-stream", false);
324 }
325
326 newRequest->sendHeader();
327 newRequest->sendFragment(chunk, chunkLen);
328
329 newRequest->requestCommands.push_back(new commands::EndCommand(onResponseReceived, onResponseParseError));
330
331 newRequest->requestPrepared(newRequest);
332
333 } else {
334 queued = false;
335 }
336
337 return queued;
338 }
339
340 bool
341 MasterRequest::send(const std::string& chunk,
342 const std::function<void(const std::shared_ptr<Request>&, const std::shared_ptr<Response>&)>& onResponseReceived,
343 const std::function<void(const std::shared_ptr<Request>&, const std::string&)>& onResponseParseError) {
344 if (!chunk.empty()) {
345 set("Content-Type", "text/html; charset=utf-8", false);
346 }
347
348 return send(chunk.data(), chunk.size(), onResponseReceived, onResponseParseError);
349 }
350
352 const std::string& url,
353 const std::string& protocols,
354 const std::function<void(bool)>& onUpgradeInitiate,
355 const std::function<void(const std::shared_ptr<Request>&, const std::shared_ptr<Response>&, bool)>& onResponseReceived,
356 const std::function<void(const std::shared_ptr<Request>&, const std::string&)>& onResponseParseError) {
357 if (isConnected()) {
358 masterRequest.lock()->url = url;
359
361 url,
362 protocols,
363 onUpgradeInitiate,
364 [masterRequest = this->masterRequest, connectionName = this->connectionName, onResponseReceived](
365 const std::shared_ptr<Request>& request, const std::shared_ptr<Response>& response) {
366 if (!masterRequest.expired() && masterRequest.lock()->isConnected()) {
367 LOG(DEBUG) << connectionName << " HTTP upgrade: Response to upgrade request: " << request->method << " "
368 << request->url << " "
369 << "HTTP/" << request->httpMajor << "." << request->httpMinor << "\n"
371 response->statusCode,
372 response->reason,
373 response->headers,
374 response->cookies,
375 response->body);
376
377 std::string socketContextUpgradeName;
378
379 if (web::http::ciContains(response->get("connection"), "Upgrade")) {
380 SocketContextUpgradeFactory* socketContextUpgradeFactory =
382
383 if (socketContextUpgradeFactory != nullptr) {
384 socketContextUpgradeName = socketContextUpgradeFactory->name();
385
386 LOG(DEBUG) << connectionName
387 << " HTTP upgrade: SocketContextUpgradeFactory create success for: " << socketContextUpgradeName;
388
389 core::socket::stream::SocketContext* socketContextUpgrade =
390 socketContextUpgradeFactory->create(masterRequest.lock()->getSocketContext()->getSocketConnection());
391
392 if (socketContextUpgrade != nullptr) {
393 LOG(DEBUG) << connectionName
394 << " HTTP upgrade: SocketContextUpgrade create success for: " << socketContextUpgradeName;
395 masterRequest.lock()->getSocketContext()->getSocketConnection()->setSocketContext(socketContextUpgrade);
396 } else {
397 LOG(DEBUG) << connectionName
398 << " HTTP upgrade: SocketContextUpgrade create failed for: " << socketContextUpgradeName;
399
400 masterRequest.lock()->getSocketContext()->close();
401 }
402 } else {
403 LOG(DEBUG) << connectionName << " HTTP upgrade: SocketContextUpgradeFactory not supported by server: "
404 << request->header("upgrade");
405
406 masterRequest.lock()->getSocketContext()->close();
407 }
408 } else {
409 LOG(DEBUG) << connectionName << " HTTP upgrade: No upgrade requested";
410
411 masterRequest.lock()->getSocketContext()->close();
412 }
413
414 LOG(DEBUG) << connectionName << " HTTP upgrade: bootstrap "
415 << (!socketContextUpgradeName.empty() ? "success" : "failed");
416 LOG(DEBUG) << " Protocol selected: " << socketContextUpgradeName;
417 LOG(DEBUG) << " requested: " << request->header("upgrade");
418 LOG(DEBUG) << " Subprotocol selected: " << response->get("Sec-WebSocket-Protocol");
419 LOG(DEBUG) << " requested: " << request->header("Sec-WebSocket-Protocol");
420
421 onResponseReceived(request, response, !socketContextUpgradeName.empty());
422 }
423 },
424 onResponseParseError));
425
426 const std::shared_ptr<MasterRequest> newRequest = std::make_shared<MasterRequest>(std::move(*this));
427
428 requestPrepared(newRequest);
429 }
430
431 return isConnected();
432 }
433
434 bool MasterRequest::requestEventSource(const std::string& url,
435 const std::function<std::size_t()>& onServerSentEvent,
436 const std::function<void()>& onOpen,
437 const std::function<void()>& onError) {
438 if (isConnected()) {
439 const std::shared_ptr<MasterRequest> newRequest = std::make_shared<MasterRequest>(std::move(*this));
440
441 newRequest->url = url;
442 newRequest->httpMajor = 1;
443 newRequest->httpMinor = 1;
444
445 newRequest->set("Connection", "keep-alive", true);
446 newRequest->set("Accept", "text/event-stream", true);
447 newRequest->set("Cache-Control", "no-cache", true);
448
449 newRequest->sendHeader();
450
451 newRequest->requestCommands.push_back(new commands::SseCommand(
452 [masterRequest = this->masterRequest, onServerSentEvent, onOpen, onError](const std::shared_ptr<Request>& request,
453 const std::shared_ptr<Response>& response) {
454 if (!masterRequest.expired() && masterRequest.lock()->isConnected()) {
455 if (web::http::ciContains(response->headers["Content-Type"], "text/event-stream") &&
456 web::http::ciContains(request->header("Accept"), "text/event-stream")) {
457 masterRequest.lock()->getSocketContext()->setSseEventReceiver(onServerSentEvent);
458
459 onOpen();
460 } else {
461 masterRequest.lock()->getSocketContext()->close();
462
463 onError();
464 }
465 }
466 },
467 [masterRequest = this->masterRequest, connectionName = this->connectionName, onError](
468 [[maybe_unused]] const std::shared_ptr<Request>& request, const std::string& status) {
469 if (!masterRequest.expired() && masterRequest.lock()->isConnected()) {
470 LOG(DEBUG) << connectionName << " error in response: " << status;
471 masterRequest.lock()->getSocketContext()->close();
472 onError();
473 }
474 }));
475
476 requestPrepared(newRequest);
477 }
478
479 return isConnected();
480 }
481
483 const std::string& file,
484 const std::function<void(int)>& onStatus,
485 const std::function<void(const std::shared_ptr<Request>&, const std::shared_ptr<Response>&)>& onResponseReceived,
486 const std::function<void(const std::shared_ptr<Request>&, const std::string&)>& onResponseParseError) {
487 bool queued = false;
488
489 if (isConnected()) {
490 const std::shared_ptr<MasterRequest> newRequest = std::make_shared<MasterRequest>(std::move(*this));
491
492 newRequest->requestCommands.push_back(new commands::SendFileCommand(file, onStatus, onResponseReceived, onResponseParseError));
493
494 requestPrepared(newRequest);
495
496 queued = true;
497 }
498
499 return queued;
500 }
501
503 if (isConnected()) {
505 }
506
507 return *this;
508 }
509
510 MasterRequest& MasterRequest::sendFragment(const char* chunk, std::size_t chunkLen) {
511 if (isConnected()) {
512 contentLength += chunkLen;
513
514 requestCommands.push_back(new commands::SendFragmentCommand(chunk, chunkLen));
515 }
516
517 return *this;
518 }
519
520 MasterRequest& MasterRequest::sendFragment(const std::string& data) {
521 return sendFragment(data.data(), data.size());
522 }
523
524 bool
525 MasterRequest::end(const std::function<void(const std::shared_ptr<Request>&, const std::shared_ptr<Response>&)>& onResponseReceived,
526 const std::function<void(const std::shared_ptr<Request>&, const std::string&)>& onResponseParseError) {
527 bool queued = true;
528
529 if (isConnected()) {
530 const std::shared_ptr<MasterRequest> newRequest = std::make_shared<MasterRequest>(std::move(*this));
531
532 newRequest->sendHeader();
533
534 newRequest->requestCommands.push_back(new commands::EndCommand(onResponseReceived, onResponseParseError));
535
536 requestPrepared(newRequest);
537 } else {
538 queued = false;
539 }
540
541 return queued;
542 }
543
544 bool MasterRequest::initiate(const std::shared_ptr<MasterRequest>& request) {
545 bool error = false;
546 bool atomar = true;
547
548 for (RequestCommand* requestCommand : requestCommands) {
549 if (!error) {
551 this->onResponseReceived = requestCommand->onResponseReceived;
552
553 atomar = requestCommand->execute(request);
554 error = requestCommand->getError();
555 }
556
557 delete requestCommand;
558 }
559 requestCommands.clear();
560
561 if (atomar && !error) {
563 }
564
565 return !error;
566 }
567
568 bool MasterRequest::executeSendFile(const std::string& file, const std::function<void(int)>& onStatus) {
569 bool atomar = true;
570
571 std::string absolutFileName = file;
572
573 if (std::filesystem::exists(absolutFileName)) {
574 std::error_code ec;
575 absolutFileName = std::filesystem::canonical(absolutFileName);
576
577 if (std::filesystem::is_regular_file(absolutFileName, ec) && !ec) {
578 core::file::FileReader::open(absolutFileName, [this, &absolutFileName, &onStatus, &atomar](int fd) {
579 onStatus(errno);
580
581 if (fd >= 0) {
582 if (httpMajor == 1) {
583 atomar = false;
584
585 set("Content-Type", web::http::MimeTypes::contentType(absolutFileName), false);
586 set("Last-Modified", httputils::file_mod_http_date(absolutFileName), false);
587 if (httpMinor == 1 && contentLength == 0) {
588 set("Transfer-Encoding", "chunked");
589 } else {
590 set("Content-Length", std::to_string(std::filesystem::file_size(absolutFileName) + contentLength));
591 }
592
594 }
595 } else {
597 }
598 })->pipe(this);
599 } else {
600 errno = EINVAL;
601 onStatus(errno);
602 }
603 } else {
604 errno = ENOENT;
605 onStatus(errno);
606 }
607
608 return atomar;
609 }
610
611 bool MasterRequest::executeUpgrade(const std::string& url, const std::string& protocols, const std::function<void(bool)>& onStatus) {
612 this->url = url;
613
614 set("Connection", "Upgrade", true);
615 set("Upgrade", protocols, true);
616
617 web::http::client::SocketContextUpgradeFactory* socketContextUpgradeFactory =
619
620 if (socketContextUpgradeFactory != nullptr) {
621 LOG(DEBUG) << connectionName << " HTTP: "
622 << "SocketContextUpgradeFactory create success: " << socketContextUpgradeFactory->name();
623 LOG(DEBUG) << connectionName << " HTTP: Initiating upgrade: " << method << " " << url
624 << " HTTP/" + std::to_string(httpMajor) + "." + std::to_string(httpMinor);
625
626 } else {
627 LOG(DEBUG) << connectionName << " HTTP: "
628 << "SocketContextUpgradeFactory create failed: " << protocols;
629 LOG(DEBUG) << connectionName << " HTTP: Not initiating upgrade " << method << " " << url
630 << " HTTP/" + std::to_string(httpMajor) + "." + std::to_string(httpMinor);
631 }
632
633 LOG(DEBUG) << connectionName << " HTTP: Upgrade request:\n"
635 url,
636 "HTTP/" + std::to_string(httpMajor) + "." + std::to_string(httpMinor),
641 std::vector<char>());
642
643 onStatus(socketContextUpgradeFactory != nullptr);
644
645 if (socketContextUpgradeFactory != nullptr) {
647
648 socketContextUpgradeFactory->checkRefCount();
649 } else {
651 }
652
653 return true;
654 }
655
657 return true;
658 }
659
661 return true;
662 }
663
665 const std::string httpVersion = "HTTP/" + std::to_string(httpMajor) + "." + std::to_string(httpMinor);
666
667 std::string queryString;
668 if (!queries.empty()) {
669 queryString += "?";
670 for (auto& [key, value] : queries) {
671 queryString += httputils::url_encode(key) + "=" + httputils::url_encode(value) + "&";
672 }
673 queryString.pop_back();
674 }
675
676 socketContext->sendToPeer(method + " " + url + queryString + " " + httpVersion + "\r\n");
678
679 if (!headers.contains("Transfer-Encoding") && contentLength > 0) {
680 set("Content-Length", std::to_string(contentLength));
681 }
682
683 for (const auto& [field, value] : headers) {
684 socketContext->sendToPeer(std::string(field).append(":").append(value).append("\r\n"));
685 }
686
687 for (const auto& [name, value] : cookies) {
688 socketContext->sendToPeer(std::string("Cookie:").append(name).append("=").append(value).append("\r\n"));
689 }
690
692
693 return true;
694 }
695
696 bool MasterRequest::executeSendFragment(const char* chunk, std::size_t chunkLen) {
698 socketContext->sendToPeer(to_hex_str(chunkLen).append("\r\n"));
699 }
700
701 socketContext->sendToPeer(chunk, chunkLen);
702 contentLengthSent += chunkLen;
703
706 contentLength += chunkLen;
707 }
708
709 return true;
710 }
711
712 void MasterRequest::requestPrepared(const std::shared_ptr<MasterRequest>& request) {
714 }
715
716 void MasterRequest::deliverResponse(const std::shared_ptr<MasterRequest>& request, const std::shared_ptr<Response>& response) {
717 onResponseReceived(request, response);
718 }
719
720 void MasterRequest::deliverResponseParseError(const std::shared_ptr<MasterRequest>& request, const std::string& message) {
721 onResponseParseError(request, message);
722 }
723
725 if (isConnected()) {
727 executeSendFragment("", 0); // For transfer encoding chunked. Terminate the chunk sequence.
728
729 if (!trailer.empty()) {
730 for (auto& [field, value] : trailer) {
731 socketContext->sendToPeer(std::string(field).append(":").append(value).append("\r\n"));
732 }
734 }
735 }
736
738 }
739 }
740
741 void MasterRequest::onSourceConnect(core::pipe::Source* source) {
742 if (isConnected()) {
744 source->start();
745 }
746 } else {
747 source->stop();
748 }
749 }
750
751 void MasterRequest::onSourceData(const char* chunk, std::size_t chunkLen) {
752 if (isConnected()) {
753 executeSendFragment(chunk, chunkLen);
754 }
755 }
756
764
765 void MasterRequest::onSourceError(int errnum) {
766 errno = errnum;
767
768 if (isConnected()) {
771
773 }
774 }
775
776} // namespace web::http::client
#define LOG(level)
Definition Logger.h:148
static FileReader * open(const std::string &path, const std::function< void(int)> &callback)
bool isStreaming() const
Definition Sink.cpp:66
void stop()
Definition Sink.cpp:70
virtual void stop()=0
bool pipe(Sink *sink)
Definition Source.cpp:60
virtual void start()=0
void sendToPeer(const std::string &data) const
const std::string & getConnectionName() const
void setSocketContext(SocketContext *socketContext)
SocketConnection * getSocketConnection() const
void sendToPeer(const char *chunk, std::size_t chunkLen) const final
bool streamToPeer(core::pipe::Source *source) const
LogMessage(Level level, int verboseLevel=-1, bool withErrno=false)
Definition Logger.cpp:280
static std::string contentType(const std::string &file)
core::socket::stream::SocketContext * create(core::socket::stream::SocketConnection *socketConnection) final
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)
Definition Request.cpp:341
bool executeUpgrade(const std::string &url, const std::string &protocols, const std::function< void(bool)> &onStatus)
Definition Request.cpp:611
bool send(const char *chunk, std::size_t chunkLen, const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &onResponseReceived, const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &onResponseParseError)
Definition Request.cpp:313
void onSourceData(const char *chunk, std::size_t chunkLen) override
Definition Request.cpp:751
web::http::client::SocketContext * socketContext
Definition Request.h:229
void onSourceConnect(core::pipe::Source *source) override
Definition Request.cpp:741
bool requestEventSource(const std::string &url, const std::function< std::size_t()> &onServerSentEvent, const std::function< void()> &onOpen, const std::function< void()> &onError)
Definition Request.cpp:434
MasterRequest(MasterRequest &&) noexcept
Definition Request.cpp:270
std::function< void(const std::shared_ptr< Request > &, const std::string &message)> onResponseParseError
Definition Request.h:227
void requestPrepared(const std::shared_ptr< MasterRequest > &request)
Definition Request.cpp:712
void deliverResponseParseError(const std::shared_ptr< MasterRequest > &request, const std::string &message)
Definition Request.cpp:720
std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> onResponseReceived
Definition Request.h:226
void deliverResponse(const std::shared_ptr< MasterRequest > &request, const std::shared_ptr< Response > &response)
Definition Request.cpp:716
void onSourceError(int errnum) override
Definition Request.cpp:765
bool upgrade(const std::string &url, const std::string &protocols, const std::function< void(bool)> &onUpgradeInitiate, const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &, bool)> &onResponseReceived, const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &onResponseParseError)
Definition Request.cpp:351
bool executeSendFragment(const char *chunk, std::size_t chunkLen)
Definition Request.cpp:696
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)
Definition Request.cpp:482
std::list< RequestCommand * > requestCommands
Definition Request.h:222
std::shared_ptr< MasterRequest > getMasterRequest() const
Definition Request.cpp:113
MasterRequest(SocketContext *socketContext, const std::string &hostFieldValue)
Definition Request.cpp:264
void setMasterRequest(const std::shared_ptr< MasterRequest > &masterRequest)
Definition Request.cpp:109
MasterRequest & sendFragment(const std::string &data)
Definition Request.cpp:520
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)
Definition Request.cpp:525
MasterRequest & sendFragment(const char *chunk, std::size_t chunkLen)
Definition Request.cpp:510
bool executeSendFile(const std::string &file, const std::function< void(int)> &onStatus)
Definition Request.cpp:568
bool initiate(const std::shared_ptr< MasterRequest > &request)
Definition Request.cpp:544
std::weak_ptr< MasterRequest > masterRequest
Definition Request.h:231
SocketContext * getSocketContext() const
Definition Request.cpp:117
virtual bool execute(const std::shared_ptr< MasterRequest > &request)=0
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
CiStringMap< std::string > headers
Definition Request.h:130
Request & set(const std::string &field, const std::string &value, bool overwrite=true)
Definition Request.cpp:143
std::map< std::string, std::string > queries
Definition Request.h:129
TransferEncoding transferEncoding
Definition Request.h:138
const CiStringMap< std::string > & getCookies() const
Definition Request.cpp:260
Request(Request &&request) noexcept
Definition Request.cpp:84
ConnectionState connectionState
Definition Request.h:139
const std::map< std::string, std::string > & getQueries() const
Definition Request.cpp:248
CiStringMap< std::string > cookies
Definition Request.h:131
Request & cookie(const std::string &name, const std::string &value)
Definition Request.cpp:203
CiStringMap< std::string > trailer
Definition Request.h:132
Request & query(const std::string &key, const std::string &value)
Definition Request.cpp:217
std::string getConnectionName() const
Definition Request.cpp:105
Request & setTrailer(const std::string &field, const std::string &value, bool overwrite=true)
Definition Request.cpp:223
Request & cookie(const std::map< std::string, std::string > &cookies)
Definition Request.cpp:209
std::string header(const std::string &field) const
Definition Request.cpp:242
std::string connectionName
Definition Request.h:134
Request(const std::string &connectionName, const std::string &host)
Definition Request.cpp:77
Request & set(const std::map< std::string, std::string > &headers, bool overwrite=true)
Definition Request.cpp:189
std::size_t contentLength
Definition Request.h:136
std::string hostFieldValue
Definition Request.h:120
const CiStringMap< std::string > & getTrailer() const
Definition Request.cpp:256
Request & append(const std::string &field, const std::string &value)
Definition Request.cpp:131
Request & host(const std::string &hostFieldValue)
Definition Request.cpp:125
Request & type(const std::string &type)
Definition Request.cpp:197
const CiStringMap< std::string > & getHeaders() const
Definition Request.cpp:252
CiStringMap< std::string > headers
Definition Response.h:83
CiStringMap< CookieOptions > cookies
Definition Response.h:84
const std::string & get(const std::string &key, int i=0) const
Definition Response.cpp:53
std::vector< char > body
Definition Response.h:85
SocketContextUpgradeFactory * select(Request &req, Response &res) override
SocketContextUpgradeFactory * select(const std::string &protocols, Request &req)
void setSseEventReceiver(const std::function< std::size_t()> &onServerSentEvent)
void requestPrepared(const std::shared_ptr< MasterRequest > &request)
EndCommand(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)
SendFileCommand(const std::string &file, const std::function< void(int)> &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)
SendFragmentCommand(const char *chunk, std::size_t chunkLen)
SseCommand(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)
UpgradeCommand(const std::string &url, const std::string &protocols, const std::function< void(bool)> &onUpgradeInitiate, const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &onResponseReceived, const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &onResponseParseError)
std::string toString(const std::string &version, const std::string &statusCode, const std::string &reason, const web::http::CiStringMap< std::string > &header, const web::http::CiStringMap< web::http::CookieOptions > &cookies, const std::vector< char > &body)
std::string url_encode(const std::string &text)
std::string file_mod_http_date(const std::string &filePath)
std::string to_http_date(struct tm *tm=nullptr)
std::string toString(const std::string &method, const std::string &url, const std::string &version, const std::map< std::string, std::string > &queries, const web::http::CiStringMap< std::string > &header, const web::http::CiStringMap< std::string > &trailer, const web::http::CiStringMap< std::string > &cookies, const std::vector< char > &body)
bool ciEquals(const std::string &str1, const std::string &str2)
bool ciContains(const std::string &str1, const std::string &str2)
#define to_hex_str(int_val)
Definition Request.cpp:73