Annotation of embedaddon/lighttpd/src/http_chunk.c, revision 1.1.1.2
1.1.1.2 ! misho 1: #include "first.h"
! 2:
1.1 misho 3: /**
4: * the HTTP chunk-API
5: *
6: *
7: */
8:
9: #include "server.h"
10: #include "chunk.h"
11: #include "http_chunk.h"
1.1.1.2 ! misho 12: #include "stat_cache.h"
1.1 misho 13: #include "log.h"
14:
15: #include <sys/types.h>
16: #include <sys/stat.h>
17:
18: #include <stdlib.h>
19: #include <fcntl.h>
20: #include <unistd.h>
21:
22: #include <stdio.h>
23: #include <errno.h>
24: #include <string.h>
25:
1.1.1.2 ! misho 26: static void http_chunk_append_len(server *srv, connection *con, uintmax_t len) {
1.1 misho 27: buffer *b;
28:
1.1.1.2 ! misho 29: force_assert(NULL != srv);
! 30:
1.1 misho 31: b = srv->tmp_chunk_len;
32:
1.1.1.2 ! misho 33: buffer_string_set_length(b, 0);
! 34: buffer_append_uint_hex(b, len);
! 35: buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
1.1 misho 36:
1.1.1.2 ! misho 37: chunkqueue_append_buffer(con->write_queue, b);
! 38: }
1.1 misho 39:
1.1.1.2 ! misho 40: static int http_chunk_append_file_open_fstat(server *srv, connection *con, buffer *fn, struct stat *st) {
! 41: if (!con->conf.follow_symlink) {
! 42: /*(preserve existing stat_cache symlink checks)*/
! 43: stat_cache_entry *sce;
! 44: if (HANDLER_ERROR == stat_cache_get_entry(srv, con, fn, &sce)) return -1;
1.1 misho 45: }
46:
1.1.1.2 ! misho 47: return stat_cache_open_rdonly_fstat(srv, con, fn, st);
1.1 misho 48: }
49:
1.1.1.2 ! misho 50: static void http_chunk_append_file_fd_range(server *srv, connection *con, buffer *fn, int fd, off_t offset, off_t len) {
! 51: chunkqueue *cq = con->write_queue;
1.1 misho 52:
1.1.1.2 ! misho 53: if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
! 54: http_chunk_append_len(srv, con, (uintmax_t)len);
! 55: }
1.1 misho 56:
1.1.1.2 ! misho 57: chunkqueue_append_file_fd(cq, fn, fd, offset, len);
1.1 misho 58:
59: if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
1.1.1.2 ! misho 60: chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
1.1 misho 61: }
1.1.1.2 ! misho 62: }
1.1 misho 63:
1.1.1.2 ! misho 64: int http_chunk_append_file_range(server *srv, connection *con, buffer *fn, off_t offset, off_t len) {
! 65: struct stat st;
! 66: const int fd = http_chunk_append_file_open_fstat(srv, con, fn, &st);
! 67: if (fd < 0) return -1;
! 68:
! 69: if (-1 == len) {
! 70: if (offset >= st.st_size) {
! 71: close(fd);
! 72: return (offset == st.st_size) ? 0 : -1;
! 73: }
! 74: len = st.st_size - offset;
! 75: } else if (st.st_size - offset < len) {
! 76: close(fd);
! 77: return -1;
1.1 misho 78: }
79:
1.1.1.2 ! misho 80: http_chunk_append_file_fd_range(srv, con, fn, fd, offset, len);
1.1 misho 81: return 0;
82: }
83:
1.1.1.2 ! misho 84: int http_chunk_append_file(server *srv, connection *con, buffer *fn) {
! 85: struct stat st;
! 86: const int fd = http_chunk_append_file_open_fstat(srv, con, fn, &st);
! 87: if (fd < 0) return -1;
1.1 misho 88:
1.1.1.2 ! misho 89: if (0 != st.st_size) {
! 90: http_chunk_append_file_fd_range(srv, con, fn, fd, 0, st.st_size);
! 91: } else {
! 92: close(fd);
! 93: }
! 94: return 0;
! 95: }
1.1 misho 96:
1.1.1.2 ! misho 97: static int http_chunk_append_to_tempfile(server *srv, connection *con, const char * mem, size_t len) {
! 98: chunkqueue * const cq = con->write_queue;
1.1 misho 99:
100: if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
1.1.1.2 ! misho 101: /*http_chunk_append_len(srv, con, len);*/
! 102: buffer *b = srv->tmp_chunk_len;
! 103:
! 104: buffer_string_set_length(b, 0);
! 105: buffer_append_uint_hex(b, len);
! 106: buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
! 107:
! 108: if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, CONST_BUF_LEN(b))) {
! 109: return -1;
! 110: }
1.1 misho 111: }
112:
1.1.1.2 ! misho 113: if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, mem, len)) {
! 114: return -1;
! 115: }
1.1 misho 116:
1.1.1.2 ! misho 117: if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
! 118: if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, CONST_STR_LEN("\r\n"))) {
! 119: return -1;
! 120: }
1.1 misho 121: }
122:
123: return 0;
124: }
125:
1.1.1.2 ! misho 126: static int http_chunk_append_data(server *srv, connection *con, buffer *b, const char * mem, size_t len) {
1.1 misho 127:
1.1.1.2 ! misho 128: chunkqueue * const cq = con->write_queue;
! 129: chunk *c = cq->last;
! 130: if (0 == len) return 0;
1.1 misho 131:
1.1.1.2 ! misho 132: /* current usage does not append_mem or append_buffer after appending
! 133: * file, so not checking if users of this interface have appended large
! 134: * (references to) files to chunkqueue, which would not be in memory */
1.1 misho 135:
1.1.1.2 ! misho 136: /*(allow slightly larger mem use if FDEVENT_STREAM_RESPONSE_BUFMIN
! 137: * to reduce creation of temp files when backend producer will be
! 138: * blocked until more data is sent to network to client)*/
! 139:
! 140: if ((c && c->type == FILE_CHUNK && c->file.is_temp)
! 141: || cq->bytes_in - cq->bytes_out + len
! 142: > 1024 * ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) ? 128 : 64)) {
! 143: return http_chunk_append_to_tempfile(srv, con, b ? b->ptr : mem, len);
1.1 misho 144: }
145:
1.1.1.2 ! misho 146: /* not appending to prior mem chunk just in case using openssl
! 147: * and need to resubmit same args as prior call to openssl (required?)*/
! 148:
1.1 misho 149: if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
1.1.1.2 ! misho 150: http_chunk_append_len(srv, con, len);
1.1 misho 151: }
152:
1.1.1.2 ! misho 153: /*(chunkqueue_append_buffer() might steal buffer contents)*/
! 154: b ? chunkqueue_append_buffer(cq, b) : chunkqueue_append_mem(cq, mem, len);
1.1 misho 155:
156: if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
1.1.1.2 ! misho 157: chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
1.1 misho 158: }
159:
160: return 0;
161: }
162:
1.1.1.2 ! misho 163: int http_chunk_append_buffer(server *srv, connection *con, buffer *mem) {
! 164: force_assert(NULL != con);
1.1 misho 165:
1.1.1.2 ! misho 166: return http_chunk_append_data(srv, con, mem, NULL, buffer_string_length(mem));
! 167: }
1.1 misho 168:
1.1.1.2 ! misho 169: int http_chunk_append_mem(server *srv, connection *con, const char * mem, size_t len) {
! 170: force_assert(NULL != con);
! 171: force_assert(NULL != mem || 0 == len);
1.1 misho 172:
1.1.1.2 ! misho 173: return http_chunk_append_data(srv, con, NULL, mem, len);
! 174: }
! 175:
! 176: void http_chunk_close(server *srv, connection *con) {
! 177: UNUSED(srv);
! 178: force_assert(NULL != con);
! 179:
! 180: if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
! 181: chunkqueue_append_mem(con->write_queue, CONST_STR_LEN("0\r\n\r\n"));
! 182: }
1.1 misho 183: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>