|
2 | 2 |
|
3 | 3 | /* This is not an example; it is a smoke test used in CI testing */ |
4 | 4 |
|
5 | | -#include <cstddef> |
6 | | -#include <cstdint> |
7 | | -#include <memory> |
8 | | -#include <sstream> |
9 | | -#include <string> |
10 | | - |
11 | | -namespace { |
12 | | - |
13 | | -const std::string writeChunk(1024, 'a'); |
14 | | -const std::string tryWritePayload(16 * 1024 * 1024, 'x'); |
15 | | -const std::string tryWriteEndPayload(32 * 1024 * 1024, 'y'); |
16 | | - |
17 | | -struct WriteState { |
18 | | - int remaining = 128; |
19 | | - bool aborted = false; |
| 5 | +struct Stream { |
| 6 | + int offset; |
| 7 | + bool aborted; |
20 | 8 | }; |
21 | 9 |
|
22 | | -struct TryWriteState { |
23 | | - const std::string *payload = nullptr; |
24 | | - uintmax_t baseOffset = 0; |
25 | | - bool aborted = false; |
26 | | -}; |
27 | | - |
28 | | -uint32_t crc32(const char *s, size_t n, uint32_t crc = 0xFFFFFFFF) { |
29 | | - |
30 | | - for (size_t i = 0; i < n; i++) { |
31 | | - unsigned char ch = static_cast<unsigned char>(s[i]); |
32 | | - for (size_t j = 0; j < 8; j++) { |
33 | | - uint32_t b = (ch ^ crc) & 1; |
34 | | - crc >>= 1; |
35 | | - if (b) crc = crc ^ 0xEDB88320; |
36 | | - ch >>= 1; |
37 | | - } |
38 | | - } |
39 | | - |
40 | | - return crc; |
| 10 | +std::string constantChunk; |
| 11 | + |
| 12 | +void streamData(auto *res, auto stream, int chunk) { |
| 13 | + |
| 14 | + if (stream->aborted) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + if (chunk < 1600) { |
| 19 | + res->cork([res, stream, chunk]() { |
| 20 | + auto ok = res->write(constantChunk); |
| 21 | + if (ok) { |
| 22 | + streamData(res, stream, chunk + 1); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + uWS::Loop::get()->defer([res, stream, chunk]() { |
| 27 | + streamData(res, stream, chunk + 1); |
| 28 | + }); |
| 29 | + }); |
| 30 | + } else { |
| 31 | + res->cork([res]() { |
| 32 | + res->end(); |
| 33 | + }); |
| 34 | + } |
41 | 35 | } |
42 | 36 |
|
43 | | -template <bool SSL> |
44 | | -bool writeLoop(uWS::HttpResponse<SSL> *res, const std::shared_ptr<WriteState> &state) { |
45 | | - while (!state->aborted && state->remaining) { |
46 | | - state->remaining--; |
47 | | - if (!res->write(writeChunk)) { |
48 | | - return false; |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - if (!state->aborted) { |
53 | | - res->end(); |
54 | | - } |
55 | | - |
56 | | - return true; |
57 | | -} |
58 | | - |
59 | | -template <bool SSL> |
60 | | -bool tryWriteLoop(uWS::HttpResponse<SSL> *res, const std::shared_ptr<TryWriteState> &state) { |
61 | | - if (state->aborted) { |
62 | | - return true; |
63 | | - } |
| 37 | +int main() { |
64 | 38 |
|
65 | | - uintmax_t sent = res->getWriteOffset() - state->baseOffset; |
66 | | - std::string_view remaining(state->payload->data() + sent, state->payload->size() - (size_t) sent); |
67 | | - if (res->tryWrite(remaining)) { |
68 | | - res->end(); |
69 | | - return true; |
| 39 | + for (int i = 0; i < 65536; i++) { |
| 40 | + constantChunk.append("a", 1); |
70 | 41 | } |
71 | 42 |
|
72 | | - return false; |
73 | | -} |
74 | | - |
75 | | -} |
76 | | - |
77 | | -int main() { |
78 | 43 | uWS::SSLApp({ |
79 | 44 | .key_file_name = "misc/key.pem", |
80 | 45 | .cert_file_name = "misc/cert.pem", |
81 | 46 | .passphrase = "1234" |
82 | | - }).get("/write", [](auto *res, auto */*req*/) { |
83 | | - auto state = std::make_shared<WriteState>(); |
84 | | - |
85 | | - res->onAborted([state]() { |
86 | | - state->aborted = true; |
87 | | - }); |
88 | | - |
89 | | - if (!writeLoop(res, state)) { |
90 | | - res->onWritable([res, state](uintmax_t) { |
91 | | - return writeLoop(res, state); |
92 | | - }); |
93 | | - } |
94 | | - }).get("/trywrite", [](auto *res, auto */*req*/) { |
95 | | - auto state = std::make_shared<TryWriteState>(); |
96 | | - state->payload = &tryWritePayload; |
97 | | - state->baseOffset = res->getWriteOffset(); |
98 | | - |
99 | | - res->onAborted([state]() { |
100 | | - state->aborted = true; |
101 | | - }); |
102 | | - |
103 | | - if (!tryWriteLoop(res, state)) { |
104 | | - res->onWritable([res, state](uintmax_t) { |
105 | | - return tryWriteLoop(res, state); |
106 | | - }); |
107 | | - } |
108 | | - }).get("/trywrite-end", [](auto *res, auto */*req*/) { |
109 | | - auto state = std::make_shared<TryWriteState>(); |
110 | | - state->payload = &tryWriteEndPayload; |
111 | | - state->baseOffset = res->getWriteOffset(); |
| 47 | + }).get("/*", [](auto *res, auto */*req*/) { |
112 | 48 |
|
113 | | - res->onAborted([state]() { |
114 | | - state->aborted = true; |
115 | | - }); |
116 | | - |
117 | | - if (!res->tryWrite(*state->payload)) { |
118 | | - res->onWritable([res, state](uintmax_t offset) { |
119 | | - if (state->aborted) { |
120 | | - return true; |
121 | | - } |
122 | | - |
123 | | - uintmax_t sent = offset - state->baseOffset; |
124 | | - std::string_view remaining(state->payload->data() + sent, state->payload->size() - (size_t) sent); |
125 | | - res->end(remaining); |
126 | | - return true; |
127 | | - }); |
128 | | - } else { |
129 | | - res->end(); |
130 | | - } |
131 | | - }).post("/*", [](auto *res, auto *req) { |
132 | | - |
133 | | - auto isAborted = std::make_shared<bool>(false); |
134 | | - uint32_t crc = 0xFFFFFFFF; |
135 | | - |
136 | | - /* Display the headers */ |
137 | | - std::cout << " --- " << req->getUrl() << " --- " << std::endl; |
138 | | - for (auto [key, value] : *req) { |
139 | | - std::cout << key << ": " << value << std::endl; |
140 | | - } |
141 | | - |
142 | | - res->onData([res, isAborted, crc](std::string_view chunk, bool isFin) mutable { |
143 | | - if (chunk.length()) { |
144 | | - crc = crc32(chunk.data(), chunk.length(), crc); |
145 | | - } |
146 | | - |
147 | | - if (isFin && !*isAborted) { |
148 | | - std::stringstream s; |
149 | | - s << std::hex << (~crc) << std::endl; |
150 | | - res->end(s.str()); |
151 | | - } |
152 | | - }); |
| 49 | + auto stream = std::make_shared<Stream>(0, false); |
| 50 | + streamData(res, stream, 0); |
153 | 51 |
|
154 | | - res->onAborted([isAborted]() { |
155 | | - *isAborted = true; |
| 52 | + res->onAborted([stream]() { |
| 53 | + stream->aborted = true; |
156 | 54 | }); |
157 | 55 | }).listen(3000, [](auto *listen_socket) { |
158 | 56 | if (listen_socket) { |
|
0 commit comments