add "lang" server option

This commit is contained in:
Omar Polo 2021-01-19 10:58:29 +00:00
parent 17b09e3cb7
commit 05c23a54ea
7 changed files with 29 additions and 6 deletions

View File

@ -1,5 +1,7 @@
2021-01-19 Omar Polo <op@omarpolo.com> 2021-01-19 Omar Polo <op@omarpolo.com>
* parse.y (servopt): add "lang" server option
* Dockerfile: add a dockerfile * Dockerfile: add a dockerfile
2021-01-18 Omar Polo <op@omarpolo.com> 2021-01-18 Omar Polo <op@omarpolo.com>

5
gmid.1
View File

@ -168,6 +168,11 @@ Enable the execution of CGI scripts if
is a prefix of the user request string. is a prefix of the user request string.
An empty path "" will effectively enable the execution of any file An empty path "" will effectively enable the execution of any file
with the executable bit set inside the root directory. with the executable bit set inside the root directory.
.It Ic lang Ar string
Specify the language tag for the text/gemini content served.
If not specified, no
.Dq lang
parameter will be added in the response.
.El .El
.Sh CGI .Sh CGI
When CGI scripts are enabled for a directory, a request for an When CGI scripts are enabled for a directory, a request for an

1
gmid.h
View File

@ -59,6 +59,7 @@ struct vhost {
const char *key; const char *key;
const char *dir; const char *dir;
const char *cgi; const char *cgi;
char *lang;
int dirfd; int dirfd;
}; };

1
lex.l
View File

@ -58,6 +58,7 @@ protocols return TPROTOCOLS;
mime return TMIME; mime return TMIME;
default return TDEFAULT; default return TDEFAULT;
type return TTYPE; type return TTYPE;
lang return TLANG;
server return TSERVER; server return TSERVER;
cert return TCERT; cert return TCERT;

View File

@ -43,7 +43,7 @@ extern void yyerror(const char*);
} }
%token TDAEMON TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TSERVER %token TDAEMON TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TSERVER
%token TCERT TKEY TROOT TCGI %token TCERT TKEY TROOT TCGI TLANG
%token TERR %token TERR
%token <str> TSTRING %token <str> TSTRING
@ -98,6 +98,10 @@ servopt : TCERT TSTRING { host->cert = $2; }
/* drop the starting '/', if any */ /* drop the starting '/', if any */
if (*host->cgi == '/') if (*host->cgi == '/')
host->cgi++; host->cgi++;
} }
| TLANG TSTRING {
free(host->lang);
host->lang = $2;
}
; ;

View File

@ -14,11 +14,14 @@ server "example.com" {
root "/var/gemini/example.com" root "/var/gemini/example.com"
} }
# another example server, this time with CGI enabled for scripts in
# /cgi-bin/
server "it.example.com" { server "it.example.com" {
cert "/path/to/cert.pem" cert "/path/to/cert.pem"
key "/path/to/key.pem" key "/path/to/key.pem"
root "/var/gemini/example.com" root "/var/gemini/example.com"
# enable CGI scripts in /cgi-bin/
cgi "/cgi-bin/" cgi "/cgi-bin/"
# optional
lang "it"
} }

View File

@ -249,14 +249,21 @@ handle_open_conn(struct pollfd *fds, struct client *c)
int int
start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason) start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
{ {
char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */ char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
int len; int len;
client->code = code; client->code = code;
client->meta = reason; client->meta = reason;
client->state = S_INITIALIZING; client->state = S_INITIALIZING;
len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason); snprintf(buf, sizeof(buf), "%d ", code);
strlcat(buf, reason, sizeof(buf));
if (!strcmp(reason, "text/gemini") && client->host->lang != NULL) {
strlcat(buf, "; lang=", sizeof(buf));
strlcat(buf, client->host->lang, sizeof(buf));
}
len = strlcat(buf, "\r\n", sizeof(buf));
assert(len < (int)sizeof(buf)); assert(len < (int)sizeof(buf));
switch (tls_write(client->ctx, buf, len)) { switch (tls_write(client->ctx, buf, len)) {