simplify and fix redirections after regex

This commit is contained in:
prx 2022-08-18 13:59:54 +02:00
parent e3932483ef
commit 27549119ef
3 changed files with 18 additions and 17 deletions

9
main.c
View File

@ -58,6 +58,7 @@ main(int argc, char **argv)
read_request(request); read_request(request);
split_request(request, hostname, path, query); split_request(request, hostname, path, query);
set_path(path, sizeof(path), virtualhost, hostname); set_path(path, sizeof(path), virtualhost, hostname);
//get_query(path, query, sizeof(query)); //get_query(path, query, sizeof(query));
/* percent decode */ /* percent decode */
@ -69,16 +70,20 @@ main(int argc, char **argv)
if (do_cgi(chroot_dir, cgi_dir, path, hostname, query) == 0) if (do_cgi(chroot_dir, cgi_dir, path, hostname, query) == 0)
stop(EXIT_SUCCESS, NULL); stop(EXIT_SUCCESS, NULL);
syslog(LOG_DAEMON, "path:%s", path);
/* *** from here, cgi didn't run *** /* *** from here, cgi didn't run ***
* check if path available * check if path available
*/ */
check_path(path, sizeof(path), hostname, virtualhost); check_path(path, sizeof(path), virtualhost, strlen(hostname));
/* split dir and filename */ /* split dir and filename */
split_dir_file(path, dir, sizeof(dir), file, sizeof(file)); split_dir_file(path, dir, sizeof(dir), file, sizeof(file));
syslog(LOG_DAEMON, "dir:%s", dir);
syslog(LOG_DAEMON, "file:%s", file);
/* go to dir */ /* go to dir */
echdir(dir); if (*dir)
echdir(dir);
/* regular file to stdout */ /* regular file to stdout */
display_file(file); display_file(file);

24
vger.c
View File

@ -384,6 +384,9 @@ set_path(char *path, size_t pathsiz, int virtualhost, const char *hostname)
{ {
char tmp[GEMINI_REQUEST_MAX] = {'\0'}; char tmp[GEMINI_REQUEST_MAX] = {'\0'};
if (strlen(path) == 0) /* this is root dir */
estrlcpy(path, "./", pathsiz);
/* path is in a subdir named hostname */ /* path is in a subdir named hostname */
if (virtualhost) { if (virtualhost) {
estrlcpy(tmp, hostname, sizeof(tmp)); estrlcpy(tmp, hostname, sizeof(tmp));
@ -396,7 +399,7 @@ set_path(char *path, size_t pathsiz, int virtualhost, const char *hostname)
} }
void void
check_path(char *path, size_t pathsiz, const char *hstnm, int virtualhost) check_path(char *path, size_t pathsiz, int virtualhost, size_t hstnm_o)
{ {
struct stat sb = {0}; struct stat sb = {0};
char tmp[PATH_MAX] = {'\0'}; char tmp[PATH_MAX] = {'\0'};
@ -416,17 +419,13 @@ check_path(char *path, size_t pathsiz, const char *hstnm, int virtualhost)
/* check if dir path end with "/" */ /* check if dir path end with "/" */
if (path[strlen(path) - 1] != '/') { if (path[strlen(path) - 1] != '/') {
/* redirect to the dir with appropriate ending '/' */ /* redirect to the dir with appropriate ending '/' */
estrlcpy(tmp, "/", sizeof(tmp));
/* remove leading '.' for redirection*/ if (virtualhost) /* skip hostname */
if (virtualhost) /* remove ./host.name */ estrlcat(tmp, path+hstnm_o+1, sizeof(tmp));
memmove(path, path+2+strlen(hstnm),
strlen(path + 2) + strlen(hstnm) + 1);
else else
memmove(path, path+1, estrlcat(tmp, path, sizeof(tmp));
strlen(path + 1) + 1); /* +1 for \0 */ estrlcat(tmp, "/", sizeof(tmp));
status(31, "%s", tmp);
estrlcat(path, "/", pathsiz);
status(31, "%s", path);
stop(EXIT_SUCCESS, NULL); stop(EXIT_SUCCESS, NULL);
} }
/* check if DEFAULT_INDEX exists in directory */ /* check if DEFAULT_INDEX exists in directory */
@ -502,9 +501,6 @@ split_request(const char *request, char *hostname, char *path, char *query)
getsubexp(request, match[1], hostname); getsubexp(request, match[1], hostname);
getsubexp(request, match[2], path); getsubexp(request, match[2], path);
getsubexp(request, match[3], query); getsubexp(request, match[3], query);
syslog(LOG_DAEMON, "hostname:%s", hostname);
syslog(LOG_DAEMON, "path:%s", path);
syslog(LOG_DAEMON, "query:%s", query);
regfree(&greg); regfree(&greg);
} }

2
vger.h
View File

@ -42,7 +42,7 @@ static char _request[GEMINI_REQUEST_MAX] = {'\0'};
ssize_t autoindex(const char *); ssize_t autoindex(const char *);
void cgi(const char *); void cgi(const char *);
char * read_request(char *); char * read_request(char *);
void check_path(char *, size_t, const char *, int); void check_path(char *, size_t, int, size_t);
ssize_t display_file(const char *); ssize_t display_file(const char *);
int do_cgi(const char *, const char *, const char *, const char *, const char *); int do_cgi(const char *, const char *, const char *, const char *, const char *);
void drop_privileges(const char *, const char *, const char *); void drop_privileges(const char *, const char *, const char *);