Rename request "r" to "req"

Now that we have response-structs called "res", the naming "r" is a
bit ambiguous.

Signed-off-by: Laslo Hunhold <dev@frign.de>
This commit is contained in:
Laslo Hunhold 2020-08-05 15:43:29 +02:00
parent c51b31d7ac
commit 2c50d0c654
No known key found for this signature in database
GPG key ID: 69576BD24CFCB980
3 changed files with 34 additions and 34 deletions

48
http.c
View file

@ -144,7 +144,7 @@ decode(char src[PATH_MAX], char dest[PATH_MAX])
} }
int int
http_get_request(int fd, struct request *r) http_get_request(int fd, struct request *req)
{ {
struct in6_addr addr; struct in6_addr addr;
size_t hlen, i, mlen; size_t hlen, i, mlen;
@ -152,7 +152,7 @@ http_get_request(int fd, struct request *r)
char h[HEADER_MAX], *p, *q; char h[HEADER_MAX], *p, *q;
/* empty all fields */ /* empty all fields */
memset(r, 0, sizeof(*r)); memset(req, 0, sizeof(*req));
/* /*
* receive header * receive header
@ -189,7 +189,7 @@ http_get_request(int fd, struct request *r)
for (i = 0; i < NUM_REQ_METHODS; i++) { for (i = 0; i < NUM_REQ_METHODS; i++) {
mlen = strlen(req_method_str[i]); mlen = strlen(req_method_str[i]);
if (!strncmp(req_method_str[i], h, mlen)) { if (!strncmp(req_method_str[i], h, mlen)) {
r->method = i; req->method = i;
break; break;
} }
} }
@ -213,8 +213,8 @@ http_get_request(int fd, struct request *r)
if (q - p + 1 > PATH_MAX) { if (q - p + 1 > PATH_MAX) {
return http_send_status(fd, S_REQUEST_TOO_LARGE); return http_send_status(fd, S_REQUEST_TOO_LARGE);
} }
memcpy(r->target, p, q - p + 1); memcpy(req->target, p, q - p + 1);
decode(r->target, r->target); decode(req->target, req->target);
/* basis for next step */ /* basis for next step */
p = q + 1; p = q + 1;
@ -278,7 +278,7 @@ http_get_request(int fd, struct request *r)
if (q - p + 1 > FIELD_MAX) { if (q - p + 1 > FIELD_MAX) {
return http_send_status(fd, S_REQUEST_TOO_LARGE); return http_send_status(fd, S_REQUEST_TOO_LARGE);
} }
memcpy(r->field[i], p, q - p + 1); memcpy(req->field[i], p, q - p + 1);
/* go to next line */ /* go to next line */
p = q + (sizeof("\r\n") - 1); p = q + (sizeof("\r\n") - 1);
@ -288,8 +288,8 @@ http_get_request(int fd, struct request *r)
* clean up host * clean up host
*/ */
p = strrchr(r->field[REQ_HOST], ':'); p = strrchr(req->field[REQ_HOST], ':');
q = strrchr(r->field[REQ_HOST], ']'); q = strrchr(req->field[REQ_HOST], ']');
/* strip port suffix but don't interfere with IPv6 bracket notation /* strip port suffix but don't interfere with IPv6 bracket notation
* as per RFC 2732 */ * as per RFC 2732 */
@ -304,13 +304,13 @@ http_get_request(int fd, struct request *r)
/* strip the brackets from the IPv6 notation and validate the address */ /* strip the brackets from the IPv6 notation and validate the address */
if (q) { if (q) {
/* brackets must be on the outside */ /* brackets must be on the outside */
if (r->field[REQ_HOST][0] != '[' || *(q + 1) != '\0') { if (req->field[REQ_HOST][0] != '[' || *(q + 1) != '\0') {
return http_send_status(fd, S_BAD_REQUEST); return http_send_status(fd, S_BAD_REQUEST);
} }
/* remove the right bracket */ /* remove the right bracket */
*q = '\0'; *q = '\0';
p = r->field[REQ_HOST] + 1; p = req->field[REQ_HOST] + 1;
/* validate the contained IPv6 address */ /* validate the contained IPv6 address */
if (inet_pton(AF_INET6, p, &addr) != 1) { if (inet_pton(AF_INET6, p, &addr) != 1) {
@ -318,7 +318,7 @@ http_get_request(int fd, struct request *r)
} }
/* copy it into the host field */ /* copy it into the host field */
memmove(r->field[REQ_HOST], p, q - p + 1); memmove(req->field[REQ_HOST], p, q - p + 1);
} }
return 0; return 0;
@ -502,7 +502,7 @@ parse_range(char *s, off_t size, off_t *lower, off_t *upper)
#define RELPATH(x) ((!*(x) || !strcmp(x, "/")) ? "." : ((x) + 1)) #define RELPATH(x) ((!*(x) || !strcmp(x, "/")) ? "." : ((x) + 1))
enum status enum status
http_send_response(int fd, struct request *r) http_send_response(int fd, struct request *req)
{ {
struct in6_addr addr; struct in6_addr addr;
struct response res = { 0 }; struct response res = { 0 };
@ -516,14 +516,14 @@ http_send_response(int fd, struct request *r)
const char *vhostmatch, *targethost; const char *vhostmatch, *targethost;
/* make a working copy of the target */ /* make a working copy of the target */
memcpy(realtarget, r->target, sizeof(realtarget)); memcpy(realtarget, req->target, sizeof(realtarget));
/* match vhost */ /* match vhost */
vhostmatch = NULL; vhostmatch = NULL;
if (s.vhost) { if (s.vhost) {
for (i = 0; i < s.vhost_len; i++) { for (i = 0; i < s.vhost_len; i++) {
/* switch to vhost directory if there is a match */ /* switch to vhost directory if there is a match */
if (!regexec(&s.vhost[i].re, r->field[REQ_HOST], 0, if (!regexec(&s.vhost[i].re, req->field[REQ_HOST], 0,
NULL, 0)) { NULL, 0)) {
if (chdir(s.vhost[i].dir) < 0) { if (chdir(s.vhost[i].dir) < 0) {
return http_send_status(fd, (errno == EACCES) ? return http_send_status(fd, (errno == EACCES) ?
@ -597,8 +597,8 @@ http_send_response(int fd, struct request *r)
} }
/* redirect if targets differ, host is non-canonical or we prefixed */ /* redirect if targets differ, host is non-canonical or we prefixed */
if (strcmp(r->target, realtarget) || (s.vhost && vhostmatch && if (strcmp(req->target, realtarget) || (s.vhost && vhostmatch &&
strcmp(r->field[REQ_HOST], vhostmatch))) { strcmp(req->field[REQ_HOST], vhostmatch))) {
res.status = S_MOVED_PERMANENTLY; res.status = S_MOVED_PERMANENTLY;
/* encode realtarget */ /* encode realtarget */
@ -607,8 +607,8 @@ http_send_response(int fd, struct request *r)
/* determine target location */ /* determine target location */
if (s.vhost) { if (s.vhost) {
/* absolute redirection URL */ /* absolute redirection URL */
targethost = r->field[REQ_HOST][0] ? vhostmatch ? targethost = req->field[REQ_HOST][0] ? vhostmatch ?
vhostmatch : r->field[REQ_HOST] : s.host ? vhostmatch : req->field[REQ_HOST] : s.host ?
s.host : "localhost"; s.host : "localhost";
/* do we need to add a port to the Location? */ /* do we need to add a port to the Location? */
@ -648,7 +648,7 @@ http_send_response(int fd, struct request *r)
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
/* append docindex to target */ /* append docindex to target */
if (esnprintf(realtarget, sizeof(realtarget), "%s%s", if (esnprintf(realtarget, sizeof(realtarget), "%s%s",
r->target, s.docindex)) { req->target, s.docindex)) {
return http_send_status(fd, S_REQUEST_TOO_LARGE); return http_send_status(fd, S_REQUEST_TOO_LARGE);
} }
@ -658,7 +658,7 @@ http_send_response(int fd, struct request *r)
/* remove index suffix and serve dir */ /* remove index suffix and serve dir */
realtarget[strlen(realtarget) - realtarget[strlen(realtarget) -
strlen(s.docindex)] = '\0'; strlen(s.docindex)] = '\0';
return resp_dir(fd, RELPATH(realtarget), r); return resp_dir(fd, RELPATH(realtarget), req);
} else { } else {
/* reject */ /* reject */
if (!S_ISREG(st.st_mode) || errno == EACCES) { if (!S_ISREG(st.st_mode) || errno == EACCES) {
@ -671,9 +671,9 @@ http_send_response(int fd, struct request *r)
} }
/* modified since */ /* modified since */
if (r->field[REQ_MOD][0]) { if (req->field[REQ_MOD][0]) {
/* parse field */ /* parse field */
if (!strptime(r->field[REQ_MOD], "%a, %d %b %Y %T GMT", &tm)) { if (!strptime(req->field[REQ_MOD], "%a, %d %b %Y %T GMT", &tm)) {
return http_send_status(fd, S_BAD_REQUEST); return http_send_status(fd, S_BAD_REQUEST);
} }
@ -685,7 +685,7 @@ http_send_response(int fd, struct request *r)
} }
/* range */ /* range */
switch (parse_range(r->field[REQ_RANGE], st.st_size, &lower, &upper)) { switch (parse_range(req->field[REQ_RANGE], st.st_size, &lower, &upper)) {
case S_RANGE_NOT_SATISFIABLE: case S_RANGE_NOT_SATISFIABLE:
res.status = S_RANGE_NOT_SATISFIABLE; res.status = S_RANGE_NOT_SATISFIABLE;
@ -713,5 +713,5 @@ http_send_response(int fd, struct request *r)
} }
} }
return resp_file(fd, RELPATH(realtarget), r, &st, mime, lower, upper); return resp_file(fd, RELPATH(realtarget), req, &st, mime, lower, upper);
} }

8
main.c
View file

@ -25,7 +25,7 @@ static char *udsname;
static void static void
serve(int infd, struct sockaddr_storage *in_sa) serve(int infd, struct sockaddr_storage *in_sa)
{ {
struct request r; struct request req;
time_t t; time_t t;
enum status status; enum status status;
char inaddr[INET6_ADDRSTRLEN /* > INET_ADDRSTRLEN */]; char inaddr[INET6_ADDRSTRLEN /* > INET_ADDRSTRLEN */];
@ -37,8 +37,8 @@ serve(int infd, struct sockaddr_storage *in_sa)
} }
/* handle request */ /* handle request */
if (!(status = http_get_request(infd, &r))) { if (!(status = http_get_request(infd, &req))) {
status = http_send_response(infd, &r); status = http_send_response(infd, &req);
} }
/* write output to log */ /* write output to log */
@ -52,7 +52,7 @@ serve(int infd, struct sockaddr_storage *in_sa)
goto cleanup; goto cleanup;
} }
printf("%s\t%s\t%d\t%s\t%s\n", tstmp, inaddr, status, printf("%s\t%s\t%d\t%s\t%s\n", tstmp, inaddr, status,
r.field[REQ_HOST], r.target); req.field[REQ_HOST], req.target);
cleanup: cleanup:
/* clean up and finish */ /* clean up and finish */
shutdown(infd, SHUT_RD); shutdown(infd, SHUT_RD);

12
resp.c
View file

@ -84,7 +84,7 @@ html_escape(char *src, char *dst, size_t dst_siz)
} }
enum status enum status
resp_dir(int fd, char *name, struct request *r) resp_dir(int fd, char *name, struct request *req)
{ {
enum status sendstatus; enum status sendstatus;
struct dirent **e; struct dirent **e;
@ -107,7 +107,7 @@ resp_dir(int fd, char *name, struct request *r)
goto cleanup; goto cleanup;
} }
if (r->method == M_GET) { if (req->method == M_GET) {
/* listing header */ /* listing header */
html_escape(name, esc, sizeof(esc)); html_escape(name, esc, sizeof(esc));
if (dprintf(fd, if (dprintf(fd,
@ -155,13 +155,13 @@ cleanup:
} }
enum status enum status
resp_file(int fd, char *name, struct request *r, struct stat *st, char *mime, resp_file(int fd, char *name, struct request *req, struct stat *st, char *mime,
off_t lower, off_t upper) off_t lower, off_t upper)
{ {
FILE *fp; FILE *fp;
enum status sendstatus; enum status sendstatus;
struct response res = { struct response res = {
.status = (r->field[REQ_RANGE][0] != '\0') ? .status = (req->field[REQ_RANGE][0] != '\0') ?
S_PARTIAL_CONTENT : S_OK, S_PARTIAL_CONTENT : S_OK,
.field[RES_ACCEPT_RANGES] = "bytes", .field[RES_ACCEPT_RANGES] = "bytes",
}; };
@ -187,7 +187,7 @@ resp_file(int fd, char *name, struct request *r, struct stat *st, char *mime,
"%zu", upper - lower + 1)) { "%zu", upper - lower + 1)) {
return http_send_status(fd, S_INTERNAL_SERVER_ERROR); return http_send_status(fd, S_INTERNAL_SERVER_ERROR);
} }
if (r->field[REQ_RANGE][0] != '\0') { if (req->field[REQ_RANGE][0] != '\0') {
if (esnprintf(res.field[RES_CONTENT_RANGE], if (esnprintf(res.field[RES_CONTENT_RANGE],
sizeof(res.field[RES_CONTENT_RANGE]), sizeof(res.field[RES_CONTENT_RANGE]),
"bytes %zd-%zd/%zu", lower, upper, "bytes %zd-%zd/%zu", lower, upper,
@ -212,7 +212,7 @@ resp_file(int fd, char *name, struct request *r, struct stat *st, char *mime,
goto cleanup; goto cleanup;
} }
if (r->method == M_GET) { if (req->method == M_GET) {
/* write data until upper bound is hit */ /* write data until upper bound is hit */
remaining = upper - lower + 1; remaining = upper - lower + 1;