response.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 RESPONSE_H
  8. #define RESPONSE_H
  9. #include <QFile>
  10. #include <QJsonDocument>
  11. #include <QNetworkCookie>
  12. #include <QTcpSocket>
  13. #include <QTextStream>
  14. #include "constants.h"
  15. #include "cppwebframework_global.h"
  16. CWF_BEGIN_NAMESPACE
  17. class Configuration;
  18. /**
  19. * @brief The Response class is responsable to response a Http request.
  20. */
  21. class CPPWEBFRAMEWORKSHARED_EXPORT Response
  22. {
  23. QTcpSocket &socket;
  24. const Configuration &configuration;
  25. int statusCode = Response::SC_OK;
  26. QByteArray content;
  27. QByteArray statusText = HTTP::OK;
  28. QMap<QByteArray, QByteArray> headers;
  29. QVector<QNetworkCookie> cookies;
  30. public:
  31. Response(QTcpSocket &socket, const Configuration &configuration);
  32. ~Response() noexcept {}
  33. void write(const QJsonObject &json, bool writeContentType = true);
  34. void write(const QJsonArray &array, bool writeContentType = true);
  35. void write(QByteArray &&data);
  36. void write(const QByteArray &data, bool flush = true);
  37. void sendError(int sc, const QByteArray &msg);
  38. void sendJsonError(int sc, const QByteArray &msg);
  39. void flushBuffer();
  40. inline int getBufferSize() const noexcept { return content.size(); }
  41. inline QByteArray getBuffer() const noexcept { return content; }
  42. inline void addHeader(const QByteArray &name, const QByteArray &value) noexcept
  43. {
  44. headers.insert(name, value);
  45. }
  46. inline void addCookie(const QNetworkCookie &cookie) noexcept { cookies.push_back(cookie); }
  47. void setStatus(int statusCode, const QByteArray &description);
  48. void sendRedirect(const QByteArray &url);
  49. enum {
  50. SC_CONTINUE = 100,
  51. SC_SWITCHING_PROTOCOLS = 101,
  52. SC_OK = 200,
  53. SC_CREATED = 201,
  54. SC_ACCEPTED = 202,
  55. SC_NON_AUTHORITATIVE_INFORMATION = 203,
  56. SC_NO_CONTENT = 204,
  57. SC_RESET_CONTENT = 205,
  58. SC_PARTIAL_CONTENT = 206,
  59. SC_MULTIPLE_CHOICES = 300,
  60. SC_MOVED_PERMANENTLY = 301,
  61. SC_MOVED_TEMPORARILY = 302,
  62. SC_FOUND = 302,
  63. SC_SEE_OTHER = 303,
  64. SC_NOT_MODIFIED = 304,
  65. SC_USE_PROXY = 305,
  66. SC_TEMPORARY_REDIRECT = 307,
  67. SC_BAD_REQUEST = 400,
  68. SC_UNAUTHORIZED = 401,
  69. SC_PAYMENT_REQUIRED = 402,
  70. SC_FORBIDDEN = 403,
  71. SC_NOT_FOUND = 404,
  72. SC_METHOD_NOT_ALLOWED = 405,
  73. SC_NOT_ACCEPTABLE = 406,
  74. SC_PROXY_AUTHENTICATION_REQUIRED = 407,
  75. SC_REQUEST_TIMEOUT = 408,
  76. SC_CONFLICT = 409,
  77. SC_GONE = 410,
  78. SC_LENGTH_REQUIRED = 411,
  79. SC_PRECONDITION_FAILED = 412,
  80. SC_REQUEST_ENTITY_TOO_LARGE = 413,
  81. SC_REQUEST_URI_TOO_LONG = 414,
  82. SC_UNSUPPORTED_MEDIA_TYPE = 415,
  83. SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416,
  84. SC_EXPECTATION_FAILED = 417,
  85. SC_INTERNAL_SERVER_ERROR = 500,
  86. SC_NOT_IMPLEMENTED = 501,
  87. SC_BAD_GATEWAY = 502,
  88. SC_SERVICE_UNAVAILABLE = 503,
  89. SC_GATEWAY_TIMEOUT = 504,
  90. SC_HTTP_VERSION_NOT_SUPPORTED = 505
  91. };
  92. };
  93. CWF_END_NAMESPACE
  94. #endif // RESPONSE_H