From 97eae758b6ba3f4f951a54282a1fc57c721cf037 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Tue, 22 Aug 2023 10:16:57 +0000 Subject: Fix redirection lookup Signed-off-by: Laurent Bercot --- src/include/tipidee/conf.h | 2 +- src/libtipidee/tipidee_conf_get_redirection.c | 51 ++++++++++++++++++--------- src/tipideed/tipideed.c | 2 +- 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 #include +#include + #include #include -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) { -- cgit v1.2.3