Hobrasoft httpd server
Embedded HTTP server for Qt and C++
httprequest.h
Go to the documentation of this file.
1 
8 #ifndef _HttpRequest_H_
9 #define _HttpRequest_H_
10 
11 #include <QTemporaryFile>
12 #include <QDateTime>
13 #include <QTcpSocket>
14 #include <QString>
15 #include "httpconnection.h"
16 
17 namespace HobrasoftHttpd {
18 
19 
23 class HttpRequest {
24  public:
28  enum Status {
34  };
35 
40 
44  HttpConnection *connection() const { return m_connection; }
45 
46 
50  QHostAddress peerAddress() const { return m_connection->peerAddress(); }
51 
52 
58  void readFromSocket(QTcpSocket *socket);
59 
63  Status status() const { return m_status; }
64 
68  QString statusString() const;
69 
73  QString method() const { return m_method; }
74 
78  QString path() const { return urlDecode(m_path); }
79 
83  QString fullPath() const { return urlDecode(m_fullpath); }
84 
88  QString version() const { return m_version; }
89 
93  QString header(const QString& name) const;
94 
100  QList<QString> headers(const QString& name) const;
101 
107  QMultiMap<QString, QString> headerMap() const { return m_headers; }
108 
116  QString parameter(const QString& name) const { return m_parameters.value(name); }
117 
121  QList<QString> parameters(const QString& name) const { return m_parameters.values(name); }
122 
126  QMultiMap<QString, QString> parameterMap() const { return m_parameters; }
127 
133  QByteArray body() const { return m_bodyData; }
134 
138  static QString urlDecode(const QString& source);
139 
143  QString cookie(const QString& name) const { return m_cookies.value(name); }
144 
148  const QMap<QString, QString>& cookieMap() { return m_cookies; }
149 
156  QTemporaryFile *uploadedFile(const QString& fieldName) { return m_uploadedFiles.value(fieldName); }
157 
168  const QMap<QString, QTemporaryFile *>& uploadedFiles() const { return m_uploadedFiles; }
169 
170 
178  const QMap<QString, QString>& contentTypes() const { return m_contentTypes; }
179 
180 
184  QString contentType(const QString& fieldName) const { return m_contentTypes.value(fieldName); }
185 
186 
190  const QDateTime& datetime() const { return m_datetime; }
191 
192 
193  private:
194  #ifndef DOXYGEN_SHOULD_SKIP_THIS
195  QMultiMap<QString, QString> m_headers;
196  QMultiMap<QString, QString> m_parameters;
197  QMap<QString, QTemporaryFile *> m_uploadedFiles;
198  QMap<QString, QString> m_contentTypes;
199  QMap<QString, QString> m_cookies;
200  QByteArray m_bodyData;
201  QString m_method;
202  QString m_path;
203  QString m_fullpath;
204  QString m_version;
205  Status m_status;
206  QByteArray m_boundary;
207  int m_currentSize;
208  int m_expectedBodySize;
209  QString m_currentHeader;
210  QTemporaryFile m_tempFile;
211  HttpConnection *m_connection;
212  QDateTime m_datetime;
213  #endif
214 
215  void parseMultiPartFile();
216 
217  void readRequest(QTcpSocket* socket);
218 
219  void readHeader (QTcpSocket* socket);
220 
221  void readBody (QTcpSocket* socket);
222 
223  void decodeRequestParams();
224 
225  void extractCookies();
226 
227 };
228 
229 }
230 
231 #endif
QList< QString > parameters(const QString &name) const
Returns all parameters of the HTTP request in QList.
Definition: httprequest.h:121
QString statusString() const
Returns current status of the request as a string.
QList< QString > headers(const QString &name) const
Returns all headers of HTTP request in QList, case insensitive.
QString fullPath() const
Returns full path of the request (/files/index.html) including parameters.
Definition: httprequest.h:83
QString path() const
Returns path of the request (/files/index.html)
Definition: httprequest.h:78
const QMap< QString, QString > & cookieMap()
Returns all cookies of the request in QMap.
Definition: httprequest.h:148
Status
Inner state of request, changes when parts of request are read from networkd.
Definition: httprequest.h:28
void readHeader(QTcpSocket *socket)
Reads HTTP headers of the request.
Definition: httprequest.cpp:90
void parseMultiPartFile()
Parses the mime multipart request.
Status status() const
Returns current status of the request.
Definition: httprequest.h:63
QHostAddress peerAddress() const
Returns peer&#39;s host address.
Definition: httprequest.h:50
QString contentType(const QString &fieldName) const
Returns content type of uploaded file.
Definition: httprequest.h:184
QString version() const
Returns version of HTTP protocol (HTTP/1.1)
Definition: httprequest.h:88
QTemporaryFile * uploadedFile(const QString &fieldName)
Returns temporary file with uploaded file from html form.
Definition: httprequest.h:156
HttpConnection * connection() const
Returns HttpConnection of the request.
Definition: httprequest.h:44
QMultiMap< QString, QString > parameterMap() const
Returns all parameters of the HTTP request in QMap.
Definition: httprequest.h:126
QString cookie(const QString &name) const
Returns cookie.
Definition: httprequest.h:143
const QMap< QString, QTemporaryFile * > & uploadedFiles() const
Returns list of uploaded files.
Definition: httprequest.h:168
QString header(const QString &name) const
Returns requested header value, case insensitive.
static QString urlDecode(const QString &source)
Converts URL encoded string to UTF8 string.
void readBody(QTcpSocket *socket)
Reads body of the request.
QMultiMap< QString, QString > headerMap() const
Returns all headers of HTTP request in QMap.
Definition: httprequest.h:107
const QMap< QString, QString > & contentTypes() const
Returns list of content types of uploaded files.
Definition: httprequest.h:178
const QDateTime & datetime() const
Returns date and time of the request.
Definition: httprequest.h:190
QByteArray body() const
Returns the body of the request.
Definition: httprequest.h:133
QString parameter(const QString &name) const
Returns parameter of the HTTP request.
Definition: httprequest.h:116
Processes HTTP request, parses headers, body and files sent by HTTP protocol.
Definition: httprequest.h:23
void readFromSocket(QTcpSocket *socket)
Reads data from socket.
Definition: httprequest.cpp:28
void readRequest(QTcpSocket *socket)
Reads request from socket.
Definition: httprequest.cpp:62
void extractCookies()
Parses cookies.
QString method() const
Returns used HTTP method of the request (GET, PUT, POST or DELETE)
Definition: httprequest.h:73
Namespace of HTTP server.
HttpRequest(HttpConnection *connection)
Constructor sets default falues from configuration.
Definition: httprequest.cpp:19
One single connection to http server.
void decodeRequestParams()
Parses parameters of the URL.