httpresponse.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. @file
  3. @author Stefan Frings
  4. */
  5. #ifndef HTTPRESPONSE_H
  6. #define HTTPRESPONSE_H
  7. #include <QMap>
  8. #include <QString>
  9. #include <QTcpSocket>
  10. #include "httpglobal.h"
  11. #include "httpcookie.h"
  12. namespace stefanfrings {
  13. /**
  14. This object represents a HTTP response, used to return something to the web client.
  15. <p>
  16. <code><pre>
  17. response.setStatus(200,"OK"); // optional, because this is the default
  18. response.writeBody("Hello");
  19. response.writeBody("World!",true);
  20. </pre></code>
  21. <p>
  22. Example how to return an error:
  23. <code><pre>
  24. response.setStatus(500,"server error");
  25. response.write("The request cannot be processed because the servers is broken",true);
  26. </pre></code>
  27. <p>
  28. In case of large responses (e.g. file downloads), a Content-Length header should be set
  29. before calling write(). Web Browsers use that information to display a progress bar.
  30. */
  31. class DECLSPEC HttpResponse {
  32. Q_DISABLE_COPY(HttpResponse)
  33. public:
  34. /**
  35. Constructor.
  36. @param socket used to write the response
  37. */
  38. HttpResponse(QTcpSocket *socket);
  39. /**
  40. Set a HTTP response header.
  41. You must call this method before the first write().
  42. @param name name of the header
  43. @param value value of the header
  44. */
  45. void setHeader(const QByteArray name, const QByteArray value);
  46. /**
  47. Set a HTTP response header.
  48. You must call this method before the first write().
  49. @param name name of the header
  50. @param value value of the header
  51. */
  52. void setHeader(const QByteArray name, const int value);
  53. /** Get the map of HTTP response headers */
  54. QMap<QByteArray,QByteArray>& getHeaders();
  55. /** Get the map of cookies */
  56. QMap<QByteArray,HttpCookie>& getCookies();
  57. /**
  58. Set status code and description. The default is 200,OK.
  59. You must call this method before the first write().
  60. */
  61. void setStatus(const int statusCode, const QByteArray description=QByteArray());
  62. /** Return the status code. */
  63. int getStatusCode() const;
  64. /**
  65. Write body data to the socket.
  66. <p>
  67. The HTTP status line, headers and cookies are sent automatically before the body.
  68. <p>
  69. If the response contains only a single chunk (indicated by lastPart=true),
  70. then a Content-Length header is automatically set.
  71. <p>
  72. Chunked mode is automatically selected if there is no Content-Length header
  73. and also no Connection:close header.
  74. @param data Data bytes of the body
  75. @param lastPart Indicates that this is the last chunk of data and flushes the output buffer.
  76. */
  77. void write(const QByteArray data, const bool lastPart=false);
  78. /**
  79. Indicates whether the body has been sent completely (write() has been called with lastPart=true).
  80. */
  81. bool hasSentLastPart() const;
  82. /**
  83. Set a cookie.
  84. You must call this method before the first write().
  85. */
  86. void setCookie(const HttpCookie& cookie);
  87. /**
  88. Send a redirect response to the browser.
  89. Cannot be combined with write().
  90. @param url Destination URL
  91. */
  92. void redirect(const QByteArray& url);
  93. /**
  94. * Flush the output buffer (of the underlying socket).
  95. * You normally don't need to call this method because flush is
  96. * automatically called after HttpRequestHandler::service() returns.
  97. */
  98. void flush();
  99. /**
  100. * May be used to check whether the connection to the web client has been lost.
  101. * This might be useful to cancel the generation of large or slow responses.
  102. */
  103. bool isConnected() const;
  104. private:
  105. /** Request headers */
  106. QMap<QByteArray,QByteArray> headers;
  107. /** Socket for writing output */
  108. QTcpSocket* socket;
  109. /** HTTP status code*/
  110. int statusCode;
  111. /** HTTP status code description */
  112. QByteArray statusText;
  113. /** Indicator whether headers have been sent */
  114. bool sentHeaders;
  115. /** Indicator whether the body has been sent completely */
  116. bool sentLastPart;
  117. /** Whether the response is sent in chunked mode */
  118. bool chunkedMode;
  119. /** Cookies */
  120. QMap<QByteArray,HttpCookie> cookies;
  121. /** Write raw data to the socket. This method blocks until all bytes have been passed to the TCP buffer */
  122. bool writeToSocket(QByteArray data);
  123. /**
  124. Write the response HTTP status and headers to the socket.
  125. Calling this method is optional, because writeBody() calls
  126. it automatically when required.
  127. */
  128. void writeHeaders();
  129. };
  130. } // end of namespace
  131. #endif // HTTPRESPONSE_H