/src/uWebSockets/src/HttpResponseData.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_HTTPRESPONSEDATA_H |
19 | | #define UWS_HTTPRESPONSEDATA_H |
20 | | |
21 | | /* This data belongs to the HttpResponse */ |
22 | | |
23 | | #include "HttpParser.h" |
24 | | #include "AsyncSocketData.h" |
25 | | #include "ProxyParser.h" |
26 | | |
27 | | #include "MoveOnlyFunction.h" |
28 | | |
29 | | namespace uWS { |
30 | | |
31 | | template <bool SSL> |
32 | | struct HttpResponseData : AsyncSocketData<SSL>, HttpParser { |
33 | | template <bool> friend struct HttpResponse; |
34 | | template <bool> friend struct HttpContext; |
35 | | |
36 | | /* When we are done with a response we mark it like so */ |
37 | 13.0k | void markDone() { |
38 | 13.0k | onAborted = nullptr; |
39 | | /* Also remove onWritable so that we do not emit when draining behind the scenes. */ |
40 | 13.0k | onWritable = nullptr; |
41 | | |
42 | | /* We are done with this request */ |
43 | 13.0k | state &= ~HttpResponseData<SSL>::HTTP_RESPONSE_PENDING; |
44 | 13.0k | } |
45 | | |
46 | | /* Caller of onWritable. It is possible onWritable calls markDone so we need to borrow it. */ |
47 | 0 | bool callOnWritable(uintmax_t offset) { |
48 | | /* Borrow real onWritable */ |
49 | 0 | MoveOnlyFunction<bool(uintmax_t)> borrowedOnWritable = std::move(onWritable); |
50 | | |
51 | | /* Set onWritable to placeholder */ |
52 | 0 | onWritable = [](uintmax_t) {return true;}; |
53 | | |
54 | | /* Run borrowed onWritable */ |
55 | 0 | bool ret = borrowedOnWritable(offset); |
56 | | |
57 | | /* If we still have onWritable (the placeholder) then move back the real one */ |
58 | 0 | if (onWritable) { |
59 | | /* We haven't reset onWritable, so give it back */ |
60 | 0 | onWritable = std::move(borrowedOnWritable); |
61 | 0 | } |
62 | |
|
63 | 0 | return ret; |
64 | 0 | } |
65 | | private: |
66 | | /* Bits of status */ |
67 | | enum { |
68 | | HTTP_STATUS_CALLED = 1, // used |
69 | | HTTP_WRITE_CALLED = 2, // used |
70 | | HTTP_END_CALLED = 4, // used |
71 | | HTTP_RESPONSE_PENDING = 8, // used |
72 | | HTTP_CONNECTION_CLOSE = 16 // used |
73 | | }; |
74 | | |
75 | | /* Per socket event handlers */ |
76 | | MoveOnlyFunction<bool(uintmax_t)> onWritable; |
77 | | MoveOnlyFunction<void()> onAborted; |
78 | | MoveOnlyFunction<void(std::string_view, bool)> inStream; // onData |
79 | | /* Outgoing offset */ |
80 | | uintmax_t offset = 0; |
81 | | |
82 | | /* Let's track number of bytes since last timeout reset in data handler */ |
83 | | unsigned int received_bytes_per_timeout = 0; |
84 | | |
85 | | /* Current state (content-length sent, status sent, write called, etc */ |
86 | | int state = 0; |
87 | | |
88 | | #ifdef UWS_WITH_PROXY |
89 | | ProxyParser proxyParser; |
90 | | #endif |
91 | | }; |
92 | | |
93 | | } |
94 | | |
95 | | #endif // UWS_HTTPRESPONSEDATA_H |