Torc  0.1
torchttprequest.h
Go to the documentation of this file.
1 #ifndef TORCHTTPREQUEST_H
2 #define TORCHTTPREQUEST_H
3 
4 // Qt
5 #include <QMap>
6 #include <QPair>
7 #include <QString>
8 #include <QDateTime>
9 
10 // Torc
11 #include "torchttpreader.h"
12 
13 class TorcSerialiser;
14 class QTcpSocket;
15 class QFile;
16 
17 typedef enum
18 {
21 } HTTPType;
22 
23 typedef enum
24 {
25  HTTPUnknownType = (0 << 0),
26  HTTPHead = (1 << 0),
27  HTTPGet = (1 << 1),
28  HTTPPost = (1 << 2),
29  HTTPPut = (1 << 3),
30  HTTPDelete = (1 << 4),
31  HTTPOptions = (1 << 5),
32  HTTPDisabled = (1 << 6),
33  HTTPAuth = (1 << 7) // require authentication
35 
36 typedef enum
37 {
56 
57 typedef enum
58 {
63 } HTTPProtocol;
64 
65 typedef enum
66 {
68  HTTP_OK = 200,
84 } HTTPStatus;
85 
86 typedef enum
87 {
92 
93 typedef enum
94 {
95  HTTPCacheNone = (1 << 0),
96  HTTPCacheShortLife = (1 << 1),
97  HTTPCacheLongLife = (1 << 2),
99  HTTPCacheETag = (1 << 4)
100 } HTTPCacheing;
101 
102 typedef enum
103 {
109 
110 #define READ_CHUNK_SIZE (1024 *64)
111 
113 {
114  friend class TorcWebSocket;
115 
116  public:
117  static HTTPRequestType RequestTypeFromString (const QString &Type);
118  static QString RequestTypeToString (HTTPRequestType Type);
119  static HTTPProtocol ProtocolFromString (const QString &Protocol);
120  static HTTPStatus StatusFromString (const QString &Status);
121  static QString ProtocolToString (HTTPProtocol Protocol);
122  static QString StatusToString (HTTPStatus Status);
123  static QString ResponseTypeToString (HTTPResponseType Response);
124  static QString AllowedToString (int Allowed);
125  static QString ConnectionToString (HTTPConnection Connection);
126  static int StringToAllowed (const QString &Allowed);
127  static QVector<QPair<quint64,quint64> > StringToRanges (const QString &Ranges, qint64 Size, qint64& SizeToSend);
128  static QString RangeToString (QPair<quint64,quint64> Range, qint64 Size);
129 
130  static char DateFormat[];
131 
132  public:
133  explicit TorcHTTPRequest(TorcHTTPReader *Reader);
134 
135  void SetConnection (HTTPConnection Connection);
136  void SetStatus (HTTPStatus Status);
137  void SetResponseType (HTTPResponseType Type);
138  void SetResponseContent (const QByteArray &Content);
139  void SetResponseFile (const QString &File);
140  void SetResponseHeader (const QString &Header, const QString &Value);
141  void SetAllowed (int Allowed);
142  void SetAllowGZip (bool Allowed);
143  void SetAllowCORS (bool Allowed);
144  void SetCache (int Cache, const QString &Tag = QStringLiteral(""));
145  void SetSecure (bool Secure);
146  bool GetSecure (void);
147  HTTPStatus GetHTTPStatus (void) const;
148  HTTPType GetHTTPType (void) const;
149  HTTPRequestType GetHTTPRequestType (void) const;
150  HTTPProtocol GetHTTPProtocol (void) const;
151  QString GetUrl (void) const;
152  QString GetPath (void) const;
153  QString GetMethod (void) const;
154  QString GetCache (void) const;
155  const QMap<QString,QString>& Headers (void) const;
156  const QMap<QString,QString>& Queries (void) const;
157  bool GetAllowCORS (void) const;
158  void Respond (QTcpSocket *Socket);
159  void Redirected (const QString &Redirected);
160  void Serialise (const QVariant &Data, const QString &Type);
161  bool Unmodified (const QDateTime &LastModified);
162  bool Unmodified (void);
163  void Authorise (HTTPAuthorisation Authorisation);
164  HTTPAuthorisation IsAuthorised (void) const;
165 
166  protected:
167  ~TorcHTTPRequest() = default;
168  void Initialise (const QString &Method);
169 
170  protected:
171  QString m_fullUrl;
172  QString m_path;
173  QString m_method;
174  QString m_query;
175  QString m_redirectedTo;
178  HTTPProtocol m_protocol;
180  QVector<QPair<quint64,quint64> > m_ranges;
181  QMap<QString,QString> m_headers;
182  QMap<QString,QString> m_queries;
183  QByteArray m_content;
184  bool m_secure;
185 
190  HTTPResponseType m_responseType;
191  int m_cache;
192  QString m_cacheTag;
194  QByteArray m_responseContent;
195  QString m_responseFile;
196  QMap<QString,QString> m_responseHeaders;
197 };
198 
199 #endif // TORCHTTPREQUEST_H
void SetAllowed(int Allowed)
bool GetAllowCORS(void) const
static char DateFormat[]
A class to encapsulate an incoming HTTP request.
QMap< QString, QString > m_queries
HTTPRequestType GetHTTPRequestType(void) const
QByteArray m_responseContent
static QString RequestTypeToString(HTTPRequestType Type)
Overlays the Websocket protocol over a QTcpSocket.
Definition: torcwebsocket.h:23
QString GetCache(void) const
void SetConnection(HTTPConnection Connection)
A convenience class to read HTTP requests from a QTcpSocket.
Definition: torchttpreader.h:8
HTTPStatus m_responseStatus
HTTPProtocol m_protocol
HTTPStatus GetHTTPStatus(void) const
static QString ResponseTypeToString(HTTPResponseType Response)
static QString StatusToString(HTTPStatus Status)
void Initialise(const QString &Method)
HTTPRequestType m_requestType
HTTPProtocol
void SetAllowCORS(bool Allowed)
HTTPAuthorisation IsAuthorised(void) const
bool Unmodified(void)
Check whether the resource is equivalent to the last seen version.
void Serialise(const QVariant &Data, const QString &Type)
static HTTPRequestType RequestTypeFromString(const QString &Type)
QString GetPath(void) const
const QMap< QString, QString > & Headers(void) const
HTTPCacheing
void SetCache(int Cache, const QString &Tag=QStringLiteral(""))
Set the caching behaviour for this response.
void SetResponseContent(const QByteArray &Content)
QString GetMethod(void) const
void SetAllowGZip(bool Allowed)
Allow gzip compression for the contents of this request.
QMap< QString, QString > m_headers
HTTPResponseType
HTTPType
TorcHTTPRequest(TorcHTTPReader *Reader)
QByteArray m_content
void SetResponseHeader(const QString &Header, const QString &Value)
HTTPStatus
static QVector< QPair< quint64, quint64 > > StringToRanges(const QString &Ranges, qint64 Size, qint64 &SizeToSend)
HTTPAuthorisation m_authorised
HTTPConnection
static QString ProtocolToString(HTTPProtocol Protocol)
void Redirected(const QString &Redirected)
static int StringToAllowed(const QString &Allowed)
void SetSecure(bool Secure)
void SetStatus(HTTPStatus Status)
HTTPAuthorisation
static HTTPProtocol ProtocolFromString(const QString &Protocol)
static QString RangeToString(QPair< quint64, quint64 > Range, qint64 Size)
HTTPRequestType
void SetResponseFile(const QString &File)
void SetResponseType(HTTPResponseType Type)
QString GetUrl(void) const
QVector< QPair< quint64, quint64 > > m_ranges
static HTTPStatus StatusFromString(const QString &Status)
static QString ConnectionToString(HTTPConnection Connection)
HTTPProtocol GetHTTPProtocol(void) const
HTTPConnection m_connection
QMap< QString, QString > m_responseHeaders
HTTPType GetHTTPType(void) const
const QMap< QString, QString > & Queries(void) const
void Authorise(HTTPAuthorisation Authorisation)
~TorcHTTPRequest()=default
static QString AllowedToString(int Allowed)
HTTPResponseType m_responseType
void Respond(QTcpSocket *Socket)