[core] consolidate backend network write handlers
[lighttpd.git] / src / http_chunk.c
blob1557c8815e2df4a22497a0b365fb5304f3c379a2
1 #include "first.h"
3 /**
4 * the HTTP chunk-API
7 */
9 #include "server.h"
10 #include "chunk.h"
11 #include "http_chunk.h"
12 #include "stat_cache.h"
13 #include "fdevent.h"
14 #include "log.h"
16 #include <sys/types.h>
17 #include <sys/stat.h>
19 #include <stdlib.h>
20 #include <unistd.h>
22 #include <errno.h>
23 #include <string.h>
25 static void http_chunk_append_len(server *srv, connection *con, uintmax_t len) {
26 buffer *b;
28 force_assert(NULL != srv);
30 b = srv->tmp_chunk_len;
32 buffer_string_set_length(b, 0);
33 buffer_append_uint_hex(b, len);
34 buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
36 chunkqueue_append_buffer(con->write_queue, b);
39 static int http_chunk_append_file_open_fstat(server *srv, connection *con, buffer *fn, struct stat *st) {
40 if (!con->conf.follow_symlink) {
41 /*(preserve existing stat_cache symlink checks)*/
42 stat_cache_entry *sce;
43 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, fn, &sce)) return -1;
46 return stat_cache_open_rdonly_fstat(srv, con, fn, st);
49 static void http_chunk_append_file_fd_range(server *srv, connection *con, buffer *fn, int fd, off_t offset, off_t len) {
50 chunkqueue *cq = con->write_queue;
52 if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
53 http_chunk_append_len(srv, con, (uintmax_t)len);
56 chunkqueue_append_file_fd(cq, fn, fd, offset, len);
58 if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
59 chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
63 int http_chunk_append_file_range(server *srv, connection *con, buffer *fn, off_t offset, off_t len) {
64 struct stat st;
65 const int fd = http_chunk_append_file_open_fstat(srv, con, fn, &st);
66 if (fd < 0) return -1;
68 if (-1 == len) {
69 if (offset >= st.st_size) {
70 close(fd);
71 return (offset == st.st_size) ? 0 : -1;
73 len = st.st_size - offset;
74 } else if (st.st_size - offset < len) {
75 close(fd);
76 return -1;
79 http_chunk_append_file_fd_range(srv, con, fn, fd, offset, len);
80 return 0;
83 int http_chunk_append_file(server *srv, connection *con, buffer *fn) {
84 struct stat st;
85 const int fd = http_chunk_append_file_open_fstat(srv, con, fn, &st);
86 if (fd < 0) return -1;
88 if (0 != st.st_size) {
89 http_chunk_append_file_fd_range(srv, con, fn, fd, 0, st.st_size);
90 } else {
91 close(fd);
93 return 0;
96 static int http_chunk_append_to_tempfile(server *srv, connection *con, const char * mem, size_t len) {
97 chunkqueue * const cq = con->write_queue;
99 if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
100 /*http_chunk_append_len(srv, con, len);*/
101 buffer *b = srv->tmp_chunk_len;
103 buffer_string_set_length(b, 0);
104 buffer_append_uint_hex(b, len);
105 buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
107 if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, CONST_BUF_LEN(b))) {
108 return -1;
112 if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, mem, len)) {
113 return -1;
116 if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
117 if (0 != chunkqueue_append_mem_to_tempfile(srv, cq, CONST_STR_LEN("\r\n"))) {
118 return -1;
122 return 0;
125 static int http_chunk_append_data(server *srv, connection *con, buffer *b, const char * mem, size_t len) {
127 chunkqueue * const cq = con->write_queue;
128 chunk *c = cq->last;
129 if (0 == len) return 0;
131 /* current usage does not append_mem or append_buffer after appending
132 * file, so not checking if users of this interface have appended large
133 * (references to) files to chunkqueue, which would not be in memory */
135 /*(allow slightly larger mem use if FDEVENT_STREAM_RESPONSE_BUFMIN
136 * to reduce creation of temp files when backend producer will be
137 * blocked until more data is sent to network to client)*/
139 if ((c && c->type == FILE_CHUNK && c->file.is_temp)
140 || cq->bytes_in - cq->bytes_out + len
141 > 1024 * ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) ? 128 : 64)) {
142 return http_chunk_append_to_tempfile(srv, con, b ? b->ptr : mem, len);
145 /* not appending to prior mem chunk just in case using openssl
146 * and need to resubmit same args as prior call to openssl (required?)*/
148 if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
149 http_chunk_append_len(srv, con, len);
152 /*(chunkqueue_append_buffer() might steal buffer contents)*/
153 b ? chunkqueue_append_buffer(cq, b) : chunkqueue_append_mem(cq, mem, len);
155 if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
156 chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
159 return 0;
162 int http_chunk_append_buffer(server *srv, connection *con, buffer *mem) {
163 force_assert(NULL != con);
165 return http_chunk_append_data(srv, con, mem, NULL, buffer_string_length(mem));
168 int http_chunk_append_mem(server *srv, connection *con, const char * mem, size_t len) {
169 force_assert(NULL != con);
170 force_assert(NULL != mem || 0 == len);
172 return http_chunk_append_data(srv, con, NULL, mem, len);
175 void http_chunk_close(server *srv, connection *con) {
176 UNUSED(srv);
177 force_assert(NULL != con);
179 if (con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) {
180 chunkqueue_append_mem(con->write_queue, CONST_STR_LEN("0\r\n\r\n"));