COROIO: NNet::TResponse Class Reference
COROIO
Loading...
Searching...
No Matches
NNet::TResponse Class Reference

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).

Detailed Description

Builds and sends an HTTP/1.1 response.

Typical usage in a router handler:

response.SetStatus(200);
response.SetHeader("Content-Type", "text/plain");
co_await response.WriteBodyFull("hello"); // sends headers with Content-Length, then the body
// -- or streaming --
response.SetHeader("Transfer-Encoding", "chunked");
co_await response.SendHeaders();
co_await response.WriteBodyChunk(data, size);

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.

Member Function Documentation

◆ SendHeaders()

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.

◆ WriteBodyChunk()

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.

◆ WriteBodyFull()

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.


The documentation for this class was generated from the following files:
  • coroio/http/httpd.hpp
  • coroio/http/httpd.cpp