Commit 584e8718c5deec5e9df4628f0bc05141f8c59c49

Authored by Josh Klontz
1 parent 5ab2b5bc

a more robust br-serve using qhttpserver

.gitmodules
... ... @@ -7,3 +7,6 @@
7 7 [submodule "openbr/janus"]
8 8 path = openbr/janus
9 9 url = https://github.com/biometrics/janus.git
  10 +[submodule "3rdparty/qhttpserver"]
  11 + path = 3rdparty/qhttpserver
  12 + url = https://github.com/nikhilm/qhttpserver.git
... ...
  1 +Subproject commit dbb705865e13d28571a67d300028e72b111e7266
... ...
app/br-serve/CMakeLists.txt
1   -include_directories(${CMAKE_CURRENT_BINARY_DIR})
2   -add_executable(br-serve br-serve.cpp ${BR_RESOURCES})
  1 +set(QHTTPSERVER_DIR "${CMAKE_SOURCE_DIR}/3rdparty/qhttpserver")
  2 +set(QHTTPSERVER_SRC ${QHTTPSERVER_DIR}/src/qhttpconnection.cpp
  3 + ${QHTTPSERVER_DIR}/src/qhttpresponse.cpp
  4 + ${QHTTPSERVER_DIR}/src/qhttprequest.cpp
  5 + ${QHTTPSERVER_DIR}/src/qhttpserver.cpp
  6 + ${QHTTPSERVER_DIR}/http-parser/http_parser.c)
  7 +include_directories(${CMAKE_SOURCE_DIR}/3rdparty/qhttpserver/src
  8 + ${CMAKE_SOURCE_DIR}/3rdparty/qhttpserver/http-parser
  9 + ${CMAKE_CURRENT_BINARY_DIR})
  10 +add_executable(br-serve br-serve.cpp ${QHTTPSERVER_SRC} ${BR_RESOURCES})
3 11 target_link_libraries(br-serve openbr ${BR_THIRDPARTY_LIBS})
4 12 qt5_use_modules(br-serve ${QT_DEPENDENCIES})
5 13 install(TARGETS br-serve RUNTIME DESTINATION bin)
... ...
app/br-serve/br-serve.cpp
... ... @@ -19,6 +19,9 @@
19 19 #include <cstdio>
20 20 #include <cstring>
21 21 #include <string>
  22 +#include <qhttpserver.h>
  23 +#include <qhttprequest.h>
  24 +#include <qhttpresponse.h>
22 25 #include <openbr/universal_template.h>
23 26  
24 27 static void help()
... ... @@ -34,53 +37,40 @@ static void help()
34 37 "* -port <int> - The port to communicate on (80 otherwise).");
35 38 }
36 39  
37   -static int port = 80;
  40 +QProcess process;
38 41  
39   -class Server : public QObject
  42 +class Handler : public QObject
40 43 {
41 44 Q_OBJECT
42   - QTcpServer *tcpServer;
43 45  
44   -public:
45   - Server()
46   - : tcpServer(new QTcpServer(this))
  46 +public slots:
  47 + void handle(QHttpRequest *request, QHttpResponse *response)
47 48 {
48   - if (!tcpServer->listen(QHostAddress::Any, port)) {
49   - qDebug() << tcpServer->errorString();
50   - exit(EXIT_FAILURE);
51   - return;
52   - }
53   -
54   - connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
55   - }
56   -
57   -private slots:
58   - void newConnection()
59   - {
60   - QByteArray block = "HTTP/1.0 200 Ok\r\n"
61   - "Content-Type: text/html; charset=\"utf-8\"\r\n"
62   - "\r\n"
63   - "<h1>Hello World!</h1>\n";
64   -
65   - QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
66   - connect(clientConnection, SIGNAL(disconnected()),
67   - clientConnection, SLOT(deleteLater()));
68   -
69   - clientConnection->write(block);
70   - clientConnection->disconnectFromHost();
  49 + (void) request;
  50 + response->setHeader("Content-Length", QString::number(11));
  51 + response->writeHead(200); // everything is OK
  52 + response->write("Hello World");
  53 + response->end();
71 54 }
72 55 };
73 56  
74 57 int main(int argc, char *argv[])
75 58 {
76 59 QCoreApplication application(argc, argv);
  60 + int port = 80;
77 61  
78 62 for (int i=1; i<argc; i++) {
79 63 if (!strcmp(argv[i], "-help")) { help(); exit(EXIT_SUCCESS); }
80   - else if (!strcmp(argv[i], "-port")) { port = atoi(argv[++i]); }
  64 + else if (!strcmp(argv[i], "-port")) port = atoi(argv[++i]);
  65 + else process.execute(argv[i]);
81 66 }
82 67  
83   - Server server;
  68 + QHttpServer server;
  69 + Handler handler;
  70 + QObject::connect(&server, SIGNAL(newRequest(QHttpRequest*, QHttpResponse*)),
  71 + &handler, SLOT(handle(QHttpRequest*, QHttpResponse*)));
  72 +
  73 + server.listen(port);
84 74 return application.exec();
85 75 }
86 76  
... ...