httprequest.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. @file
  3. @author Stefan Frings
  4. */
  5. #ifndef HTTPREQUEST_H
  6. #define HTTPREQUEST_H
  7. #include <QByteArray>
  8. #include <QHostAddress>
  9. #include <QTcpSocket>
  10. #include <QMap>
  11. #include <QMultiMap>
  12. #include <QSettings>
  13. #include <QTemporaryFile>
  14. #include <QUuid>
  15. #include "httpglobal.h"
  16. namespace stefanfrings {
  17. /**
  18. This object represents a single HTTP request. It reads the request
  19. from a TCP socket and provides getters for the individual parts
  20. of the request.
  21. <p>
  22. The following config settings are required:
  23. <code><pre>
  24. maxRequestSize=16000
  25. maxMultiPartSize=1000000
  26. </pre></code>
  27. <p>
  28. MaxRequestSize is the maximum size of a HTTP request. In case of
  29. multipart/form-data requests (also known as file-upload), the maximum
  30. size of the body must not exceed maxMultiPartSize.
  31. */
  32. class DECLSPEC HttpRequest {
  33. Q_DISABLE_COPY(HttpRequest)
  34. friend class HttpSessionStore;
  35. public:
  36. /** Values for getStatus() */
  37. enum RequestStatus {waitForRequest, waitForHeader, waitForBody, complete, abort_size, abort_broken};
  38. /**
  39. Constructor.
  40. @param settings Configuration settings
  41. */
  42. HttpRequest(const QSettings* settings);
  43. /**
  44. Destructor.
  45. */
  46. virtual ~HttpRequest();
  47. /**
  48. Read the HTTP request from a socket.
  49. This method is called by the connection handler repeatedly
  50. until the status is RequestStatus::complete or RequestStatus::abort.
  51. @param socket Source of the data
  52. */
  53. void readFromSocket(QTcpSocket *socket);
  54. /**
  55. Get the status of this reqeust.
  56. @see RequestStatus
  57. */
  58. RequestStatus getStatus() const;
  59. /** Get the method of the HTTP request (e.g. "GET") */
  60. const QByteArray& getMethod() const;
  61. /** Get the decoded path of the HTPP request (e.g. "/index.html") */
  62. QByteArray getPath() const;
  63. /** Get the raw path of the HTTP request (e.g. "/file%20with%20spaces.html") */
  64. const QByteArray& getRawPath() const;
  65. /** Get the version of the HTPP request (e.g. "HTTP/1.1") */
  66. const QByteArray& getVersion() const;
  67. /**
  68. Get the value of a HTTP request header.
  69. @param name Name of the header, not case-senitive.
  70. @return If the header occurs multiple times, only the last
  71. one is returned.
  72. */
  73. QByteArray getHeader(const QByteArray& name) const;
  74. /**
  75. Get the values of a HTTP request header.
  76. @param name Name of the header, not case-senitive.
  77. */
  78. QList<QByteArray> getHeaders(const QByteArray& name) const;
  79. /**
  80. * Get all HTTP request headers. Note that the header names
  81. * are returned in lower-case.
  82. */
  83. const QMultiMap<QByteArray,QByteArray>& getHeaderMap() const;
  84. /**
  85. Get the value of a HTTP request parameter.
  86. @param name Name of the parameter, case-sensitive.
  87. @return If the parameter occurs multiple times, only the last
  88. one is returned.
  89. */
  90. QByteArray getParameter(const QByteArray& name) const;
  91. /**
  92. Get the values of a HTTP request parameter.
  93. @param name Name of the parameter, case-sensitive.
  94. */
  95. QList<QByteArray> getParameters(const QByteArray& name) const;
  96. /** Get all HTTP request parameters. */
  97. const QMultiMap<QByteArray,QByteArray>& getParameterMap() const;
  98. /** Get the HTTP request body. */
  99. const QByteArray& getBody() const;
  100. /**
  101. Decode an URL parameter.
  102. E.g. replace "%23" by '#' and replace '+' by ' '.
  103. @param source The url encoded strings
  104. @see QUrl::toPercentEncoding for the reverse direction
  105. */
  106. static QByteArray urlDecode(const QByteArray source);
  107. /**
  108. Get an uploaded file. The file is already open. It will
  109. be closed and deleted by the destructor of this HttpRequest
  110. object (after processing the request).
  111. <p>
  112. For uploaded files, the method getParameters() returns
  113. the original fileName as provided by the calling web browser.
  114. */
  115. QTemporaryFile* getUploadedFile(const QByteArray fieldName) const;
  116. /**
  117. Get the value of a cookie.
  118. @param name Name of the cookie
  119. */
  120. QByteArray getCookie(const QByteArray& name) const;
  121. /** Get all cookies. */
  122. const QMap<QByteArray,QByteArray>& getCookieMap() const;
  123. /**
  124. Get the address of the connected client.
  125. Note that multiple clients may have the same IP address, if they
  126. share an internet connection (which is very common).
  127. */
  128. const QHostAddress& getPeerAddress() const;
  129. private:
  130. /** Request headers */
  131. QMultiMap<QByteArray,QByteArray> headers;
  132. /** Parameters of the request */
  133. QMultiMap<QByteArray,QByteArray> parameters;
  134. /** Uploaded files of the request, key is the field name. */
  135. QMap<QByteArray,QTemporaryFile*> uploadedFiles;
  136. /** Received cookies */
  137. QMap<QByteArray,QByteArray> cookies;
  138. /** Storage for raw body data */
  139. QByteArray bodyData;
  140. /** Request method */
  141. QByteArray method;
  142. /** Request path (in raw encoded format) */
  143. QByteArray path;
  144. /** Request protocol version */
  145. QByteArray version;
  146. /**
  147. Status of this request. For the state engine.
  148. @see RequestStatus
  149. */
  150. RequestStatus status;
  151. /** Address of the connected peer. */
  152. QHostAddress peerAddress;
  153. /** Maximum size of requests in bytes. */
  154. int maxSize;
  155. /** Maximum allowed size of multipart forms in bytes. */
  156. int maxMultiPartSize;
  157. /** Current size */
  158. int currentSize;
  159. /** Expected size of body */
  160. int expectedBodySize;
  161. /** Name of the current header, or empty if no header is being processed */
  162. QByteArray currentHeader;
  163. /** Boundary of multipart/form-data body. Empty if there is no such header */
  164. QByteArray boundary;
  165. /** Temp file, that is used to store the multipart/form-data body */
  166. QTemporaryFile* tempFile;
  167. /** Parse the multipart body, that has been stored in the temp file. */
  168. void parseMultiPartFile();
  169. /** Sub-procedure of readFromSocket(), read the first line of a request. */
  170. void readRequest(QTcpSocket* socket);
  171. /** Sub-procedure of readFromSocket(), read header lines. */
  172. void readHeader(QTcpSocket* socket);
  173. /** Sub-procedure of readFromSocket(), read the request body. */
  174. void readBody(QTcpSocket* socket);
  175. /** Sub-procedure of readFromSocket(), extract and decode request parameters. */
  176. void decodeRequestParams();
  177. /** Sub-procedure of readFromSocket(), extract cookies from headers */
  178. void extractCookies();
  179. /** Buffer for collecting characters of request and header lines */
  180. QByteArray lineBuffer;
  181. };
  182. } // end of namespace
  183. #endif // HTTPREQUEST_H