From 57ec3e776e0333167134b5b186f9c72870eb228d Mon Sep 17 00:00:00 2001 From: Omar Polo Date: Mon, 8 Feb 2021 20:50:30 +0000 Subject: [PATCH] refactor apply_block_return move the strip and fmt logic to their own function --- server.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/server.c b/server.c index d7e5108..6626648 100644 --- a/server.c +++ b/server.c @@ -51,6 +51,8 @@ static void open_file(struct client*); static void load_file(struct client*); static void check_for_cgi(struct client*); static void handle_handshake(int, short, void*); +static char *strip_path(char*, int); +static void fmt_sbuf(const char*, struct client*, const char*); static int apply_block_return(struct client*); static void handle_open_conn(int, short, void*); static void start_reply(struct client*, int, const char*); @@ -400,20 +402,11 @@ err: start_reply(c, BAD_REQUEST, "Wrong/malformed host or missing SNI"); } -/* 1 if a matching `block return' (and apply it), 0 otherwise */ -static int -apply_block_return(struct client *c) +static char * +strip_path(char *path, int strip) { - char *t, *path, buf[32]; - const char *fmt; - int strip, code; - size_t i; + char *t; - if (!vhost_block_return(c->host, c->iri.path, &code, &fmt)) - return 0; - - strip = vhost_strip(c->host, c->iri.path); - path = c->iri.path; while (strip > 0) { if ((t = strchr(path, '/')) == NULL) { path = strchr(path, '\0'); @@ -423,6 +416,15 @@ apply_block_return(struct client *c) strip--; } + return path; +} + +static void +fmt_sbuf(const char *fmt, struct client *c, const char *path) +{ + size_t i; + char buf[32]; + memset(buf, 0, sizeof(buf)); for (i = 0; *fmt; ++fmt) { if (i == sizeof(buf)-1 || *fmt == '%') { @@ -462,6 +464,20 @@ apply_block_return(struct client *c) if (i != 0) strlcat(c->sbuf, buf, sizeof(c->sbuf)); +} + +/* 1 if a matching `block return' (and apply it), 0 otherwise */ +static int +apply_block_return(struct client *c) +{ + const char *fmt, *path; + int code; + + if (!vhost_block_return(c->host, c->iri.path, &code, &fmt)) + return 0; + + path = strip_path(c->iri.path, vhost_strip(c->host, c->iri.path)); + fmt_sbuf(fmt, c, path); start_reply(c, code, c->sbuf); return 1;