diff options
author | Omar Polo <op@omarpolo.com> | 2021-01-19 11:28:41 +0000 |
---|---|---|
committer | Omar Polo <op@omarpolo.com> | 2021-01-19 11:28:41 +0000 |
commit | 6119e13e8aa794988b3875614a0a2c3ce0f07e7b (patch) | |
tree | 40680a673d8721c7f515adc57a9d5e467ba41972 | |
parent | df79b4c1d5c7dda510accf2d407e318b33bb936d (diff) |
moving "default type" from global options to server options
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | gmid.1 | 14 | ||||
-rw-r--r-- | gmid.h | 5 | ||||
-rw-r--r-- | mime.c | 26 | ||||
-rw-r--r-- | parse.y | 6 | ||||
-rw-r--r-- | server.c | 2 |
6 files changed, 23 insertions, 31 deletions
@@ -1,6 +1,7 @@ 2021-01-19 Omar Polo <op@omarpolo.com> * parse.y (servopt): add "lang" server option + (servopt): moving "default type" from global options to server options * Dockerfile: add a dockerfile @@ -129,13 +129,6 @@ Add a mapping for the given to the given .Ar mime-type . Both argument are strings. -.It Ic default type Ar string -Set the default media type that is used if the media type for a -specified extension is not found. -If not specified, the -.Ic default type -is set to -.Dq application/octet-stream . .El .Ss Servers Every virtual host is defined by a @@ -168,6 +161,13 @@ Enable the execution of CGI scripts if is a prefix of the user request string. An empty path "" will effectively enable the execution of any file with the executable bit set inside the root directory. +.It Ic default type Ar string +Set the default media type that is used if the media type for a +specified extension is not found. +If not specified, the +.Ic default type +is set to +.Dq application/octet-stream . .It Ic lang Ar string Specify the language tag for the text/gemini content served. If not specified, no @@ -61,6 +61,7 @@ struct vhost { const char *cgi; char *lang; int dirfd; + char *default_mime; }; extern struct vhost hosts[HOSTSLEN]; @@ -71,7 +72,6 @@ struct etm { /* extension to mime */ }; struct mimes { - char *def; struct etm *t; size_t len; size_t cap; @@ -163,11 +163,10 @@ extern int yylex(void); /* mime.c */ void init_mime(void); -void set_default_mime(const char*); void add_mime(const char*, const char*); void load_default_mime(void); int load_mime_file(const char*); -const char *mime(const char*); +const char *mime(struct vhost*, const char*); /* server.c */ int check_path(struct client*, const char*, int*); @@ -24,24 +24,11 @@ void init_mime(void) { conf.mimes.len = 0; - conf.mimes.cap = 2; + conf.mimes.cap = 16; conf.mimes.t = calloc(conf.mimes.cap, sizeof(struct etm)); if (conf.mimes.t == NULL) fatal("calloc: %s", strerror(errno)); - - conf.mimes.def = strdup("application/octet-stream"); - if (conf.mimes.def == NULL) - fatal("strdup: %s", strerror(errno)); - -} - -void -set_default_mime(const char *m) -{ - free(conf.mimes.def); - if ((conf.mimes.def = strdup(m)) == NULL) - fatal("strdup: %s", strerror(errno)); } /* register mime for the given extension */ @@ -102,17 +89,20 @@ path_ext(const char *path) } const char * -mime(const char *path) +mime(struct vhost *host, const char *path) { - const char *ext; + const char *def, *ext; struct etm *t; + if ((def = host->default_mime) == NULL) + def = "application/octet-stream"; + if ((ext = path_ext(path)) == NULL) - return conf.mimes.def; + return def; for (t = conf.mimes.t; t->mime != NULL; ++t) if (!strcmp(ext, t->ext)) return t->mime; - return conf.mimes.def; + return def; } @@ -66,7 +66,6 @@ option : TDAEMON TBOOL { conf.foreground = !$2; } errx(1, "invalid protocols string \"%s\"", $2); } | TMIME TSTRING TSTRING { add_mime($2, $3); } - | TDEFAULT TTYPE TSTRING { set_default_mime($3); } ; vhosts : /* empty */ @@ -99,9 +98,12 @@ servopt : TCERT TSTRING { host->cert = $2; } if (*host->cgi == '/') host->cgi++; } + | TDEFAULT TTYPE TSTRING { + free(host->default_mime); + host->default_mime = $3; + } | TLANG TSTRING { free(host->lang); host->lang = $2; } ; - @@ -77,7 +77,7 @@ open_file(char *fpath, char *query, struct pollfd *fds, struct client *c) return 0; } c->i = c->buf; - return start_reply(fds, c, SUCCESS, mime(fpath)); + return start_reply(fds, c, SUCCESS, mime(c->host, fpath)); case FILE_DIRECTORY: LOGD(c, "%s is a directory, trying %s/index.gmi", fpath, fpath); |