httpparser.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Copyright 2017 Herik Lima de Castro and Marcelo Medeiros Eler
  3. Distributed under MIT license, or public domain if desired and
  4. recognized in your jurisdiction.
  5. See file LICENSE for detail.
  6. */
  7. #ifndef HTTPPARSER_H
  8. #define HTTPPARSER_H
  9. #include <QByteArray>
  10. #include <QMap>
  11. #include <QNetworkCookie>
  12. #include <QVector>
  13. #include "cppwebframework_global.h"
  14. #include "urlencoder.h"
  15. CWF_BEGIN_NAMESPACE
  16. /**
  17. * @brief The class parses a HTTP message.
  18. */
  19. class CPPWEBFRAMEWORKSHARED_EXPORT HttpParser
  20. {
  21. friend class HttpReadRequest;
  22. qint64 contentLenght = 0;
  23. QByteArray contentType;
  24. QByteArray httpVersion;
  25. QByteArray method;
  26. QByteArray body;
  27. QByteArray sessionId;
  28. QByteArray url;
  29. QMultiMap<QByteArray, QByteArray> parameters;
  30. QMultiMap<QByteArray, QByteArray> headerField;
  31. QMultiMap<QByteArray, QByteArray> files;
  32. QVector<QNetworkCookie> cookies;
  33. bool valid = false;
  34. bool multiPart = false;
  35. bool readFile = false;
  36. bool extractHeaderAndBody(QByteArray &httpMessage);
  37. void doParse(QByteArray &httpMessage);
  38. void doParseHttpHeader(QByteArray &httpMessage);
  39. void doParseUrl();
  40. void doParseBody();
  41. void doParseFiles();
  42. void extractCookies();
  43. public:
  44. /**
  45. * @brief This constructor receives a HTTP message and parses it.
  46. * @param QByteArray &httpMessage : HTTP message.
  47. */
  48. explicit HttpParser(QByteArray &httpMessage) { doParse(httpMessage); }
  49. /**
  50. * @brief Returns the content lenght.
  51. * @return qint64 : Content length.
  52. */
  53. inline qint64 getContentLenght() const noexcept { return contentLenght; }
  54. /**
  55. * @brief Returns the content type.
  56. * @return QByteArray : Content type.
  57. */
  58. inline QByteArray getContentType() const noexcept { return contentType; }
  59. /**
  60. * @brief Returns the HTTP version.
  61. * @return QByteArray : HTTP version.
  62. */
  63. inline QByteArray getHttpVersion() const noexcept { return httpVersion; }
  64. /**
  65. * @brief Returns HTTP method.
  66. * @return QByteArray : HTTP method.
  67. */
  68. inline QByteArray getMethod() const noexcept { return method; }
  69. /**
  70. * @brief Returns HTTP body message.
  71. * @return QByteArray : HTTP body message.
  72. */
  73. inline QByteArray getBody() const noexcept { return body; }
  74. /**
  75. * @brief Returns session id.
  76. * @return QByteArray : session id.
  77. */
  78. inline QByteArray getSessionId() const noexcept { return sessionId; }
  79. /**
  80. * @brief Returns the url.
  81. * @return QByteArray : url.
  82. */
  83. inline QByteArray getUrl() const noexcept { return url; }
  84. /**
  85. * @brief Returns a specific parameter given a name.
  86. * If the parameter name does not exists, the function returns defaultValue.
  87. * If no defaultValue is specified, the function returns a default-constructed value.
  88. * If there are multiple parameters with a name, the value of the most recently inserted one is returned.
  89. * @param const QByteArray &name : Parameter name.
  90. * @return QByteArray : Parameter value.
  91. */
  92. inline QByteArray getParameter(const QByteArray &name,
  93. bool urlDecode = true,
  94. bool replacePlusForSpace = true) const noexcept
  95. {
  96. return urlDecode
  97. ? URLEncoder::paramDecode(parameters.value(name), replacePlusForSpace).toUtf8()
  98. : parameters.value(name);
  99. }
  100. /**
  101. * @brief Returns all parameters with a specific name.
  102. * @param const QByteArray &name : Parameter name.
  103. * @return QByteArrayList : Parameters list.
  104. */
  105. inline QByteArrayList getParameters(const QByteArray &name) const noexcept
  106. {
  107. return parameters.values(name);
  108. }
  109. /**
  110. * @brief Returns all parameters.
  111. * @return QMap<QByteArray, QByteArray> : Parameters name and value.
  112. */
  113. inline QMultiMap<QByteArray, QByteArray> getParameters() const noexcept { return parameters; }
  114. /**
  115. * @brief Returns all uploaded files.
  116. * @return QMultiMap<QByteArray, QByteArray> : Files name and content.
  117. */
  118. inline QMultiMap<QByteArray, QByteArray> getUploadedFiles() const noexcept { return files; }
  119. /**
  120. * @brief Returns all cookies.
  121. * @return QVector<QNetworkCookie> : Cookies.
  122. */
  123. inline QVector<QNetworkCookie> getCookies() const noexcept { return cookies; }
  124. /**
  125. * @brief Returns all header fields given a specific name.
  126. * @param const QByteArray &headerField : Header field name.
  127. * @return QByteArrayList : Header fields.
  128. */
  129. inline QByteArrayList getHeaderFields(const QByteArray &name) const noexcept
  130. {
  131. return headerField.values(name);
  132. }
  133. /**
  134. * @brief Returns a specific header field given a name.
  135. * If the header field name does not exists, the function returns defaultValue.
  136. * If no defaultValue is specified, the function returns a default-constructed value.
  137. * If there are multiple header field with a name, the value of the most recently inserted one is returned.
  138. * @param const QByteArray &name : Parameter name.
  139. * @return QByteArray : Parameter value.
  140. */
  141. inline QByteArray getHeaderField(const QByteArray &name) const noexcept
  142. {
  143. return headerField.value(name);
  144. }
  145. /**
  146. * @brief Returns true if HTTP is valid, else it returns false.
  147. * @return bool : HTTP message valid.
  148. */
  149. inline bool isValid() const noexcept { return valid; }
  150. /**
  151. * @brief Returns the multi part.
  152. * @return bool : Multi part.
  153. */
  154. inline bool isMultiPart() const noexcept { return multiPart; }
  155. /**
  156. * @brief Returns true if all message was read.
  157. * @return bool : Read file.
  158. */
  159. inline bool getReadFile() const noexcept { return readFile; }
  160. };
  161. CWF_END_NAMESPACE
  162. #endif // HTTPPARSER_H