-
Notifications
You must be signed in to change notification settings - Fork 1
/
MeowHttpd_nossl_nolocal.h
executable file
·201 lines (127 loc) · 4.34 KB
/
MeowHttpd_nossl_nolocal.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef MEOWHTTPD_H_
#define MEOWHTTPD_H_
#ifndef QT_NETWORK_LIB
# error("Please add network in pro file")
#endif
#ifndef QT_CONCURRENT_LIB
# error("Please add concurrent in pro file")
#endif
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
// C++ lib import
#include <functional>
// Qt lib import
#include <QObject>
#include <QSharedPointer>
#include <QPointer>
#include <QVector>
#include <QMap>
#include <QSet>
#include <QMutex>
#include <QHostAddress>
#include <QUrl>
#include<QTcpSocket>
class QIODevice;
class QThreadPool;
class QHostAddress;
class QTimer;
class QImage;
class QTcpServer;
namespace MeowHttpd
{
class Session: public QObject
{
Q_OBJECT
Q_DISABLE_COPY( Session )
public:
Session(const QPointer<QTcpSocket> &tcpSocket);
~Session();
inline void setHandleAcceptedCallback(const std::function< void(const QPointer< Session > &) > &callback) { handleAcceptedCallback_ = callback; }
inline QString requestMethod() const { return requestMethod_; }
inline QString requestUrl() const { return requestUrl_; }
inline QString requestCrlf() const { return requestCrlf_; }
inline QMap< QString, QString > requestHeader() const { return requestHeader_; }
inline QByteArray requestBody() const { return requestBody_; }
QString requestUrlPath() const;
QStringList requestUrlPathSplitToList() const;
QMap< QString, QString > requestGet() const;
QMap< QString , QByteArray > requestPost() const;
QPointer<QTcpSocket> socket() const{return ioDevice_;}
public slots:
void replyText(const QString &replyData, const int &httpStatusCode = 200);
void replyRedirects(const QUrl &targetUrl, const int &httpStatusCode = 200);
void replyJsonObject(const QJsonObject &jsonObject, const int &httpStatusCode = 200);
void replyJsonArray(const QJsonArray &jsonArray, const int &httpStatusCode = 200);
void replyFile(const QString &filePath, const int &httpStatusCode = 200);
void replyImage(const QImage &image, const int &httpStatusCode = 200);
void replyOptions();
private:
void inspectionBufferSetup1();
void inspectionBufferSetup2();
private:
QPointer< QTcpSocket > ioDevice_;
std::function< void(const QPointer< Session > &) > handleAcceptedCallback_;
QSharedPointer< QTimer > timerForClose_;
QByteArray buffer_;
QString requestMethod_;
QString requestUrl_;
QString requestCrlf_;
QMap< QString, QString > requestHeader_;
bool headerAcceptedFinish_ = false;
qint64 contentLength_ = -1;
bool alreadyReply_ = false;
QByteArray requestBody_;
qint64 waitWrittenByteCount_ = 0;
QSharedPointer< QIODevice > ioDeviceForReply_;
};
class AbstractManage: public QObject
{
Q_OBJECT
Q_DISABLE_COPY( AbstractManage )
public:
AbstractManage(const int &handleMaxThreadCount);
virtual ~AbstractManage();
inline void setHttpAcceptedCallback(const std::function< void(const QPointer< Session > &session) > &httpAcceptedCallback) { httpAcceptedCallback_ = httpAcceptedCallback; }
inline QSharedPointer< QThreadPool > handleThreadPool() { return handleThreadPool_; }
inline QSharedPointer< QThreadPool > serverThreadPool() { return serverThreadPool_; }
virtual bool isRunning() = 0;
protected Q_SLOTS:
bool begin();
void close();
protected:
virtual bool onStart() = 0;
virtual void onFinish() = 0;
bool startServerThread();
void stopHandleThread();
void stopServerThread();
void newSession(const QPointer< Session > &session);
void handleAccepted(const QPointer< Session > &session);
signals:
void readyToClose();
protected:
QSharedPointer< QThreadPool > serverThreadPool_;
QSharedPointer< QThreadPool > handleThreadPool_;
QMutex mutex_;
std::function< void(const QPointer< Session > &session) > httpAcceptedCallback_;
QSet< Session * > availableSessions_;
};
class TcpServerManage: public AbstractManage
{
Q_OBJECT
Q_DISABLE_COPY( TcpServerManage )
public:
TcpServerManage(const int &handleMaxThreadCount = 2);
~TcpServerManage();
bool listen(const QHostAddress &address, const quint16 &port);
private:
bool isRunning();
bool onStart();
void onFinish();
private:
QPointer< QTcpServer > tcpServer_;
QHostAddress listenAddress_ = QHostAddress::Any;
quint16 listenPort_ = 0;
};
}
#endif