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

Abstract class for processing unified HTTP requests to server's API. More...

#include <abstractcontroller.h>

Inheritance diagram for Example::Httpd::AbstractController:
HobrasoftHttpd::HttpRequestHandler Example::Httpd::ControllerExample

Public Member Functions

 AbstractController (HobrasoftHttpd::HttpConnection *parent)
 Constructor.
 
virtual void service (HobrasoftHttpd::HttpRequest *request, HobrasoftHttpd::HttpResponse *response)
 Request processing.
 
void setCheckId (bool x)
 If the checkId is set then the exixtence of the item is checked in requests PUT and POST.
 
bool checkId () const
 Returns the state of checkId. More...
 
- Public Member Functions inherited from HobrasoftHttpd::HttpRequestHandler
 HttpRequestHandler (HttpConnection *parent)
 Konstruktor.
 
HttpResponseresponse ()
 Returns new instance of HttpResponse class. More...
 

Protected Member Functions

virtual bool exists (const QString &id) const
 Check existence of th ID. More...
 
virtual void serviceOK (HobrasoftHttpd::HttpRequest *request, HobrasoftHttpd::HttpResponse *response, const QVariant &data=QVariant())
 Sends 200 OK response. More...
 
- Protected Member Functions inherited from HobrasoftHttpd::HttpRequestHandler
HttpConnectionconnection () const
 Returns pointer to parent HttpConnection class.
 
const HttpSettingssettings () const
 Returns pointer to HttpSettings used in the HttpServer.
 

Detailed Description

Abstract class for processing unified HTTP requests to server's API.

Data are passed in QVariant structures and converted to/from JSON for HTTP transport. The data can be created like this:

QVariantMap data;
data["ID"] = "1";
data["Description"] = "Some text";
QVariantList list;
list << "abc" << "xyz";
data["List"] = list;
// corresponding json: { "ID" : "1", "Description" : "Some text", "List" : [ "abc", "xyz" ] }

Class is a virtual template for unified HTTP requests. Requests are created with stable structure, for example the list of rooms:

http://localhost:8086/room
http://localhost:8086/room/events
http://localhost:8086/room/e40f2af7ea281baba30381db700311f7
http://localhost:8086/room/e40f2af7ea281baba30381db700311f7/events

The path consist of few components:

Different methods are called to retrieve data:

Derived class implementation

Default implementation returns error 501. In the derived class all method should be implemeted, which returns some useful data. It can be important to implement also the exists() method - it returns true if the requested ID exists. If it returns false, then the default AbstractController method returned error 404 before your own implementation is called and the connection is closed. Your own implementation can avoid all the care about error states:

void ControllerRoom::serviceIdGet (HobrasoftHttpd::HttpRequest *request, HobrasoftHttpd::HttpResponse *response, const QString& id) {
// It is useles to find if the room exists, abstract class cares about it
serviceOK(request, response, ROOMSLIST->room(id))
}

HTML5 event stream implementation

The HTML5 event stream can be simply implemented in serviceEvent() method. The method sends responses formated as an event - the data are formated properly and the HTTP headers are supressed. Implementation of event stream can look like this:

void ControllerRoom::serviceEvents (HobrasoftHttpd::HttpRequest *request, HobrasoftHttpd::HttpResponse *response) {
connect (ROOMSLIST, SIGNAL( statusChanged(const Room *)),
this, SLOT(slotSendUpdate(const Room *)));
QList<Room *> list = ROMSLIST->rooms().toList();
for (int i=0; i<list.size(); i++) {
slotSendUpdate(list[i]);
}
}
void ControllerRoom::serviceIdEvents (HobrasoftHttpd::HttpRequest *request, HobrasoftHttpd::HttpResponse *response, const QString& id) {
connect(room, SIGNAL( statusChanged(const Room *)),
this, SLOT(slotSendUpdate(const Room *)));
slotSendUpdate(room);
}
void ControllerRoom::slotSendUpdate(const Room *skupina) {
serviceEvent(NULL, NULL, room->webStatus());
}

Data are passed in QVariant structures and formated in JSON to be send to clients.

Definition at line 114 of file abstractcontroller.h.

Member Function Documentation

◆ checkId()

bool Example::Httpd::AbstractController::checkId ( ) const
inline

Returns the state of checkId.

See also
setCheckId()

Definition at line 138 of file abstractcontroller.h.

◆ exists()

virtual bool Example::Httpd::AbstractController::exists ( const QString &  id) const
inlineprotectedvirtual

Check existence of th ID.

Returns
true if the ID exists

The method should be reimplemented in derived classes. Depending of the result the decission is made if the 404 error is sent to the request, of if the reimplemented method will be called (one of serviceId(), serviceIdEvents(), serviceIdDelete()).

Reimplemented in Example::Httpd::ControllerExample.

Definition at line 152 of file abstractcontroller.h.

◆ serviceOK()

void AbstractController::serviceOK ( HobrasoftHttpd::HttpRequest request,
HobrasoftHttpd::HttpResponse response,
const QVariant &  data = QVariant() 
)
protectedvirtual

Sends 200 OK response.

Parameters
data- data are send instead of default '{ "ok" : true }'

Definition at line 173 of file abstractcontroller.cpp.


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