Builds and sends an HTTP/1.1 response. More...
#include <httpd.hpp>
Public Member Functions | |
| TResponse (std::function< TFuture< ssize_t >(const void *, size_t)> writer) | |
| void | SetStatus (int statusCode) |
| Sets the HTTP status code (default is 200). Must be called before SendHeaders. | |
| void | SetHeader (const std::string &name, const std::string &value) |
| Adds or replaces a response header. Must be called before SendHeaders. | |
| TFuture< void > | SendHeaders () |
| Sends the status line and all accumulated headers to the client. | |
| TFuture< void > | WriteBodyChunk (const char *data, size_t size) |
| Writes data as one chunk using Transfer-Encoding: chunked. | |
| TFuture< void > | WriteBodyFull (const std::string &data) |
| Writes the full body, sending the headers first if they were not sent yet. | |
| bool | IsClosed () const |
| Returns true if the connection has been closed or an error occurred. | |
| int | StatusCode () const |
| Returns the status code set via SetStatus (default 200). | |
Builds and sends an HTTP/1.1 response.
Typical usage in a router handler:
SetStatus and SetHeader must be called before the headers are sent (by SendHeaders or, implicitly, by WriteBodyFull). After that, only body-write methods may be called.
| TFuture< void > NNet::TResponse::SendHeaders | ( | ) |
Sends the status line and all accumulated headers to the client.
Call it after SetStatus/SetHeader and before any body write, except when WriteBodyFull is the only body write: it sends the headers itself. A repeated call has no effect.
| TFuture< void > NNet::TResponse::WriteBodyChunk | ( | const char * | data, |
| size_t | size ) |
Writes data as one chunk using Transfer-Encoding: chunked.
Call SendHeaders() first (without a Content-Length header). Repeat for each chunk; the final zero-length chunk is sent automatically when the connection closes.
| TFuture< void > NNet::TResponse::WriteBodyFull | ( | const std::string & | data | ) |
Writes the full body, sending the headers first if they were not sent yet.
If SendHeaders() has not been called, sets Content-Length to the body size (unless Content-Length or Transfer-Encoding is already set) and sends the headers. If it has, only the body is written.