opal-up 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/up_ext/App.h +665 -544
- data/ext/up_ext/AsyncSocket.h +307 -284
- data/ext/up_ext/AsyncSocketData.h +35 -51
- data/ext/up_ext/BloomFilter.h +37 -42
- data/ext/up_ext/ChunkedEncoding.h +174 -175
- data/ext/up_ext/ClientApp.h +20 -23
- data/ext/up_ext/HttpContext.h +476 -381
- data/ext/up_ext/HttpContextData.h +20 -20
- data/ext/up_ext/HttpErrors.h +14 -10
- data/ext/up_ext/HttpParser.h +631 -563
- data/ext/up_ext/HttpResponse.h +526 -460
- data/ext/up_ext/HttpResponseData.h +59 -55
- data/ext/up_ext/HttpRouter.h +328 -310
- data/ext/up_ext/Loop.h +174 -168
- data/ext/up_ext/LoopData.h +60 -67
- data/ext/up_ext/MoveOnlyFunction.h +71 -80
- data/ext/up_ext/PerMessageDeflate.h +218 -198
- data/ext/up_ext/ProxyParser.h +100 -99
- data/ext/up_ext/QueryParser.h +91 -84
- data/ext/up_ext/TopicTree.h +273 -268
- data/ext/up_ext/Utilities.h +25 -25
- data/ext/up_ext/WebSocket.h +376 -310
- data/ext/up_ext/WebSocketContext.h +487 -372
- data/ext/up_ext/WebSocketContextData.h +74 -62
- data/ext/up_ext/WebSocketData.h +53 -46
- data/ext/up_ext/WebSocketExtensions.h +194 -178
- data/ext/up_ext/WebSocketHandshake.h +115 -110
- data/ext/up_ext/WebSocketProtocol.h +441 -398
- data/ext/up_ext/up_ext.c +43 -5
- data/lib/up/ruby/cluster.rb +29 -6
- data/lib/up/version.rb +1 -1
- metadata +2 -2
data/ext/up_ext/HttpContext.h
CHANGED
@@ -20,483 +20,578 @@
|
|
20
20
|
|
21
21
|
/* This class defines the main behavior of HTTP and emits various events */
|
22
22
|
|
23
|
-
#include "
|
23
|
+
#include "AsyncSocket.h"
|
24
24
|
#include "HttpContextData.h"
|
25
25
|
#include "HttpResponseData.h"
|
26
|
-
#include "
|
26
|
+
#include "Loop.h"
|
27
27
|
#include "WebSocketData.h"
|
28
28
|
|
29
|
-
#include <string_view>
|
30
|
-
#include <iostream>
|
31
29
|
#include "MoveOnlyFunction.h"
|
30
|
+
#include <iostream>
|
31
|
+
#include <string_view>
|
32
32
|
|
33
33
|
namespace uWS {
|
34
|
-
template<bool> struct HttpResponse;
|
35
|
-
|
36
|
-
template <bool SSL>
|
37
|
-
struct HttpContext {
|
38
|
-
template<bool> friend struct TemplatedApp;
|
39
|
-
template<bool> friend struct HttpResponse;
|
40
|
-
private:
|
41
|
-
HttpContext() = delete;
|
42
|
-
|
43
|
-
/* Maximum delay allowed until an HTTP connection is terminated due to outstanding request or rejected data (slow loris protection) */
|
44
|
-
static const int HTTP_IDLE_TIMEOUT_S = 10;
|
34
|
+
template <bool> struct HttpResponse;
|
45
35
|
|
46
|
-
|
47
|
-
|
36
|
+
template <bool SSL> struct HttpContext {
|
37
|
+
template <bool> friend struct TemplatedApp;
|
38
|
+
template <bool> friend struct HttpResponse;
|
48
39
|
|
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
|
-
|
40
|
+
private:
|
41
|
+
HttpContext() = delete;
|
42
|
+
|
43
|
+
/* Maximum delay allowed until an HTTP connection is terminated due to
|
44
|
+
* outstanding request or rejected data (slow loris protection) */
|
45
|
+
static const int HTTP_IDLE_TIMEOUT_S = 10;
|
46
|
+
|
47
|
+
/* Minimum allowed receive throughput per second (clients uploading less than
|
48
|
+
* 16kB/sec get dropped) */
|
49
|
+
static const int HTTP_RECEIVE_THROUGHPUT_BYTES = 16 * 1024;
|
50
|
+
|
51
|
+
us_loop_t *getLoop() {
|
52
|
+
return us_socket_context_loop(SSL, getSocketContext());
|
53
|
+
}
|
54
|
+
|
55
|
+
us_socket_context_t *getSocketContext() {
|
56
|
+
return (us_socket_context_t *)this;
|
57
|
+
}
|
58
|
+
|
59
|
+
static us_socket_context_t *getSocketContext(us_socket_t *s) {
|
60
|
+
return (us_socket_context_t *)us_socket_context(SSL, s);
|
61
|
+
}
|
62
|
+
|
63
|
+
HttpContextData<SSL> *getSocketContextData() {
|
64
|
+
return (HttpContextData<SSL> *)us_socket_context_ext(SSL,
|
65
|
+
getSocketContext());
|
66
|
+
}
|
67
|
+
|
68
|
+
static HttpContextData<SSL> *getSocketContextDataS(us_socket_t *s) {
|
69
|
+
return (HttpContextData<SSL> *)us_socket_context_ext(SSL,
|
70
|
+
getSocketContext(s));
|
71
|
+
}
|
72
|
+
|
73
|
+
/* Init the HttpContext by registering libusockets event handlers */
|
74
|
+
HttpContext<SSL> *init() {
|
75
|
+
/* Handle socket connections */
|
76
|
+
us_socket_context_on_open(
|
77
|
+
SSL, getSocketContext(),
|
78
|
+
[](us_socket_t *s, int /*is_client*/, char * /*ip*/,
|
79
|
+
int /*ip_length*/) {
|
80
|
+
/* Any connected socket should timeout until it has a request */
|
81
|
+
us_socket_timeout(SSL, s, HTTP_IDLE_TIMEOUT_S);
|
82
|
+
|
83
|
+
/* Init socket ext */
|
84
|
+
new (us_socket_ext(SSL, s)) HttpResponseData<SSL>;
|
85
|
+
|
86
|
+
/* Call filter */
|
87
|
+
HttpContextData<SSL> *httpContextData = getSocketContextDataS(s);
|
88
|
+
for (auto &f : httpContextData->filterHandlers) {
|
89
|
+
f((HttpResponse<SSL> *)s, 1);
|
90
|
+
}
|
91
|
+
|
92
|
+
return s;
|
86
93
|
});
|
87
94
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
95
|
+
/* Handle socket disconnections */
|
96
|
+
us_socket_context_on_close(
|
97
|
+
SSL, getSocketContext(),
|
98
|
+
[](us_socket_t *s, int /*code*/, void * /*reason*/) {
|
99
|
+
/* Get socket ext */
|
100
|
+
HttpResponseData<SSL> *httpResponseData =
|
101
|
+
(HttpResponseData<SSL> *)us_socket_ext(SSL, s);
|
102
|
+
|
103
|
+
/* Call filter */
|
104
|
+
HttpContextData<SSL> *httpContextData = getSocketContextDataS(s);
|
105
|
+
for (auto &f : httpContextData->filterHandlers) {
|
106
|
+
f((HttpResponse<SSL> *)s, -1);
|
107
|
+
}
|
108
|
+
|
109
|
+
/* Signal broken HTTP request only if we have a pending request */
|
110
|
+
if (httpResponseData->onAborted) {
|
111
|
+
httpResponseData->onAborted();
|
112
|
+
}
|
113
|
+
|
114
|
+
/* Destruct socket ext */
|
115
|
+
httpResponseData->~HttpResponseData<SSL>();
|
116
|
+
|
117
|
+
return s;
|
108
118
|
});
|
109
119
|
|
110
|
-
|
111
|
-
|
120
|
+
/* Handle HTTP data streams */
|
121
|
+
us_socket_context_on_data(
|
122
|
+
SSL, getSocketContext(), [](us_socket_t *s, char *data, int length) {
|
123
|
+
// total overhead is about 210k down to 180k
|
124
|
+
// ~210k req/sec is the original perf with write in data
|
125
|
+
// ~200k req/sec is with cork and formatting
|
126
|
+
// ~190k req/sec is with http parsing
|
127
|
+
// ~180k - 190k req/sec is with varying routing
|
112
128
|
|
113
|
-
|
114
|
-
// ~210k req/sec is the original perf with write in data
|
115
|
-
// ~200k req/sec is with cork and formatting
|
116
|
-
// ~190k req/sec is with http parsing
|
117
|
-
// ~180k - 190k req/sec is with varying routing
|
129
|
+
HttpContextData<SSL> *httpContextData = getSocketContextDataS(s);
|
118
130
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
return s;
|
124
|
-
}
|
131
|
+
/* Do not accept any data while in shutdown state */
|
132
|
+
if (us_socket_is_shut_down(SSL, (us_socket_t *)s)) {
|
133
|
+
return s;
|
134
|
+
}
|
125
135
|
|
126
|
-
|
136
|
+
HttpResponseData<SSL> *httpResponseData =
|
137
|
+
(HttpResponseData<SSL> *)us_socket_ext(SSL, s);
|
127
138
|
|
128
|
-
|
129
|
-
|
139
|
+
/* Cork this socket */
|
140
|
+
((AsyncSocket<SSL> *)s)->cork();
|
130
141
|
|
131
|
-
|
132
|
-
|
142
|
+
/* Mark that we are inside the parser now */
|
143
|
+
httpContextData->isParsingHttp = true;
|
133
144
|
|
134
|
-
|
135
|
-
|
145
|
+
// clients need to know the cursor after http parse, not servers!
|
146
|
+
// how far did we read then? we need to know to continue with
|
147
|
+
// websocket parsing data? or?
|
136
148
|
|
137
|
-
|
149
|
+
void *proxyParser = nullptr;
|
138
150
|
#ifdef UWS_WITH_PROXY
|
139
|
-
|
151
|
+
proxyParser = &httpResponseData->proxyParser;
|
140
152
|
#endif
|
141
153
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
154
|
+
/* The return value is entirely up to us to interpret. The HttpParser
|
155
|
+
* only care for whether the returned value is DIFFERENT or not from
|
156
|
+
* passed user */
|
157
|
+
auto [err, returnedSocket] = httpResponseData->consumePostPadded(
|
158
|
+
data, (unsigned int)length, s, proxyParser,
|
159
|
+
[httpContextData](void *s, HttpRequest *httpRequest) -> void * {
|
160
|
+
/* For every request we reset the timeout and hang until user
|
161
|
+
* makes action */
|
162
|
+
/* Warning: if we are in shutdown state, resetting the timer is
|
163
|
+
* a security issue! */
|
164
|
+
us_socket_timeout(SSL, (us_socket_t *)s, 0);
|
147
165
|
|
148
166
|
/* Reset httpResponse */
|
149
|
-
HttpResponseData<SSL> *httpResponseData =
|
167
|
+
HttpResponseData<SSL> *httpResponseData =
|
168
|
+
(HttpResponseData<SSL> *)us_socket_ext(SSL,
|
169
|
+
(us_socket_t *)s);
|
150
170
|
httpResponseData->offset = 0;
|
151
171
|
|
152
|
-
/* Are we not ready for another request yet? Terminate the
|
153
|
-
|
154
|
-
|
155
|
-
|
172
|
+
/* Are we not ready for another request yet? Terminate the
|
173
|
+
* connection. */
|
174
|
+
if (httpResponseData->state &
|
175
|
+
HttpResponseData<SSL>::HTTP_RESPONSE_PENDING) {
|
176
|
+
us_socket_close(SSL, (us_socket_t *)s, 0, nullptr);
|
177
|
+
return nullptr;
|
156
178
|
}
|
157
179
|
|
158
180
|
/* Mark pending request and emit it */
|
159
|
-
httpResponseData->state =
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
181
|
+
httpResponseData->state =
|
182
|
+
HttpResponseData<SSL>::HTTP_RESPONSE_PENDING;
|
183
|
+
|
184
|
+
/* Mark this response as connectionClose if ancient or
|
185
|
+
* connection: close */
|
186
|
+
if (httpRequest->isAncient() ||
|
187
|
+
httpRequest->getHeader("connection").length() == 5) {
|
188
|
+
httpResponseData->state |=
|
189
|
+
HttpResponseData<SSL>::HTTP_CONNECTION_CLOSE;
|
164
190
|
}
|
165
191
|
|
166
192
|
/* Select the router based on SNI (only possible for SSL) */
|
167
193
|
auto *selectedRouter = &httpContextData->router;
|
168
194
|
if constexpr (SSL) {
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
195
|
+
void *domainRouter = us_socket_server_name_userdata(
|
196
|
+
SSL, (struct us_socket_t *)s);
|
197
|
+
if (domainRouter) {
|
198
|
+
selectedRouter = (decltype(selectedRouter))domainRouter;
|
199
|
+
}
|
173
200
|
}
|
174
201
|
|
175
202
|
/* Route the method and URL */
|
176
|
-
selectedRouter->getUserData() = {(HttpResponse<SSL> *)
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
203
|
+
selectedRouter->getUserData() = {(HttpResponse<SSL> *)s,
|
204
|
+
httpRequest};
|
205
|
+
if (!selectedRouter->route(
|
206
|
+
httpRequest->getCaseSensitiveMethod(),
|
207
|
+
httpRequest->getUrl())) {
|
208
|
+
/* We have to force close this socket as we have no handler
|
209
|
+
* for it */
|
210
|
+
us_socket_close(SSL, (us_socket_t *)s, 0, nullptr);
|
211
|
+
return nullptr;
|
181
212
|
}
|
182
213
|
|
183
|
-
/* First of all we need to check if this socket was deleted due
|
214
|
+
/* First of all we need to check if this socket was deleted due
|
215
|
+
* to upgrade */
|
184
216
|
if (httpContextData->upgradedWebSocket) {
|
185
|
-
|
186
|
-
|
217
|
+
/* We differ between closed and upgraded below */
|
218
|
+
return nullptr;
|
187
219
|
}
|
188
220
|
|
189
221
|
/* Was the socket closed? */
|
190
|
-
if (us_socket_is_closed(SSL, (struct us_socket_t *)
|
191
|
-
|
222
|
+
if (us_socket_is_closed(SSL, (struct us_socket_t *)s)) {
|
223
|
+
return nullptr;
|
192
224
|
}
|
193
225
|
|
194
226
|
/* We absolutely have to terminate parsing if shutdown */
|
195
|
-
if (us_socket_is_shut_down(SSL, (us_socket_t *)
|
196
|
-
|
227
|
+
if (us_socket_is_shut_down(SSL, (us_socket_t *)s)) {
|
228
|
+
return nullptr;
|
197
229
|
}
|
198
230
|
|
199
|
-
/* Returning from a request handler without responding or
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
231
|
+
/* Returning from a request handler without responding or
|
232
|
+
* attaching an onAborted handler is ill-use */
|
233
|
+
if (!((HttpResponse<SSL> *)s)->hasResponded() &&
|
234
|
+
!httpResponseData->onAborted) {
|
235
|
+
/* Throw exception here? */
|
236
|
+
std::cerr << "Error: Returning from a request handler "
|
237
|
+
"without responding or attaching an abort "
|
238
|
+
"handler is forbidden!"
|
239
|
+
<< std::endl;
|
240
|
+
std::terminate();
|
204
241
|
}
|
205
242
|
|
206
|
-
/* If we have not responded and we have a data handler, we need
|
207
|
-
|
208
|
-
|
243
|
+
/* If we have not responded and we have a data handler, we need
|
244
|
+
* to timeout to enfore client sending the data */
|
245
|
+
if (!((HttpResponse<SSL> *)s)->hasResponded() &&
|
246
|
+
httpResponseData->inStream) {
|
247
|
+
us_socket_timeout(SSL, (us_socket_t *)s, HTTP_IDLE_TIMEOUT_S);
|
209
248
|
}
|
210
249
|
|
211
250
|
/* Continue parsing */
|
212
251
|
return s;
|
213
|
-
|
214
|
-
|
252
|
+
},
|
253
|
+
[httpResponseData](void *user, std::string_view data,
|
254
|
+
bool fin) -> void * {
|
215
255
|
/* We always get an empty chunk even if there is no data */
|
216
256
|
if (httpResponseData->inStream) {
|
217
257
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
258
|
+
/* Todo: can this handle timeout for non-post as well? */
|
259
|
+
if (fin) {
|
260
|
+
/* If we just got the last chunk (or empty chunk), disable
|
261
|
+
* timeout */
|
262
|
+
us_socket_timeout(SSL, (struct us_socket_t *)user, 0);
|
263
|
+
} else {
|
264
|
+
/* We still have some more data coming in later, so reset
|
265
|
+
* timeout */
|
266
|
+
/* Only reset timeout if we got enough bytes (16kb/sec)
|
267
|
+
* since last time we reset here */
|
268
|
+
httpResponseData->received_bytes_per_timeout +=
|
269
|
+
(unsigned int)data.length();
|
270
|
+
if (httpResponseData->received_bytes_per_timeout >=
|
271
|
+
HTTP_RECEIVE_THROUGHPUT_BYTES * HTTP_IDLE_TIMEOUT_S) {
|
272
|
+
us_socket_timeout(SSL, (struct us_socket_t *)user,
|
273
|
+
HTTP_IDLE_TIMEOUT_S);
|
274
|
+
httpResponseData->received_bytes_per_timeout = 0;
|
230
275
|
}
|
276
|
+
}
|
231
277
|
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
/* Was the socket closed? */
|
236
|
-
if (us_socket_is_closed(SSL, (struct us_socket_t *) user)) {
|
237
|
-
return nullptr;
|
238
|
-
}
|
278
|
+
/* We might respond in the handler, so do not change timeout
|
279
|
+
* after this */
|
280
|
+
httpResponseData->inStream(data, fin);
|
239
281
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
282
|
+
/* Was the socket closed? */
|
283
|
+
if (us_socket_is_closed(SSL, (struct us_socket_t *)user)) {
|
284
|
+
return nullptr;
|
285
|
+
}
|
244
286
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
287
|
+
/* We absolutely have to terminate parsing if shutdown */
|
288
|
+
if (us_socket_is_shut_down(SSL, (us_socket_t *)user)) {
|
289
|
+
return nullptr;
|
290
|
+
}
|
291
|
+
|
292
|
+
/* If we were given the last data chunk, reset data handler to
|
293
|
+
* ensure following requests on the same socket won't trigger
|
294
|
+
* any previously registered behavior */
|
295
|
+
if (fin) {
|
296
|
+
httpResponseData->inStream = nullptr;
|
297
|
+
}
|
250
298
|
}
|
251
299
|
return user;
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
300
|
+
});
|
301
|
+
|
302
|
+
/* Mark that we are no longer parsing Http */
|
303
|
+
httpContextData->isParsingHttp = false;
|
304
|
+
|
305
|
+
/* If we got fullptr that means the parser wants us to close the
|
306
|
+
* socket from error (same as calling the errorHandler) */
|
307
|
+
if (returnedSocket == FULLPTR) {
|
308
|
+
/* For errors, we only deliver them "at most once". We don't care if
|
309
|
+
* they get halfways delivered or not. */
|
310
|
+
us_socket_write(SSL, s, httpErrorResponses[err].data(),
|
311
|
+
(int)httpErrorResponses[err].length(), false);
|
312
|
+
us_socket_shutdown(SSL, s);
|
313
|
+
/* Close any socket on HTTP errors */
|
314
|
+
us_socket_close(SSL, s, 0, nullptr);
|
315
|
+
/* This just makes the following code act as if the socket was
|
316
|
+
* closed from error inside the parser. */
|
317
|
+
returnedSocket = nullptr;
|
318
|
+
}
|
319
|
+
|
320
|
+
/* We need to uncork in all cases, except for nullptr (closed socket,
|
321
|
+
* or upgraded socket) */
|
322
|
+
if (returnedSocket != nullptr) {
|
323
|
+
/* Timeout on uncork failure */
|
324
|
+
auto [written, failed] =
|
325
|
+
((AsyncSocket<SSL> *)returnedSocket)->uncork();
|
326
|
+
if (failed) {
|
327
|
+
/* All Http sockets timeout by this, and this behavior match the
|
328
|
+
* one in HttpResponse::cork */
|
329
|
+
/* Warning: both HTTP_IDLE_TIMEOUT_S and HTTP_TIMEOUT_S are 10
|
330
|
+
* seconds and both are used the same */
|
331
|
+
((AsyncSocket<SSL> *)s)->timeout(HTTP_IDLE_TIMEOUT_S);
|
266
332
|
}
|
267
333
|
|
268
|
-
/* We need to
|
269
|
-
if (
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
/* We need to check if we should close this socket here now */
|
279
|
-
if (httpResponseData->state & HttpResponseData<SSL>::HTTP_CONNECTION_CLOSE) {
|
280
|
-
if ((httpResponseData->state & HttpResponseData<SSL>::HTTP_RESPONSE_PENDING) == 0) {
|
281
|
-
if (((AsyncSocket<SSL> *) s)->getBufferedAmount() == 0) {
|
282
|
-
((AsyncSocket<SSL> *) s)->shutdown();
|
283
|
-
/* We need to force close after sending FIN since we want to hinder
|
284
|
-
* clients from keeping to send their huge data */
|
285
|
-
((AsyncSocket<SSL> *) s)->close();
|
286
|
-
}
|
287
|
-
}
|
334
|
+
/* We need to check if we should close this socket here now */
|
335
|
+
if (httpResponseData->state &
|
336
|
+
HttpResponseData<SSL>::HTTP_CONNECTION_CLOSE) {
|
337
|
+
if ((httpResponseData->state &
|
338
|
+
HttpResponseData<SSL>::HTTP_RESPONSE_PENDING) == 0) {
|
339
|
+
if (((AsyncSocket<SSL> *)s)->getBufferedAmount() == 0) {
|
340
|
+
((AsyncSocket<SSL> *)s)->shutdown();
|
341
|
+
/* We need to force close after sending FIN since we want to
|
342
|
+
* hinder clients from keeping to send their huge data */
|
343
|
+
((AsyncSocket<SSL> *)s)->close();
|
288
344
|
}
|
289
|
-
|
290
|
-
return (us_socket_t *) returnedSocket;
|
345
|
+
}
|
291
346
|
}
|
292
347
|
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
/*
|
314
|
-
|
348
|
+
return (us_socket_t *)returnedSocket;
|
349
|
+
}
|
350
|
+
|
351
|
+
/* If we upgraded, check here (differ between nullptr close and
|
352
|
+
* nullptr upgrade) */
|
353
|
+
if (httpContextData->upgradedWebSocket) {
|
354
|
+
/* This path is only for upgraded websockets */
|
355
|
+
AsyncSocket<SSL> *asyncSocket =
|
356
|
+
(AsyncSocket<SSL> *)httpContextData->upgradedWebSocket;
|
357
|
+
|
358
|
+
/* Uncork here as well (note: what if we failed to uncork and we
|
359
|
+
* then pub/sub before we even upgraded?) */
|
360
|
+
auto [written, failed] = asyncSocket->uncork();
|
361
|
+
|
362
|
+
/* If we succeeded in uncorking, check if we have sent WebSocket FIN
|
363
|
+
*/
|
364
|
+
if (!failed) {
|
365
|
+
WebSocketData *webSocketData =
|
366
|
+
(WebSocketData *)asyncSocket->getAsyncSocketData();
|
367
|
+
if (webSocketData->isShuttingDown) {
|
368
|
+
/* In that case, also send TCP FIN (this is similar to what we
|
369
|
+
* have in ws drain handler) */
|
370
|
+
asyncSocket->shutdown();
|
371
|
+
}
|
315
372
|
}
|
316
373
|
|
317
|
-
/*
|
318
|
-
|
319
|
-
|
320
|
-
/* We cannot return nullptr to the underlying stack in any case */
|
321
|
-
return s;
|
322
|
-
});
|
374
|
+
/* Reset upgradedWebSocket before we return */
|
375
|
+
httpContextData->upgradedWebSocket = nullptr;
|
323
376
|
|
324
|
-
|
325
|
-
|
377
|
+
/* Return the new upgraded websocket */
|
378
|
+
return (us_socket_t *)asyncSocket;
|
379
|
+
}
|
326
380
|
|
327
|
-
|
328
|
-
|
381
|
+
/* It is okay to uncork a closed socket and we need to */
|
382
|
+
((AsyncSocket<SSL> *)s)->uncork();
|
329
383
|
|
330
|
-
|
331
|
-
|
332
|
-
/* We are now writable, so hang timeout again, the user does not have to do anything so we should hang until end or tryEnd rearms timeout */
|
333
|
-
us_socket_timeout(SSL, s, 0);
|
334
|
-
|
335
|
-
/* We expect the developer to return whether or not write was successful (true).
|
336
|
-
* If write was never called, the developer should still return true so that we may drain. */
|
337
|
-
bool success = httpResponseData->callOnWritable(httpResponseData->offset);
|
338
|
-
|
339
|
-
/* The developer indicated that their onWritable failed. */
|
340
|
-
if (!success) {
|
341
|
-
/* Skip testing if we can drain anything since that might perform an extra syscall */
|
342
|
-
return s;
|
343
|
-
}
|
344
|
-
|
345
|
-
/* We don't want to fall through since we don't want to mess with timeout.
|
346
|
-
* It makes little sense to drain any backpressure when the user has registered onWritable. */
|
347
|
-
return s;
|
348
|
-
}
|
349
|
-
|
350
|
-
/* Drain any socket buffer, this might empty our backpressure and thus finish the request */
|
351
|
-
/*auto [written, failed] = */asyncSocket->write(nullptr, 0, true, 0);
|
352
|
-
|
353
|
-
/* Should we close this connection after a response - and is this response really done? */
|
354
|
-
if (httpResponseData->state & HttpResponseData<SSL>::HTTP_CONNECTION_CLOSE) {
|
355
|
-
if ((httpResponseData->state & HttpResponseData<SSL>::HTTP_RESPONSE_PENDING) == 0) {
|
356
|
-
if (asyncSocket->getBufferedAmount() == 0) {
|
357
|
-
asyncSocket->shutdown();
|
358
|
-
/* We need to force close after sending FIN since we want to hinder
|
359
|
-
* clients from keeping to send their huge data */
|
360
|
-
asyncSocket->close();
|
361
|
-
}
|
362
|
-
}
|
363
|
-
}
|
364
|
-
|
365
|
-
/* Expect another writable event, or another request within the timeout */
|
366
|
-
asyncSocket->timeout(HTTP_IDLE_TIMEOUT_S);
|
367
|
-
|
368
|
-
return s;
|
384
|
+
/* We cannot return nullptr to the underlying stack in any case */
|
385
|
+
return s;
|
369
386
|
});
|
370
387
|
|
371
|
-
|
372
|
-
|
388
|
+
/* Handle HTTP write out (note: SSL_read may trigger this spuriously, the
|
389
|
+
* app need to handle spurious calls) */
|
390
|
+
us_socket_context_on_writable(SSL, getSocketContext(), [](us_socket_t *s) {
|
391
|
+
AsyncSocket<SSL> *asyncSocket = (AsyncSocket<SSL> *)s;
|
392
|
+
HttpResponseData<SSL> *httpResponseData =
|
393
|
+
(HttpResponseData<SSL> *)asyncSocket->getAsyncSocketData();
|
394
|
+
|
395
|
+
/* Ask the developer to write data and return success (true) or failure
|
396
|
+
* (false), OR skip sending anything and return success (true). */
|
397
|
+
if (httpResponseData->onWritable) {
|
398
|
+
/* We are now writable, so hang timeout again, the user does not have to
|
399
|
+
* do anything so we should hang until end or tryEnd rearms timeout */
|
400
|
+
us_socket_timeout(SSL, s, 0);
|
401
|
+
|
402
|
+
/* We expect the developer to return whether or not write was successful
|
403
|
+
* (true). If write was never called, the developer should still return
|
404
|
+
* true so that we may drain. */
|
405
|
+
bool success =
|
406
|
+
httpResponseData->callOnWritable(httpResponseData->offset);
|
407
|
+
|
408
|
+
/* The developer indicated that their onWritable failed. */
|
409
|
+
if (!success) {
|
410
|
+
/* Skip testing if we can drain anything since that might perform an
|
411
|
+
* extra syscall */
|
412
|
+
return s;
|
413
|
+
}
|
373
414
|
|
374
|
-
|
375
|
-
|
376
|
-
|
415
|
+
/* We don't want to fall through since we don't want to mess with
|
416
|
+
* timeout. It makes little sense to drain any backpressure when the
|
417
|
+
* user has registered onWritable. */
|
418
|
+
return s;
|
419
|
+
}
|
420
|
+
|
421
|
+
/* Drain any socket buffer, this might empty our backpressure and thus
|
422
|
+
* finish the request */
|
423
|
+
/*auto [written, failed] = */ asyncSocket->write(nullptr, 0, true, 0);
|
424
|
+
|
425
|
+
/* Should we close this connection after a response - and is this response
|
426
|
+
* really done? */
|
427
|
+
if (httpResponseData->state &
|
428
|
+
HttpResponseData<SSL>::HTTP_CONNECTION_CLOSE) {
|
429
|
+
if ((httpResponseData->state &
|
430
|
+
HttpResponseData<SSL>::HTTP_RESPONSE_PENDING) == 0) {
|
431
|
+
if (asyncSocket->getBufferedAmount() == 0) {
|
432
|
+
asyncSocket->shutdown();
|
433
|
+
/* We need to force close after sending FIN since we want to hinder
|
434
|
+
* clients from keeping to send their huge data */
|
435
|
+
asyncSocket->close();
|
436
|
+
}
|
437
|
+
}
|
438
|
+
}
|
377
439
|
|
378
|
-
|
440
|
+
/* Expect another writable event, or another request within the timeout */
|
441
|
+
asyncSocket->timeout(HTTP_IDLE_TIMEOUT_S);
|
379
442
|
|
380
|
-
|
381
|
-
|
443
|
+
return s;
|
444
|
+
});
|
382
445
|
|
383
|
-
|
384
|
-
|
385
|
-
|
446
|
+
/* Handle FIN, HTTP does not support half-closed sockets, so simply close */
|
447
|
+
us_socket_context_on_end(SSL, getSocketContext(), [](us_socket_t *s) {
|
448
|
+
/* We do not care for half closed sockets */
|
449
|
+
AsyncSocket<SSL> *asyncSocket = (AsyncSocket<SSL> *)s;
|
450
|
+
return asyncSocket->close();
|
451
|
+
});
|
386
452
|
|
387
|
-
|
453
|
+
/* Handle socket timeouts, simply close them so to not confuse client with
|
454
|
+
* FIN */
|
455
|
+
us_socket_context_on_timeout(SSL, getSocketContext(), [](us_socket_t *s) {
|
456
|
+
/* Force close rather than gracefully shutdown and risk confusing the
|
457
|
+
* client with a complete download */
|
458
|
+
AsyncSocket<SSL> *asyncSocket = (AsyncSocket<SSL> *)s;
|
459
|
+
return asyncSocket->close();
|
460
|
+
});
|
388
461
|
|
389
|
-
|
390
|
-
|
462
|
+
return this;
|
463
|
+
}
|
391
464
|
|
392
465
|
public:
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
httpContext = (HttpContext *) us_create_socket_context(SSL, (us_loop_t *) loop, sizeof(HttpContextData<SSL>), options);
|
398
|
-
|
399
|
-
if (!httpContext) {
|
400
|
-
return nullptr;
|
401
|
-
}
|
402
|
-
|
403
|
-
/* Init socket context data */
|
404
|
-
new ((HttpContextData<SSL> *) us_socket_context_ext(SSL, (us_socket_context_t *) httpContext)) HttpContextData<SSL>();
|
405
|
-
return httpContext->init();
|
406
|
-
}
|
466
|
+
/* Construct a new HttpContext using specified loop */
|
467
|
+
static HttpContext *create(Loop *loop,
|
468
|
+
us_socket_context_options_t options = {}) {
|
469
|
+
HttpContext *httpContext;
|
407
470
|
|
408
|
-
|
409
|
-
|
410
|
-
/* Destruct socket context data */
|
411
|
-
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
412
|
-
httpContextData->~HttpContextData<SSL>();
|
471
|
+
httpContext = (HttpContext *)us_create_socket_context(
|
472
|
+
SSL, (us_loop_t *)loop, sizeof(HttpContextData<SSL>), options);
|
413
473
|
|
414
|
-
|
415
|
-
|
474
|
+
if (!httpContext) {
|
475
|
+
return nullptr;
|
416
476
|
}
|
417
477
|
|
418
|
-
|
419
|
-
|
478
|
+
/* Init socket context data */
|
479
|
+
new ((HttpContextData<SSL> *)us_socket_context_ext(
|
480
|
+
SSL, (us_socket_context_t *)httpContext)) HttpContextData<SSL>();
|
481
|
+
return httpContext->init();
|
482
|
+
}
|
483
|
+
|
484
|
+
/* Destruct the HttpContext, it does not follow RAII */
|
485
|
+
void free() {
|
486
|
+
/* Destruct socket context data */
|
487
|
+
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
488
|
+
httpContextData->~HttpContextData<SSL>();
|
489
|
+
|
490
|
+
/* Free the socket context in whole */
|
491
|
+
us_socket_context_free(SSL, getSocketContext());
|
492
|
+
}
|
493
|
+
|
494
|
+
void
|
495
|
+
filter(MoveOnlyFunction<void(HttpResponse<SSL> *, int)> &&filterHandler) {
|
496
|
+
getSocketContextData()->filterHandlers.emplace_back(
|
497
|
+
std::move(filterHandler));
|
498
|
+
}
|
499
|
+
|
500
|
+
/* Register an HTTP route handler acording to URL pattern */
|
501
|
+
void
|
502
|
+
onHttp(std::string method, std::string pattern,
|
503
|
+
MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler,
|
504
|
+
bool upgrade = false) {
|
505
|
+
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
506
|
+
|
507
|
+
/* Todo: This is ugly, fix */
|
508
|
+
std::vector<std::string> methods;
|
509
|
+
if (method == "*") {
|
510
|
+
methods = {"*"};
|
511
|
+
} else {
|
512
|
+
methods = {method};
|
420
513
|
}
|
421
514
|
|
422
|
-
|
423
|
-
|
424
|
-
|
515
|
+
uint32_t priority =
|
516
|
+
method == "*"
|
517
|
+
? httpContextData->currentRouter->LOW_PRIORITY
|
518
|
+
: (upgrade ? httpContextData->currentRouter->HIGH_PRIORITY
|
519
|
+
: httpContextData->currentRouter->MEDIUM_PRIORITY);
|
425
520
|
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
} else {
|
431
|
-
methods = {method};
|
432
|
-
}
|
433
|
-
|
434
|
-
uint32_t priority = method == "*" ? httpContextData->currentRouter->LOW_PRIORITY : (upgrade ? httpContextData->currentRouter->HIGH_PRIORITY : httpContextData->currentRouter->MEDIUM_PRIORITY);
|
435
|
-
|
436
|
-
/* If we are passed nullptr then remove this */
|
437
|
-
if (!handler) {
|
438
|
-
httpContextData->currentRouter->remove(methods[0], pattern, priority);
|
439
|
-
return;
|
440
|
-
}
|
441
|
-
|
442
|
-
/* Record this route's parameter offsets */
|
443
|
-
std::map<std::string, unsigned short, std::less<>> parameterOffsets;
|
444
|
-
unsigned short offset = 0;
|
445
|
-
for (unsigned int i = 0; i < pattern.length(); i++) {
|
446
|
-
if (pattern[i] == ':') {
|
447
|
-
i++;
|
448
|
-
unsigned int start = i;
|
449
|
-
while (i < pattern.length() && pattern[i] != '/') {
|
450
|
-
i++;
|
451
|
-
}
|
452
|
-
parameterOffsets[std::string(pattern.data() + start, i - start)] = offset;
|
453
|
-
//std::cout << "<" << std::string(pattern.data() + start, i - start) << "> is offset " << offset;
|
454
|
-
offset++;
|
455
|
-
}
|
456
|
-
}
|
457
|
-
|
458
|
-
httpContextData->currentRouter->add(methods, pattern, [handler = std::move(handler), parameterOffsets = std::move(parameterOffsets)](auto *r) mutable {
|
459
|
-
auto user = r->getUserData();
|
460
|
-
user.httpRequest->setYield(false);
|
461
|
-
user.httpRequest->setParameters(r->getParameters());
|
462
|
-
user.httpRequest->setParameterOffsets(¶meterOffsets);
|
463
|
-
|
464
|
-
/* Middleware? Automatically respond to expectations */
|
465
|
-
std::string_view expect = user.httpRequest->getHeader("expect");
|
466
|
-
if (expect.length() && expect == "100-continue") {
|
467
|
-
user.httpResponse->writeContinue();
|
468
|
-
}
|
469
|
-
|
470
|
-
handler(user.httpResponse, user.httpRequest);
|
471
|
-
|
472
|
-
/* If any handler yielded, the router will keep looking for a suitable handler. */
|
473
|
-
if (user.httpRequest->getYield()) {
|
474
|
-
return false;
|
475
|
-
}
|
476
|
-
return true;
|
477
|
-
}, priority);
|
521
|
+
/* If we are passed nullptr then remove this */
|
522
|
+
if (!handler) {
|
523
|
+
httpContextData->currentRouter->remove(methods[0], pattern, priority);
|
524
|
+
return;
|
478
525
|
}
|
479
526
|
|
480
|
-
/*
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
527
|
+
/* Record this route's parameter offsets */
|
528
|
+
std::map<std::string, unsigned short, std::less<>> parameterOffsets;
|
529
|
+
unsigned short offset = 0;
|
530
|
+
for (unsigned int i = 0; i < pattern.length(); i++) {
|
531
|
+
if (pattern[i] == ':') {
|
532
|
+
i++;
|
533
|
+
unsigned int start = i;
|
534
|
+
while (i < pattern.length() && pattern[i] != '/') {
|
535
|
+
i++;
|
536
|
+
}
|
537
|
+
parameterOffsets[std::string(pattern.data() + start, i - start)] =
|
538
|
+
offset;
|
539
|
+
// std::cout << "<" << std::string(pattern.data() + start, i - start) <<
|
540
|
+
// "> is offset " << offset;
|
541
|
+
offset++;
|
542
|
+
}
|
492
543
|
}
|
493
544
|
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
545
|
+
httpContextData->currentRouter->add(
|
546
|
+
methods, pattern,
|
547
|
+
[handler = std::move(handler),
|
548
|
+
parameterOffsets = std::move(parameterOffsets)](auto *r) mutable {
|
549
|
+
auto user = r->getUserData();
|
550
|
+
user.httpRequest->setYield(false);
|
551
|
+
user.httpRequest->setParameters(r->getParameters());
|
552
|
+
user.httpRequest->setParameterOffsets(¶meterOffsets);
|
553
|
+
|
554
|
+
/* Middleware? Automatically respond to expectations */
|
555
|
+
std::string_view expect = user.httpRequest->getHeader("expect");
|
556
|
+
if (expect.length() && expect == "100-continue") {
|
557
|
+
user.httpResponse->writeContinue();
|
558
|
+
}
|
559
|
+
|
560
|
+
handler(user.httpResponse, user.httpRequest);
|
561
|
+
|
562
|
+
/* If any handler yielded, the router will keep looking for a suitable
|
563
|
+
* handler. */
|
564
|
+
if (user.httpRequest->getYield()) {
|
565
|
+
return false;
|
566
|
+
}
|
567
|
+
return true;
|
568
|
+
},
|
569
|
+
priority);
|
570
|
+
}
|
571
|
+
|
572
|
+
/* Listen to port using this HttpContext */
|
573
|
+
us_listen_socket_t *listen(const char *host, int port, int options) {
|
574
|
+
return us_socket_context_listen(SSL, getSocketContext(), host, port,
|
575
|
+
options, sizeof(HttpResponseData<SSL>));
|
576
|
+
}
|
577
|
+
|
578
|
+
/* Listen to unix domain socket using this HttpContext */
|
579
|
+
us_listen_socket_t *listen(const char *path, int options) {
|
580
|
+
return us_socket_context_listen_unix(SSL, getSocketContext(), path, options,
|
581
|
+
sizeof(HttpResponseData<SSL>));
|
582
|
+
}
|
583
|
+
|
584
|
+
void onPreOpen(LIBUS_SOCKET_DESCRIPTOR (*handler)(LIBUS_SOCKET_DESCRIPTOR)) {
|
585
|
+
us_socket_context_on_pre_open(SSL, getSocketContext(), handler);
|
586
|
+
}
|
587
|
+
|
588
|
+
/* Adopt an externally accepted socket into this HttpContext */
|
589
|
+
us_socket_t *adoptAcceptedSocket(LIBUS_SOCKET_DESCRIPTOR accepted_fd) {
|
590
|
+
return us_adopt_accepted_socket(SSL, getSocketContext(), accepted_fd,
|
591
|
+
sizeof(HttpResponseData<SSL>), 0, 0);
|
592
|
+
}
|
498
593
|
};
|
499
594
|
|
500
|
-
}
|
595
|
+
} // namespace uWS
|
501
596
|
|
502
597
|
#endif // UWS_HTTPCONTEXT_H
|