Extracted chunk_format function

master
Nicolas Favre-Felix 13 years ago
parent a0b0c0cb30
commit ebe4282d4a

@ -78,12 +78,13 @@ format_send_reply(struct cmd *cmd, const char *p, size_t sz, const char *content
http_response_set_header(resp, "Content-Type", ct);
http_response_set_keep_alive(resp, 1);
http_response_set_header(resp, "Transfer-Encoding", "chunked");
http_response_set_body(resp, p, sz);
http_response_write(resp, cmd->fd);
} else {
/* Asynchronous chunk write. */
http_response_write_chunk(cmd->fd, cmd->w, p, sz);
}
/* Asynchronous chunk write. */
http_response_write_chunk(cmd->fd, cmd->w, p, sz);
} else {
/* compute ETag */
char *etag = etag_new(p, sz);

@ -132,6 +132,24 @@ http_schedule_write(int fd, struct http_response *r) {
}
static char *
format_chunk(const char *p, size_t sz, size_t *out_sz) {
char *out, tmp[64];
int chunk_size;
/* 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);
return out;
}
void
http_response_write(struct http_response *r, int fd) {
@ -191,9 +209,11 @@ http_response_write(struct http_response *r, int fd) {
/* append body if there is one. */
if(r->body && r->body_len) {
r->out = realloc(r->out, r->out_sz + r->body_len);
memcpy(r->out + r->out_sz, r->body, r->body_len);
r->out_sz += r->body_len;
if(r->chunked) {
r->out = realloc(r->out, r->out_sz + r->body_len);
memcpy(r->out + r->out_sz, r->body, r->body_len);
r->out_sz += r->body_len;
}
}
/* send buffer to client */
@ -275,26 +295,13 @@ http_send_options(struct http_client *c) {
void
http_response_write_chunk(int fd, struct worker *w, const char *p, size_t sz) {
char *out, tmp[64];
size_t out_sz;
int chunk_size;
struct http_response *r = http_response_init(w, 0, NULL);
r->keep_alive = 1; /* chunks are always keep-alive */
/* 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);
/* format packet */
r->out = format_chunk(p, sz, &r->out_sz);
/* send async write */
r->out = out;
r->out_sz = out_sz;
http_schedule_write(fd, r);
}

Loading…
Cancel
Save