ApacheServerResponse.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // ApacheServerResponse.h
  3. //
  4. // Copyright (c) 2006-2011, Applied Informatics Software Engineering GmbH.
  5. // and Contributors.
  6. //
  7. // SPDX-License-Identifier: BSL-1.0
  8. //
  9. #ifndef ApacheConnector_ApacheServerResponse_INCLUDED
  10. #define ApacheConnector_ApacheServerResponse_INCLUDED
  11. #include "ApacheConnector.h"
  12. #include "ApacheStream.h"
  13. #include "Poco/Net/Net.h"
  14. #include "Poco/Net/HTTPServerResponse.h"
  15. class ApacheServerRequest;
  16. class ApacheServerResponse: public Poco::Net::HTTPServerResponse
  17. /// This subclass of HTTPResponse is used for
  18. /// representing server-side HTTP responses for apache.
  19. ///
  20. /// A ApacheServerResponse is passed to the
  21. /// handleRequest() method of HTTPRequestHandler.
  22. ///
  23. /// handleRequest() must set a status code
  24. /// and optional reason phrase, set headers
  25. /// as necessary, and provide a message body.
  26. {
  27. public:
  28. ApacheServerResponse(ApacheServerRequest* pRequest);
  29. /// Creates the ApacheServerResponse.
  30. ~ApacheServerResponse();
  31. /// Destroys the ApacheServerResponse.
  32. void sendContinue();
  33. /// Sends a 100 Continue response to the
  34. /// client.
  35. void sendErrorResponse(int status);
  36. /// Sends an error response with the given
  37. /// status back to the client.
  38. std::ostream& send();
  39. /// Sends the response header to the client and
  40. /// returns an output stream for sending the
  41. /// response body.
  42. ///
  43. /// The returned stream is valid until the response
  44. /// object is destroyed.
  45. ///
  46. /// Must not be called after sendFile(), sendBuffer()
  47. /// or redirect() has been called.
  48. void sendFile(const std::string& path, const std::string& mediaType);
  49. /// Sends the response header to the client, followed
  50. /// by the content of the given file.
  51. ///
  52. /// Must not be called after send(), sendBuffer()
  53. /// or redirect() has been called.
  54. ///
  55. /// Throws a FileNotFoundException if the file
  56. /// cannot be found, or an OpenFileException if
  57. /// the file cannot be opened.
  58. void sendBuffer(const void* pBuffer, std::size_t length);
  59. /// Sends the response header to the client, followed
  60. /// by the contents of the given buffer.
  61. ///
  62. /// The Content-Length header of the response is set
  63. /// to length and chunked transfer encoding is disabled.
  64. ///
  65. /// If both the HTTP message header and body (from the
  66. /// given buffer) fit into one single network packet, the
  67. /// complete response can be sent in one network packet.
  68. ///
  69. /// Must not be called after send(), sendFile()
  70. /// or redirect() has been called.
  71. void redirect(const std::string& uri, Poco::Net::HTTPResponse::HTTPStatus status);
  72. /// Sets the status code, which must be one of
  73. /// HTTP_MOVED_PERMANENTLY (301), HTTP_FOUND (302),
  74. /// or HTTP_SEE_OTHER (303),
  75. /// and sets the "Location" header field
  76. /// to the given URI, which according to
  77. /// the HTTP specification, must be absolute.
  78. ///
  79. /// Must not be called after send() has been called.
  80. void requireAuthentication(const std::string& realm);
  81. /// Sets the status code to 401 (Unauthorized)
  82. /// and sets the "WWW-Authenticate" header field
  83. /// according to the given realm.
  84. bool sent() const;
  85. /// Returns true if the response (header) has been sent.
  86. private:
  87. void initApacheOutputStream();
  88. /// Initializes the ApacheOutputStram
  89. ApacheOutputStream* _pStream;
  90. ApacheRequestRec* _pApacheRequest;
  91. };
  92. //
  93. // inlines
  94. //
  95. inline bool ApacheServerResponse::sent() const
  96. {
  97. return _pStream != 0;
  98. }
  99. #endif // ApacheConnector_ApacheServerResponse_INCLUDED