diff options
author | Omar Polo <op@omarpolo.com> | 2023-07-24 09:56:37 +0000 |
---|---|---|
committer | Omar Polo <op@omarpolo.com> | 2023-07-24 09:56:37 +0000 |
commit | 39273368555fdc9eefcc349ae31a0f21efcac6f2 (patch) | |
tree | 06c3ca13fd8c83b50ee96e1431c673bc76d23ce9 /titan.c | |
parent | 56d32bb51aa0b660b4a8d53c3d74f3ea0798a1eb (diff) |
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.
Diffstat (limited to 'titan.c')
-rw-r--r-- | titan.c | 30 |
1 files changed, 26 insertions, 4 deletions
@@ -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); } } } |