forked from awwit/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerHttp2Stream.cpp
More file actions
110 lines (75 loc) · 2.66 KB
/
ServerHttp2Stream.cpp
File metadata and controls
110 lines (75 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "ServerHttp2Stream.h"
#include "extensions/Sendfile.h"
namespace HttpServer
{
ServerHttp2Stream::ServerHttp2Stream(Socket::Adapter &sock, const ServerSettings &settings, ServerControls &controls, Http2::IncStream *stream) noexcept
: ServerHttp2Protocol(sock, settings, controls, stream)
{
}
void ServerHttp2Stream::close()
{
this->stream->close();
}
ServerProtocol *ServerHttp2Stream::process()
{
struct Request req;
req.timeout = std::chrono::milliseconds(5000);
req.protocol_variant = Transfer::ProtocolVariant::HTTP_2;
req.incoming_headers = std::move(this->stream->incoming_headers);
req.incoming_data = std::move(this->stream->incoming_data);
req.incoming_files = std::move(this->stream->incoming_files);
auto const &headers = req.incoming_headers;
auto const it_scheme = headers.find(":scheme");
if (headers.cend() == it_scheme)
{
return this;
}
const std::string &scheme = it_scheme->second;
const int default_port = (scheme == "https") ? 443 : (scheme == "http") ? 80 : 0;
auto const it_host = headers.find(":authority");
if (headers.cend() == it_host)
{
return this;
}
const std::string &host_header = it_host->second;
// Поиск разделителя, за которым помещается номер порта, если указан
const size_t delimiter = host_header.find(':');
// Получить имя (или адрес)
req.host = host_header.substr(0, delimiter);
// Получить номер порта
const int port = (std::string::npos != delimiter) ? std::strtol(host_header.substr(delimiter + 1).c_str(), nullptr, 10) : default_port;
const ServerApplicationSettings *app_sets = this->settings.apps_tree.find(req.host);
// Если приложение найдено
if (nullptr == app_sets || (app_sets->ports.cend() == app_sets->ports.find(port) && app_sets->tls_ports.cend() == app_sets->tls_ports.find(port) ) )
{
return this;
}
auto const it_method = headers.find(":method");
if (headers.cend() == it_method)
{
return this;
}
req.method = it_method->second;
auto const it_path = headers.find(":path");
if (headers.cend() == it_path)
{
return this;
}
req.path = it_path->second;
req.app_exit_code = EXIT_FAILURE;
this->runApplication(req, *app_sets);
for (auto const &it : req.incoming_files)
{
std::remove(it.second.getTmpName().c_str() );
}
if (EXIT_SUCCESS == req.app_exit_code)
{
// Http2::OutStream out(*stream);
// auto tmp = req.protocol_data;
// req.protocol_data = &out;
Sendfile::xSendfile(std::ref(*this), req, this->settings.mimes_types);
// req.protocol_data = tmp;
}
return this;
}
};