tufao  1.3.0
An asynchronous web framework for C++ built on top of Qt
Tufao::HttpServer Class Reference

The Tufao::HttpServer class provides an implementation of the HTTP protocol. More...

+ Inheritance diagram for Tufao::HttpServer:
+ Collaboration diagram for Tufao::HttpServer:

Public Types

typedef std::function< void(HttpServerRequest &request, const QByteArray &)> UpgradeHandler
 A typedef to http upgrade request handler. More...
 

Public Slots

void close ()
 Closes the server. More...
 

Signals

void requestReady (Tufao::HttpServerRequest &request, Tufao::HttpServerResponse &response)
 This signal is emitted each time there is request. More...
 

Public Member Functions

 HttpServer (QObject *parent=0)
 Constructs a Tufao::HttpServer object. More...
 
 ~HttpServer ()
 Destroys the object.
 
bool listen (const QHostAddress &address=QHostAddress::Any, quint16 port=0)
 Tells the server to listen for incoming connections on address address and port port. More...
 
bool isListening () const
 Returns true if the server is listening for incoming connections.
 
quint16 serverPort () const
 Returns the server's port if the server is listening; otherwise returns 0. More...
 
void setTimeout (int msecs=0)
 Sets the timeout of new connections to msecs miliseconds. More...
 
int timeout () const
 Returns the current set timeout.
 
void setUpgradeHandler (UpgradeHandler functor)
 This method sets the handler that will be called to handle http upgrade requests. More...
 

Static Public Member Functions

static UpgradeHandler defaultUpgradeHandler ()
 Returns the default http upgrade request's handler. More...
 

Protected Member Functions

void handleConnection (QAbstractSocket *connection)
 Call this function will make Tufao::HttpServer handle the connection connection. More...
 
virtual void incomingConnection (qintptr socketDescriptor)
 This virtual function is called by HttpServer when a new connection is available. More...
 
virtual void checkContinue (HttpServerRequest &request, HttpServerResponse &response)
 This virtual function is called by HttpServer when a client do a request with the HTTP header "Expect: 100-continue". More...
 

Detailed Description

The Tufao::HttpServer class provides an implementation of the HTTP protocol.

The HTTP is a stateless request-response based protoccol. It let you create distributed dynamic collaborative applications.

To create a webserver, all you need to do is call Tufao::HttpServer::listen and handle the Tufao::HttpServer::requestReady signal.

#include <Tufao/HttpServer>
class WebServer: public QObject
{
Q_OBJECT
public:
explicit WebServer(QObject *parent = NULL) :
QObject(parent),
httpServer(new Tufao::HttpServer(this))
{
using namespace Tufao;
connect(httpServer, &HttpServer::requestReady,
[](HttpServerRequest &request, HttpServerResponse &response) {
response.writeHead(200, "OK");
response.headers().insert("Content-Type", "text/plain");
response.write("Hello World\n");
response.end();
});
httpServer->listen(QHostAddress::Any, 8080);
}
private:
Tufao::HttpServer *httpServer;
};
See also
Tufao::HttpServerRequest
Tufao::HttpServerResponse

Member Typedef Documentation

typedef std::function<void(HttpServerRequest &request, const QByteArray&)> Tufao::HttpServer::UpgradeHandler

A typedef to http upgrade request handler.

See also
AbstractHttpUpgradeHandler setUpgradeHandler
Since
1.0

Constructor & Destructor Documentation

Tufao::HttpServer::HttpServer ( QObject *  parent = 0)
explicit

Constructs a Tufao::HttpServer object.

parent is passed to the QObject constructor.

Member Function Documentation

virtual void Tufao::HttpServer::checkContinue ( HttpServerRequest request,
HttpServerResponse response 
)
protectedvirtual

This virtual function is called by HttpServer when a client do a request with the HTTP header "Expect: 100-continue".

The base implementation call Tufao::HttpServerRequest::writeContinue and emit the Tufao::HttpServer::requestReady signal.

Reimplement this function to alter the server's behavior when a "Expect: 100-continue" request is received.

Note
Don't delete the request or the response object, they will be deleted when the connection closes. If you need delete them before, just close the connection or call the QObject::deleteLater.
Since
1.0
void Tufao::HttpServer::close ( )
slot

Closes the server.

The server will no longer listen for incoming connections.

static UpgradeHandler Tufao::HttpServer::defaultUpgradeHandler ( )
static

Returns the default http upgrade request's handler.

The default handler closes the connection.

Since
1.0
void Tufao::HttpServer::handleConnection ( QAbstractSocket *  connection)
protected

Call this function will make Tufao::HttpServer handle the connection connection.

The Tufao::HttpServer object will take ownership of the connection object and delete it when appropriate.

virtual void Tufao::HttpServer::incomingConnection ( qintptr  socketDescriptor)
protectedvirtual

This virtual function is called by HttpServer when a new connection is available.

The base implementation creates a QTcpSocket, sets the socket descriptor and call Tufao::HttpServer::handleConnection.

Reimplement this function to alter the server's behavior when a connection is available.

bool Tufao::HttpServer::listen ( const QHostAddress &  address = QHostAddress::Any,
quint16  port = 0 
)

Tells the server to listen for incoming connections on address address and port port.

If port is 0, a port is chosen automatically. The default registered port to HTTP server is 80.

If address is QHostAddress::Any, the server will listen on all network interfaces.

Returns
true on success
See also
Tufao::HttpServer::isListening Tufao::HttpServer::serverPort
void Tufao::HttpServer::requestReady ( Tufao::HttpServerRequest request,
Tufao::HttpServerResponse response 
)
signal

This signal is emitted each time there is request.

Note
There may be multiple requests per connection (in the case of keep-alive connections) and HttpServer reutilizes request objects, so you can't, as an example, create a map using request as key to identify sessions.
Warning
You MUST NOT delete request and response. request and response are deleted when the connection closes. Additionally, response will also be deleted when you are done with it (eg., calling Tufao::HttpServerResponse::end).
Note
If this is a POST request for a big file, you should increase the timeout for this individual request.
Parameters
requestAn instance of Tufao::HttpServerRequest
responseAn instance of Tufao::HttpServerResponse
Since
1.0
quint16 Tufao::HttpServer::serverPort ( ) const

Returns the server's port if the server is listening; otherwise returns 0.

See also
Tufao::HttpServer::listen Tufao::HttpServer::isListening
void Tufao::HttpServer::setTimeout ( int  msecs = 0)

Sets the timeout of new connections to msecs miliseconds.

If you set the timeout to 0, then timeout feature will be disabled. You should NOT disable this feature to help to protect against DoS attacks.

The default timeout is 2 minutes (120000 miliseconds).

Note
You should call this function before Tufao::HttpServer::listen.
void Tufao::HttpServer::setUpgradeHandler ( UpgradeHandler  functor)

This method sets the handler that will be called to handle http upgrade requests.

Note
The connection object associated with request parameter (Tufao::HttpServerRequest::socket) will be deleted when disconnected. If you need to delete it sooner, just call QIODevice::close or QObject::deleteLater.

If you pass an empty std::function object, this function does nothing.
See also
defaultUpgradeHandler
Since
1.0

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