commit 1ccaac023caa4ae415041c190516b0f7ae6ba647
parent 50c85ec642d1327135eb5a58c6d1ffc1ee0d41dc
Author: Laslo Hunhold <dev@frign.de>
Date: Sun, 23 Aug 2020 11:02:38 +0200
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>
Diffstat:
M | http.c | | | 40 | ++++++++++++++++++++-------------------- |
M | main.c | | | 48 | ++++++++++++++++++++++++------------------------ |
2 files changed, 44 insertions(+), 44 deletions(-)
diff --git a/http.c b/http.c
@@ -549,7 +549,7 @@ parse_range(const char *str, size_t size, size_t *lower, size_t *upper)
enum status
http_prepare_response(const struct request *req, struct response *res,
- const struct server *s)
+ const struct server *srv)
{
enum status returnstatus;
struct in6_addr addr;
@@ -573,16 +573,16 @@ http_prepare_response(const struct request *req, struct response *res,
/* match vhost */
vhost = NULL;
- if (s->vhost) {
- for (i = 0; i < s->vhost_len; i++) {
- if (!regexec(&(s->vhost[i].re), req->field[REQ_HOST],
+ if (srv->vhost) {
+ for (i = 0; i < srv->vhost_len; i++) {
+ if (!regexec(&(srv->vhost[i].re), req->field[REQ_HOST],
0, NULL, 0)) {
/* we have a matching vhost */
- vhost = &(s->vhost[i]);
+ vhost = &(srv->vhost[i]);
break;
}
}
- if (i == s->vhost_len) {
+ if (i == srv->vhost_len) {
return S_NOT_FOUND;
}
@@ -594,19 +594,19 @@ http_prepare_response(const struct request *req, struct response *res,
}
/* apply URI prefix mapping */
- for (i = 0; i < s->map_len; i++) {
- len = strlen(s->map[i].from);
- if (!strncmp(realuri, s->map[i].from, len)) {
+ for (i = 0; i < srv->map_len; i++) {
+ len = strlen(srv->map[i].from);
+ if (!strncmp(realuri, srv->map[i].from, len)) {
/* match canonical host if vhosts are enabled and
* the mapping specifies a canonical host */
- if (s->vhost && s->map[i].chost &&
- strcmp(s->map[i].chost, vhost->chost)) {
+ if (srv->vhost && srv->map[i].chost &&
+ strcmp(srv->map[i].chost, vhost->chost)) {
continue;
}
/* swap out URI prefix */
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;
}
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
* 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))) {
res->status = S_MOVED_PERMANENTLY;
@@ -656,14 +656,14 @@ http_prepare_response(const struct request *req, struct response *res,
encode(realuri, tmpuri);
/* determine target location */
- if (s->vhost) {
+ if (srv->vhost) {
/* absolute redirection URL */
targethost = req->field[REQ_HOST][0] ? vhost->chost ?
- vhost->chost : req->field[REQ_HOST] : s->host ?
- s->host : "localhost";
+ vhost->chost : req->field[REQ_HOST] :
+ srv->host ? srv->host : "localhost";
/* 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
* 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 ? "[" : "",
targethost,
ipv6host ? "]" : "", hasport ? ":" : "",
- hasport ? s->port : "", tmpuri)) {
+ hasport ? srv->port : "", tmpuri)) {
return S_REQUEST_TOO_LARGE;
}
} else {
@@ -715,13 +715,13 @@ http_prepare_response(const struct request *req, struct response *res,
* the URI
*/
if (esnprintf(tmpuri, sizeof(tmpuri), "%s%s",
- req->uri, s->docindex)) {
+ req->uri, srv->docindex)) {
return S_REQUEST_TOO_LARGE;
}
/* stat the docindex, which must be a regular file */
if (stat(RELPATH(tmpuri), &st) < 0 || !S_ISREG(st.st_mode)) {
- if (s->listdirs) {
+ if (srv->listdirs) {
/* serve directory listing */
res->type = RESTYPE_DIRLISTING;
res->status = (access(res->path, R_OK)) ?
diff --git a/main.c b/main.c
@@ -24,7 +24,7 @@
static char *udsname;
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 };
time_t t;
@@ -40,7 +40,7 @@ serve(int infd, const struct sockaddr_storage *in_sa, const struct server *s)
/* handle request */
if ((status = http_recv_header(c.fd, c.header, LEN(c.header), &c.off)) ||
(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);
} else {
status = http_send_header(c.fd, &c.res);
@@ -189,7 +189,7 @@ main(int argc, char *argv[])
struct group *grp = NULL;
struct passwd *pwd = NULL;
struct rlimit rlim;
- struct server s = {
+ struct server srv = {
.docindex = "index.html",
};
struct sockaddr_storage in_sa;
@@ -213,28 +213,28 @@ main(int argc, char *argv[])
group = EARGF(usage());
break;
case 'h':
- s.host = EARGF(usage());
+ srv.host = EARGF(usage());
break;
case 'i':
- s.docindex = EARGF(usage());
- if (strchr(s.docindex, '/')) {
+ srv.docindex = EARGF(usage());
+ if (strchr(srv.docindex, '/')) {
die("The document index must not contain '/'");
}
break;
case 'l':
- s.listdirs = 1;
+ srv.listdirs = 1;
break;
case 'm':
if (spacetok(EARGF(usage()), tok, 3) || !tok[0] || !tok[1]) {
usage();
}
- if (!(s.map = reallocarray(s.map, ++s.map_len,
+ if (!(srv.map = reallocarray(srv.map, ++srv.map_len,
sizeof(struct map)))) {
die("reallocarray:");
}
- s.map[s.map_len - 1].from = tok[0];
- s.map[s.map_len - 1].to = tok[1];
- s.map[s.map_len - 1].chost = tok[2];
+ srv.map[srv.map_len - 1].from = tok[0];
+ srv.map[srv.map_len - 1].to = tok[1];
+ srv.map[srv.map_len - 1].chost = tok[2];
break;
case 'n':
maxnprocs = strtonum(EARGF(usage()), 1, INT_MAX, &err);
@@ -243,7 +243,7 @@ main(int argc, char *argv[])
}
break;
case 'p':
- s.port = EARGF(usage());
+ srv.port = EARGF(usage());
break;
case 'U':
udsname = EARGF(usage());
@@ -256,14 +256,14 @@ main(int argc, char *argv[])
!tok[2]) {
usage();
}
- if (!(s.vhost = reallocarray(s.vhost, ++s.vhost_len,
- sizeof(struct vhost)))) {
+ if (!(srv.vhost = reallocarray(srv.vhost, ++srv.vhost_len,
+ sizeof(*srv.vhost)))) {
die("reallocarray:");
}
- s.vhost[s.vhost_len - 1].chost = tok[0];
- s.vhost[s.vhost_len - 1].regex = tok[1];
- s.vhost[s.vhost_len - 1].dir = tok[2];
- s.vhost[s.vhost_len - 1].prefix = tok[3];
+ srv.vhost[srv.vhost_len - 1].chost = tok[0];
+ srv.vhost[srv.vhost_len - 1].regex = tok[1];
+ srv.vhost[srv.vhost_len - 1].dir = tok[2];
+ srv.vhost[srv.vhost_len - 1].prefix = tok[3];
break;
default:
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*/
- if ((s.host && udsname) || !(s.port || udsname)) {
+ if ((srv.host && udsname) || !(srv.port || udsname)) {
usage();
}
@@ -284,11 +284,11 @@ main(int argc, char *argv[])
}
/* compile and check the supplied vhost regexes */
- for (i = 0; i < s.vhost_len; i++) {
- if (regcomp(&s.vhost[i].re, s.vhost[i].regex,
+ for (i = 0; i < srv.vhost_len; i++) {
+ if (regcomp(&srv.vhost[i].re, srv.vhost[i].regex,
REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
die("regcomp '%s': invalid regex",
- s.vhost[i].regex);
+ srv.vhost[i].regex);
}
}
@@ -317,7 +317,7 @@ main(int argc, char *argv[])
/* bind socket */
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()) {
case -1:
@@ -380,7 +380,7 @@ main(int argc, char *argv[])
/* fork and handle */
switch (fork()) {
case 0:
- serve(infd, &in_sa, &s);
+ serve(infd, &in_sa, &srv);
exit(0);
break;
case -1: