From 39273368555fdc9eefcc349ae31a0f21efcac6f2 Mon Sep 17 00:00:00 2001 From: Omar Polo Date: Mon, 24 Jul 2023 09:56:37 +0000 Subject: titan: parse the response code and exit accordingly Exit with 0 if the response code was in the 2x or 3x range, or with 2 for other codes. It already exits with 1 upon any other error (including parsing errors.) Print the redirect code on 3x to stdout and the meta to stderr for the 1x, 4x, 5x and 6x ranges. --- titan.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'titan.c') diff --git a/titan.c b/titan.c index d3b781a..cc5bd21 100644 --- a/titan.c +++ b/titan.c @@ -190,6 +190,29 @@ open_input_file(int argc, char **argv) return fp; } +static int +parse_response(char *r) +{ + int code; + + if (r[0] < '0' || r[0] > '9' || + r[1] < '0' || r[1] > '9' || + r[2] != ' ') + errx(1, "illegal response"); + + code = (r[0] - '0') * 10 + (r[1] - '0'); + if (code < 10 || code >= 70) + errx(1, "invalid response code: %d", code); + if (code >= 20 && code < 30) + return 0; + if (code >= 30 && code < 40) { + puts(r + 3); + return 0; + } + warnx("server error: %s", r + 3); + return 2; +} + static void __dead usage(void) { @@ -212,7 +235,7 @@ main(int argc, char **argv) char iribuf[1025]; char resbuf[1025]; char *req; - int sock, ch; + int sock, ch, ret = 0; if (pledge("stdio rpath tmppath inet dns", NULL) == -1) err(1, "pledge"); @@ -322,8 +345,7 @@ main(int argc, char **argv) if ((m = memmem(resbuf, w, "\r\n", 2)) == NULL) errx(1, "invalid reply"); *m = '\0'; - /* XXX parse */ - puts(resbuf); + ret = parse_response(resbuf); break; } } @@ -347,7 +369,7 @@ main(int argc, char **argv) /* fallthrough */ default: tls_free(ctx); - return (0); + return (ret); } } } -- cgit v1.2.3