From 584e8718c5deec5e9df4628f0bc05141f8c59c49 Mon Sep 17 00:00:00 2001 From: Josh Klontz Date: Thu, 12 Jun 2014 12:12:04 -0400 Subject: [PATCH] a more robust br-serve using qhttpserver --- .gitmodules | 3 +++ 3rdparty/qhttpserver | 1 + app/br-serve/CMakeLists.txt | 12 ++++++++++-- app/br-serve/br-serve.cpp | 52 +++++++++++++++++++++------------------------------- 4 files changed, 35 insertions(+), 33 deletions(-) create mode 160000 3rdparty/qhttpserver diff --git a/.gitmodules b/.gitmodules index d835b01..ab28352 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "openbr/janus"] path = openbr/janus url = https://github.com/biometrics/janus.git +[submodule "3rdparty/qhttpserver"] + path = 3rdparty/qhttpserver + url = https://github.com/nikhilm/qhttpserver.git diff --git a/3rdparty/qhttpserver b/3rdparty/qhttpserver new file mode 160000 index 0000000..dbb7058 --- /dev/null +++ b/3rdparty/qhttpserver @@ -0,0 +1 @@ +Subproject commit dbb705865e13d28571a67d300028e72b111e7266 diff --git a/app/br-serve/CMakeLists.txt b/app/br-serve/CMakeLists.txt index 471463d..8064b25 100644 --- a/app/br-serve/CMakeLists.txt +++ b/app/br-serve/CMakeLists.txt @@ -1,5 +1,13 @@ -include_directories(${CMAKE_CURRENT_BINARY_DIR}) -add_executable(br-serve br-serve.cpp ${BR_RESOURCES}) +set(QHTTPSERVER_DIR "${CMAKE_SOURCE_DIR}/3rdparty/qhttpserver") +set(QHTTPSERVER_SRC ${QHTTPSERVER_DIR}/src/qhttpconnection.cpp + ${QHTTPSERVER_DIR}/src/qhttpresponse.cpp + ${QHTTPSERVER_DIR}/src/qhttprequest.cpp + ${QHTTPSERVER_DIR}/src/qhttpserver.cpp + ${QHTTPSERVER_DIR}/http-parser/http_parser.c) +include_directories(${CMAKE_SOURCE_DIR}/3rdparty/qhttpserver/src + ${CMAKE_SOURCE_DIR}/3rdparty/qhttpserver/http-parser + ${CMAKE_CURRENT_BINARY_DIR}) +add_executable(br-serve br-serve.cpp ${QHTTPSERVER_SRC} ${BR_RESOURCES}) target_link_libraries(br-serve openbr ${BR_THIRDPARTY_LIBS}) qt5_use_modules(br-serve ${QT_DEPENDENCIES}) install(TARGETS br-serve RUNTIME DESTINATION bin) diff --git a/app/br-serve/br-serve.cpp b/app/br-serve/br-serve.cpp index d82e263..2f25d87 100644 --- a/app/br-serve/br-serve.cpp +++ b/app/br-serve/br-serve.cpp @@ -19,6 +19,9 @@ #include #include #include +#include +#include +#include #include static void help() @@ -34,53 +37,40 @@ static void help() "* -port - The port to communicate on (80 otherwise)."); } -static int port = 80; +QProcess process; -class Server : public QObject +class Handler : public QObject { Q_OBJECT - QTcpServer *tcpServer; -public: - Server() - : tcpServer(new QTcpServer(this)) +public slots: + void handle(QHttpRequest *request, QHttpResponse *response) { - if (!tcpServer->listen(QHostAddress::Any, port)) { - qDebug() << tcpServer->errorString(); - exit(EXIT_FAILURE); - return; - } - - connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection())); - } - -private slots: - void newConnection() - { - QByteArray block = "HTTP/1.0 200 Ok\r\n" - "Content-Type: text/html; charset=\"utf-8\"\r\n" - "\r\n" - "

Hello World!

\n"; - - QTcpSocket *clientConnection = tcpServer->nextPendingConnection(); - connect(clientConnection, SIGNAL(disconnected()), - clientConnection, SLOT(deleteLater())); - - clientConnection->write(block); - clientConnection->disconnectFromHost(); + (void) request; + response->setHeader("Content-Length", QString::number(11)); + response->writeHead(200); // everything is OK + response->write("Hello World"); + response->end(); } }; int main(int argc, char *argv[]) { QCoreApplication application(argc, argv); + int port = 80; for (int i=1; i