Browse Source

[core] perf: prepend headers directly into write q

prepend response headers directly into write queue chunkqueue
personal/stbuehler/fix-fdevent
Glenn Strauss 4 years ago
parent
commit
23c72fc606
  1. 27
      src/chunk.c
  2. 4
      src/chunk.h
  3. 5
      src/connections.c
  4. 6
      src/response.c

27
src/chunk.c

@ -271,19 +271,6 @@ void chunkqueue_append_buffer(chunkqueue *cq, buffer *mem) {
chunkqueue_append_chunk(cq, c);
}
void chunkqueue_prepend_buffer(chunkqueue *cq, buffer *mem) {
chunk *c;
if (buffer_string_is_empty(mem)) return;
c = chunkqueue_get_unused_chunk(cq);
c->type = MEM_CHUNK;
force_assert(NULL != c->mem);
buffer_move(c->mem, mem);
chunkqueue_prepend_chunk(cq, c);
}
void chunkqueue_append_mem(chunkqueue *cq, const char * mem, size_t len) {
chunk *c;
@ -315,6 +302,20 @@ void chunkqueue_append_chunkqueue(chunkqueue *cq, chunkqueue *src) {
}
buffer * chunkqueue_prepend_buffer_open(chunkqueue *cq) {
chunk *c = chunkqueue_get_unused_chunk(cq);
c->type = MEM_CHUNK;
chunkqueue_prepend_chunk(cq, c);
buffer_string_prepare_append(c->mem, 4095);
return c->mem;
}
void chunkqueue_prepend_buffer_commit(chunkqueue *cq) {
cq->bytes_in += buffer_string_length(cq->first->mem);
}
void chunkqueue_get_memory(chunkqueue *cq, char **mem, size_t *len, size_t min_size, size_t alloc_size) {
static const size_t REALLOC_MAX_SIZE = 256;
chunk *c;

4
src/chunk.h

@ -60,9 +60,11 @@ void chunkqueue_append_file(chunkqueue *cq, buffer *fn, off_t offset, off_t len)
void chunkqueue_append_file_fd(chunkqueue *cq, buffer *fn, int fd, off_t offset, off_t len); /* copies "fn" */
void chunkqueue_append_mem(chunkqueue *cq, const char *mem, size_t len); /* copies memory */
void chunkqueue_append_buffer(chunkqueue *cq, buffer *mem); /* may reset "mem" */
void chunkqueue_prepend_buffer(chunkqueue *cq, buffer *mem); /* may reset "mem" */
void chunkqueue_append_chunkqueue(chunkqueue *cq, chunkqueue *src);
buffer * chunkqueue_prepend_buffer_open(chunkqueue *cq);
void chunkqueue_prepend_buffer_commit(chunkqueue *cq);
struct server; /*(declaration)*/
int chunkqueue_append_mem_to_tempfile(struct server *srv, chunkqueue *cq, const char *mem, size_t len);

5
src/connections.c

@ -404,11 +404,10 @@ static int connection_handle_write_prepare(server *srv, connection *con) {
con->response.send_chunked = 1;
if (qlen) {
/* create initial Transfer-Encoding: chunked segment */
buffer *b = srv->tmp_chunk_len;
buffer_string_set_length(b, 0);
buffer * const b = chunkqueue_prepend_buffer_open(con->write_queue);
buffer_append_uint_hex(b, (uintmax_t)qlen);
buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
chunkqueue_prepend_buffer(con->write_queue, b);
chunkqueue_prepend_buffer_commit(con->write_queue);
chunkqueue_append_mem(con->write_queue, CONST_STR_LEN("\r\n"));
}
http_header_response_append(con, HTTP_HEADER_TRANSFER_ENCODING, CONST_STR_LEN("Transfer-Encoding"), CONST_STR_LEN("chunked"));

6
src/response.c

@ -24,7 +24,7 @@
#include <time.h>
int http_response_write_header(server *srv, connection *con) {
buffer * const b = buffer_init();
buffer * const b = chunkqueue_prepend_buffer_open(con->write_queue);
if (con->request.http_version == HTTP_VERSION_1_1) {
buffer_copy_string_len(b, CONST_STR_LEN("HTTP/1.1 "));
@ -112,9 +112,7 @@ int http_response_write_header(server *srv, connection *con) {
log_error_write(srv, __FILE__, __LINE__, "sSb", "Response-Header:", "\n", b);
}
chunkqueue_prepend_buffer(con->write_queue, b);
buffer_free(b);
chunkqueue_prepend_buffer_commit(con->write_queue);
return 0;
}

Loading…
Cancel
Save