add "mime" and "default type" option for the configuration

This commit is contained in:
Omar Polo 2021-01-18 18:43:47 +00:00
parent 0fbe79b33c
commit 982069a120
5 changed files with 28 additions and 5 deletions

2
gmid.h
View File

@ -149,8 +149,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*);
/* server.c */

3
lex.l
View File

@ -55,6 +55,9 @@ daemon return TDAEMON;
ipv6 return TIPV6;
port return TPORT;
protocols return TPROTOCOLS;
mime return TMIME;
default return TDEFAULT;
type return TTYPE;
server return TSERVER;
cert return TCERT;

20
mime.c
View File

@ -26,6 +26,7 @@ struct etm { /* extension to mime */
};
struct mimes {
char *def;
struct etm *t;
size_t len;
size_t cap;
@ -41,6 +42,19 @@ init_mime(void)
if ((mimes.t = calloc(mimes.cap, sizeof(struct etm))) == NULL)
fatal("calloc: %s", strerror(errno));
mimes.def = strdup("application/octet-stream");
if (mimes.def == NULL)
fatal("strdup: %s", strerror(errno));
}
void
set_default_mime(const char *m)
{
free(mimes.def);
if ((mimes.def = strdup(m)) == NULL)
fatal("strdup: %s", strerror(errno));
}
/* register mime for the given extension */
@ -102,15 +116,15 @@ path_ext(const char *path)
const char *
mime(const char *path)
{
const char *ext, *def = "application/octet-stream";
const char *ext;
struct etm *t;
if ((ext = path_ext(path)) == NULL)
return def;
return mimes.def;
for (t = mimes.t; t->mime != NULL; ++t)
if (!strcmp(ext, t->ext))
return t->mime;
return def;
return mimes.def;
}

View File

@ -42,8 +42,7 @@ extern void yyerror(const char*);
int num;
}
%token TBOOL TSTRING TNUM
%token TDAEMON TIPV6 TPORT TPROTOCOLS TSERVER
%token TDAEMON TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TSERVER
%token TCERT TKEY TROOT TCGI
%token TERR
@ -66,6 +65,8 @@ option : TDAEMON TBOOL { conf.foreground = !$2; }
if (tls_config_parse_protocols(&conf.protos, $2) == -1)
errx(1, "invalid protocols string \"%s\"", $2);
}
| TMIME TSTRING TSTRING { add_mime($2, $3); }
| TDEFAULT TTYPE TSTRING { set_default_mime($3); }
;
vhosts : /* empty */

View File

@ -4,6 +4,9 @@ daemon on # enable daemon mode
# decomment to allow only TLSv1.3
#protocols "tlsv1.3"
# add the mapping for the extension rtf to the MIME application/rtf
mime "application/rtf" "rtf"
# server block example
server "example.com" {
cert "/path/to/cert.pem"