version 1.1, 2013/10/14 10:32:47
|
version 1.1.1.2, 2016/11/02 10:35:00
|
Line 1
|
Line 1
|
|
#include "first.h" |
|
|
/** |
/** |
* the HTTP chunk-API |
* the HTTP chunk-API |
* |
* |
Line 7
|
Line 9
|
#include "server.h" |
#include "server.h" |
#include "chunk.h" |
#include "chunk.h" |
#include "http_chunk.h" |
#include "http_chunk.h" |
|
#include "stat_cache.h" |
#include "log.h" |
#include "log.h" |
|
|
#include <sys/types.h> |
#include <sys/types.h> |
Line 20
|
Line 23
|
#include <errno.h> |
#include <errno.h> |
#include <string.h> |
#include <string.h> |
|
|
static int http_chunk_append_len(server *srv, connection *con, size_t len) { | static void http_chunk_append_len(server *srv, connection *con, uintmax_t len) { |
size_t i, olen = len, j; | |
buffer *b; |
buffer *b; |
|
|
|
force_assert(NULL != srv); |
|
|
b = srv->tmp_chunk_len; |
b = srv->tmp_chunk_len; |
|
|
if (len == 0) { | buffer_string_set_length(b, 0); |
buffer_copy_string_len(b, CONST_STR_LEN("0")); | buffer_append_uint_hex(b, len); |
} else { | buffer_append_string_len(b, CONST_STR_LEN("\r\n")); |
for (i = 0; i < 8 && len; i++) { | |
len >>= 4; | |
} | |
|
|
/* i is the number of hex digits we have */ | chunkqueue_append_buffer(con->write_queue, b); |
buffer_prepare_copy(b, i + 1); | } |
|
|
for (j = i-1, len = olen; j+1 > 0; j--) { | static int http_chunk_append_file_open_fstat(server *srv, connection *con, buffer *fn, struct stat *st) { |
b->ptr[j] = (len & 0xf) + (((len & 0xf) <= 9) ? '0' : 'a' - 10); | if (!con->conf.follow_symlink) { |
len >>= 4; | /*(preserve existing stat_cache symlink checks)*/ |
} | stat_cache_entry *sce; |
b->used = i; | if (HANDLER_ERROR == stat_cache_get_entry(srv, con, fn, &sce)) return -1; |
b->ptr[b->used++] = '\0'; | |
} |
} |
|
|
buffer_append_string_len(b, CONST_STR_LEN("\r\n")); | return stat_cache_open_rdonly_fstat(srv, con, fn, st); |
chunkqueue_append_buffer(con->write_queue, b); | |
| |
return 0; | |
} |
} |
|
|
|
static void http_chunk_append_file_fd_range(server *srv, connection *con, buffer *fn, int fd, off_t offset, off_t len) { |
|
chunkqueue *cq = con->write_queue; |
|
|
int http_chunk_append_file(server *srv, connection *con, buffer *fn, off_t offset, off_t len) { | if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) { |
chunkqueue *cq; | http_chunk_append_len(srv, con, (uintmax_t)len); |
| } |
|
|
if (!con) return -1; | chunkqueue_append_file_fd(cq, fn, fd, offset, len); |
|
|
cq = con->write_queue; |
|
|
|
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) { |
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) { |
http_chunk_append_len(srv, con, len); | chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n")); |
} |
} |
|
} |
|
|
chunkqueue_append_file(cq, fn, offset, len); | int http_chunk_append_file_range(server *srv, connection *con, buffer *fn, off_t offset, off_t len) { |
| struct stat st; |
| const int fd = http_chunk_append_file_open_fstat(srv, con, fn, &st); |
| if (fd < 0) return -1; |
|
|
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED && len > 0) { | if (-1 == len) { |
chunkqueue_append_mem(cq, "\r\n", 2 + 1); | if (offset >= st.st_size) { |
| close(fd); |
| return (offset == st.st_size) ? 0 : -1; |
| } |
| len = st.st_size - offset; |
| } else if (st.st_size - offset < len) { |
| close(fd); |
| return -1; |
} |
} |
|
|
|
http_chunk_append_file_fd_range(srv, con, fn, fd, offset, len); |
return 0; |
return 0; |
} |
} |
|
|
int http_chunk_append_buffer(server *srv, connection *con, buffer *mem) { | int http_chunk_append_file(server *srv, connection *con, buffer *fn) { |
chunkqueue *cq; | struct stat st; |
| const int fd = http_chunk_append_file_open_fstat(srv, con, fn, &st); |
| if (fd < 0) return -1; |
|
|
if (!con) return -1; | if (0 != st.st_size) { |
| http_chunk_append_file_fd_range(srv, con, fn, fd, 0, st.st_size); |
| } else { |
| close(fd); |
| } |
| return 0; |
| } |
|
|
cq = con->write_queue; | static int http_chunk_append_to_tempfile(server *srv, connection *con, const char * mem, size_t len) { |
| chunkqueue * const cq = con->write_queue; |
|
|
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) { |
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) { |
http_chunk_append_len(srv, con, mem->used - 1); | /*http_chunk_append_len(srv, con, len);*/ |
| buffer *b = srv->tmp_chunk_len; |
| |
| buffer_string_set_length(b, 0); |
| buffer_append_uint_hex(b, len); |
| buffer_append_string_len(b, CONST_STR_LEN("\r\n")); |
| |
| if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, CONST_BUF_LEN(b))) { |
| return -1; |
| } |
} |
} |
|
|
chunkqueue_append_buffer(cq, mem); | if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, mem, len)) { |
| return -1; |
| } |
|
|
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED && mem->used > 0) { | if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) { |
chunkqueue_append_mem(cq, "\r\n", 2 + 1); | if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, CONST_STR_LEN("\r\n"))) { |
| return -1; |
| } |
} |
} |
|
|
return 0; |
return 0; |
} |
} |
|
|
int http_chunk_append_mem(server *srv, connection *con, const char * mem, size_t len) { | static int http_chunk_append_data(server *srv, connection *con, buffer *b, const char * mem, size_t len) { |
chunkqueue *cq; | |
|
|
if (!con) return -1; | chunkqueue * const cq = con->write_queue; |
| chunk *c = cq->last; |
| if (0 == len) return 0; |
|
|
cq = con->write_queue; | /* current usage does not append_mem or append_buffer after appending |
| * file, so not checking if users of this interface have appended large |
| * (references to) files to chunkqueue, which would not be in memory */ |
|
|
if (len == 0) { | /*(allow slightly larger mem use if FDEVENT_STREAM_RESPONSE_BUFMIN |
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) { | * to reduce creation of temp files when backend producer will be |
chunkqueue_append_mem(cq, "0\r\n\r\n", 5 + 1); | * blocked until more data is sent to network to client)*/ |
} else { | |
chunkqueue_append_mem(cq, "", 1); | if ((c && c->type == FILE_CHUNK && c->file.is_temp) |
} | || cq->bytes_in - cq->bytes_out + len |
return 0; | > 1024 * ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) ? 128 : 64)) { |
| return http_chunk_append_to_tempfile(srv, con, b ? b->ptr : mem, len); |
} |
} |
|
|
|
/* not appending to prior mem chunk just in case using openssl |
|
* and need to resubmit same args as prior call to openssl (required?)*/ |
|
|
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) { |
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) { |
http_chunk_append_len(srv, con, len - 1); | http_chunk_append_len(srv, con, len); |
} |
} |
|
|
chunkqueue_append_mem(cq, mem, len); | /*(chunkqueue_append_buffer() might steal buffer contents)*/ |
| b ? chunkqueue_append_buffer(cq, b) : chunkqueue_append_mem(cq, mem, len); |
|
|
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) { |
if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) { |
chunkqueue_append_mem(cq, "\r\n", 2 + 1); | chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n")); |
} |
} |
|
|
return 0; |
return 0; |
} |
} |
|
|
|
int http_chunk_append_buffer(server *srv, connection *con, buffer *mem) { |
|
force_assert(NULL != con); |
|
|
off_t http_chunkqueue_length(server *srv, connection *con) { | return http_chunk_append_data(srv, con, mem, NULL, buffer_string_length(mem)); |
if (!con) { | } |
log_error_write(srv, __FILE__, __LINE__, "s", "connection is NULL!!"); | |
|
|
return 0; | int http_chunk_append_mem(server *srv, connection *con, const char * mem, size_t len) { |
} | force_assert(NULL != con); |
| force_assert(NULL != mem || 0 == len); |
|
|
return chunkqueue_length(con->write_queue); | return http_chunk_append_data(srv, con, NULL, mem, len); |
| } |
| |
| void http_chunk_close(server *srv, connection *con) { |
| UNUSED(srv); |
| force_assert(NULL != con); |
| |
| if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) { |
| chunkqueue_append_mem(con->write_queue, CONST_STR_LEN("0\r\n\r\n")); |
| } |
} |
} |