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

You can use this class to serve static files under Tufão. More...

+ Inheritance diagram for Tufao::HttpFileServer:
+ Collaboration diagram for Tufao::HttpFileServer:

Public Slots

bool handleRequest (Tufao::HttpServerRequest &request, Tufao::HttpServerResponse &response) override
 It searchs for the file requested in the root dir and respond to the request, if the file is found. More...
 

Public Member Functions

 HttpFileServer (const QString &dir, QObject *parent=0)
 Constructs a HttpFileServer object. More...
 
 ~HttpFileServer ()
 Destroys the object.
 
void setDir (const QString &dir)
 Set the root dir. More...
 
QString dir () const
 Return the root dir containing the files to be served.
 
bool canHandleRequest (const HttpServerRequest &request)
 Returns true iff HttpFileServer::handleRequest will return true. More...
 
- Public Member Functions inherited from Tufao::AbstractHttpServerRequestHandler
 operator std::function< bool (HttpServerRequest &, HttpServerResponse &)>()
 Implicit conversion operator to std::function functor object. More...
 
virtual bool handleRequest (Tufao::HttpServerRequest &request, Tufao::HttpServerResponse &response)=0
 Handles the request using the response object. More...
 

Static Public Member Functions

static void serveFile (const QString &fileName, HttpServerRequest &request, HttpServerResponse &response)
 Analyze the request and serve the file pointed by filename. More...
 
static bool serveFile (const QString &fileName, HttpServerResponse &response, HttpResponseStatus statusCode)
 This member function doesn't serve any file, just set the response body to the contents in the file pointed by filename. More...
 
static qint64 bufferSize ()
 Return the buffer size used. More...
 
static void setBufferSize (qint64 size)
 Set the buffer size.
 
static bool canHandleRequest (const HttpServerRequest &request, const QString &root)
 Returns true iff HttpFileServer::handleRequest will return true. More...
 
static std::function< bool(HttpServerRequest &, HttpServerResponse &)> handler (const QString &rootDir)
 Returns a handler that don't depends on another object. More...
 

Detailed Description

You can use this class to serve static files under Tufão.

It provides a robust HTTP file server, supporting conditional and byte-range requests.

The two main approaches are:

The algorithm used to serve files will handle the following set of headers:

  • If-Modified-Since
  • If-Unmodified-Since
  • If-Range
  • Range
  • Content-Type through QMimeDatabase (Since version 1.0)

It won't handle:

  • ETag (If-Match and If-None-Match headers)
  • Cache-Control response header: Useful for set cache max age
  • Content-Disposition response header
  • Content-MD5 response header
Since
0.3

Constructor & Destructor Documentation

Tufao::HttpFileServer::HttpFileServer ( const QString &  dir,
QObject *  parent = 0 
)
explicit

Constructs a HttpFileServer object.

parent is passed to the QObject constructor.

dir is used as root dir to serve files.

Member Function Documentation

static qint64 Tufao::HttpFileServer::bufferSize ( )
static

Return the buffer size used.

When serving files, HttpFileServer allocates some bytes of the file in the memory before sending it to the network. The maximum space allocated is the buffer size. This method returns what number is this.

Note
The buffer size is global to all HttpFileServer objects.
See also
setBufferSize
bool Tufao::HttpFileServer::canHandleRequest ( const HttpServerRequest request)

Returns true iff HttpFileServer::handleRequest will return true.

Since
1.3
static bool Tufao::HttpFileServer::canHandleRequest ( const HttpServerRequest request,
const QString &  root 
)
static

Returns true iff HttpFileServer::handleRequest will return true.

Since
1.3
static std::function<bool(HttpServerRequest&, HttpServerResponse&)> Tufao::HttpFileServer::handler ( const QString &  rootDir)
static

Returns a handler that don't depends on another object.

The purpose of this alternative handler is to free you of the worry of maintain the HttpFileServer's object (lifetime) while the functor object is being used.

Parameters
rootDirThe root dir to serve files.
Since
1.0
bool Tufao::HttpFileServer::handleRequest ( Tufao::HttpServerRequest request,
Tufao::HttpServerResponse response 
)
overrideslot

It searchs for the file requested in the root dir and respond to the request, if the file is found.

Note
This method won't let requests access files outside the root dir folder and should be prefered over self-made implementations, as its safer.
Since
1.0
static void Tufao::HttpFileServer::serveFile ( const QString &  fileName,
HttpServerRequest request,
HttpServerResponse response 
)
static

Analyze the request and serve the file pointed by filename.

Since
1.0
static bool Tufao::HttpFileServer::serveFile ( const QString &  fileName,
HttpServerResponse response,
HttpResponseStatus  statusCode 
)
static

This member function doesn't serve any file, just set the response body to the contents in the file pointed by filename.

It's useful in some scenarios, like serving 404-pages.

Since
1.0
void Tufao::HttpFileServer::setDir ( const QString &  dir)

Set the root dir.

The root dir is the dir containing the files to be served by the HttpFileServer object.


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