Starts the server; make slight changes in server code
so that it understands qt's resource system (qrc)
we can use that to manage the html related files
(would be stored in app bundle for mac osx, and in
perhaps /usr/local/share for Linux by qt)
Change-Id: I8cc9e7bd99000d287bac5e6c8bd86008924351c6
diff --git a/server/request_handler.cpp b/server/request_handler.cpp
index bd1f5d2..d1d5061 100644
--- a/server/request_handler.cpp
+++ b/server/request_handler.cpp
@@ -16,12 +16,19 @@
#include "mime_types.hpp"
#include "reply.hpp"
#include "request.hpp"
+#include <QIODevice>
+#include <QFile>
+#include <QDataStream>
+#include <QString>
+#include "logging.h"
+
+INIT_LOGGER("HttpServer")
namespace http {
namespace server {
request_handler::request_handler(const std::string& doc_root)
- : doc_root_(doc_root)
+ : doc_root_(doc_root.c_str())
{
}
@@ -59,19 +66,36 @@
}
// Open the file to send back.
- std::string full_path = doc_root_ + request_path;
- std::ifstream is(full_path.c_str(), std::ios::in | std::ios::binary);
- if (!is)
+ // The following is a hack to make the server understands Qt's
+ // resource system, so that the html resources can be managed using Qt's
+ // resource system (e.g. no need to worry about the location of html)
+ // in Mac OS, it will be inside the bundle, in Linux, perhaps somewhere
+ // in /usr/local/share
+ QString full_path = doc_root_.absolutePath() + QString(request_path.c_str());
+ QFile file(full_path);
+ if (!file.exists() || !file.open(QIODevice::ReadOnly))
{
rep = reply::stock_reply(reply::not_found);
return;
}
+ _LOG_DEBUG("Serving file: " << request_path);
// Fill out the reply to be sent to the client.
rep.status = reply::ok;
char buf[512];
- while (is.read(buf, sizeof(buf)).gcount() > 0)
- rep.content.append(buf, is.gcount());
+ QDataStream in(&file);
+ while (true)
+ {
+ int bytes = in.readRawData(buf, sizeof(buf));
+ if (bytes > 0)
+ {
+ rep.content.append(buf, bytes);
+ }
+ else
+ {
+ break;
+ }
+ }
rep.headers.resize(2);
rep.headers[0].name = "Content-Length";
rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size());
diff --git a/server/request_handler.hpp b/server/request_handler.hpp
index 661bad7..384907c 100644
--- a/server/request_handler.hpp
+++ b/server/request_handler.hpp
@@ -13,6 +13,7 @@
#include <string>
#include <boost/noncopyable.hpp>
+#include <QDir>
namespace http {
namespace server {
@@ -33,7 +34,7 @@
private:
/// The directory containing the files to be served.
- std::string doc_root_;
+ QDir doc_root_;
/// Perform URL-decoding on a string. Returns false if the encoding was
/// invalid.
diff --git a/server/server.cpp b/server/server.cpp
index 5fcc5d9..fa20be7 100644
--- a/server/server.cpp
+++ b/server/server.cpp
@@ -11,7 +11,9 @@
#include "server.hpp"
#include <boost/bind.hpp>
#include <signal.h>
+#include "logging.h"
+INIT_LOGGER ("HttpServer");
namespace http {
namespace server {
@@ -44,6 +46,8 @@
acceptor_.listen();
start_accept();
+
+ _LOG_DEBUG("Listen on [" << address << ": " << port << "] with doc_root = " << doc_root);
}
void server::run()