commit 39273368555fdc9eefcc349ae31a0f21efcac6f2 from: Omar Polo date: Mon Jul 24 09:56:37 2023 UTC 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. commit - 56d32bb51aa0b660b4a8d53c3d74f3ea0798a1eb commit + 39273368555fdc9eefcc349ae31a0f21efcac6f2 blob - d3b781af8a9a75fa134602ad7c945738b5f63e44 blob + cc5bd218c4bbae6b431cf8ffc00f01b66f47f57e --- titan.c +++ titan.c @@ -188,6 +188,29 @@ open_input_file(int argc, char **argv) err(1, "fseeko"); 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 @@ -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); } } }