Rename s to srv
This improves readability a bit and helps iron out confusions with status-variables called s in other methods. Signed-off-by: Laslo Hunhold <dev@frign.de>
This commit is contained in:
parent
50c85ec642
commit
1ccaac023c
2 changed files with 44 additions and 44 deletions
40
http.c
40
http.c
|
@ -549,7 +549,7 @@ parse_range(const char *str, size_t size, size_t *lower, size_t *upper)
|
||||||
|
|
||||||
enum status
|
enum status
|
||||||
http_prepare_response(const struct request *req, struct response *res,
|
http_prepare_response(const struct request *req, struct response *res,
|
||||||
const struct server *s)
|
const struct server *srv)
|
||||||
{
|
{
|
||||||
enum status returnstatus;
|
enum status returnstatus;
|
||||||
struct in6_addr addr;
|
struct in6_addr addr;
|
||||||
|
@ -573,16 +573,16 @@ http_prepare_response(const struct request *req, struct response *res,
|
||||||
|
|
||||||
/* match vhost */
|
/* match vhost */
|
||||||
vhost = NULL;
|
vhost = NULL;
|
||||||
if (s->vhost) {
|
if (srv->vhost) {
|
||||||
for (i = 0; i < s->vhost_len; i++) {
|
for (i = 0; i < srv->vhost_len; i++) {
|
||||||
if (!regexec(&(s->vhost[i].re), req->field[REQ_HOST],
|
if (!regexec(&(srv->vhost[i].re), req->field[REQ_HOST],
|
||||||
0, NULL, 0)) {
|
0, NULL, 0)) {
|
||||||
/* we have a matching vhost */
|
/* we have a matching vhost */
|
||||||
vhost = &(s->vhost[i]);
|
vhost = &(srv->vhost[i]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i == s->vhost_len) {
|
if (i == srv->vhost_len) {
|
||||||
return S_NOT_FOUND;
|
return S_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -594,19 +594,19 @@ http_prepare_response(const struct request *req, struct response *res,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* apply URI prefix mapping */
|
/* apply URI prefix mapping */
|
||||||
for (i = 0; i < s->map_len; i++) {
|
for (i = 0; i < srv->map_len; i++) {
|
||||||
len = strlen(s->map[i].from);
|
len = strlen(srv->map[i].from);
|
||||||
if (!strncmp(realuri, s->map[i].from, len)) {
|
if (!strncmp(realuri, srv->map[i].from, len)) {
|
||||||
/* match canonical host if vhosts are enabled and
|
/* match canonical host if vhosts are enabled and
|
||||||
* the mapping specifies a canonical host */
|
* the mapping specifies a canonical host */
|
||||||
if (s->vhost && s->map[i].chost &&
|
if (srv->vhost && srv->map[i].chost &&
|
||||||
strcmp(s->map[i].chost, vhost->chost)) {
|
strcmp(srv->map[i].chost, vhost->chost)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* swap out URI prefix */
|
/* swap out URI prefix */
|
||||||
memmove(realuri, realuri + len, strlen(realuri) + 1);
|
memmove(realuri, realuri + len, strlen(realuri) + 1);
|
||||||
if (prepend(realuri, LEN(realuri), s->map[i].to)) {
|
if (prepend(realuri, LEN(realuri), srv->map[i].to)) {
|
||||||
return S_REQUEST_TOO_LARGE;
|
return S_REQUEST_TOO_LARGE;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -648,7 +648,7 @@ http_prepare_response(const struct request *req, struct response *res,
|
||||||
* redirect if the original URI and the "real" URI differ or if
|
* redirect if the original URI and the "real" URI differ or if
|
||||||
* the requested host is non-canonical
|
* the requested host is non-canonical
|
||||||
*/
|
*/
|
||||||
if (strcmp(req->uri, realuri) || (s->vhost && vhost &&
|
if (strcmp(req->uri, realuri) || (srv->vhost && vhost &&
|
||||||
strcmp(req->field[REQ_HOST], vhost->chost))) {
|
strcmp(req->field[REQ_HOST], vhost->chost))) {
|
||||||
res->status = S_MOVED_PERMANENTLY;
|
res->status = S_MOVED_PERMANENTLY;
|
||||||
|
|
||||||
|
@ -656,14 +656,14 @@ http_prepare_response(const struct request *req, struct response *res,
|
||||||
encode(realuri, tmpuri);
|
encode(realuri, tmpuri);
|
||||||
|
|
||||||
/* determine target location */
|
/* determine target location */
|
||||||
if (s->vhost) {
|
if (srv->vhost) {
|
||||||
/* absolute redirection URL */
|
/* absolute redirection URL */
|
||||||
targethost = req->field[REQ_HOST][0] ? vhost->chost ?
|
targethost = req->field[REQ_HOST][0] ? vhost->chost ?
|
||||||
vhost->chost : req->field[REQ_HOST] : s->host ?
|
vhost->chost : req->field[REQ_HOST] :
|
||||||
s->host : "localhost";
|
srv->host ? srv->host : "localhost";
|
||||||
|
|
||||||
/* do we need to add a port to the Location? */
|
/* do we need to add a port to the Location? */
|
||||||
hasport = s->port && strcmp(s->port, "80");
|
hasport = srv->port && strcmp(srv->port, "80");
|
||||||
|
|
||||||
/* RFC 2732 specifies to use brackets for IPv6-addresses
|
/* RFC 2732 specifies to use brackets for IPv6-addresses
|
||||||
* in URLs, so we need to check if our host is one and
|
* in URLs, so we need to check if our host is one and
|
||||||
|
@ -680,7 +680,7 @@ http_prepare_response(const struct request *req, struct response *res,
|
||||||
ipv6host ? "[" : "",
|
ipv6host ? "[" : "",
|
||||||
targethost,
|
targethost,
|
||||||
ipv6host ? "]" : "", hasport ? ":" : "",
|
ipv6host ? "]" : "", hasport ? ":" : "",
|
||||||
hasport ? s->port : "", tmpuri)) {
|
hasport ? srv->port : "", tmpuri)) {
|
||||||
return S_REQUEST_TOO_LARGE;
|
return S_REQUEST_TOO_LARGE;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -715,13 +715,13 @@ http_prepare_response(const struct request *req, struct response *res,
|
||||||
* the URI
|
* the URI
|
||||||
*/
|
*/
|
||||||
if (esnprintf(tmpuri, sizeof(tmpuri), "%s%s",
|
if (esnprintf(tmpuri, sizeof(tmpuri), "%s%s",
|
||||||
req->uri, s->docindex)) {
|
req->uri, srv->docindex)) {
|
||||||
return S_REQUEST_TOO_LARGE;
|
return S_REQUEST_TOO_LARGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* stat the docindex, which must be a regular file */
|
/* stat the docindex, which must be a regular file */
|
||||||
if (stat(RELPATH(tmpuri), &st) < 0 || !S_ISREG(st.st_mode)) {
|
if (stat(RELPATH(tmpuri), &st) < 0 || !S_ISREG(st.st_mode)) {
|
||||||
if (s->listdirs) {
|
if (srv->listdirs) {
|
||||||
/* serve directory listing */
|
/* serve directory listing */
|
||||||
res->type = RESTYPE_DIRLISTING;
|
res->type = RESTYPE_DIRLISTING;
|
||||||
res->status = (access(res->path, R_OK)) ?
|
res->status = (access(res->path, R_OK)) ?
|
||||||
|
|
48
main.c
48
main.c
|
@ -24,7 +24,7 @@
|
||||||
static char *udsname;
|
static char *udsname;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
serve(int infd, const struct sockaddr_storage *in_sa, const struct server *s)
|
serve(int infd, const struct sockaddr_storage *in_sa, const struct server *srv)
|
||||||
{
|
{
|
||||||
struct connection c = { .fd = infd };
|
struct connection c = { .fd = infd };
|
||||||
time_t t;
|
time_t t;
|
||||||
|
@ -40,7 +40,7 @@ serve(int infd, const struct sockaddr_storage *in_sa, const struct server *s)
|
||||||
/* handle request */
|
/* handle request */
|
||||||
if ((status = http_recv_header(c.fd, c.header, LEN(c.header), &c.off)) ||
|
if ((status = http_recv_header(c.fd, c.header, LEN(c.header), &c.off)) ||
|
||||||
(status = http_parse_header(c.header, &c.req)) ||
|
(status = http_parse_header(c.header, &c.req)) ||
|
||||||
(status = http_prepare_response(&c.req, &c.res, s))) {
|
(status = http_prepare_response(&c.req, &c.res, srv))) {
|
||||||
status = http_send_status(c.fd, status);
|
status = http_send_status(c.fd, status);
|
||||||
} else {
|
} else {
|
||||||
status = http_send_header(c.fd, &c.res);
|
status = http_send_header(c.fd, &c.res);
|
||||||
|
@ -189,7 +189,7 @@ main(int argc, char *argv[])
|
||||||
struct group *grp = NULL;
|
struct group *grp = NULL;
|
||||||
struct passwd *pwd = NULL;
|
struct passwd *pwd = NULL;
|
||||||
struct rlimit rlim;
|
struct rlimit rlim;
|
||||||
struct server s = {
|
struct server srv = {
|
||||||
.docindex = "index.html",
|
.docindex = "index.html",
|
||||||
};
|
};
|
||||||
struct sockaddr_storage in_sa;
|
struct sockaddr_storage in_sa;
|
||||||
|
@ -213,28 +213,28 @@ main(int argc, char *argv[])
|
||||||
group = EARGF(usage());
|
group = EARGF(usage());
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
s.host = EARGF(usage());
|
srv.host = EARGF(usage());
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
s.docindex = EARGF(usage());
|
srv.docindex = EARGF(usage());
|
||||||
if (strchr(s.docindex, '/')) {
|
if (strchr(srv.docindex, '/')) {
|
||||||
die("The document index must not contain '/'");
|
die("The document index must not contain '/'");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
s.listdirs = 1;
|
srv.listdirs = 1;
|
||||||
break;
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
if (spacetok(EARGF(usage()), tok, 3) || !tok[0] || !tok[1]) {
|
if (spacetok(EARGF(usage()), tok, 3) || !tok[0] || !tok[1]) {
|
||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
if (!(s.map = reallocarray(s.map, ++s.map_len,
|
if (!(srv.map = reallocarray(srv.map, ++srv.map_len,
|
||||||
sizeof(struct map)))) {
|
sizeof(struct map)))) {
|
||||||
die("reallocarray:");
|
die("reallocarray:");
|
||||||
}
|
}
|
||||||
s.map[s.map_len - 1].from = tok[0];
|
srv.map[srv.map_len - 1].from = tok[0];
|
||||||
s.map[s.map_len - 1].to = tok[1];
|
srv.map[srv.map_len - 1].to = tok[1];
|
||||||
s.map[s.map_len - 1].chost = tok[2];
|
srv.map[srv.map_len - 1].chost = tok[2];
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
maxnprocs = strtonum(EARGF(usage()), 1, INT_MAX, &err);
|
maxnprocs = strtonum(EARGF(usage()), 1, INT_MAX, &err);
|
||||||
|
@ -243,7 +243,7 @@ main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
s.port = EARGF(usage());
|
srv.port = EARGF(usage());
|
||||||
break;
|
break;
|
||||||
case 'U':
|
case 'U':
|
||||||
udsname = EARGF(usage());
|
udsname = EARGF(usage());
|
||||||
|
@ -256,14 +256,14 @@ main(int argc, char *argv[])
|
||||||
!tok[2]) {
|
!tok[2]) {
|
||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
if (!(s.vhost = reallocarray(s.vhost, ++s.vhost_len,
|
if (!(srv.vhost = reallocarray(srv.vhost, ++srv.vhost_len,
|
||||||
sizeof(struct vhost)))) {
|
sizeof(*srv.vhost)))) {
|
||||||
die("reallocarray:");
|
die("reallocarray:");
|
||||||
}
|
}
|
||||||
s.vhost[s.vhost_len - 1].chost = tok[0];
|
srv.vhost[srv.vhost_len - 1].chost = tok[0];
|
||||||
s.vhost[s.vhost_len - 1].regex = tok[1];
|
srv.vhost[srv.vhost_len - 1].regex = tok[1];
|
||||||
s.vhost[s.vhost_len - 1].dir = tok[2];
|
srv.vhost[srv.vhost_len - 1].dir = tok[2];
|
||||||
s.vhost[s.vhost_len - 1].prefix = tok[3];
|
srv.vhost[srv.vhost_len - 1].prefix = tok[3];
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
|
@ -274,7 +274,7 @@ main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
/* can't have both host and UDS but must have one of port or UDS*/
|
/* can't have both host and UDS but must have one of port or UDS*/
|
||||||
if ((s.host && udsname) || !(s.port || udsname)) {
|
if ((srv.host && udsname) || !(srv.port || udsname)) {
|
||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -284,11 +284,11 @@ main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
/* compile and check the supplied vhost regexes */
|
/* compile and check the supplied vhost regexes */
|
||||||
for (i = 0; i < s.vhost_len; i++) {
|
for (i = 0; i < srv.vhost_len; i++) {
|
||||||
if (regcomp(&s.vhost[i].re, s.vhost[i].regex,
|
if (regcomp(&srv.vhost[i].re, srv.vhost[i].regex,
|
||||||
REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
|
REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
|
||||||
die("regcomp '%s': invalid regex",
|
die("regcomp '%s': invalid regex",
|
||||||
s.vhost[i].regex);
|
srv.vhost[i].regex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -317,7 +317,7 @@ main(int argc, char *argv[])
|
||||||
|
|
||||||
/* bind socket */
|
/* bind socket */
|
||||||
insock = udsname ? sock_get_uds(udsname, pwd->pw_uid, grp->gr_gid) :
|
insock = udsname ? sock_get_uds(udsname, pwd->pw_uid, grp->gr_gid) :
|
||||||
sock_get_ips(s.host, s.port);
|
sock_get_ips(srv.host, srv.port);
|
||||||
|
|
||||||
switch (fork()) {
|
switch (fork()) {
|
||||||
case -1:
|
case -1:
|
||||||
|
@ -380,7 +380,7 @@ main(int argc, char *argv[])
|
||||||
/* fork and handle */
|
/* fork and handle */
|
||||||
switch (fork()) {
|
switch (fork()) {
|
||||||
case 0:
|
case 0:
|
||||||
serve(infd, &in_sa, &s);
|
serve(infd, &in_sa, &srv);
|
||||||
exit(0);
|
exit(0);
|
||||||
break;
|
break;
|
||||||
case -1:
|
case -1:
|
||||||
|
|
Loading…
Reference in a new issue