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 "Framework/Endian.h"
16#include <fmt/format.h>
17#include <uv.h>
18#include <string>
19#include <vector>
20#include <map>
21
22namespace o2::framework
23{
24
25struct __attribute__((__packed__)) WebSocketFrameTiny {
26#if O2_HOST_BYTE_ORDER == O2_LITTLE_ENDIAN
27 unsigned char opcode : 4;
28 unsigned char rsv3 : 1;
29 unsigned char rsv2 : 1;
30 unsigned char rsv1 : 1;
31 unsigned char fin : 1;
32 unsigned char len : 7;
33 unsigned char mask : 1;
34#elif O2_HOST_BYTE_ORDER == O2_BIG_ENDIAN
35 unsigned char fin : 1;
36 unsigned char rsv1 : 1;
37 unsigned char rsv2 : 1;
38 unsigned char rsv3 : 1;
39 unsigned char opcode : 4;
40 unsigned char mask : 1;
41 unsigned char len : 7;
42#else
43#error Uknown endiannes
44#endif
45};
46
47struct __attribute__((__packed__)) WebSocketFrameShort {
48#if O2_HOST_BYTE_ORDER == O2_LITTLE_ENDIAN
49 unsigned char opcode : 4;
50 unsigned char rsv3 : 1;
51 unsigned char rsv2 : 1;
52 unsigned char rsv1 : 1;
53 unsigned char fin : 1;
54 unsigned char len : 7;
55 unsigned char mask : 1;
56#elif O2_HOST_BYTE_ORDER == O2_BIG_ENDIAN
57 unsigned char fin : 1;
58 unsigned char rsv1 : 1;
59 unsigned char rsv2 : 1;
60 unsigned char rsv3 : 1;
61 unsigned char opcode : 4;
62 unsigned char mask : 1;
63 unsigned char len : 7;
64#else
65#error Uknown endiannes
66#endif
67 uint16_t len16;
68};
69
70struct __attribute__((__packed__)) WebSocketFrameHuge {
71#if O2_HOST_BYTE_ORDER == O2_LITTLE_ENDIAN
72 unsigned char opcode : 4;
73 unsigned char rsv3 : 1;
74 unsigned char rsv2 : 1;
75 unsigned char rsv1 : 1;
76 unsigned char fin : 1;
77 unsigned char len : 7;
78 unsigned char mask : 1;
79#elif O2_HOST_BYTE_ORDER == O2_BIG_ENDIAN
80 unsigned char fin : 1;
81 unsigned char rsv1 : 1;
82 unsigned char rsv2 : 1;
83 unsigned char rsv3 : 1;
84 unsigned char opcode : 4;
85 unsigned char mask : 1;
86 unsigned char len : 7;
87#else
88#error Uknown endiannes
89#endif
90 uint64_t len64;
91};
92
93enum struct WebSocketFrameKind {
94 Tiny,
95 Short,
96 Huge
97};
98
99enum struct WebSocketOpCode : uint8_t {
100 Continuation = 0,
101 Text = 1,
102 Binary = 2,
103 Close = 8,
104 Ping = 9,
105 Pong = 10
106};
107
114 constexpr static size_t MaxChunkSize = 0x10000;
115};
116
123std::string encode_websocket_handshake_request(const char* path, const char* protocol, int version, char const* nonce,
124 std::vector<std::pair<std::string, std::string>> headers = {});
125
128std::string encode_websocket_handshake_reply(char const* nonce);
129
133void encode_websocket_frames(std::vector<uv_buf_t>& outputs, char const* src, size_t size, WebSocketOpCode opcode, uint32_t mask);
134
137 virtual ~WebSocketHandler() = default;
138
140 virtual void headers(std::map<std::string, std::string> const& headers){};
142 virtual void beginFragmentation(){};
145 virtual void frame(char const* frame, size_t s) {}
147 virtual void beginChunk() {}
149 virtual void endChunk() {}
151 virtual void endFragmentation() {}
153 virtual void control(char const* frame, size_t s) {}
154
156 size_t remainingSize = 0;
158 size_t pendingSize = 0;
160 char* pendingBuffer = nullptr;
163 char* pendingHeader = nullptr;
164};
165
168void decode_websocket(char* src, size_t size, WebSocketHandler& handler);
169
202
204 std::string remaining;
205 std::string error;
206 std::vector<HTTPState> states;
207 virtual void method(std::string_view const& s){};
208 virtual void target(std::string_view const& s){};
209 virtual void version(std::string_view const& s){};
210 virtual void header(std::string_view const& k, std::string_view const& v){};
211 virtual void endHeaders(){};
215 virtual void body(char* data, size_t s){};
216 virtual void replyVersion(std::string_view const& s){};
217 virtual void replyCode(std::string_view const& s){};
218 virtual void replyMessage(std::string_view const& s){};
219};
220
223 static std::string calculateAccept(const char* nonce);
224};
225
226void parse_http_request(char* start, size_t size, HTTPParser* parser);
227
228std::pair<std::string, unsigned short> parse_websocket_url(const char* s);
229} // namespace o2::framework
230#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 PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
@ 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)
std::string encode_websocket_handshake_reply(char const *nonce)
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)
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:216
virtual void header(std::string_view const &k, std::string_view const &v)
Definition HTTPParser.h:210
virtual void method(std::string_view const &s)
Definition HTTPParser.h:207
virtual void replyCode(std::string_view const &s)
Definition HTTPParser.h:217
virtual void endHeaders()
Definition HTTPParser.h:211
virtual void body(char *data, size_t s)
Definition HTTPParser.h:215
virtual void target(std::string_view const &s)
Definition HTTPParser.h:208
std::vector< HTTPState > states
Definition HTTPParser.h:206
virtual void replyMessage(std::string_view const &s)
Definition HTTPParser.h:218
virtual void version(std::string_view const &s)
Definition HTTPParser.h:209
static constexpr size_t MaxChunkSize
Definition HTTPParser.h:114
An handler for a websocket message stream.
Definition HTTPParser.h:136
size_t pendingHeaderSize
Bytes from an incomplete header.
Definition HTTPParser.h:162
virtual void endChunk()
Invoked whenever we have no more input to process.
Definition HTTPParser.h:149
virtual void frame(char const *frame, size_t s)
Definition HTTPParser.h:145
char * pendingBuffer
A buffer large enough to contain the next frame to be processed.
Definition HTTPParser.h:160
virtual ~WebSocketHandler()=default
virtual void control(char const *frame, size_t s)
FIXME: not implemented.
Definition HTTPParser.h:153
size_t remainingSize
Bytes which are still to be received for the previous, half delivered frame.
Definition HTTPParser.h:156
size_t pendingSize
Bytes which are already there from the previous, half delivered frame.
Definition HTTPParser.h:158
virtual void headers(std::map< std::string, std::string > const &headers)
Invoked when all the headers are received.
Definition HTTPParser.h:140
virtual void beginChunk()
Invoked before processing the next round of input.
Definition HTTPParser.h:147
virtual void beginFragmentation()
FIXME: not implemented.
Definition HTTPParser.h:142
virtual void endFragmentation()
FIXME: not implemented.
Definition HTTPParser.h:151