forked from RipcordSoftware/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunked_response_stream.cpp
More file actions
54 lines (45 loc) · 1.96 KB
/
Copy pathchunked_response_stream.cpp
File metadata and controls
54 lines (45 loc) · 1.96 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
#include "chunked_response_stream.h"
const rs::httpserver::Stream::byte rs::httpserver::ChunkedResponseStream::endOfLine_[2] = { '\r', '\n' };
const rs::httpserver::Stream::byte rs::httpserver::ChunkedResponseStream::endOfBlocks_[5] = { '0', '\r', '\n', '\r', '\n' };
int rs::httpserver::ChunkedResponseStream::Write(const byte* buffer, int offset, int count) {
auto blocks = count / Config::MaxResponseChunkSize;
auto overflow = count % Config::MaxResponseChunkSize;
for (int i = 0; i < blocks; i++) {
stream_.Write(&maxBlockSizeHeader_[0], 0, maxBlockSizeHeader_.size());
stream_.Write(buffer, offset, Config::MaxResponseChunkSize);
stream_.Write(endOfLine_, 0, sizeof(endOfLine_));
offset += Config::MaxResponseChunkSize;
position_ += Config::MaxResponseChunkSize;
length_ += Config::MaxResponseChunkSize;
}
if (overflow > 0) {
auto header = GetHeader(overflow);
stream_.Write(&header[0], 0, header.size());
stream_.Write(buffer, offset, overflow);
stream_.Write(endOfLine_, 0, sizeof(endOfLine_));
position_ += overflow;
length_ += overflow;
}
return count;
}
void rs::httpserver::ChunkedResponseStream::Flush() {
stream_.Write(endOfBlocks_, 0, sizeof(endOfBlocks_));
stream_.Flush();
}
std::vector<rs::httpserver::Stream::byte> rs::httpserver::ChunkedResponseStream::GetHeader(int blockSize) {
if (blockSize <= 0) {
return std::vector<Stream::byte>({'0', '\r', '\n'});
} else {
auto chars = 0;
while ((blockSize >> (4 * chars)) > 0) ++chars;
std::vector<Stream::byte> buffer(chars + 2);
for (auto i = 0; i < chars; ++i) {
auto index = chars - 1 - i;
buffer[index] = (blockSize >> (4 * i)) & 0x0f;
buffer[index] += buffer[index] > 9 ? ('a' - 10) : '0';
}
buffer[chars] = '\r';
buffer[chars + 1] = '\n';
return buffer;
}
}