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)
 Sends headers (with Content-Length) and the full body in one call.
 
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.SendHeaders();
co_await response.WriteBodyFull("hello"); // sets Content-Length automatically
// -- or streaming --
co_await response.WriteBodyChunk(data, size); // Transfer-Encoding: chunked

SetStatus and SetHeader must be called before SendHeaders. After SendHeaders, 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.

Must be called exactly once, after SetStatus/SetHeader and before any body write. If WriteBodyFull is used instead, SendHeaders is called implicitly.

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

Sends headers (with Content-Length) and the full body in one call.

Implies SendHeaders() — do not call it separately when using this method.


The documentation for this class was generated from the following files: