diff --git a/src/lib/fastcgi.c b/src/lib/fastcgi.c index bcaf82d..a9f62ff 100644 --- a/src/lib/fastcgi.c +++ b/src/lib/fastcgi.c @@ -416,8 +416,17 @@ int fastcgi_header(fastcgi_cnx_t *cnx, http_res *res, char *err_msg) { return 0; } -int fastcgi_dump(fastcgi_cnx_t *cnx, char *buf, long len) { - return sock_recv_x(&cnx->socket, buf, len, 0) == -1 ? -1 : 0; +int fastcgi_dump(fastcgi_cnx_t *cnx, char *buf, const long len) { + for (long ret, rcv = 0; rcv < len; rcv += ret) { + if ((ret = sock_recv_chunk_header(&cnx->out)) == -1) { + return -1; + } + const long min = ret > len - rcv ? len - rcv : ret; + if ((ret = sock_recv_x(&cnx->out, buf + rcv, min, 0)) <= 0) { + return -1; + } + } + return 0; } int fastcgi_receive(fastcgi_cnx_t *cnx, sock *client, unsigned long len) { diff --git a/src/worker/fastcgi_handler.c b/src/worker/fastcgi_handler.c index a8e3a81..48e7bdd 100644 --- a/src/worker/fastcgi_handler.c +++ b/src/worker/fastcgi_handler.c @@ -145,16 +145,17 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t **fcgi_cnx) { content_type != NULL && strstarts(content_type, "text/html") && ctx->content_length != -1 && - ctx->content_length <= sizeof(ctx->msg_content) - 1) + ctx->content_length < sizeof(ctx->msg_content)) { - fastcgi_dump(*fcgi_cnx, ctx->msg_content, sizeof(ctx->msg_content)); + fastcgi_dump(*fcgi_cnx, ctx->msg_content, ctx->content_length); + ctx->msg_content[ctx->content_length] = 0; return 1; } ctx->use_fastcgi = 1; ctx->content_length = -1; - if (http_get_header_field(&res->hdr, "Content-Length") == NULL) { + if (content_length_f == NULL) { http_add_header_field(&res->hdr, "Transfer-Encoding", "chunked"); }