Skip to content

Commit

Permalink
Adds function HttpFileServer::canHandleRequest that returns true iff …
Browse files Browse the repository at this point in the history
…HttpFileServer::handleRequest will return true.
  • Loading branch information
saemy authored and vinipsmaker committed Mar 22, 2015
1 parent 3c1566a commit bfb7c19
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/httpfileserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,24 @@ bool HttpFileServer::handleRequest(HttpServerRequest &request,
return handleRequest(request, response, priv->rootDir);
}

inline bool HttpFileServer::handleRequest(HttpServerRequest &request,
HttpServerResponse &response,
inline static QString filenameFromRequest(const HttpServerRequest &request,
const QString &rootDir)
{
QString fileName{
return QString{
QDir::cleanPath(rootDir
+ QDir::toNativeSeparators(request.url()
.path(QUrl::FullyDecoded)))};
}

bool HttpFileServer::canHandleRequest(const HttpServerRequest &request)
{
return canHandleRequest(request, priv->rootDir);
}

bool HttpFileServer::canHandleRequest(const HttpServerRequest &request,
const QString &rootDir)
{
QString fileName = filenameFromRequest(request, rootDir);
if (!fileName.startsWith(rootDir + "/"))
return false;

Expand All @@ -369,6 +379,17 @@ inline bool HttpFileServer::handleRequest(HttpServerRequest &request,
return false;
}

return true;
}

inline bool HttpFileServer::handleRequest(HttpServerRequest &request,
HttpServerResponse &response,
const QString &rootDir)
{
if (!canHandleRequest(request, rootDir))
return false;

QString fileName = filenameFromRequest(request, rootDir);
serveFile(fileName, request, response);
return true;
}
Expand Down
11 changes: 11 additions & 0 deletions src/httpfileserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ class TUFAO_EXPORT HttpFileServer: public QObject,
*/
static void setBufferSize(qint64 size);

/*!
Returns true iff HttpFileServer::handleRequest will return true.
*/
bool canHandleRequest(const HttpServerRequest &request);

/*!
Returns true iff HttpFileServer::handleRequest will return true.
*/
static bool canHandleRequest(const HttpServerRequest &request,
const QString &root);

/*!
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
Expand Down

0 comments on commit bfb7c19

Please sign in to comment.