Skip to content

Commit

Permalink
Extracted chunk_format function
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasff committed Sep 6, 2011
1 parent a0b0c0c commit ebe4282
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
7 changes: 4 additions & 3 deletions formats/common.c
Expand Up @@ -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);
Expand Down
43 changes: 25 additions & 18 deletions http.c
Expand Up @@ -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) {

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}

0 comments on commit ebe4282

Please sign in to comment.