[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 13/16] app subdir


MIME-Version: 1.0
Content-Transfer-Encoding: 8bit

From: arf20 <aruizfernandez05@xxxxxxxxx>

---
 config.c           | 23 +++++++++++++++++------
 config.h           |  2 +-
 main.c             | 15 ++++++++++++---
 search.cfg         | 20 --------------------
 search.cfg.example | 14 ++++++++++++++
 5 files changed, 44 insertions(+), 30 deletions(-)
 delete mode 100644 search.cfg

diff --git a/config.c b/config.c
index fdb6380..8a0883c 100644
--- a/config.c
+++ b/config.c
@@ -28,7 +28,8 @@
 #include <errno.h>
 
 unsigned short port = 0;
-char *tmpl_path = NULL, *root = NULL, *subdir = NULL;
+char *tmpl_path = NULL, *root = NULL, *app_subdir = NULL,
+    *result_subdir = NULL;
 int magic_enable = 0, period = 86400;
 
 int
@@ -75,10 +76,15 @@ config_load(const char *conf_path)
             root = strdup(value);
             printf("\troot: %s\n", root);
         }
-        else if (strcmp(line, "subdir") == 0) {
+        else if (strcmp(line, "app_subdir") == 0) {
             value[strlen(value) - 1] = '\0';
-            subdir = strdup(value);
-            printf("\tsubdir: %s\n", subdir);
+            app_subdir = strdup(value);
+            printf("\tapp_subdir: %s\n", app_subdir);
+        }
+        else if (strcmp(line, "result_subdir") == 0) {
+            value[strlen(value) - 1] = '\0';
+            result_subdir = strdup(value);
+            printf("\tresult_subdir: %s\n", result_subdir);
         }
         else if (strcmp(line, "magic") == 0) {
             value[strlen(value) - 1] = '\0';
@@ -113,8 +119,13 @@ config_load(const char *conf_path)
         return -1;
     }
 
-    if (!subdir) {
-        fprintf(stderr, "[config] E: no link subdirectory given\n");
+    if (!app_subdir) {
+        fprintf(stderr, "[config] E: no application subdirectory given\n");
+        return -1;
+    }
+
+    if (!result_subdir) {
+        fprintf(stderr, "[config] E: no result link subdirectory given\n");
         return -1;
     }
 
diff --git a/config.h b/config.h
index f9364b8..a59c8bd 100644
--- a/config.h
+++ b/config.h
@@ -31,7 +31,7 @@
 
 /* config */
 extern unsigned short port;
-extern char *tmpl_path, *root, *subdir;
+extern char *tmpl_path, *root, *app_subdir, *result_subdir;
 extern int magic_enable, period;
 
 
diff --git a/main.c b/main.c
index 7cdd4fd..164f9dd 100644
--- a/main.c
+++ b/main.c
@@ -129,7 +129,7 @@ generate_results_html(results_t *results)
         struct tm *tm_mtim = gmtime(&data->stat.st_mtime);
         strftime(timebuf, 256, "%b %d %Y", tm_mtim);
 
-        snprintf(urlbuf, 4096, "%s%s", subdir, data->path);
+        snprintf(urlbuf, 4096, "%s%s", result_subdir, data->path);
 
         pos += snprintf(pos, 1024,
             result_html_template,
@@ -143,6 +143,13 @@ generate_results_html(results_t *results)
     return buff;
 }
 
+const char *
+subdir_endpoint(const char *endpoint) {
+    static char subdir_endpoint[256];
+    snprintf(subdir_endpoint, 256, "%s%s", app_subdir, endpoint);
+    return subdir_endpoint;
+}
+
 enum MHD_Result answer_to_connection(
     void *cls, struct MHD_Connection *connection,
     const char *url,
@@ -169,7 +176,7 @@ enum MHD_Result answer_to_connection(
     struct MHD_Response *response;
     int ret;
 
-    if (strcmp(method, "GET") == 0 && strcmp(url, "/") == 0) {
+    if (strcmp(method, "GET") == 0 && strcmp(url, subdir_endpoint("/")) == 0) {
         snprintf(buff, BUFF_SIZE, index_format_template, "", "", "", "", "", "",
             "");
 
@@ -180,7 +187,9 @@ enum MHD_Result answer_to_connection(
         ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
         MHD_destroy_response(response);
     }
-    else if (strcmp(method, "GET") == 0 && strcmp(url, "/query") == 0) {
+    else if (strcmp(method, "GET") == 0 && strcmp(url,
+        subdir_endpoint("/query")) == 0)
+    {
         /* get query */
         const char *query = MHD_lookup_connection_value(connection,
             MHD_GET_ARGUMENT_KIND, "q");
diff --git a/search.cfg b/search.cfg
deleted file mode 100644
index 83aa8f1..0000000
--- a/search.cfg
+++ /dev/null
@@ -1,20 +0,0 @@
-# search config
-
-# listen port
-port=8888
-
-# html template path
-template=index.htm.tmpl
-
-# root
-root=/home/arf20/projects
-
-# read magic numbers (mime type)
-magic=false
-
-# indexing period (seconds)
-period=86400
-
-# http subdirectory for file links
-subdir=/files/
-
diff --git a/search.cfg.example b/search.cfg.example
index bb7e8ef..d21b334 100644
--- a/search.cfg.example
+++ b/search.cfg.example
@@ -6,4 +6,18 @@ port=8888
 # html template path
 template=index.htm.tmpl
 
+# app subdirectory for http server
+app_subdir=/search/
+
+# root
+root=/home/arf20/projects
+
+# read magic numbers (mime type)
+magic=false
+
+# indexing period (seconds)
+period=86400
+
+# http subdirectory for file links
+result_subdir=/files/
 
-- 
2.47.3


References:
[arfnet2-search PATCH 00/16] Pre-release deploymentarf20 <arf20@xxxxxxxxx>