Responder Class Referenceabstract

#include <Pt/Http/Responder.h>

Handles one HTTP request and writes the reply. More...

Inherited by WebSocketResponder.

Public Member Functions

 Responder (Service &s)
 Construct with service.
virtual ~Responder ()
 Destructor.
Serviceservice ()
 Returns the service for which to respond.
const Serviceservice () const
 Returns the service for which to respond.
void beginRequest (Request &request, Reply &reply, System::EventLoop &loop)
 Called when the request header was received.
void readRequest (Request &request, Reply &reply, System::EventLoop &loop)
 Called when request body data was received.
void beginReply (const Request &request, Reply &reply, System::EventLoop &loop)
 Called when request is complete.
void writeReply (const Request &request, Reply &reply, System::EventLoop &loop)
 Write responding reply.

Protected Member Functions

virtual void onBeginRequest (Request &request, Reply &reply, System::EventLoop &loop)=0
 Called when the request header was received.
virtual void onReadRequest (Request &request, Reply &reply, System::EventLoop &loop)=0
 Called for each chunk of the request body.
virtual void onBeginReply (const Request &request, Reply &reply, System::EventLoop &loop)=0
 Called when request is complete.
virtual void onWriteReply (const Request &request, Reply &reply, System::EventLoop &loop)=0
 Write responding reply.
void setReady (bool isFinished)
 Sets whether the current step is complete.
void setFinished (bool isFinished)
 Sets whether the exchange is finished.

Detailed Description

Responder is the per-exchange handler in the server model. A Service creates it for a mapped request and releases it when the reply has been sent. Derive from it and implement the four callbacks, which the server calls in order.

onBeginRequest() runs when the request header is available, so the responder can inspect fields and prepare the reply. onReadRequest() runs for each chunk of the request body, and may be called more than once. onBeginReply() runs when the request is complete and the reply should start. onWriteReply() runs when a previous Reply::beginSend(false) needs another chunk of the reply body, which is how a large reply is written in chunked encoding.

Finish the reply with Reply::beginSend() and the completion flag set to true. That call may be made from an earlier callback, in which case the remaining callbacks are skipped and the rest of the request is ignored. service() is the service that created this responder.

The example is a responder that ignores the request body and writes a fixed reply. beginSend(true) completes the reply in onBeginReply(), so onWriteReply() stays empty.

class HelloResponder : public Pt::Http::Responder
{
public:
explicit HelloResponder(Pt::Http::Service& s)
: Pt::Http::Responder(s)
{}
protected:
virtual void onBeginRequest(Pt::Http::Request& request,
{}
virtual void onReadRequest(Pt::Http::Request& request,
{}
virtual void onBeginReply(const Pt::Http::Request& request,
{
reply.body() << "Hello World!";
reply.beginSend(true);
}
virtual void onWriteReply(const Pt::Http::Request& request,
Pt::Http::Reply& reply,
Pt::System::EventLoop& loop)
{}
};
std::iostream & body()
Returns the body of the message.
Definition Message.h:480
HTTP reply message.
Definition Reply.h:64
void beginSend(bool finish=true)
Begins sending the reply.
HTTP request message.
Definition Request.h:65
Handles one HTTP request and writes the reply.
Definition Responder.h:109
virtual void onWriteReply(const Request &request, Reply &reply, System::EventLoop &loop)=0
Write responding reply.
Responder(Service &s)
Construct with service.
virtual void onReadRequest(Request &request, Reply &reply, System::EventLoop &loop)=0
Called for each chunk of the request body.
virtual void onBeginReply(const Request &request, Reply &reply, System::EventLoop &loop)=0
Called when request is complete.
virtual void onBeginRequest(Request &request, Reply &reply, System::EventLoop &loop)=0
Called when the request header was received.
Factory for request responders.
Definition Service.h:77
Event loop of a thread or process.
Definition EventLoop.h:83
HTTP clients and servers.
Core module.
Definition Allocator.h:33