commit b27dc2b0a33b5087f2c505b82dcf2cc73208dcaa from: Omar Polo date: Tue Aug 08 16:06:17 2023 UTC draft the PATH_INFO and SCRIPT_NAME handling The idea is to require SCRIPT_NAME to be defined and strip it from the beginning of the path to get PATH_INFO. Soon(tm) a `fastcgi request strip' option will be added too. Maybe even `fastcgi script name "path"` that sets SCRIPT_NAME automatically. commit - 08c0f676fdb596b51b491a21913d73af141eb3fc commit + b27dc2b0a33b5087f2c505b82dcf2cc73208dcaa blob - 281dfaa37bc6bbc0c3481155016e4d7e19493925 blob + 1874c8577080f5a99bed47a821b818010292125c --- fcgi.c +++ fcgi.c @@ -373,12 +373,38 @@ fcgi_error(struct bufferevent *bev, short err, void *d void fcgi_req(struct client *c, struct location *loc) { - char buf[22]; - char *qs; + char buf[22], path[GEMINI_URL_LEN]; + char *qs, *pathinfo, *scriptname = NULL; + size_t l; time_t tim; + int r; struct tm tminfo; struct envlist *p; + TAILQ_FOREACH(p, &loc->params, envs) { + if (!strcmp(p->name, "SCRIPT_NAME")) { + scriptname = p->value; + break; + } + } + if (scriptname == NULL) + scriptname = ""; + + r = snprintf(path, sizeof(path), "/%s", c->iri.path); + if (r < 0 || (size_t)r >= sizeof(c->iri.path)) { + log_warn("snprintf failure?"); + fcgi_error(c->cgibev, EVBUFFER_ERROR, c); + return; + } + + pathinfo = path; + l = strlen(scriptname); + while (l > 0 && scriptname[l - 1] == '/') + l--; + if (!strncmp(scriptname, pathinfo, l)) + pathinfo += l; + log_warnx("scriptname=%s ; pathinfo=%s", scriptname, pathinfo); + fcgi_begin_request(c->cgibev); fcgi_send_param(c->cgibev, "GATEWAY_INTERFACE", "CGI/1.1"); fcgi_send_param(c->cgibev, "GEMINI_URL_PATH", c->iri.path); @@ -390,6 +416,9 @@ fcgi_req(struct client *c, struct location *loc) fcgi_send_param(c->cgibev, "SERVER_PROTOCOL", "GEMINI"); fcgi_send_param(c->cgibev, "SERVER_SOFTWARE", GMID_VERSION); + fcgi_send_param(c->cgibev, "SCRIPT_NAME", scriptname); + fcgi_send_param(c->cgibev, "PATH_INFO", pathinfo); + if (*c->iri.query != '\0' && strchr(c->iri.query, '=') == NULL && (qs = strdup(c->iri.query)) != NULL) { @@ -399,6 +428,8 @@ fcgi_req(struct client *c, struct location *loc) } TAILQ_FOREACH(p, &loc->params, envs) { + if (!strcmp(p->name, "SCRIPT_NAME")) + continue; fcgi_send_param(c->cgibev, p->name, p->value); }