QHttpEngine  0.1.0
Simple and secure HTTP server for Qt applications
qhttpsocket.h
1 /*
2  * Copyright (c) 2015 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_QHTTPSOCKET_H
24 #define QHTTPENGINE_QHTTPSOCKET_H
25 
26 #include <QTcpSocket>
27 
28 #include "qhttpengine.h"
29 #include "qhttpparser.h"
30 
31 class QHTTPENGINE_EXPORT QHttpSocketPrivate;
32 
88 class QHTTPENGINE_EXPORT QHttpSocket : public QIODevice
89 {
90  Q_OBJECT
91 
92 public:
93 
97  enum {
99  OK = 200,
101  MovedPermanently = 301,
103  Found = 302,
105  BadRequest = 400,
107  Forbidden = 403,
109  NotFound = 404,
111  MethodNotAllowed = 405,
113  InternalServerError = 500
114  };
115 
122  QHttpSocket(QTcpSocket *socket, QObject *parent = 0);
123 
130  virtual qint64 bytesAvailable() const;
131 
137  virtual bool isSequential() const;
138 
145  virtual void close();
146 
153  QByteArray method() const;
154 
161  QByteArray path() const;
162 
166  bool isHeadersParsed() const;
167 
175  QHttpHeaderMap headers() const;
176 
183  qint64 contentLength() const;
184 
194  void setStatusCode(int statusCode, const QByteArray &statusReason = QByteArray());
195 
203  void setHeader(const QByteArray &name, const QByteArray &value);
204 
211  void setHeaders(const QHttpHeaderMap &headers);
212 
219  void writeHeaders();
220 
224  void writeRedirect(const QByteArray &path, bool permanent = false);
225 
229  void writeError(int statusCode, const QByteArray &statusReason = QByteArray());
230 
231 Q_SIGNALS:
232 
242 
243 protected:
244 
248  virtual qint64 readData(char *data, qint64 maxlen);
249 
253  virtual qint64 writeData(const char *data, qint64 len);
254 
255 private:
256 
257  QHttpSocketPrivate *const d;
258  friend class QHttpSocketPrivate;
259 };
260 
261 #endif // QHTTPENGINE_QHTTPSOCKET_H
Implementation of the HTTP protocol.
Definition: qhttpsocket.h:89
virtual void close()
Close the device and underlying socket.
virtual qint64 readData(char *data, qint64 maxlen)
Implementation of QIODevice::readData()
QHttpHeaderMap headers() const
Retrieve a map of request headers.
qint64 contentLength() const
Retrieve the length of the content.
QHttpSocket(QTcpSocket *socket, QObject *parent=0)
Create a new QHttpSocket from a QTcpSocket.
QByteArray method() const
Retrieve the request method.
virtual bool isSequential() const
Determine if the device is sequential.
virtual qint64 bytesAvailable() const
Retrieve the number of bytes available for reading.
void setStatusCode(int statusCode, const QByteArray &statusReason=QByteArray())
Set the response code.
void setHeaders(const QHttpHeaderMap &headers)
Set the response headers.
void writeError(int statusCode, const QByteArray &statusReason=QByteArray())
Write an HTTP error to the socket.
void writeRedirect(const QByteArray &path, bool permanent=false)
Write an HTTP 3xx redirect to the socket.
QByteArray path() const
Retrieve the request path.
bool isHeadersParsed() const
Determine if the request headers have been parsed yet.
void setHeader(const QByteArray &name, const QByteArray &value)
Set a response header to a specific value.
void headersParsed()
Indicate that request headers have been parsed.
virtual qint64 writeData(const char *data, qint64 len)
Implementation of QIODevice::writeData()
void writeHeaders()
Write response headers to the socket.