[dss-commits] r8900 - dss/trunk/core

dss-commits at forum.digitalstrom.org dss-commits at forum.digitalstrom.org
Mon Nov 9 16:50:30 CET 2009


Author: pstaehlin
Date: 2009-11-09 16:50:30 +0100 (Mon, 09 Nov 2009)
New Revision: 8900

Modified:
   dss/trunk/core/webserver.cpp
   dss/trunk/core/webserver.h
Log:
Download file interface implemented.

Closes #162, #165, #166

Example configuration to download the config.xml from:
 http://dss:8080/download/config.xml

<property name="subsystems/WebServer/files" type="string">
  <property name="config.xml" type="string">
    <value>data/config.xml</value>
  </property>
</property>

Modified: dss/trunk/core/webserver.cpp
===================================================================
--- dss/trunk/core/webserver.cpp	2009-11-09 15:50:28 UTC (rev 8899)
+++ dss/trunk/core/webserver.cpp	2009-11-09 15:50:30 UTC (rev 8900)
@@ -426,6 +426,7 @@
 
     mg_set_uri_callback(m_mgContext, "/browse/*", &httpBrowseProperties, NULL);
     mg_set_uri_callback(m_mgContext, "/json/*", &jsonHandler, NULL);
+    mg_set_uri_callback(m_mgContext, "/download/*", &downloadHandler, NULL);
 
     loadPlugins();
 
@@ -1361,13 +1362,12 @@
 
   string WebServer::handleSystemCall(const std::string& _method, HashMapConstStringString& _parameter, struct mg_connection* _connection, bool& _handled, Session* _session) {
     _handled = true;
-    string result;
     if(endsWith(_method, "/version")) {
       return ResultToJSON(true, DSS::getInstance()->versionString());
     } else {
       _handled = false;
+      return std::string();
     }
-    return result;
   } // handleEventCall
 
   string WebServer::handleStructureCall(const std::string& _method,
@@ -1919,6 +1919,39 @@
     mg_write(_connection, result.c_str(), result.length());
   } // jsonHandler
 
+  void WebServer::downloadHandler(struct mg_connection* _connection,
+                                  const struct mg_request_info* _info, 
+                                  void* _userData) {
+    const std::string kURLID = "/download/";
+    std::string uri = _info->uri;
+
+    std::string givenFileName = uri.substr(uri.find(kURLID) + kURLID.size());
+
+    WebServer& self = DSS::getInstance()->getWebServer();
+    self.log("Processing call to download/" + givenFileName);
+
+    // TODO: make the files-node readonly as this might pose a security threat
+    //       (you could download any file on the disk if you add it as a subnode
+    //        of files)
+    PropertyNodePtr filesNode = self.getDSS().getPropertySystem().getProperty(
+                                    self.getConfigPropertyBasePath() + "files"
+                                );
+    std::string fileName;
+    if(filesNode != NULL) {
+      PropertyNodePtr fileNode = filesNode->getProperty(givenFileName);
+      if(fileNode != NULL) {
+        fileName = fileNode->getStringValue();
+      }
+    }
+    self.log("Using local file: " + fileName);
+    struct mgstat st;
+    if(mg_stat(fileName.c_str(), &st) != 0) {
+      self.log("Not found");
+      memset(&st, '\0', sizeof(st));
+    }
+    mg_send_file(_connection, fileName.c_str(), &st);
+  } // downloadHandler
+
   void WebServer::httpBrowseProperties(struct mg_connection* _connection,
                                        const struct mg_request_info* _info,
                                        void* _userData) {

Modified: dss/trunk/core/webserver.h
===================================================================
--- dss/trunk/core/webserver.h	2009-11-09 15:50:28 UTC (rev 8899)
+++ dss/trunk/core/webserver.h	2009-11-09 15:50:30 UTC (rev 8900)
@@ -82,6 +82,9 @@
     static void jsonHandler(struct mg_connection* _connection,
                             const struct mg_request_info* _info, 
                             void* _userData);
+    static void downloadHandler(struct mg_connection* _connection,
+                            const struct mg_request_info* _info, 
+                            void* _userData);
 
       static void emitHTTPHeader(int _code, struct mg_connection* _connection, const std::string& _contentType = "text/html");
   protected:



More information about the dss-commits mailing list