commit 5ce4c55cc90bdd51007f40008c5044d451c6ea09 from: Omar Polo date: Sat Sep 02 07:55:29 2023 UTC amused-web: simplify http_reply commit - 20dff2d28b240611ed334328b1279305169e5821 commit + 5ce4c55cc90bdd51007f40008c5044d451c6ea09 blob - 7ce53ce038aa1fe95ae993216a80c7a7af157365 blob + 6f9ed2e503f1d40a0a2ccc237fdaeb2faa9f056a --- web/http.c +++ web/http.c @@ -188,7 +188,6 @@ int http_reply(struct client *clt, int code, const char *reason, const char *ctype) { const char *version, *location = NULL; - int r; log_debug("> %d %s", code, reason); @@ -201,25 +200,22 @@ http_reply(struct client *clt, int code, const char *r if (clt->req.version == HTTP_1_0) version = "HTTP/1.0"; - r = bufio_compose_fmt(&clt->bio, "%s %d %s\r\n" + if (bufio_compose_fmt(&clt->bio, "%s %d %s\r\n" "Connection: close\r\n" - "Cache-Control: no-store\r\n" - "%s%s%s" - "%s%s%s" - "%s" - "\r\n", - version, code, reason, - ctype == NULL ? "" : "Content-Type: ", - ctype == NULL ? "" : ctype, - ctype == NULL ? "" : "\r\n", - location == NULL ? "" : "Location: ", - location == NULL ? "" : location, - location == NULL ? "" : "\r\n", - clt->chunked ? "Transfer-Encoding: chunked\r\n" : ""); - if (r == -1) { - clt->err = 1; - return -1; - } + "Cache-Control: no-store\r\n", + version, code, reason) == -1) + goto err; + if (ctype != NULL && + bufio_compose_fmt(&clt->bio, "Content-Type: %s\r\n", ctype) == -1) + goto err; + if (location != NULL && + bufio_compose_fmt(&clt->bio, "Location: %s\r\n", location) == -1) + goto err; + if (clt->chunked && bufio_compose_str(&clt->bio, + "Transfer-Encoding: chunked\r\n") == -1) + goto err; + if (bufio_compose(&clt->bio, "\r\n", 2) == -1) + goto err; bufio_set_chunked(&clt->bio, clt->chunked); @@ -233,6 +229,10 @@ http_reply(struct client *clt, int code, const char *r } return 0; + + err: + clt->err = 1; + return -1; } int