2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#include "web/http/server/Response.h"
45#include "core/file/FileReader.h"
46#include "core/socket/stream/SocketConnection.h"
47#include "web/http/MimeTypes.h"
48#include "web/http/StatusCodes.h"
49#include "web/http/server/SocketContextUpgradeFactorySelector.h"
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
53#include "log/Logger.h"
54#include "utils/system/time.h"
55#include "web/http/CiStringMap.h"
56#include "web/http/http_utils.h"
63#include <system_error>
68#define to_hex_str(int_val) (static_cast<std::ostringstream const&>(std::ostringstream() << std::uppercase << std::hex << int_val)).str()
70namespace web::http::
server {
109 const std::map<std::string, std::string>::iterator it =
headers.find(field);
112 set(field
, it->second.append(
", ").append(value)
);
121 for (
const auto& [field, value] : headers) {
129 if (!value.empty()) {
131 headers.insert_or_assign(field, value);
133 headers.insert({field, value});
145 headers.erase(
"Transfer-Encoding");
149 headers.erase(
"Content-Length");
175 return set("Content-Type", type
);
178 Response&
Response::
cookie(
const std::string& name,
const std::string& value,
const std::map<std::string, std::string>& options) {
185 std::map<std::string, std::string> opts = options;
187 opts.erase(
"Max-Age");
188 const time_t time = 0;
195 if (!value.empty()) {
197 trailer.insert_or_assign(field, value);
199 trailer.insert({field, value});
201 if (!
headers.contains(
"Trailer")) {
204 headers[
"Trailer"].append(
"," + field);
215 set("Content-Type", "application/octet-stream", false);
217 set("Content-Length", std::to_string(chunkLen)
);
225 if (!chunk.empty()) {
226 set("Content-Type", "text/html; charset=utf-8", false);
229 send(chunk.data()
, chunk.size()
);
240
241
242
243
248 if (request !=
nullptr) {
253 if (socketContextUpgradeFactory !=
nullptr) {
254 name = socketContextUpgradeFactory
->name();
256 <<
" HTTP upgrade: SocketContextUpgradeFactory created successful: " << name;
262 <<
" HTTP upgrade: SocketContextUpgrade created successful: " << name;
265 <<
" HTTP upgrade: Create SocketContextUpgrade failed: " << name;
271 <<
" HTTP upgrade: SocketContextUpgradeFactory not supported: " << request->
get("upgrade");
286 LOG(ERROR) <<
"HTTP upgrade: SocketContext has gone away";
292 void Response::
sendFile(
const std::string& file,
const std::function<
void(
int)>& callback) {
294 std::string absolutFileName = file;
296 if (std::filesystem::exists(absolutFileName)) {
298 absolutFileName = std::filesystem::canonical(absolutFileName);
300 if (std::filesystem::is_regular_file(absolutFileName, ec) && !ec) {
309 set("Transfer-Encoding", "chunked");
311 set("Content-Length", std::to_string(std::filesystem::file_size(absolutFileName))
);
348 set("X-Powered-By", "snode.c");
350 for (
const auto& [field, value] :
headers) {
354 for (
const auto& [cookie, cookieValue] :
cookies) {
355 const std::string cookieString = std::accumulate(
359 [](
const std::string& str,
const std::pair<
const std::string&,
const std::string&> option) -> std::string {
360 return str +
"; " + option.first + (!option.second.empty() ?
"=" + option.second :
"");
398 for (
auto& [field, value] :
trailer) {
static FileReader * open(const std::string &path)
void pipe(Sink *sink, const std::function< void(int)> &callback)
void sendToPeer(const std::string &data) const
const std::string & getConnectionName() const
SocketConnection * getSocketConnection() const
virtual void switchSocketContext(SocketContext *newSocketContext)
void sendToPeer(const char *chunk, std::size_t chunkLen) const final
bool streamToPeer(core::pipe::Source *source) const
const std::map< std::string, std::string > & getOptions() const
CookieOptions(const std::string &value, const std::map< std::string, std::string > &options)
const std::string & getValue() const
static std::string contentType(const std::string &file)
core::socket::stream::SocketContext * create(core::socket::stream::SocketConnection *socketConnection) final
virtual std::string name()=0
static std::string reason(int status)
const std::string & get(const std::string &key, int i=0) const
void onSourceData(const char *chunk, std::size_t chunkLen) override
Response(SocketContext *socketContext)
SocketContext * getSocketContext() const
SocketContext * socketContext
void onSourceConnect(core::pipe::Source *source) override
web::http::CiStringMap< web::http::CookieOptions > cookies
Response & status(int statusCode)
void send(const std::string &chunk)
void send(const char *chunk, std::size_t chunkLen)
Response & setTrailer(const std::string &field, const std::string &value, bool overwrite=true)
Response & set(const std::map< std::string, std::string > &headers, bool overwrite=true)
web::http::CiStringMap< std::string > trailer
web::http::CiStringMap< std::string > headers
Response & set(const std::string &field, const std::string &value, bool overwrite=true)
TransferEncoding transferEncoding
void upgrade(const std::shared_ptr< Request > &request, const std::function< void(const std::string &)> &status)
Response & type(const std::string &type)
void onSourceError(int errnum) override
core::socket::stream::SocketContext * socketContextUpgrade
std::size_t contentLength
Response & sendFragment(const std::string &chunk)
Response & cookie(const std::string &name, const std::string &value, const std::map< std::string, std::string > &options={})
Response & append(const std::string &field, const std::string &value)
void sendStatus(int statusCode)
Response & sendFragment(const char *chunk, std::size_t chunkLen)
void onSourceEof() override
void sendFile(const std::string &file, const std::function< void(int)> &callback)
Response & clearCookie(const std::string &name, const std::map< std::string, std::string > &options={})
ConnectionState connectionState
const std::string & header(const std::string &field)
SocketContextUpgradeFactory * select(Request &req, Response &res) override
static SocketContextUpgradeFactorySelector * instance()
void responseCompleted(bool success)
std::string to_http_date(struct tm *tm)
std::string file_mod_http_date(const std::string &filePath)
struct tm * gmtime(const time_t *timep)
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)