QHttpEngine  1.0.0
Simple and secure HTTP server for Qt applications
Public Member Functions | Protected Member Functions | Friends | List of all members
QHttpEngine::Handler Class Reference

Base class for HTTP handlers. More...

#include <qhttpengine/handler.h>

Inheritance diagram for QHttpEngine::Handler:
QHttpEngine::FilesystemHandler QHttpEngine::ProxyHandler QHttpEngine::QObjectHandler

Public Member Functions

 Handler (QObject *parent=0)
 Base constructor for a handler.
 
void addMiddleware (Middleware *middleware)
 Add middleware to the handler.
 
void addRedirect (const QRegExp &pattern, const QString &path)
 Add a redirect for a specific pattern. More...
 
void addSubHandler (const QRegExp &pattern, Handler *handler)
 Add a handler for a specific pattern. More...
 
void route (Socket *socket, const QString &path)
 Route an incoming request.
 

Protected Member Functions

virtual void process (Socket *socket, const QString &path)
 Process a request. More...
 

Friends

class HandlerPrivate
 

Detailed Description

When a request is received by a Server, it invokes the route() method of the root handler which is used to determine what happens to the request. All HTTP handlers derive from this class and should override the protected process() method in order to process the request. Each handler also maintains a list of redirects and sub-handlers which are used in place of invoking process() when one of the patterns match.

To add a redirect, use the addRedirect() method. The first parameter is a QRegExp pattern that the request path will be tested against. If it matches, an HTTP 302 redirect will be written to the socket and the request closed. For example, to have the root path "/" redirect to "/index.html":

handler.addRedirect(QRegExp("^$"), "/index.html");

To add a sub-handler, use the addSubHandler() method. Again, the first parameter is a QRegExp pattern. If the pattern matches, the portion of the path that matched the pattern is removed from the path and it is passed to the sub-handler's route() method. For example, to have a sub-handler invoked when the path begins with "/api/":

QHttpEngine::Handler handler, subHandler;
handler.addSubHandler(QRegExp("^api/"), &subHandler);

If the request doesn't match any redirect or sub-handler patterns, it is passed along to the process() method, which is expected to either process the request or write an error to the socket. The default implementation of process() simply returns an HTTP 404 error.

Member Function Documentation

§ addRedirect()

void QHttpEngine::Handler::addRedirect ( const QRegExp &  pattern,
const QString &  path 
)

The pattern and path will be added to an internal list that will be used when the route() method is invoked to determine whether the request matches any patterns. The order of the list is preserved.

The destination path may use "%1", "%2", etc. to refer to captured parts of the pattern. The client will receive an HTTP 302 redirect.

§ addSubHandler()

void QHttpEngine::Handler::addSubHandler ( const QRegExp &  pattern,
Handler handler 
)

The pattern and handler will be added to an internal list that will be used when the route() method is invoked to determine whether the request matches any patterns. The order of the list is preserved.

§ process()

virtual void QHttpEngine::Handler::process ( Socket socket,
const QString &  path 
)
protectedvirtual

This method should process the request either by fulfilling it, sending a redirect with Socket::writeRedirect(), or writing an error to the socket using Socket::writeError().

Reimplemented in QHttpEngine::QObjectHandler, QHttpEngine::FilesystemHandler, and QHttpEngine::ProxyHandler.


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