Web API: send API documentation if path is invalid

This commit is contained in:
f4exb 2017-11-20 18:38:26 +01:00
parent 7fa26835bd
commit 1b5f944302
9 changed files with 9091 additions and 5 deletions

View File

@ -8,6 +8,8 @@
#ifndef HTTPSERVER_HTTPDOCROOTSETTINGS_H_
#define HTTPSERVER_HTTPDOCROOTSETTINGS_H_
#include <QString>
namespace qtwebapp {
struct HttpDocrootSettings

View File

@ -61,7 +61,13 @@ StaticFileController::StaticFileController(const HttpDocrootSettings& settings,
void StaticFileController::service(HttpRequest& request, HttpResponse& response)
{
QByteArray path=request.getPath();
QByteArray path = request.getPath();
service(path, response);
}
void StaticFileController::service(QByteArray& path, HttpResponse& response)
{
//QByteArray path=request.getPath();
// Check if we have the file in cache
qint64 now=QDateTime::currentMSecsSinceEpoch();
mutex.lock();
@ -114,7 +120,8 @@ void StaticFileController::service(HttpRequest& request, HttpResponse& response)
entry->created=now;
entry->filename=path;
mutex.lock();
cache.insert(request.getPath(),entry,entry->document.size());
//cache.insert(request.getPath(),entry,entry->document.size());
cache.insert(path,entry,entry->document.size());
mutex.unlock();
}
else

View File

@ -55,9 +55,12 @@ public:
/** Constructor with settings structure */
StaticFileController(const HttpDocrootSettings& settings, QObject* parent = NULL);
/** Generates the response */
/** Generates the response from HTTP request */
void service(HttpRequest& request, HttpResponse& response);
/** Generates the response directly from the path */
void service(QByteArray& path, HttpResponse& response);
private:
/** Encoding of text files */

View File

@ -234,9 +234,16 @@ endif (BUILD_DEBIAN)
add_definitions(${QT_DEFINITIONS})
add_definitions(-DQT_SHARED)
set(sdrbase_RESOURCES
resources/res.qrc
)
qt5_add_resources(sdrbase_RESOURCES_RCC ${sdrbase_RESOURCES})
add_library(sdrbase SHARED
${sdrbase_SOURCES}
${sdrbase_HEADERS_MOC}
${sdrbase_RESOURCES_RCC}
)
include_directories(

9045
sdrbase/resources/index.html Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>index.html</file>
</qresource>
</RCC>

View File

@ -200,5 +200,7 @@ HEADERS += audio/audiodeviceinfo.h\
LIBS += -L../httpserver/$${build_subdir} -lhttpserver
LIBS += -L../swagger/$${build_subdir} -lswagger
RESOURCES = resources/res.qrc
CONFIG(ANDROID):CONFIG += mobility
CONFIG(ANDROID):MOBILITY =

View File

@ -16,6 +16,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include "httpdocrootsettings.h"
#include "webapirequestmapper.h"
#include "SWGInstanceSummaryResponse.h"
#include "SWGErrorResponse.h"
@ -23,7 +24,16 @@
WebAPIRequestMapper::WebAPIRequestMapper(QObject* parent) :
HttpRequestHandler(parent),
m_adapter(0)
{ }
{
qtwebapp::HttpDocrootSettings docrootSettings;
docrootSettings.path = ":/";
m_staticFileController = new qtwebapp::StaticFileController(docrootSettings, parent);
}
WebAPIRequestMapper::~WebAPIRequestMapper()
{
delete m_staticFileController;
}
void WebAPIRequestMapper::service(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
@ -61,7 +71,9 @@ void WebAPIRequestMapper::service(qtwebapp::HttpRequest& request, qtwebapp::Http
}
else
{
response.setStatus(404,"Not found");
QByteArray path = "/";
m_staticFileController->service(path, response);
//response.setStatus(404,"Not found");
}
}
}

View File

@ -22,17 +22,20 @@
#include "httprequesthandler.h"
#include "httprequest.h"
#include "httpresponse.h"
#include "staticfilecontroller.h"
#include "webapiadapterinterface.h"
class WebAPIRequestMapper : public qtwebapp::HttpRequestHandler {
Q_OBJECT
public:
WebAPIRequestMapper(QObject* parent=0);
~WebAPIRequestMapper();
void service(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response);
void setAdapter(WebAPIAdapterInterface *adapter) { m_adapter = adapter; }
private:
WebAPIAdapterInterface *m_adapter;
qtwebapp::StaticFileController *m_staticFileController;
};
#endif /* SDRBASE_WEBAPI_WEBAPIREQUESTMAPPER_H_ */