Coverage Report

Created: 2023-09-25 07:18

/src/uWebSockets/src/HttpContext.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Authored by Alex Hultman, 2018-2020.
3
 * Intellectual property of third-party.
4
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
#ifndef UWS_HTTPCONTEXT_H
19
#define UWS_HTTPCONTEXT_H
20
21
/* This class defines the main behavior of HTTP and emits various events */
22
23
#include "Loop.h"
24
#include "HttpContextData.h"
25
#include "HttpResponseData.h"
26
#include "AsyncSocket.h"
27
#include "WebSocketData.h"
28
29
#include <string_view>
30
#include <iostream>
31
#include "MoveOnlyFunction.h"
32
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;
45
46
    /* Minimum allowed receive throughput per second (clients uploading less than 16kB/sec get dropped) */
47
    static const int HTTP_RECEIVE_THROUGHPUT_BYTES = 16 * 1024;
48
49
121k
    us_socket_context_t *getSocketContext() {
50
121k
        return (us_socket_context_t *) this;
51
121k
    }
52
53
2.46M
    static us_socket_context_t *getSocketContext(us_socket_t *s) {
54
2.46M
        return (us_socket_context_t *) us_socket_context(SSL, s);
55
2.46M
    }
56
57
87.9k
    HttpContextData<SSL> *getSocketContextData() {
58
87.9k
        return (HttpContextData<SSL> *) us_socket_context_ext(SSL, getSocketContext());
59
87.9k
    }
60
61
2.46M
    static HttpContextData<SSL> *getSocketContextDataS(us_socket_t *s) {
62
2.46M
        return (HttpContextData<SSL> *) us_socket_context_ext(SSL, getSocketContext(s));
63
2.46M
    }
64
65
    /* Init the HttpContext by registering libusockets event handlers */
