QHttpEngine 1.0.1
Simple and secure HTTP server for Qt applications
Loading...
Searching...
No Matches
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
34class QJsonDocument;
35class QTcpSocket;
36
37namespace QHttpEngine
38{
39
40class QHTTPENGINE_EXPORT SocketPrivate;
41
96class QHTTPENGINE_EXPORT Socket : public QIODevice
97{
98 Q_OBJECT
99
100public:
101
105 typedef QMultiMap<QString, QString> QueryStringMap;
106
114 typedef QMultiMap<IByteArray, QByteArray> HeaderMap;
115
122 enum Method {
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,
156 Found = 302,
164 NotFound = 404,
168 Conflict = 409,
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
252
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
313
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
337Q_SIGNALS:
338
348
353
354protected:
355
359 virtual qint64 readData(char *data, qint64 maxlen);
360
364 virtual qint64 writeData(const char *data, qint64 len);
365
366private:
367
368 SocketPrivate *const d;
369 friend class SocketPrivate;
370};
371
372}
373
374#endif // QHTTPENGINE_SOCKET_H
Method
Definition socket.h:122
@ CONNECT
Proxy connection.
Definition socket.h:138
@ GET
Request resource.
Definition socket.h:126
@ DELETE
Delete resource.
Definition socket.h:134
@ PUT
Store resource.
Definition socket.h:132
@ TRACE
Diagnostic trace.
Definition socket.h:136
@ HEAD
Request resource without body.
Definition socket.h:128
@ POST
Store subordinate resource.
Definition socket.h:130
@ OPTIONS
Request for communications options.
Definition socket.h:124
@ BadRequest
Bad client request.
Definition socket.h:158
@ MethodNotAllowed
Method is not valid for the resource.
Definition socket.h:166
@ BadGateway
Invalid response from server while acting as a gateway.
Definition socket.h:172
@ InternalServerError
An internal server error occurred.
Definition socket.h:170
@ HttpVersionNotSupported
Server does not supports the HTTP version in the request
Definition socket.h:176
@ Created
Request was successful and a resource was created.
Definition socket.h:148
@ Forbidden
Access to the resource is forbidden.
Definition socket.h:162
@ MovedPermanently
Resource has moved permanently.
Definition socket.h:154
@ OK
Request was successful.
Definition socket.h:146
@ Found
Resource is available at an alternate URI.
Definition socket.h:156
@ NotFound
Resource was not found.
Definition socket.h:164
@ Conflict
The request could not be completed due to a conflict with the current state of the resource.
Definition socket.h:168
@ PartialContent
Range request was successful
Definition socket.h:152
@ Unauthorized
Client is unauthorized to access the resource.
Definition socket.h:160
@ Accepted
Request was accepted for processing, not completed yet.
Definition socket.h:150
@ ServiceUnavailable
Server unable to handle request due to overload
Definition socket.h:174
bool isHeadersParsed() const
Determine if the request headers have been parsed yet.
QHostAddress peerAddress() const
Retrive the address of the remote peer.
void writeRedirect(const QByteArray &path, bool permanent=false)
Write an HTTP 3xx redirect to the socket and close it.
virtual qint64 writeData(const char *data, qint64 len)
Implementation of QIODevice::writeData()
QString path() const
Retrieve the decoded path with the query string removed.
void setHeaders(const HeaderMap &headers)
Set the response headers.
virtual qint64 bytesAvailable() const
Retrieve the number of bytes available for reading.
virtual qint64 readData(char *data, qint64 maxlen)
Implementation of QIODevice::readData()
HeaderMap headers() const
Retrieve a map of request headers.
void writeJson(const QJsonDocument &document, int statusCode=OK)
Write the specified JSON document to the socket and close it.
void setStatusCode(int statusCode, const QByteArray &statusReason=QByteArray())
Set the response code.
QueryStringMap queryString() const
Retrieve the query string.
virtual void close()
Close the device and underlying socket.
QByteArray rawPath() const
Retrieve the raw request path.
QMultiMap< QString, QString > QueryStringMap
Map consisting of query string values.
Definition socket.h:105
void writeHeaders()
Write response headers to the socket.
void headersParsed()
Indicate that request headers have been parsed.
virtual bool isSequential() const
Determine if the device is sequential.
void setHeader(const QByteArray &name, const QByteArray &value, bool replace=true)
Set a response header to a specific value.
bool readJson(QJsonDocument &document)
Parse the request body as a JSON document.
void writeError(int statusCode, const QByteArray &statusReason=QByteArray())
Write an HTTP error to the socket and close it.
QMultiMap< IByteArray, QByteArray > HeaderMap
Map consisting of HTTP headers.
Definition socket.h:114
Socket(QTcpSocket *socket, QObject *parent=0)
Create a new socket from a QTcpSocket.
qint64 contentLength() const
Retrieve the length of the content.
Method method() const
Retrieve the request method.
void disconnected()
Indicate that the client has disconnected.