diff --git a/formats/common.c b/formats/common.c index 71b3506..f9dbcd0 100644 --- a/formats/common.c +++ b/formats/common.c @@ -80,8 +80,9 @@ format_send_reply(struct cmd *cmd, const char *p, size_t sz, const char *content http_response_set_header(resp, "Transfer-Encoding", "chunked"); http_response_write(resp, cmd->fd); } - /* FIXME: make this asynchronous. */ - http_response_write_chunk(cmd->fd, p, sz); + + /* Asynchronous chunk write. */ + http_response_write_chunk(cmd->fd, cmd->w, p, sz); } else { /* compute ETag */ diff --git a/http.c b/http.c index 044bae2..89f7ccc 100644 --- a/http.c +++ b/http.c @@ -94,6 +94,8 @@ http_response_cleanup(struct http_response *r, int fd, int success) { free(r->headers[i].val); } free(r->headers); + + free(r); } static void @@ -195,19 +197,6 @@ http_response_write(struct http_response *r, int fd) { /* send buffer to client */ r->sent = 0; http_schedule_write(fd, r); - -#if 0 - p = r->out; - while(r->out_sz) { - ret = write(fd, p, r->out_sz); - if(ret > 0) { /* block */ - r->out_sz -= ret; - p += ret; - } - } - - http_response_cleanup(r, ret); -#endif } static void @@ -281,15 +270,27 @@ http_send_options(struct http_client *c) { * Write HTTP chunk. */ void -http_response_write_chunk(int fd, const char *p, size_t sz) { +http_response_write_chunk(int fd, struct worker *w, const char *p, size_t sz) { - char buf[64]; - int ret, chunk_size; + char *out, tmp[64]; + size_t out_sz; + int chunk_size; + struct http_response *r = http_response_init(w, 0, NULL); - chunk_size = sprintf(buf, "%x\r\n", (int)sz); - ret = write(fd, buf, chunk_size); - ret = write(fd, p, sz); - ret = write(fd, "\r\n", 2); - (void)ret; + /* calculate format size */ + chunk_size = sprintf(tmp, "%x\r\n", (int)sz); + + out_sz = chunk_size + sz + 2; + out = malloc(out_sz); + memcpy(out, tmp, chunk_size); + memcpy(out + chunk_size, p, sz); + memcpy(out + chunk_size + sz, "\r\n", 2); + + + /* send async write */ + r->out = out; + r->out_sz = out_sz; + + http_schedule_write(fd, r); } diff --git a/http.h b/http.h index 8fea4a7..eac1ffe 100644 --- a/http.h +++ b/http.h @@ -67,7 +67,7 @@ void http_send_options(struct http_client *c); void -http_response_write_chunk(int fd, const char *p, size_t sz); +http_response_write_chunk(int fd, struct worker *w, const char *p, size_t sz); void http_response_set_keep_alive(struct http_response *r, int enabled);