From e7a2a99b5acfd15f0a0bba63344ec028a36700b3 Mon Sep 17 00:00:00 2001 From: Omar Polo Date: Sun, 24 Jan 2021 09:14:01 +0000 Subject: [PATCH] added index option --- gmid.1 | 4 ++++ gmid.h | 1 + lex.l | 3 ++- parse.y | 4 ++++ regress/runtime | 11 +++++++++++ server.c | 19 ++++++++----------- 6 files changed, 30 insertions(+), 12 deletions(-) diff --git a/gmid.1 b/gmid.1 index 35b9e14..453c188 100644 --- a/gmid.1 +++ b/gmid.1 @@ -177,6 +177,10 @@ Specify the language tag for the text/gemini content served. If not specified, no .Dq lang parameter will be added in the response. +.It Ic index Ar string +Set the directory index file. +If not specified, it defaults to +.Pa index.gmi .El .Sh CGI When CGI scripts are enabled for a directory, a request for an diff --git a/gmid.h b/gmid.h index 4a2c2c3..5388023 100644 --- a/gmid.h +++ b/gmid.h @@ -65,6 +65,7 @@ struct vhost { char *lang; int dirfd; char *default_mime; + char *index; }; extern struct vhost hosts[HOSTSLEN]; diff --git a/lex.l b/lex.l index 154fa69..0932bc3 100644 --- a/lex.l +++ b/lex.l @@ -58,13 +58,14 @@ protocols return TPROTOCOLS; mime return TMIME; default return TDEFAULT; type return TTYPE; -lang return TLANG; server return TSERVER; cert return TCERT; key return TKEY; root return TROOT; cgi return TCGI; +lang return TLANG; +index return TINDEX; [{}] return *yytext; diff --git a/parse.y b/parse.y index e72f2f0..2d8f0b8 100644 --- a/parse.y +++ b/parse.y @@ -106,4 +106,8 @@ servopt : TCERT TSTRING { host->cert = $2; } free(host->lang); host->lang = $2; } + | TINDEX TSTRING { + free(host->index); + host->index = $2; + } ; diff --git a/regress/runtime b/regress/runtime index fdccdb7..6e62029 100755 --- a/regress/runtime +++ b/regress/runtime @@ -173,3 +173,14 @@ echo OK GET /invalid with cgi check "should be running" quit + +config '' 'index "foo.gmi"' +checkconf +run + +eq "$(head /dir/)" "20 text/gemini" "Unexpected head for /" +eq "$(get /dir/)" "# hello world$ln" "Unexpected body for error" +echo OK GET /dir/ with custom index + +check "should be running" +quit diff --git a/server.c b/server.c index 69d79c1..62fef97 100644 --- a/server.c +++ b/server.c @@ -371,19 +371,14 @@ void send_dir(struct pollfd *fds, struct client *c) { size_t len; + const char *index = "index.gmi"; - /* guard against a re-entrant call: - * - * open_file -> send_dir -> open_file -> send_dir - * - * this can happen only if: + /* guard against a re-entrant call: open_file -> send_dir -> + * open_file -> send_dir. This can happen only if: * * - user requested a dir, say foo/ - * - we try to serve foo/index.gmi - * - foo/index.gmi is a directory. - * - * It's an unlikely case, but can happen. We then redirect - * to foo/index.gmi + * - we try to serve foo/$INDEX + * - foo/$INDEX is a directory. */ if (c->iri.path == c->sbuf) { goodbye(fds, c, TEMP_REDIRECT, c->sbuf); @@ -406,7 +401,9 @@ send_dir(struct pollfd *fds, struct client *c) if (!ends_with(c->sbuf, "/")) strlcat(c->sbuf, "/", sizeof(c->sbuf)); - len = strlcat(c->sbuf, "index.gmi", sizeof(c->sbuf)); + if (c->host->index != NULL) + index = c->host->index; + len = strlcat(c->sbuf, index, sizeof(c->sbuf)); if (len >= sizeof(c->sbuf)) { goodbye(fds, c, TEMP_FAILURE, "internal server error");