Project
Loading...
Searching...
No Matches
HTTPParser.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef O2_FRAMEWORK_HTTPPARSER_H_
13#define O2_FRAMEWORK_HTTPPARSER_H_
14
15#include <bit>
16#include <cstdint>
17#include <fmt/format.h>
18#include <uv.h>
19#include <string>
20#include <vector>
21#include <map>
22
23namespace o2::framework
24{
25
26template <std::endian E>
27struct __attribute__((__packed__)) WebSocketFrameTiny;
28
29template <>
30struct __attribute__((__packed__)) WebSocketFrameTiny<std::endian::little> {
31 unsigned char opcode : 4;
32 unsigned char rsv3 : 1;
33 unsigned char rsv2 : 1;
34 unsigned char rsv1 : 1;
35 unsigned char fin : 1;
36 unsigned char len : 7;
37 unsigned char mask : 1;
38};
39
40template <>
41struct __attribute__((__packed__)) WebSocketFrameTiny<std::endian::big> {
42 unsigned char fin : 1;
43 unsigned char rsv1 : 1;
44 unsigned char rsv2 : 1;
45 unsigned char rsv3 : 1;
46 unsigned char opcode : 4;
47 unsigned char mask : 1;
48 unsigned char len : 7;
49};
50
51template <std::endian E>
52struct __attribute__((__packed__)) WebSocketFrameShort;
53
54template <>
55struct __attribute__((__packed__)) WebSocketFrameShort<std::endian::little> {
56 unsigned char opcode : 4;
57 unsigned char rsv3 : 1;
58 unsigned char rsv2 : 1;
59 unsigned char rsv1 : 1;
60 unsigned char fin : 1;
61 unsigned char len : 7;
62 unsigned char mask : 1;
63 uint16_t len16;
64};
65
66template <>
67struct __attribute__((__packed__)) WebSocketFrameShort<std::endian::big> {
68 unsigned char fin : 1;
69 unsigned char rsv1 : 1;
70 unsigned char rsv2 : 1;
71 unsigned char rsv3 : 1;
72 unsigned char opcode : 4;
73 unsigned char mask : 1;
74 unsigned char len : 7;
75 uint16_t len16;
76};
77
78template <std::endian E>
79struct __attribute__((__packed__)) WebSocketFrameHuge;
80
81template <>
82struct __attribute__((__packed__)) WebSocketFrameHuge<std::endian::little> {
83 unsigned char opcode : 4;
84 unsigned char rsv3 : 1;
85 unsigned char rsv2 : 1;
86 unsigned char rsv1 : 1;
87 unsigned char fin : 1;
88 unsigned char len : 7;
89 unsigned char mask : 1;
90 uint64_t len64;
91};
92
93template <>
94struct __attribute__((__packed__)) WebSocketFrameHuge<std::endian::big> {
95 unsigned char fin : 1;
96 unsigned char rsv1 : 1;
97 unsigned char rsv2 : 1;
98 unsigned char rsv3 : 1;
99 unsigned char opcode : 4;
100 unsigned char mask : 1;
101 unsigned char len : 7;
102 uint64_t len64;
103};
104
106 Tiny,
107 Short,
108 Huge
109};
110
111enum struct WebSocketOpCode : uint8_t {
112 Continuation = 0,
113 Text = 1,
114 Binary = 2,
115 Close = 8,
116 Ping = 9,
117 Pong = 10
118};
119
126 constexpr static size_t MaxChunkSize = 0x10000;
127};
128
135std::string encode_websocket_handshake_request(const char* path, const char* protocol, int version, char const* nonce,
136 std::vector<std::pair<std::string, std::string>> headers = {});
137
141std::string encode_websocket_handshake_reply(char const* nonce, char const* protocol = "");
142
146void encode_websocket_frames(std::vector<uv_buf_t>& outputs, char const* src, size_t size, WebSocketOpCode opcode, uint32_t mask);
147
150 virtual ~WebSocketHandler() = default;
151
153 virtual void headers(std::map<std::string, std::string> const& headers) {};
155 virtual void beginFragmentation() {};
158 virtual void frame(char const* frame, size_t s) {}
160 virtual void beginChunk() {}
162 virtual void endChunk() {}
164 virtual void endFragmentation() {}
166 virtual void control(char const* frame, size_t s) {}
167
169 size_t remainingSize = 0;
171 size_t pendingSize = 0;
173 char* pendingBuffer = nullptr;
176 char* pendingHeader = nullptr;
177};
178
181void decode_websocket(char* src, size_t size, WebSocketHandler& handler);
182
215
217 std::string remaining;
218 std::string error;
219 std::vector<HTTPState> states;
220 virtual void method(std::string_view const& s) {};
221 virtual void target(std::string_view const& s) {};
222 virtual void version(std::string_view const& s) {};
223 virtual void header(std::string_view const& k, std::string_view const& v) {};
224 virtual void endHeaders() {};
228 virtual void body(char* data, size_t s) {};
229 virtual void replyVersion(std::string_view const& s) {};
230 virtual void replyCode(std::string_view const& s) {};
231 virtual void replyMessage(std::string_view const& s) {};
232};
233
236 static std::string calculateAccept(const char* nonce);
237};
238
239void parse_http_request(char* start, size_t size, HTTPParser* parser);
240
241std::pair<std::string, unsigned short> parse_websocket_url(const char* s);
242} // namespace o2::framework
243#endif
uint32_t version
Definition RawData.h:8
GLenum src
Definition glcorearb.h:1767
GLsizeiptr size
Definition glcorearb.h:659
const GLdouble * v
Definition glcorearb.h:832
GLboolean * data
Definition glcorearb.h:298
GLsizei const GLchar *const * path
Definition glcorearb.h:3591
GLuint start
Definition glcorearb.h:469
GLenum GLenum GLsizei len
Definition glcorearb.h:4232
GLint GLuint mask
Definition glcorearb.h:291
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
@ IN_SEPARATOR
capture until a specific "separator"
@ IN_CAPTURE_SEPARATOR
capture until any or the "delimiters" characters
@ IN_CHUNKED
skip a specific "separator"
@ IN_CAPTURE_DELIMITERS
skip any "delimiters" char.
void encode_websocket_frames(std::vector< uv_buf_t > &outputs, char const *src, size_t size, WebSocketOpCode opcode, uint32_t mask)
std::pair< std::string, unsigned short > parse_websocket_url(char const *url)
void parse_http_request(char *start, size_t size, HTTPParser *parser)
std::string encode_websocket_handshake_request(const char *endpoint, const char *protocol, int version, char const *nonce, std::vector< std::pair< std::string, std::string > > headers)
void decode_websocket(char *start, size_t size, WebSocketHandler &handler)
std::string encode_websocket_handshake_reply(char const *nonce, const char *protocol)
static std::string calculateAccept(const char *nonce)
Helper to calculate the reply to a nonce.
virtual void replyVersion(std::string_view const &s)
Definition HTTPParser.h:229
virtual void header(std::string_view const &k, std::string_view const &v)
Definition HTTPParser.h:223
virtual void method(std::string_view const &s)
Definition HTTPParser.h:220
virtual void replyCode(std::string_view const &s)
Definition HTTPParser.h:230
virtual void endHeaders()
Definition HTTPParser.h:224
virtual void body(char *data, size_t s)
Definition HTTPParser.h:228
virtual void target(std::string_view const &s)
Definition HTTPParser.h:221
std::vector< HTTPState > states
Definition HTTPParser.h:219
virtual void replyMessage(std::string_view const &s)
Definition HTTPParser.h:231
virtual void version(std::string_view const &s)
Definition HTTPParser.h:222
static constexpr size_t MaxChunkSize
Definition HTTPParser.h:126
An handler for a websocket message stream.
Definition HTTPParser.h:149
size_t pendingHeaderSize
Bytes from an incomplete header.
Definition HTTPParser.h:175
virtual void endChunk()
Invoked whenever we have no more input to process.
Definition HTTPParser.h:162
virtual void frame(char const *frame, size_t s)
Definition HTTPParser.h:158
char * pendingBuffer
A buffer large enough to contain the next frame to be processed.
Definition HTTPParser.h:173
virtual ~WebSocketHandler()=default
virtual void control(char const *frame, size_t s)
FIXME: not implemented.
Definition HTTPParser.h:166
size_t remainingSize
Bytes which are still to be received for the previous, half delivered frame.
Definition HTTPParser.h:169
size_t pendingSize
Bytes which are already there from the previous, half delivered frame.
Definition HTTPParser.h:171
virtual void headers(std::map< std::string, std::string > const &headers)
Invoked when all the headers are received.
Definition HTTPParser.h:153
virtual void beginChunk()
Invoked before processing the next round of input.
Definition HTTPParser.h:160
virtual void beginFragmentation()
FIXME: not implemented.
Definition HTTPParser.h:155
virtual void endFragmentation()
FIXME: not implemented.
Definition HTTPParser.h:164