about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-08-22 10:16:57 +0000
committerLaurent Bercot <ska@appnovation.com>2023-08-22 10:16:57 +0000
commit97eae758b6ba3f4f951a54282a1fc57c721cf037 (patch)
tree2fe155007c1a906742e69395152347115cdcdc6e
parenta127bde4222baccfb77b561b1461c93e1911e8f0 (diff)
downloadtipidee-97eae758b6ba3f4f951a54282a1fc57c721cf037.tar.gz
tipidee-97eae758b6ba3f4f951a54282a1fc57c721cf037.tar.xz
tipidee-97eae758b6ba3f4f951a54282a1fc57c721cf037.zip
Fix redirection lookup
Signed-off-by: Laurent Bercot <ska@appnovation.com>
-rw-r--r--src/include/tipidee/conf.h2
-rw-r--r--src/libtipidee/tipidee_conf_get_redirection.c51
-rw-r--r--src/tipideed/tipideed.c2
3 files changed, 37 insertions, 18 deletions
diff --git a/src/include/tipidee/conf.h b/src/include/tipidee/conf.h
index bc66d76..abc8313 100644
--- a/src/include/tipidee/conf.h
+++ b/src/include/tipidee/conf.h
@@ -38,7 +38,7 @@ extern int tipidee_conf_get_uint32 (tipidee_conf const *, char const *, uint32_t
 extern unsigned int tipidee_conf_get_argv (tipidee_conf const *, char const *, char const **, unsigned int, size_t *) ;
 
 extern char const *tipidee_conf_get_docroot (tipidee_conf const *, tipidee_uri const *, uint16_t) ;
-extern int tipidee_conf_get_redirection (tipidee_conf const *, char const *, size_t, tipidee_redirection *) ;
+extern int tipidee_conf_get_redirection (tipidee_conf const *, char const *, size_t, char const *, tipidee_redirection *) ;
 extern char const *tipidee_conf_get_content_type (tipidee_conf const *, char const *) ;
 
 #endif
diff --git a/src/libtipidee/tipidee_conf_get_redirection.c b/src/libtipidee/tipidee_conf_get_redirection.c
index 62ada34..9d40de2 100644
--- a/src/libtipidee/tipidee_conf_get_redirection.c
+++ b/src/libtipidee/tipidee_conf_get_redirection.c
@@ -3,33 +3,52 @@
 #include <errno.h>
 #include <string.h>
 
+#include <skalibs/lolstdio.h>
+
 #include <tipidee/conf.h>
 
 #include <skalibs/posixishard.h>
 
-int tipidee_conf_get_redirection (tipidee_conf const *conf, char const *res, size_t docrootlen, tipidee_redirection *r)
+static int get_redir (tipidee_conf const *conf, size_t minl, char *key, size_t l, char const *path, tipidee_redirection *r)
 {
-  size_t reslen = strlen(res) ;
-  size_t l = 2 + reslen ;
-  char const *v = 0 ;
-  char key[3 + reslen] ;
-  key[0] = 'R' ; key[1] = ':' ;
-  memcpy(key + 2, res, reslen) ;
-  key[2 + reslen] = '/' ;
-  errno = ENOENT ;
-  while (!v)
+  char const *v ;
+  key[0] = 'R' ;
+  key[l] = '/' ;
+  for (;;)
   {
-    if (errno != ENOENT) return -1 ;
-    while (l > 2  + docrootlen && key[l] != '/') l-- ;
-    if (l <= 2 + docrootlen) break ;
+    while (l > minl && key[l] != '/') l-- ;
+    if (l <= minl) return 0 ;
     key[l--] = 0 ;
-    key[0] = 'r' ;
     v = tipidee_conf_get_string(conf, key) ;
+    if (v) break ;
+    if (errno != ENOENT) return -1 ;
+    key[0] = 'r' ;
   }
-  if (!v) return 0 ;
   if (v[0] < '@' || v[0] > 'C') return (errno = EPROTO, -1) ;
   r->type = v[0] & ~'@' ;
   r->location = v+1 ;
-  r->sub = res + l - 2 ;
+  r->sub = path + (l - minl + 1) ;
+  LOLDEBUG("get_redir: found redirection of type %c to %s with sub %s", v[0], r->location, r->sub) ;
   return 1 ;
 }
+
+int tipidee_conf_get_redirection (tipidee_conf const *conf, char const *docroot, size_t docrootlen, char const *path, tipidee_redirection *r)
+{
+  size_t pathlen = strlen(path) ;
+  char key[docrootlen + pathlen + 3] ;
+  key[1] = ':' ;
+  memcpy(key + 2, docroot, docrootlen) ;
+  memcpy(key + 2 + docrootlen, path, pathlen + 1) ;
+  {
+    int e = get_redir(conf, 2 + docrootlen, key, 2 + docrootlen + pathlen, path, r) ;
+    if (e) return e ;
+  }
+  {
+    char *p = memchr(docroot, ':', docrootlen) ;
+    if (!p) return 0 ;
+    docrootlen = p - docroot ;
+  }
+  memcpy(key + 2, docroot, docrootlen) ;
+  memcpy(key + 2 + docrootlen, path, pathlen + 1) ;
+  return get_redir(conf, 2 + docrootlen, key, 2 + docrootlen + pathlen, path, r) ;
+}
diff --git a/src/tipideed/tipideed.c b/src/tipideed/tipideed.c
index 42e65a6..e1021d3 100644
--- a/src/tipideed/tipideed.c
+++ b/src/tipideed/tipideed.c
@@ -229,7 +229,7 @@ static inline int serve (tipidee_rql *rql, char const *docroot, size_t docrootle
   if (rql->m != TIPIDEE_METHOD_OPTIONS)
   {
     tipidee_redirection rd = TIPIDEE_REDIRECTION_ZERO ;
-    int e = tipidee_conf_get_redirection(&g.conf, fn, docrootlen, &rd) ;
+    int e = tipidee_conf_get_redirection(&g.conf, docroot, docrootlen, rql->uri.path, &rd) ;
     if (e == -1) die500sys(rql, 111, "get redirection data for ", fn) ;
     if (e)
     {