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