forked from awwit/httpserverapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerHttp2.cpp
More file actions
174 lines (116 loc) · 4.6 KB
/
ServerHttp2.cpp
File metadata and controls
174 lines (116 loc) · 4.6 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "ServerHttp2.h"
#include "../../transfer/http2/HPack.h"
#include <array>
#include <random>
namespace HttpServer
{
ServerHttp2::ServerHttp2(Socket::Adapter *sock, Http2::OutStream *stream)
: ServerProtocol(sock), stream(stream)
{
}
ServerHttp2::~ServerHttp2() noexcept {
delete this->stream;
}
static uint8_t getPaddingSize(const size_t dataSize)
{
if (0 == dataSize) {
return 0;
}
std::random_device rd;
uint8_t padding = rd();
while (dataSize <= padding) {
padding /= 2;
}
return padding;
}
bool ServerHttp2::sendHeaders(const Http::StatusCode status, std::vector<std::pair<std::string, std::string> > &headers, const std::chrono::milliseconds &timeout, const bool endStream) const
{
std::vector<char> buf;
buf.reserve(4096);
buf.resize(Http2::FRAME_HEADER_SIZE + sizeof(uint8_t) );
headers.emplace(headers.begin(), ":status", std::to_string(static_cast<int>(status) ) );
HPack::pack(buf, headers, this->stream->dynamic_table);
uint32_t data_size = buf.size() - Http2::FRAME_HEADER_SIZE - sizeof(uint8_t);
const uint8_t padding = getPaddingSize(data_size);
const uint16_t padding_size = padding + sizeof(uint8_t);
if (data_size + padding_size > this->stream->settings.max_frame_size) {
data_size = this->stream->settings.max_frame_size - padding_size;
}
const size_t frame_size = data_size + padding_size;
buf.resize(frame_size + Http2::FRAME_HEADER_SIZE);
Http2::FrameFlag flags = Http2::FrameFlag::END_HEADERS;
if (endStream) {
flags |= Http2::FrameFlag::END_STREAM;
}
flags |= Http2::FrameFlag::PADDED;
buf[Http2::FRAME_HEADER_SIZE] = padding;
if (padding) {
std::fill(buf.end() - padding, buf.end(), 0);
}
this->stream->setHttp2FrameHeader(reinterpret_cast<uint8_t *>(buf.data() ), frame_size, Http2::FrameType::HEADERS, flags);
const std::unique_lock<std::mutex> lock(*this->stream->mtx);
return this->sock->nonblock_send(buf.data(), buf.size(), timeout) > 0;
}
void ServerHttp2::sendWindowUpdate(const uint32_t size, const std::chrono::milliseconds &timeout) const
{
std::array<uint8_t, Http2::FRAME_HEADER_SIZE + sizeof(uint32_t)> buf;
uint8_t *addr = buf.data();
addr = this->stream->setHttp2FrameHeader(addr, sizeof(uint32_t), Http2::FrameType::WINDOW_UPDATE, Http2::FrameFlag::EMPTY);
*reinterpret_cast<uint32_t *>(addr) = ::htonl(size);
const std::unique_lock<std::mutex> lock(*this->stream->mtx);
this->sock->nonblock_send(buf.data(), buf.size(), timeout);
}
long ServerHttp2::sendData(const void *src, const size_t size, const std::chrono::milliseconds &timeout, const bool endStream) const
{
const uint8_t *data = reinterpret_cast<const uint8_t *>(src);
std::vector<uint8_t> buf;
buf.reserve(this->stream->settings.max_frame_size + Http2::FRAME_HEADER_SIZE);
size_t total = 0;
while (total < size)
{
size_t data_size = (size - total < this->stream->settings.max_frame_size) ? size - total : this->stream->settings.max_frame_size;
const uint8_t padding = getPaddingSize(data_size);
const uint16_t padding_size = padding + sizeof(uint8_t);
if (data_size + padding_size > this->stream->settings.max_frame_size) {
data_size = this->stream->settings.max_frame_size - padding_size;
}
const size_t frame_size = data_size + padding_size;
buf.resize(frame_size + Http2::FRAME_HEADER_SIZE);
if (static_cast<int32_t>(this->stream->window_size_out - this->stream->settings.max_frame_size) <= 0)
{
size_t update_size = this->stream->settings.initial_window_size + (size - total) - this->stream->window_size_out;
if (update_size > Http2::MAX_WINDOW_UPDATE) {
update_size = Http2::MAX_WINDOW_UPDATE;
}
this->sendWindowUpdate(static_cast<uint32_t>(update_size), timeout);
this->stream->window_size_out += update_size;
}
Http2::FrameFlag flags = Http2::FrameFlag::EMPTY;
if (endStream && (total + data_size >= size) ) {
flags |= Http2::FrameFlag::END_STREAM;
}
size_t cur = Http2::FRAME_HEADER_SIZE;
if (padding_size) {
flags |= Http2::FrameFlag::PADDED;
buf[cur] = padding;
++cur;
}
this->stream->setHttp2FrameHeader(buf.data(), frame_size, Http2::FrameType::DATA, flags);
std::copy(data, data + data_size, buf.begin() + cur);
if (padding) {
std::fill(buf.end() - padding, buf.end(), 0);
}
this->stream->lock();
const long sended = this->sock->nonblock_send(buf.data(), buf.size(), timeout);
this->stream->unlock();
if (sended <= 0) {
total = 0;
break;
}
this->stream->window_size_out -= frame_size;
data += data_size;
total += data_size;
}
return static_cast<long>(total);
}
}