Hobrasoft httpd server
Embedded HTTP server for Qt and C++
Public Member Functions | Protected Member Functions | List of all members
HobrasoftHttpd::HttpRequestHandler Class Reference

Processes incoming requests. More...

#include <httprequesthandler.h>

Inheritance diagram for HobrasoftHttpd::HttpRequestHandler:
Example::Httpd::AbstractController Example::Httpd::RequestMapper HobrasoftHttpd::ShtmlController HobrasoftHttpd::StaticFileController Example::Httpd::ControllerExample

Public Member Functions

 HttpRequestHandler (HttpConnection *parent)
 Konstruktor.
 
virtual void service (HttpRequest *request, HttpResponse *response)
 Request processing. Should be reimplemented in derived class. More...
 
HttpResponseresponse ()
 Returns new instance of HttpResponse class. More...
 

Protected Member Functions

HttpConnectionconnection () const
 Returns pointer to parent HttpConnection class.
 
const HttpSettingssettings () const
 Returns pointer to HttpSettings used in the HttpServer.
 

Detailed Description

Processes incoming requests.

When building your own http server, you should reimplement this class.

You need to reimplement the service() method, for example:

void RequestMapper::service(HttpRequest *request, HttpResponse *response) {
QString path = request->path();
#define ROUTER(address, hclass) \
if (path.startsWith(address)) { \
HttpRequestHandler *controller = new hclass (connection()); \
controller->service(request, response); \
return; \
}
ROUTER("/translations", ControllerTranslations);
ROUTER("/errorevents", ControllerErrorEvents);
ROUTER("/callqueue", ControllerCallQueue);
}

Controller classes in the example are derived from HttpRequstHandler, too.

How the event streams should be implemented

When implementing event streams you should make a slot to receive information about changed status. Create new HttpResponse in the slot and write your data to it:

MyOwnController::MyOwnController(HttpCOnnection *parent) : HttpRequestHandler(parent) {
connect(myclass, SIGNAL( statusChanged(MyClass *)),
this, SLOT(slotSendUpdate(MyClass *)));
}
void MyOwnController::slotSendUpdate(MyClass *object) {
HttpResponse response = HttpRequestHandler::response();
resopnse.setHeader("Transfer-Encoding", "chunked");
response.setHeader("Content-Type", "text/event-stream");
response.setHeader("Cache-Control", "no-cache,public");
response.setSendHeaders(false); // suppress the header to be send
QByteArray datagram;
datagram += "event: status\n";
datagram += "data: ";
datagram += object->status(); // method returns text formated status of the object
datagram += "\n";
datagram += "\n";
response.write(datagram);
response.flush();
}

If you want to use simplier way to make your own event streams, use the class AbstractController from example. AbstractController class implements simple json API to get/put/delete objects, lists, event streams.

Definition at line 81 of file httprequesthandler.h.

Member Function Documentation

◆ response()

HttpResponse * HttpRequestHandler::response ( )

Returns new instance of HttpResponse class.

The instance is connected with opened socket. Method is used in derived classes in specialized handlers used to handle text/event-stream requests. In such case one request invokes multiple responses. Each response should be created calling this method.

Volá HttpConnection::response().

Definition at line 36 of file httprequesthandler.cpp.

◆ service()

void HttpRequestHandler::service ( HttpRequest request,
HttpResponse response 
)
virtual

Request processing. Should be reimplemented in derived class.

This method should be reimplemeted in derived class. Default implementations can handle static content and shtml files only.

Reimplemented in Example::Httpd::AbstractController, Example::Httpd::RequestMapper, HobrasoftHttpd::StaticFileController, and HobrasoftHttpd::ShtmlController.

Definition at line 23 of file httprequesthandler.cpp.


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