directory: Don't append a separator when it's not needed
authorHugo Beauzée-Luyssen <hugo@beauzee.fr>
Wed, 19 Feb 2020 08:58:28 +0000 (09:58 +0100)
committerHugo Beauzée-Luyssen <hugo@beauzee.fr>
Mon, 24 Feb 2020 08:13:59 +0000 (09:13 +0100)
modules/access/directory.c

index 8fa3c0a..72182bd 100644 (file)
@@ -44,6 +44,7 @@
 typedef struct
 {
     char *base_uri;
+    bool need_separator;
     DIR *dir;
 } access_sys_t;
 
@@ -67,6 +68,12 @@ int DirInit (stream_t *access, DIR *dir)
     if (unlikely(sys->base_uri == NULL))
         goto error;
 
+    char last_char = sys->base_uri[strlen(sys->base_uri) - 1];
+    sys->need_separator =
+#ifdef _WIN32
+            last_char != '\\' &&
+#endif
+            last_char != '/';
     sys->dir = dir;
 
     access->p_sys = sys;
@@ -174,7 +181,9 @@ int DirRead (stream_t *access, input_item_node_t *node)
         }
 
         char *uri;
-        if (unlikely(asprintf(&uri, "%s/%s", sys->base_uri, encoded) == -1))
+        if (unlikely(asprintf(&uri, "%s%s%s", sys->base_uri,
+                              sys->need_separator ? "/" : "",
+                              encoded) == -1))
             uri = NULL;
         free(encoded);
         if (unlikely(uri == NULL))