|
12 | 12 | #ifndef O2_FRAMEWORK_HTTPPARSER_H_ |
13 | 13 | #define O2_FRAMEWORK_HTTPPARSER_H_ |
14 | 14 |
|
15 | | -#include "Framework/Endian.h" |
| 15 | +#include <bit> |
| 16 | +#include <cstdint> |
16 | 17 | #include <fmt/format.h> |
17 | 18 | #include <uv.h> |
18 | 19 | #include <string> |
|
22 | 23 | namespace o2::framework |
23 | 24 | { |
24 | 25 |
|
25 | | -struct __attribute__((__packed__)) WebSocketFrameTiny { |
26 | | -#if O2_HOST_BYTE_ORDER == O2_LITTLE_ENDIAN |
| 26 | +template <std::endian E> |
| 27 | +struct __attribute__((__packed__)) WebSocketFrameTiny; |
| 28 | + |
| 29 | +template <> |
| 30 | +struct __attribute__((__packed__)) WebSocketFrameTiny<std::endian::little> { |
27 | 31 | unsigned char opcode : 4; |
28 | 32 | unsigned char rsv3 : 1; |
29 | 33 | unsigned char rsv2 : 1; |
30 | 34 | unsigned char rsv1 : 1; |
31 | 35 | unsigned char fin : 1; |
32 | 36 | unsigned char len : 7; |
33 | 37 | unsigned char mask : 1; |
34 | | -#elif O2_HOST_BYTE_ORDER == O2_BIG_ENDIAN |
| 38 | +}; |
| 39 | + |
| 40 | +template <> |
| 41 | +struct __attribute__((__packed__)) WebSocketFrameTiny<std::endian::big> { |
35 | 42 | unsigned char fin : 1; |
36 | 43 | unsigned char rsv1 : 1; |
37 | 44 | unsigned char rsv2 : 1; |
38 | 45 | unsigned char rsv3 : 1; |
39 | 46 | unsigned char opcode : 4; |
40 | 47 | unsigned char mask : 1; |
41 | 48 | unsigned char len : 7; |
42 | | -#else |
43 | | -#error Uknown endiannes |
44 | | -#endif |
45 | 49 | }; |
46 | 50 |
|
47 | | -struct __attribute__((__packed__)) WebSocketFrameShort { |
48 | | -#if O2_HOST_BYTE_ORDER == O2_LITTLE_ENDIAN |
| 51 | +using WebSocketFrameTiny = WebSocketFrameTiny<std::endian::native>; |
| 52 | + |
| 53 | +template <std::endian E> |
| 54 | +struct __attribute__((__packed__)) WebSocketFrameShort; |
| 55 | + |
| 56 | +template <> |
| 57 | +struct __attribute__((__packed__)) WebSocketFrameShort<std::endian::little> { |
49 | 58 | unsigned char opcode : 4; |
50 | 59 | unsigned char rsv3 : 1; |
51 | 60 | unsigned char rsv2 : 1; |
52 | 61 | unsigned char rsv1 : 1; |
53 | 62 | unsigned char fin : 1; |
54 | 63 | unsigned char len : 7; |
55 | 64 | unsigned char mask : 1; |
56 | | -#elif O2_HOST_BYTE_ORDER == O2_BIG_ENDIAN |
| 65 | + uint16_t len16; |
| 66 | +}; |
| 67 | + |
| 68 | +template <> |
| 69 | +struct __attribute__((__packed__)) WebSocketFrameShort<std::endian::big> { |
57 | 70 | unsigned char fin : 1; |
58 | 71 | unsigned char rsv1 : 1; |
59 | 72 | unsigned char rsv2 : 1; |
60 | 73 | unsigned char rsv3 : 1; |
61 | 74 | unsigned char opcode : 4; |
62 | 75 | unsigned char mask : 1; |
63 | 76 | unsigned char len : 7; |
64 | | -#else |
65 | | -#error Uknown endiannes |
66 | | -#endif |
67 | 77 | uint16_t len16; |
68 | 78 | }; |
69 | 79 |
|
70 | | -struct __attribute__((__packed__)) WebSocketFrameHuge { |
71 | | -#if O2_HOST_BYTE_ORDER == O2_LITTLE_ENDIAN |
| 80 | +using WebSocketFrameShort = WebSocketFrameShort<std::endian::native>; |
| 81 | + |
| 82 | +template <std::endian E> |
| 83 | +struct __attribute__((__packed__)) WebSocketFrameHuge; |
| 84 | + |
| 85 | +template <> |
| 86 | +struct __attribute__((__packed__)) WebSocketFrameHuge<std::endian::little> { |
72 | 87 | unsigned char opcode : 4; |
73 | 88 | unsigned char rsv3 : 1; |
74 | 89 | unsigned char rsv2 : 1; |
75 | 90 | unsigned char rsv1 : 1; |
76 | 91 | unsigned char fin : 1; |
77 | 92 | unsigned char len : 7; |
78 | 93 | unsigned char mask : 1; |
79 | | -#elif O2_HOST_BYTE_ORDER == O2_BIG_ENDIAN |
| 94 | + uint64_t len64; |
| 95 | +}; |
| 96 | + |
| 97 | +template <> |
| 98 | +struct __attribute__((__packed__)) WebSocketFrameHuge<std::endian::big> { |
80 | 99 | unsigned char fin : 1; |
81 | 100 | unsigned char rsv1 : 1; |
82 | 101 | unsigned char rsv2 : 1; |
83 | 102 | unsigned char rsv3 : 1; |
84 | 103 | unsigned char opcode : 4; |
85 | 104 | unsigned char mask : 1; |
86 | 105 | unsigned char len : 7; |
87 | | -#else |
88 | | -#error Uknown endiannes |
89 | | -#endif |
90 | 106 | uint64_t len64; |
91 | 107 | }; |
92 | 108 |
|
| 109 | +using WebSocketFrameHuge = WebSocketFrameHuge<std::endian::native>; |
| 110 | + |
93 | 111 | enum struct WebSocketFrameKind { |
94 | 112 | Tiny, |
95 | 113 | Short, |
@@ -138,9 +156,9 @@ struct WebSocketHandler { |
138 | 156 | virtual ~WebSocketHandler() = default; |
139 | 157 |
|
140 | 158 | /// Invoked when all the headers are received. |
141 | | - virtual void headers(std::map<std::string, std::string> const& headers){}; |
| 159 | + virtual void headers(std::map<std::string, std::string> const& headers) {}; |
142 | 160 | /// FIXME: not implemented |
143 | | - virtual void beginFragmentation(){}; |
| 161 | + virtual void beginFragmentation() {}; |
144 | 162 | /// Invoked when a frame it's parsed. Notice you do not own the data and you must |
145 | 163 | /// not free the memory. |
146 | 164 | virtual void frame(char const* frame, size_t s) {} |
@@ -205,18 +223,18 @@ struct HTTPParser { |
205 | 223 | std::string remaining; |
206 | 224 | std::string error; |
207 | 225 | std::vector<HTTPState> states; |
208 | | - virtual void method(std::string_view const& s){}; |
209 | | - virtual void target(std::string_view const& s){}; |
210 | | - virtual void version(std::string_view const& s){}; |
211 | | - virtual void header(std::string_view const& k, std::string_view const& v){}; |
212 | | - virtual void endHeaders(){}; |
| 226 | + virtual void method(std::string_view const& s) {}; |
| 227 | + virtual void target(std::string_view const& s) {}; |
| 228 | + virtual void version(std::string_view const& s) {}; |
| 229 | + virtual void header(std::string_view const& k, std::string_view const& v) {}; |
| 230 | + virtual void endHeaders() {}; |
213 | 231 | /// Invoked whenever we are parsing data. |
214 | 232 | /// In order to allow for xoring (as required by the websocket standard) |
215 | 233 | /// in place, we pass it as a mutable pointer. |
216 | | - virtual void body(char* data, size_t s){}; |
217 | | - virtual void replyVersion(std::string_view const& s){}; |
218 | | - virtual void replyCode(std::string_view const& s){}; |
219 | | - virtual void replyMessage(std::string_view const& s){}; |
| 234 | + virtual void body(char* data, size_t s) {}; |
| 235 | + virtual void replyVersion(std::string_view const& s) {}; |
| 236 | + virtual void replyCode(std::string_view const& s) {}; |
| 237 | + virtual void replyMessage(std::string_view const& s) {}; |
220 | 238 | }; |
221 | 239 |
|
222 | 240 | struct HTTPParserHelpers { |
|
0 commit comments