Make minor corrections to curl example

This makes the code size smaller and correctly uses the firstnonnull()
API which aborts if both arguments are NULL.
This commit is contained in:
Justine Tunney 2021-02-18 20:17:07 -08:00
parent 2c15efc249
commit 45b994ea23

View file

@ -84,14 +84,13 @@ int main(int argc, char *argv[]) {
got = rc; got = rc;
CHECK(startswith(buf, "HTTP/1.1 200"), "%`'.*s", got, buf); CHECK(startswith(buf, "HTTP/1.1 200"), "%`'.*s", got, buf);
CHECK_NOTNULL((crlfcrlf = memmem(buf, got, "\r\n\r\n", 4))); CHECK_NOTNULL((crlfcrlf = memmem(buf, got, "\r\n\r\n", 4)));
need = need = strtol((char *)firstnonnull(
strtol((char *)firstnonnull( memmem(buf, crlfcrlf - buf, "\r\nContent-Length: ", 18),
firstnonnull( firstnonnull(memmem(buf, crlfcrlf - buf,
memmem(buf, crlfcrlf - buf, "\r\nContent-Length: ", 18), "\r\ncontent-length: ", 18),
memmem(buf, crlfcrlf - buf, "\r\ncontent-length: ", 18)), "\r\nContent-Length: -1")) +
"\r\nContent-Length: -1") + 18,
18, NULL, 10);
NULL, 10);
got = MIN(got - (crlfcrlf + 4 - buf), need); got = MIN(got - (crlfcrlf + 4 - buf), need);
CHECK_EQ(got, write(1, crlfcrlf + 4, got)); CHECK_EQ(got, write(1, crlfcrlf + 4, got));
for (toto = got; toto < need; toto += got) { for (toto = got; toto < need; toto += got) {
@ -100,7 +99,7 @@ int main(int argc, char *argv[]) {
got = MIN(got, need - toto); got = MIN(got, need - toto);
CHECK_EQ(got, write(1, buf, got)); CHECK_EQ(got, write(1, buf, got));
} }
LOGIFNEG1(close(sock)); close(sock);
break; break;
} }
return 0; return 0;