Improve vhost handling and fix an uninitialized variable use

It was not a good thing to depend on the value of i so late in the code
again. If for some reason a loop is added beforehand it would break the
logic.
This commit is contained in:
Laslo Hunhold 2017-08-02 08:50:55 +02:00
parent 63bafbf1d1
commit 075e314bdd

14
quark.c
View file

@ -561,9 +561,10 @@ sendresponse(int fd, struct request *r)
int hasport, ipv6host; int hasport, ipv6host;
static char realtarget[PATH_MAX], tmptarget[PATH_MAX], t[TIMESTAMP_LEN]; static char realtarget[PATH_MAX], tmptarget[PATH_MAX], t[TIMESTAMP_LEN];
char *p, *q, *mime; char *p, *q, *mime;
const char *err; const char *vhostmatch, *err;
/* match vhost */ /* match vhost */
vhostmatch = NULL;
if (vhosts) { if (vhosts) {
for (i = 0; i < LEN(vhost); i++) { for (i = 0; i < LEN(vhost); i++) {
/* switch to vhost directory if there is a match */ /* switch to vhost directory if there is a match */
@ -573,6 +574,7 @@ sendresponse(int fd, struct request *r)
return sendstatus(fd, (errno == EACCES) ? return sendstatus(fd, (errno == EACCES) ?
S_FORBIDDEN : S_NOT_FOUND); S_FORBIDDEN : S_NOT_FOUND);
} }
vhostmatch = vhost[i].name;
break; break;
} }
} }
@ -610,8 +612,8 @@ sendresponse(int fd, struct request *r)
} }
/* redirect if targets differ or host is non-canonical */ /* redirect if targets differ or host is non-canonical */
if (strcmp(r->target, realtarget) || (vhosts && r->field[REQ_HOST][0] && if (strcmp(r->target, realtarget) || (vhosts && vhostmatch &&
i < LEN(vhost) && strcmp(r->field[REQ_HOST], vhost[i].name))) { strcmp(r->field[REQ_HOST], vhostmatch))) {
/* do we need to add a port to the Location? */ /* do we need to add a port to the Location? */
hasport = strcmp(port, "80"); hasport = strcmp(port, "80");
@ -636,8 +638,8 @@ sendresponse(int fd, struct request *r)
S_MOVED_PERMANENTLY, S_MOVED_PERMANENTLY,
status_str[S_MOVED_PERMANENTLY], status_str[S_MOVED_PERMANENTLY],
timestamp(time(NULL), t), ipv6host ? "[" : "", timestamp(time(NULL), t), ipv6host ? "[" : "",
r->field[REQ_HOST][0] ? (vhosts && i < LEN(vhost)) ? r->field[REQ_HOST][0] ? (vhosts && vhostmatch) ?
vhost[i].name : r->field[REQ_HOST] : host, vhostmatch : r->field[REQ_HOST] : host,
ipv6host ? "]" : "", hasport ? ":" : "", ipv6host ? "]" : "", hasport ? ":" : "",
hasport ? port : "", tmptarget) < 0) { hasport ? port : "", tmptarget) < 0) {
return S_REQUEST_TIMEOUT; return S_REQUEST_TIMEOUT;
@ -695,10 +697,10 @@ sendresponse(int fd, struct request *r)
/* range */ /* range */
lower = 0; lower = 0;
upper = st.st_size - 1; upper = st.st_size - 1;
if (r->field[REQ_RANGE][0]) { if (r->field[REQ_RANGE][0]) {
/* parse field */ /* parse field */
p = r->field[REQ_RANGE]; p = r->field[REQ_RANGE];
err = NULL;
if (strncmp(p, "bytes=", sizeof("bytes=") - 1)) { if (strncmp(p, "bytes=", sizeof("bytes=") - 1)) {
return sendstatus(fd, S_BAD_REQUEST); return sendstatus(fd, S_BAD_REQUEST);