66
4.21k
    HttpContext<SSL> *init() {
67
        /* Handle socket connections */
68
1.04M
        us_socket_context_on_open(SSL, getSocketContext(), [](us_socket_t *s, int /*is_client*/, char */*ip*/, int /*ip_length*/) {
69
            /* Any connected socket should timeout until it has a request */
70
1.04M
            us_socket_timeout(SSL, s, HTTP_IDLE_TIMEOUT_S);
71
72
            /* Init socket ext */
73
1.04M
            new (us_socket_ext(SSL, s)) HttpResponseData<SSL>;
74
75
            /* Call filter */
76
1.04M
            HttpContextData<SSL> *httpContextData = getSocketContextDataS(s);
77
1.04M
            for (auto &f : httpContextData->filterHandlers) {
78
0
                f((HttpResponse<SSL> *) s, 1);
79
0
            }
80
81
1.04M
            return s;
82
1.04M
        });
83
84
        /* Handle socket disconnections */
85
970k
        us_socket_context_on_close(SSL, getSocketContext(), [](us_socket_t *s, int /*code*/, void */*reason*/) {
86
            /* Get socket ext */
87
970k
            HttpResponseData<SSL> *httpResponseData = (HttpResponseData<SSL> *) us_socket_ext(SSL, s);
88
89
            /* Call filter */
90
970k
            HttpContextData<SSL> *httpContextData = getSocketContextDataS(s);
91
970k
            for (auto &f : httpContextData->filterHandlers) {
92
0
                f((HttpResponse<SSL> *) s, -1);
93
0
            }
94
95
            /* Signal broken HTTP request only if we have a pending request */
96
970k
            if (httpResponseData->onAborted) {
97
0
                httpResponseData->onAborted();
98
0
            }
99
100
            /* Destruct socket ext */
101
970k
            httpResponseData->~HttpResponseData<SSL>();
102
103
970k
            return s;
104
970k
        });
105
106
        /* Handle HTTP data streams */
107
443k
        us_socket_context_on_data(SSL, getSocketContext(), [](us_socket_t *s, char *data, int length) {
108
109
            // total overhead is about 210k down to 180k
110
            // ~210k req/sec is the original perf with write in data
111
            // ~200k req/sec is with cork and formatting
112
            // ~190k req/sec is with http parsing
113
            // ~180k - 190k req/sec is with varying routing
114
115
443k
            HttpContextData<SSL> *httpContextData = getSocketContextDataS(s);
116
117
            /* Do not accept any data while in shutdown state */
118
443k
            if (us_socket_is_shut_down(SSL, (us_socket_t *) s)) {
119
0
                return s;
120
0
            }
121
122
443k
            HttpResponseData<SSL> *httpResponseData = (HttpResponseData<SSL> *) us_socket_ext(SSL, s);
123
124
            /* Cork this socket */
125
443k
            ((AsyncSocket<SSL> *) s)->cork();
126
127
            /* Mark that we are inside the parser now */
128
443k
            httpContextData->isParsingHttp = true;
129
130
            // clients need to know the cursor after http parse, not servers!
131
            // how far did we read then? we need to know to continue with websocket parsing data? or?
132
133
443k
            void *proxyParser = nullptr;
134
#ifdef UWS_WITH_PROXY
135
            proxyParser = &httpResponseData->proxyParser;
136
#endif
137
138
            /* The return value is entirely up to us to interpret. The HttpParser only care for whether the returned value is DIFFERENT or not from passed user */
139
443k
            auto [err, returnedSocket] = httpResponseData->consumePostPadded(data, (unsigned int) length, s, proxyParser, [httpContextData](void *s, HttpRequest *httpRequest) -> void * {
140
                /* For every request we reset the timeout and hang until user makes action */
141
                /* Warning: if we are in shutdown state, resetting the timer is a security issue! */
142
90.5k
                us_socket_timeout(SSL, (us_socket_t *) s, 0);
143
144
                /* Reset httpResponse */
145
90.5k
                HttpResponseData<SSL> *httpResponseData = (HttpResponseData<SSL> *) us_socket_ext(SSL, (us_socket_t *) s);
146
90.5k
                httpResponseData->offset = 0;
147
148
                /* Are we not ready for another request yet? Terminate the connection. */
149
90.5k
                if (httpResponseData->state & HttpResponseData<SSL>::HTTP_RESPONSE_PENDING) {
150
0
                    us_socket_close(SSL, (us_socket_t *) s, 0, nullptr);
151
0
                    return nullptr;
152
0
                }
153
154
                /* Mark pending request and emit it */
155
90.5k
                httpResponseData->state = HttpResponseData<SSL>::HTTP_RESPONSE_PENDING;
156
157
                /* Mark this response as connectionClose if ancient or connection: close */
158
90.5k
                if (httpRequest->isAncient() || httpRequest->getHeader("connection").length() == 5) {
159
196
                    httpResponseData->state |= HttpResponseData<SSL>::HTTP_CONNECTION_CLOSE;
160
196
                }
161
162
                /* Select the router based on SNI (only possible for SSL) */
163
90.5k
                auto *selectedRouter = &httpContextData->router;
164
90.5k
                if constexpr (SSL) {
165
90.5k
                    void *domainRouter = us_socket_server_name_userdata(SSL, (struct us_socket_t *) s);
166
90.5k
                    if (domainRouter) {
167
0
                        selectedRouter = (decltype(selectedRouter)) domainRouter;
168
0
                    }
169
90.5k
                }
170
171
                /* Route the method and URL */
172
90.5k
                selectedRouter->getUserData() = {(HttpResponse<SSL> *) s, httpRequest};
173
90.5k
                if (!selectedRouter->route(httpRequest->getCaseSensitiveMethod(), httpRequest->getUrl())) {
174
                    /* We don't care if it reaches the client or not */
175
11.0k
                    us_socket_write(SSL, (us_socket_t *) s, httpErrorResponses[HTTP_ERROR_404_FILE_NOT_FOUND].data(), (int) httpErrorResponses[HTTP_ERROR_404_FILE_NOT_FOUND].length(), false);
176
11.0k
                    us_socket_shutdown(SSL, (us_socket_t *) s);
177
178
                    /* We have to force close this socket as we have no handler for it */
179
11.0k
                    us_socket_close(SSL, (us_socket_t *) s, 0, nullptr);
180
11.0k
                    return nullptr;
181
11.0k
                }
182
183
                /* First of all we need to check if this socket was deleted due to upgrade */
184
79.5k
                if (httpContextData->upgradedWebSocket) {
185
                    /* We differ between closed and upgraded below */
186
79.5k
                    return nullptr;
187
79.5k
                }
188
189
                /* Was the socket closed? */
190
0
                if (us_socket_is_closed(SSL, (struct us_socket_t *) s)) {
191
0
                    return nullptr;
192
0
                }
193
194
                /* We absolutely have to terminate parsing if shutdown */
195
0
                if (us_socket_is_shut_down(SSL, (us_socket_t *) s)) {
196
0
                    return nullptr;
197
0
                }
198
199
                /* Returning from a request handler without responding or attaching an onAborted handler is ill-use */
200
0
                if (!((HttpResponse<SSL> *) s)->hasResponded() && !httpResponseData->onAborted) {
201
                    /* Throw exception here? */
202
0
                    std::cerr << "Error: Returning from a request handler without responding or attaching an abort handler is forbidden!" << std::endl;
203
0
                    std::terminate();
204
0
                }
205
206
                /* If we have not responded and we have a data handler, we need to timeout to enfore client sending the data */
207
0
                if (!((HttpResponse<SSL> *) s)->hasResponded() && httpResponseData->inStream) {
208
0
                    us_socket_timeout(SSL, (us_socket_t *) s, HTTP_IDLE_TIMEOUT_S);
209
0
                }
210
211
                /* Continue parsing */
212
0
                return s;
213
214
0
            }, [httpResponseData](void *user, std::string_view data, bool fin) -> void * {
215
                /* We always get an empty chunk even if there is no data */
216
0
                if (httpResponseData->inStream) {
217
218
                    /* Todo: can this handle timeout for non-post as well? */
219
0
                    if (fin) {
220
                        /* If we just got the last chunk (or empty chunk), disable timeout */
221
0
                        us_socket_timeout(SSL, (struct us_socket_t *) user, 0);
222
0
                    } else {
223
                        /* We still have some more data coming in later, so reset timeout */
224
                        /* Only reset timeout if we got enough bytes (16kb/sec) since last time we reset here */
225
0
                        httpResponseData->received_bytes_per_timeout += (unsigned int) data.length();
226
0
                        if (httpResponseData->received_bytes_per_timeout >= HTTP_RECEIVE_THROUGHPUT_BYTES * HTTP_IDLE_TIMEOUT_S) {
227
0
                            us_socket_timeout(SSL, (struct us_socket_t *) user, HTTP_IDLE_TIMEOUT_S);
228
0
                            httpResponseData->received_bytes_per_timeout = 0;
229
0
                        }
230
0
                    }
231
232
                    /* We might respond in the handler, so do not change timeout after this */
233
0
                    httpResponseData->inStream(data, fin);
234
235
                    /* Was the socket closed? */
236
0
                    if (us_socket_is_closed(SSL, (struct us_socket_t *) user)) {
237
0
                        return nullptr;
238
0
                    }
239
240
                    /* We absolutely have to terminate parsing if shutdown */
241
0
                    if (us_socket_is_shut_down(SSL, (us_socket_t *) user)) {
242
0
                        return nullptr;
243
0
                    }
244
245
                    /* If we were given the last data chunk, reset data handler to ensure following
246
                     * requests on the same socket won't trigger any previously registered behavior */
247
0
                    if (fin) {
248
0
                        httpResponseData->inStream = nullptr;
249
0
                    }
250
0
                }
251
0
                return user;
252
0
            });
253
254
            /* Mark that we are no longer parsing Http */
255
443k
            httpContextData->isParsingHttp = false;
256
257
            /* If we got fullptr that means the parser wants us to close the socket from error (same as calling the errorHandler) */
258
443k
            if (returnedSocket == FULLPTR) {
259
                /* For errors, we only deliver them "at most once". We don't care if they get halfways delivered or not. */
260
325k
                us_socket_write(SSL, s, httpErrorResponses[err].data(), (int) httpErrorResponses[err].length(), false);
261
325k
                us_socket_shutdown(SSL, s);
262
                /* Close any socket on HTTP errors */
263
325k
                us_socket_close(SSL, s, 0, nullptr);
264
                /* This just makes the following code act as if the socket was closed from error inside the parser. */
265
325k
                returnedSocket = nullptr;
266
325k
            }
267
268
            /* We need to uncork in all cases, except for nullptr (closed socket, or upgraded socket) */
269
443k
            if (returnedSocket != nullptr) {
270
                /* Timeout on uncork failure */
271
27.7k
                auto [written, failed] = ((AsyncSocket<SSL> *) returnedSocket)->uncork();
272
27.7k
                if (failed) {
273
                    /* All Http sockets timeout by this, and this behavior match the one in HttpResponse::cork */
274
                    /* Warning: both HTTP_IDLE_TIMEOUT_S and HTTP_TIMEOUT_S are 10 seconds and both are used the same */
275
0
                    ((AsyncSocket<SSL> *) s)->timeout(HTTP_IDLE_TIMEOUT_S);
276
0
                }
277
278
                /* We need to check if we should close this socket here now */
279
27.7k
                if (httpResponseData->state & HttpResponseData<SSL>::HTTP_CONNECTION_CLOSE) {
280
0
                    if ((httpResponseData->state & HttpResponseData<SSL>::HTTP_RESPONSE_PENDING) == 0) {
281
0
                        if (((AsyncSocket<SSL> *) s)->getBufferedAmount() == 0) {
282
0
                            ((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
0
                            ((AsyncSocket<SSL> *) s)->close();
286
0
                        }
287
0
                    }
288
0
                }
289
290
27.7k
                return (us_socket_t *) returnedSocket;
291
27.7k
            }
292
293
            /* If we upgraded, check here (differ between nullptr close and nullptr upgrade) */
294
416k
            if (httpContextData->upgradedWebSocket) {
295
                /* This path is only for upgraded websockets */
296
79.5k
                AsyncSocket<SSL> *asyncSocket = (AsyncSocket<SSL> *) httpContextData->upgradedWebSocket;
297
298
                /* Uncork here as well (note: what if we failed to uncork and we then pub/sub before we even upgraded?) */
299
79.5k
                auto [written, failed] = asyncSocket->uncork();
300
301
                /* If we succeeded in uncorking, check if we have sent WebSocket FIN */
302
79.5k
                if (!failed) {
303
572
                    WebSocketData *webSocketData = (WebSocketData *) asyncSocket->getAsyncSocketData();
304
572
                    if (webSocketData->isShuttingDown) {
305
                        /* In that case, also send TCP FIN (this is similar to what we have in ws drain handler) */
306
0
                        asyncSocket->shutdown();
307
0
                    }
308
572
                }
309
310
                /* Reset upgradedWebSocket before we return */
311
79.5k
                httpContextData->upgradedWebSocket = nullptr;
312
313
                /* Return the new upgraded websocket */
314
79.5k
                return (us_socket_t *) asyncSocket;
315
79.5k
            }
316
317
            /* It is okay to uncork a closed socket and we need to */
318
336k
            ((AsyncSocket<SSL> *) s)->uncork();
319
320
            /* We cannot return nullptr to the underlying stack in any case */
321
336k
            return s;
322
416k
        });
323
324
        /* Handle HTTP write out (note: SSL_read may trigger this spuriously, the app need to handle spurious calls) */
325
4.21k
        us_socket_context_on_writable(SSL, getSocketContext(), [](us_socket_t *s) {
326
327
0
            AsyncSocket<SSL> *asyncSocket = (AsyncSocket<SSL> *) s;
328
0
            HttpResponseData<SSL> *httpResponseData = (HttpResponseData<SSL> *) asyncSocket->getAsyncSocketData();
329
330
            /* Ask the developer to write data and return success (true) or failure (false), OR skip sending anything and return success (true). */
331
0
            if (httpResponseData->onWritable) {
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
0
                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
0
                bool success = httpResponseData->callOnWritable(httpResponseData->offset);
338
339
                /* The developer indicated that their onWritable failed. */
340
0
                if (!success) {
341
                    /* Skip testing if we can drain anything since that might perform an extra syscall */
342
0
                    return s;
343
0
                }
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
0
                return s;
348
0
            }
349
350
            /* Drain any socket buffer, this might empty our backpressure and thus finish the request */
351
0
            /*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
0
            if (httpResponseData->state & HttpResponseData<SSL>::HTTP_CONNECTION_CLOSE) {
355
0
                if ((httpResponseData->state & HttpResponseData<SSL>::HTTP_RESPONSE_PENDING) == 0) {
356
0
                    if (asyncSocket->getBufferedAmount() == 0) {
357
0
                        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
0
                        asyncSocket->close();
361
0
                    }
362
0
                }
363
0
            }
364
365
            /* Expect another writable event, or another request within the timeout */
366
0
            asyncSocket->timeout(HTTP_IDLE_TIMEOUT_S);
367
368
0
            return s;
369
0
        });
370
371
        /* Handle FIN, HTTP does not support half-closed sockets, so simply close */
372
83.7k
        us_socket_context_on_end(SSL, getSocketContext(), [](us_socket_t *s) {
373
374
            /* We do not care for half closed sockets */
375
83.7k
            AsyncSocket<SSL> *asyncSocket = (AsyncSocket<SSL> *) s;
376
83.7k
            return asyncSocket->close();
377
378
83.7k
        });
379
380
        /* Handle socket timeouts, simply close them so to not confuse client with FIN */
381
4.21k
        us_socket_context_on_timeout(SSL, getSocketContext(), [](us_socket_t *s) {
382
383
            /* Force close rather than gracefully shutdown and risk confusing the client with a complete download */
384
3.96k
            AsyncSocket<SSL> *asyncSocket = (AsyncSocket<SSL> *) s;
385
3.96k
            return asyncSocket->close();
386
387
3.96k
        });
388
389
4.21k
        return this;
390
4.21k
    }
391
392
public:
393
    /* Construct a new HttpContext using specified loop */
394
4.21k
    static HttpContext *create(Loop *loop, us_socket_context_options_t options = {}) {
395
4.21k
        HttpContext *httpContext;
396
397
4.21k
        httpContext = (HttpContext *) us_create_socket_context(SSL, (us_loop_t *) loop, sizeof(HttpContextData<SSL>), options);
398
399
4.21k
        if (!httpContext) {
400
0
            return nullptr;
401
0
        }
402
403
        /* Init socket context data */
404
4.21k
        new ((HttpContextData<SSL> *) us_socket_context_ext(SSL, (us_socket_context_t *) httpContext)) HttpContextData<SSL>();
405
4.21k
        return httpContext->init();
406
4.21k
    }
407
408
    /* Destruct the HttpContext, it does not follow RAII */
409
4.21k
    void free() {
410
        /* Destruct socket context data */
411
4.21k
        HttpContextData<SSL> *httpContextData = getSocketContextData();
412
4.21k
        httpContextData->~HttpContextData<SSL>();
413
414
        /* Free the socket context in whole */
415
4.21k
        us_socket_context_free(SSL, getSocketContext());
416
4.21k
    }
417
418
    void filter(MoveOnlyFunction<void(HttpResponse<SSL> *, int)> &&filterHandler) {
419
        getSocketContextData()->filterHandlers.emplace_back(std::move(filterHandler));
420
    }
421
422
    /* Register an HTTP route handler acording to URL pattern */
423
4.21k
    void onHttp(std::string method, std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler, bool upgrade = false) {
424
4.21k
        HttpContextData<SSL> *httpContextData = getSocketContextData();
425
426
        /* Todo: This is ugly, fix */
427
4.21k
        std::vector<std::string> methods;
428
4.21k
        if (method == "*") {
429
0
            methods = httpContextData->currentRouter->upperCasedMethods;
430
4.21k
        } else {
431
4.21k
            methods = {method};
432
4.21k
        }
433
434
4.21k
        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
4.21k
        if (!handler) {
438
0
            httpContextData->currentRouter->remove(methods[0], pattern, priority);
439
0
            return;
440
0
        }
441
442
82.7k
        httpContextData->currentRouter->add(methods, pattern, [handler = std::move(handler)](auto *r) mutable {
443
82.7k
            auto user = r->getUserData();
444
82.7k
            user.httpRequest->setYield(false);
445
82.7k
            user.httpRequest->setParameters(r->getParameters());
446
447
            /* Middleware? Automatically respond to expectations */
448
82.7k
            std::string_view expect = user.httpRequest->getHeader("expect");
449
82.7k
            if (expect.length() && expect == "100-continue") {
450
1.01k
                user.httpResponse->writeContinue();
451
1.01k
            }
452
453
82.7k
            handler(user.httpResponse, user.httpRequest);
454
455
            /* If any handler yielded, the router will keep looking for a suitable handler. */
456
82.7k
            if (user.httpRequest->getYield()) {
457
3.26k
                return false;
458
3.26k
            }
459
79.5k
            return true;
460
82.7k
        }, priority);
461
4.21k
    }
462
463
    /* Listen to port using this HttpContext */
464
4.21k
    us_listen_socket_t *listen(const char *host, int port, int options) {
465
4.21k
        return us_socket_context_listen(SSL, getSocketContext(), host, port, options, sizeof(HttpResponseData<SSL>));
466
4.21k
    }
467
468
    /* Listen to unix domain socket using this HttpContext */
469
    us_listen_socket_t *listen(const char *path, int options) {
470
        return us_socket_context_listen_unix(SSL, getSocketContext(), path, options, sizeof(HttpResponseData<SSL>));
471
    }
472
};
473
474
}
475
476
#endif // UWS_HTTPCONTEXT_H