diff options
author | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2013-02-28 14:00:53 (GMT) |
---|---|---|
committer | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2013-02-28 14:00:53 (GMT) |
commit | c1ccbef7a59df33e6ff0c9a4609caab7e668ba77 (patch) | |
tree | 23d94e90f346db5d4ecff7afb6a0283fc5fe8eba /src/uscxml/server | |
parent | 49c3c43d18c9cce6de305aae77cc8bd839506129 (diff) | |
download | uscxml-c1ccbef7a59df33e6ff0c9a4609caab7e668ba77.zip uscxml-c1ccbef7a59df33e6ff0c9a4609caab7e668ba77.tar.gz uscxml-c1ccbef7a59df33e6ff0c9a4609caab7e668ba77.tar.bz2 |
Prepared everything for FE-Design demo
Diffstat (limited to 'src/uscxml/server')
-rw-r--r-- | src/uscxml/server/HTTPServer.cpp | 36 | ||||
-rw-r--r-- | src/uscxml/server/HTTPServer.h | 4 |
2 files changed, 36 insertions, 4 deletions
diff --git a/src/uscxml/server/HTTPServer.cpp b/src/uscxml/server/HTTPServer.cpp index d8a6474..a7957ea 100644 --- a/src/uscxml/server/HTTPServer.cpp +++ b/src/uscxml/server/HTTPServer.cpp @@ -1,3 +1,5 @@ +#include "uscxml/config.h" + #ifdef _WIN32 #include <winsock2.h> #include <windows.h> @@ -31,9 +33,9 @@ #endif namespace uscxml { - + HTTPServer::HTTPServer(unsigned short port) { - _port = port; + _port = port; _base = event_base_new(); _http = evhttp_new(_base); _handle = NULL; @@ -51,10 +53,30 @@ HTTPServer::~HTTPServer() { HTTPServer* HTTPServer::_instance = NULL; tthread::recursive_mutex HTTPServer::_instanceMutex; +std::map<std::string, std::string> HTTPServer::mimeTypes; HTTPServer* HTTPServer::getInstance(int port) { tthread::lock_guard<tthread::recursive_mutex> lock(_instanceMutex); if (_instance == NULL) { + + // this is but a tiny list, supply a content-type <header> yourself + mimeTypes["txt"] = "text/plain"; + mimeTypes["c"] = "text/plain"; + mimeTypes["h"] = "text/plain"; + mimeTypes["html"] = "text/html"; + mimeTypes["htm"] = "text/htm"; + mimeTypes["css"] = "text/css"; + mimeTypes["gif"] = "image/gif"; + mimeTypes["jpg"] = "image/jpeg"; + mimeTypes["jpeg"] = "image/jpeg"; + mimeTypes["mpg"] = "video/mpeg"; + mimeTypes["mov"] = "video/quicktime"; + mimeTypes["png"] = "image/png"; + mimeTypes["pdf"] = "application/pdf"; + mimeTypes["ps"] = "application/postscript"; + mimeTypes["tif"] = "image/tiff"; + mimeTypes["tiff"] = "image/tiff"; + #ifndef _WIN32 evthread_use_pthreads(); #else @@ -66,6 +88,12 @@ HTTPServer* HTTPServer::getInstance(int port) { return _instance; } +std::string HTTPServer::mimeTypeForExtension(const std::string& ext) { + if (mimeTypes.find(ext) != mimeTypes.end()) + return mimeTypes[ext]; + return ""; +} + void HTTPServer::httpRecvReqCallback(struct evhttp_request *req, void *callbackData) { // std::cout << (uintptr_t)req << ": Replying" << std::endl; // evhttp_send_reply(req, 200, NULL, NULL); @@ -194,6 +222,10 @@ void HTTPServer::processByMatchingServlet(const Request& request) { void HTTPServer::reply(const Reply& reply) { struct evbuffer *evb = evbuffer_new(); + if (reply.content.size() > 0 && reply.headers.find("Content-Type") == reply.headers.end()) { + LOG(INFO) << "Sending content without Content-Type header"; + } + std::map<std::string, std::string>::const_iterator headerIter = reply.headers.begin(); while(headerIter != reply.headers.end()) { evhttp_add_header(evhttp_request_get_output_headers(reply.curlReq), headerIter->first.c_str(), headerIter->second.c_str()); diff --git a/src/uscxml/server/HTTPServer.h b/src/uscxml/server/HTTPServer.h index 1ec28c7..9bab1a1 100644 --- a/src/uscxml/server/HTTPServer.h +++ b/src/uscxml/server/HTTPServer.h @@ -41,7 +41,7 @@ public: static std::string getBaseURL(); static void reply(const Reply& reply); - + static std::string mimeTypeForExtension(const std::string& ext); static bool registerServlet(const std::string& path, HTTPServlet* servlet); ///< Register a servlet, returns false if path is already taken static void unregisterServlet(HTTPServlet* servlet); @@ -58,7 +58,7 @@ private: static void httpRecvReqCallback(struct evhttp_request *req, void *callbackData); void processByMatchingServlet(const Request& request); - + static std::map<std::string, std::string> mimeTypes; std::map<std::string, HTTPServlet*> _servlets; typedef std::map<std::string, HTTPServlet*>::iterator servlet_iter_t; |