cutelyst 5.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
protocol.h
1/*
2 * SPDX-FileCopyrightText: (C) 2016-2018 Daniel Nicoletti <dantti12@gmail.com>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#ifndef PROTOCOL_H
6#define PROTOCOL_H
7
8#include <QDebug>
9#include <QObject>
10
11class QIODevice;
12
13namespace Cutelyst {
14
15class Server;
16class Socket;
17class Protocol;
19{
20 Q_GADGET
21public:
22 ProtocolData(Socket *sock, int bufferSize);
23 virtual ~ProtocolData();
24
25 ProtocolData(const ProtocolData &) = delete;
26 ProtocolData &operator=(const ProtocolData &) = delete;
27
28 enum class HeaderConnection {
29 NotSet = 0,
30 Keep,
31 Close,
32 Upgrade,
33 };
34 Q_ENUM(HeaderConnection)
35
36 enum ParserState {
37 MethodLine = 0,
38 HeaderLine,
39 ContentBody,
40 H2Frames,
41 };
42 Q_ENUM(ParserState)
43
44 virtual void resetData()
45 {
46 connState = MethodLine;
47 buf_size = 0;
48 headerConnection = HeaderConnection::NotSet;
49 headerHost = false;
50 X_Forwarded_For = false;
51 X_Forwarded_Host = false;
52 X_Forwarded_Proto = false;
53 }
54
55 virtual void socketDisconnected() {}
56 virtual void setupNewConnection(Socket *sock) = 0;
57
58 qint64 contentLength = 0;
59 Socket *sock; // temporary
60 QIODevice *io;
61 ProtocolData *upgradedFrom = nullptr;
62 int buf_size = 0;
63 ParserState connState = MethodLine;
64 HeaderConnection headerConnection = HeaderConnection::NotSet;
65 char *buffer;
66 bool headerHost = false;
67 bool X_Forwarded_For = false;
68 bool X_Forwarded_Host = false;
69 bool X_Forwarded_Proto = false;
70};
71
73{
74 Q_GADGET
75public:
76 enum class Type { Unknown, Http11, Http11Websocket, Http2, FastCGI1 };
77 Q_ENUM(Type)
78
79 Protocol(const Server *server);
80 virtual ~Protocol();
81
82 virtual Type type() const;
83
84 virtual void parse(Socket *sock, QIODevice *io) const = 0;
85
86 virtual ProtocolData *createData(Socket *sock) const = 0;
87
88 QIODevice *createBody(qint64 contentLength) const;
89
90 qint64 m_postBufferSize;
91 qint64 m_postBuffering;
92 char *m_postBuffer;
93 int m_bufferSize;
94 bool const useStats;
95};
96
97inline quint64 net_be64(const char *buf)
98{
99 quint64 ret = 0;
100 auto src = reinterpret_cast<const quint64 *>(buf);
101 auto ptr = reinterpret_cast<quint8 *>(&ret);
102 ptr[0] = static_cast<quint8>((*src >> 56) & 0xff);
103 ptr[1] = static_cast<quint8>((*src >> 48) & 0xff);
104 ptr[2] = static_cast<quint8>((*src >> 40) & 0xff);
105 ptr[3] = static_cast<quint8>((*src >> 32) & 0xff);
106 ptr[4] = static_cast<quint8>((*src >> 24) & 0xff);
107 ptr[5] = static_cast<quint8>((*src >> 16) & 0xff);
108 ptr[6] = static_cast<quint8>((*src >> 8) & 0xff);
109 ptr[7] = static_cast<quint8>(*src & 0xff);
110 return ret;
111}
112
113inline quint32 net_be32(const char *buf)
114{
115 quint32 ret = 0;
116 auto src = reinterpret_cast<const quint32 *>(buf);
117 auto ptr = reinterpret_cast<quint8 *>(&ret);
118 ptr[0] = static_cast<quint8>((*src >> 24) & 0xff);
119 ptr[1] = static_cast<quint8>((*src >> 16) & 0xff);
120 ptr[2] = static_cast<quint8>((*src >> 8) & 0xff);
121 ptr[3] = static_cast<quint8>(*src & 0xff);
122 return ret;
123}
124
125inline quint32 net_be24(const char *buf)
126{
127 quint32 ret = 0;
128 auto src = reinterpret_cast<const quint32 *>(buf);
129 auto ptr = reinterpret_cast<quint8 *>(&ret);
130 ptr[0] = static_cast<quint8>((*src >> 16) & 0xff);
131 ptr[1] = static_cast<quint8>((*src >> 8) & 0xff);
132 ptr[2] = static_cast<quint8>(*src & 0xff);
133 return ret;
134}
135
136inline quint16 net_be16(const char *buf)
137{
138 quint16 ret = 0;
139 auto src = reinterpret_cast<const quint16 *>(buf);
140 auto ptr = reinterpret_cast<quint8 *>(&ret);
141 ptr[0] = static_cast<quint8>((*src >> 8) & 0xff);
142 ptr[1] = static_cast<quint8>(*src & 0xff);
143 return ret;
144}
145
146} // namespace Cutelyst
147
148#endif // PROTOCOL_H
Implements a web server.
Definition server.h:60
The Cutelyst namespace holds all public Cutelyst API.