From 48a579ae496a9a969ea0011031f25e0a51c1fc7d Mon Sep 17 00:00:00 2001
From: ZappoMan <bradh@konamoxt.com>
Date: Tue, 17 Sep 2013 12:18:46 -0700
Subject: [PATCH] first cut at domain server config support

---
 domain-server/src/main.cpp | 41 +++++++++++++++++++++++++++++++++++---
 1 file changed, 38 insertions(+), 3 deletions(-)

diff --git a/domain-server/src/main.cpp b/domain-server/src/main.cpp
index 044ea0b369..843044b83d 100644
--- a/domain-server/src/main.cpp
+++ b/domain-server/src/main.cpp
@@ -27,7 +27,10 @@
 #include <stdlib.h>
 
 #include <QtCore/QCoreApplication>
+#include <QtCore/QMap>
 #include <QtCore/QMutex>
+#include <QtCore/QString>
+#include <QtCore/QStringList>
 
 #include <civetweb.h>
 
@@ -45,6 +48,7 @@ const int NODE_COUNT_STAT_INTERVAL_MSECS = 5000;
 
 QMutex assignmentQueueMutex;
 std::deque<Assignment*> assignmentQueue;
+QMap<QString, QString> configMap;
 
 unsigned char* addNodeToBroadcastPacket(unsigned char* currentPosition, Node* nodeToAdd) {
     *currentPosition++ = nodeToAdd->getType();
@@ -58,7 +62,7 @@ unsigned char* addNodeToBroadcastPacket(unsigned char* currentPosition, Node* no
 }
 
 static int mongooseRequestHandler(struct mg_connection *conn) {
-    const struct mg_request_info *ri = mg_get_request_info(conn);
+    const struct mg_request_info* ri = mg_get_request_info(conn);
     
     if (strcmp(ri->uri, "/assignment") == 0 && strcmp(ri->request_method, "POST") == 0) {
         // return a 200
@@ -66,6 +70,37 @@ static int mongooseRequestHandler(struct mg_connection *conn) {
         // upload the file
         mg_upload(conn, "/tmp");
         
+        return 1;
+    } else if (strncmp(ri->uri, "/config", strlen("/config")) == 0 && strcmp(ri->request_method, "GET") == 0) {
+    
+        // Let's split up the URL into it's pieces so we can service it
+        QString uri(ri->uri);
+        QString delimiterPattern("/");
+        QStringList uriParts = uri.split(delimiterPattern);
+
+        QString configGuid = uriParts[2];
+
+        // first part should be empty        
+        // second part should be "config"
+        // third part should be a guid, and therefore should not be empty
+        if (!uriParts[0].isEmpty() || uriParts[1] != "config" || configGuid.isEmpty()) {
+            mg_printf(conn, "%s", "HTTP/1.0 400 Bad Request\r\nContent-Type: text/plain\r\n\r\nInvalid parameters.");
+            return 1;
+        }
+        
+        // assuming the request format was valid, look up the guid in our configs
+        if (!::configMap.contains(configGuid)) {
+            mg_printf(conn, "%s", "HTTP/1.0 404 Not Found\r\nContent-Type: text/plain\r\n\r\nInvalid config GUID.");
+            return 1;
+        }
+        
+        const char * configPayload = configMap[configGuid].toLocal8Bit().data();
+
+        // return a 200
+        mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n");
+        // return the configuration "payload"
+        mg_printf(conn, "%s", configPayload);
+        
         return 1;
     } else {
         // have mongoose process this request from the document_root
@@ -157,13 +192,13 @@ int main(int argc, const char* argv[]) {
     localSocket.sin_addr.s_addr = serverLocalAddress;
     
     // setup the mongoose web server
-    struct mg_context *ctx;
+    struct mg_context* ctx;
     struct mg_callbacks callbacks = {};
     
     QString documentRoot = QString("%1/resources/web").arg(QCoreApplication::applicationDirPath());
     
     // list of options. Last element must be NULL.
-    const char *options[] = {"listening_ports", "8080",
+    const char* options[] = {"listening_ports", "8080",
                              "document_root", documentRoot.toStdString().c_str(), NULL};
     
     callbacks.begin_request = mongooseRequestHandler;