cutelyst 4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
protocolhttp.h
1/*
2 * SPDX-FileCopyrightText: (C) 2016-2018 Daniel Nicoletti <dantti12@gmail.com>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#ifndef PROTOCOLHTTP_H
6#define PROTOCOLHTTP_H
7
8#include "protocol.h"
9#include "socket.h"
10
11#include <Cutelyst/Context>
12
13#include <QObject>
14
15namespace Cutelyst {
16class Server;
17class Socket;
19 : public ProtocolData
21{
22 Q_GADGET
23public:
24 enum WebSocketPhase {
25 WebSocketPhaseHeaders,
26 WebSocketPhaseSize,
27 WebSocketPhaseMask,
28 WebSocketPhasePayload,
29 };
30 Q_ENUM(WebSocketPhase)
31
32 enum OpCode {
33 OpCodeContinue = 0x0,
34 OpCodeText = 0x1,
35 OpCodeBinary = 0x2,
36 OpCodeReserved3 = 0x3,
37 OpCodeReserved4 = 0x4,
38 OpCodeReserved5 = 0x5,
39 OpCodeReserved6 = 0x6,
40 OpCodeReserved7 = 0x7,
41 OpCodeClose = 0x8,
42 OpCodePing = 0x9,
43 OpCodePong = 0xA,
44 OpCodeReservedB = 0xB,
45 OpCodeReservedC = 0xC,
46 OpCodeReservedD = 0xD,
47 OpCodeReservedE = 0xE,
48 OpCodeReservedF = 0xF
49 };
50 Q_ENUM(OpCode)
51
52 ProtoRequestHttp(Socket *sock, int bufferSize);
53 ~ProtoRequestHttp() override;
54
55 void setupNewConnection(Socket *sock) override final;
56
57 bool writeHeaders(quint16 status, const Cutelyst::Headers &headers) override final;
58
59 qint64 doWrite(const char *data, qint64 len) override final;
60 inline qint64 doWrite(const QByteArray &data) { return doWrite(data.constData(), data.size()); }
61
62 void processingFinished() override final;
63
64 bool webSocketSendTextMessage(const QString &message) override final;
65
66 bool webSocketSendBinaryMessage(const QByteArray &message) override final;
67
68 bool webSocketSendPing(const QByteArray &payload) override final;
69
70 bool webSocketClose(quint16 code, const QString &reason) override final;
71
72 inline void resetData() override final
73 {
74 ProtocolData::resetData();
75
76 // EngineRequest
77
78 // If we deleteLater the context, there might
79 // be an event that tries to finalize the request
80 // and it will encounter a null context pointer
81 delete context;
82 context = nullptr;
83 body = nullptr;
84
85 startOfRequest = TimePointSteady{};
86 status = InitialState;
87
88 websocketUpgraded = false;
89 last = 0;
90 beginLine = 0;
91
92 serverAddress = sock->serverAddress;
93 remoteAddress = sock->remoteAddress;
94 remotePort = sock->remotePort;
95 isSecure = sock->isSecure;
96 }
97
98 virtual void socketDisconnected() override final;
99
100 QByteArray websocket_message;
101 QByteArray websocket_payload;
102 quint64 websocket_payload_size = 0;
103 quint32 websocket_need = 0;
104 quint32 websocket_mask = 0;
105 int last = 0;
106 int beginLine = 0;
107 int websocket_start_of_frame = 0;
108 int websocket_phase = 0;
109 quint8 websocket_continue_opcode = 0;
110 quint8 websocket_finn_opcode = 0;
111 bool websocketUpgraded = false;
112
113protected:
114 bool webSocketHandshakeDo(const QByteArray &key,
115 const QByteArray &origin,
116 const QByteArray &protocol) override final;
117};
118
119class ProtocolHttp2;
121class ProtocolHttp final : public Protocol
122{
123public:
124 ProtocolHttp(Server *wsgi, ProtocolHttp2 *upgradeH2c = nullptr);
125 ~ProtocolHttp() override;
126
127 Type type() const override;
128
129 void parse(Socket *sock, QIODevice *io) const override final;
130
131 ProtocolData *createData(Socket *sock) const override final;
132
133private:
134 inline bool processRequest(Socket *sock, QIODevice *io) const;
135 inline void parseMethod(const char *ptr, const char *end, Socket *sock) const;
136 inline void parseHeader(const char *ptr, const char *end, Socket *sock) const;
137
138protected:
139 friend class ProtoRequestHttp;
140
141 ProtocolWebSocket *m_websocketProto;
142 ProtocolHttp2 *m_upgradeH2c;
143 bool usingFrontendProxy;
144};
145
146} // namespace Cutelyst
147
148#endif // PROTOCOLHTTP_H
TimePointSteady startOfRequest
Container for HTTP headers.
Definition headers.h:24
bool writeHeaders(quint16 status, const Cutelyst::Headers &headers) override final
qint64 doWrite(const char *data, qint64 len) override final
void processingFinished() override final
Implements a web server.
Definition server.h:60
The Cutelyst namespace holds all public Cutelyst API.
const char * constData() const const
qsizetype size() const const