QHttpEngine  1.0.0
Simple and secure HTTP server for Qt applications
 All Classes Functions Typedefs Enumerations Enumerator Pages
socket.h
1 /*
2  * Copyright (c) 2017 Nathan Osman
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 
23 #ifndef QHTTPENGINE_SOCKET_H
24 #define QHTTPENGINE_SOCKET_H
25 
26 #include <QHostAddress>
27 #include <QIODevice>
28 #include <QMultiMap>
29 
30 #include <qhttpengine/ibytearray.h>
31 
32 #include "qhttpengine_export.h"
33 
34 class QJsonDocument;
35 class QTcpSocket;
36 
37 namespace QHttpEngine
38 {
39 
40 class QHTTPENGINE_EXPORT SocketPrivate;
41 
96 class QHTTPENGINE_EXPORT Socket : public QIODevice
97 {
98  Q_OBJECT
99 
100 public:
101 
105  typedef QMultiMap<QString, QString> QueryStringMap;
106 
114  typedef QMultiMap<IByteArray, QByteArray> HeaderMap;
115 
122  enum Method {
124  OPTIONS = 1,
126  GET = 1 << 1,
128  HEAD = 1 << 2,
130  POST = 1 << 3,
132  PUT = 1 << 4,
134  DELETE = 1 << 5,
136  TRACE = 1 << 6,
138  CONNECT = 1 << 7
139  };
140 
144  enum {
146  OK = 200,
148  Created = 201,
150  Accepted = 202,
152  PartialContent = 206,
154  MovedPermanently = 301,
156  Found = 302,
158  BadRequest = 400,
160  Unauthorized = 401,
162  Forbidden = 403,
164  NotFound = 404,
166  MethodNotAllowed = 405,
168  Conflict = 409,
170  InternalServerError = 500,
172  BadGateway = 502,
174  ServiceUnavailable = 503,
176  HttpVersionNotSupported = 505
177  };
178 
185  Socket(QTcpSocket *socket, QObject *parent = 0);
186 
193  virtual qint64 bytesAvailable() const;
194 
200  virtual bool isSequential() const;
201 
209  virtual void close();
210 
214  QHostAddress peerAddress() const;
215 
219  bool isHeadersParsed() const;
220 
227  Method method() const;
228 
235  QByteArray rawPath() const;
236 
243  QString path() const;
244 
251  QueryStringMap queryString() const;
252 
260  HeaderMap headers() const;
261 
268  qint64 contentLength() const;
269 
284  bool readJson(QJsonDocument &document);
285 
295  void setStatusCode(int statusCode, const QByteArray &statusReason = QByteArray());
296 
304  void setHeader(const QByteArray &name, const QByteArray &value, bool replace = true);
305 
312  void setHeaders(const HeaderMap &headers);
313 
320  void writeHeaders();
321 
325  void writeRedirect(const QByteArray &path, bool permanent = false);
326 
330  void writeError(int statusCode, const QByteArray &statusReason = QByteArray());
331 
335  void writeJson(const QJsonDocument &document, int statusCode = OK);
336 
337 Q_SIGNALS:
338 
347  void headersParsed();
348 
352  void disconnected();
353 
354 protected:
355 
359  virtual qint64 readData(char *data, qint64 maxlen);
360 
364  virtual qint64 writeData(const char *data, qint64 len);
365 
366 private:
367 
368  SocketPrivate *const d;
369  friend class SocketPrivate;
370 };
371 
372 }
373 
374 #endif // QHTTPENGINE_SOCKET_H
QMultiMap< IByteArray, QByteArray > HeaderMap
Map consisting of HTTP headers.
Definition: socket.h:114
Method
Definition: socket.h:122
QMultiMap< QString, QString > QueryStringMap
Map consisting of query string values.
Definition: socket.h:105
Implementation of the HTTP protocol.
Definition: socket.h:96