1 /* 2 * http_chunk - append response to chunkqueue, possibly in "chunked" encoding 3 * 4 * Fully-rewritten from original 5 * Copyright(c) 2019 Glenn Strauss gstrauss()gluelogic.com All rights reserved 6 * License: BSD 3-clause (same as lighttpd) 7 */ 8 #include "first.h" 9 10 #include "http_chunk.h" 11 #include "base.h" 12 #include "chunk.h" 13 #include "stat_cache.h" 14 #include "fdevent.h" 15 #include "log.h" 16 17 #include <sys/types.h> 18 #include <sys/stat.h> 19 20 #include <stdlib.h> 21 #include <unistd.h> 22 23 #include <errno.h> 24 #include <string.h> 25 26 static void http_chunk_len_append(chunkqueue * const cq, uintmax_t len) { 27 char buf[24]; /* 64-bit (8 bytes) is 16 hex chars (+2 \r\n, +1 \0 = 19) */ 28 #if 0 29 buffer b = { buf, 0, sizeof(buf) }; 30 buffer_append_uint_hex(&b, len); 31 buffer_append_string_len(&b, CONST_STR_LEN("\r\n")); 32 chunkqueue_append_mem(cq, b.ptr, b.used-1); 33 #else 34 int i = (int)(sizeof(buf)); 35 buf[--i] = '\n'; 36 buf[--i] = '\r'; 37 do { buf[--i] = "0123456789abcdef"[len & 0x0F]; } while (len >>= 4); 38 chunkqueue_append_mem(cq, buf+i, sizeof(buf)-i); 39 #endif 40 } 41 42 static int http_chunk_len_append_tempfile(chunkqueue * const cq, uintmax_t len, log_error_st * const errh) { 43 char buf[24]; /* 64-bit (8 bytes) is 16 hex chars (+2 \r\n, +1 \0 = 19) */ 44 #if 0 45 buffer b = { buf, 0, sizeof(buf) }; 46 buffer_append_uint_hex(&b, len); 47 buffer_append_string_len(&b, CONST_STR_LEN("\r\n")); 48 return chunkqueue_append_mem_to_tempfile(cq, b.ptr, b.used-1, errh); 49 #else 50 int i = (int)(sizeof(buf)); 51 buf[--i] = '\n'; 52 buf[--i] = '\r'; 53 do { buf[--i] = "0123456789abcdef"[len & 0x0F]; } while (len >>= 4); 54 return chunkqueue_append_mem_to_tempfile(cq, buf+i, sizeof(buf)-i, errh); 55 #endif 56 } 57 58 static int http_chunk_append_file_open_fstat(const request_st * const r, const buffer * const fn, struct stat * const st) { 59 return 60 (r->conf.follow_symlink 61 || !stat_cache_path_contains_symlink(fn, r->conf.errh)) 62 ? stat_cache_open_rdonly_fstat(fn, st, r->conf.follow_symlink) 63 : -1; 64 } 65 66 static int http_chunk_append_read_fd_range(request_st * const r, const buffer * const fn, const int fd, off_t offset, off_t len) { 67 /* note: this routine should not be used for range requests 68 * unless the total size of ranges requested is small */ 69 /* note: future: could read into existing MEM_CHUNK in cq->last if 70 * there is sufficient space, but would need to adjust for existing 71 * offset in for cq->bytes_in in chunkqueue_append_buffer_commit() */ 72 UNUSED(fn); 73 74 chunkqueue * const cq = r->write_queue; 75 76 if (r->resp_send_chunked) 77 http_chunk_len_append(cq, (uintmax_t)len); 78 79 if (0 != offset && -1 == lseek(fd, offset, SEEK_SET)) return -1; 80 buffer * const b = chunkqueue_append_buffer_open_sz(cq, len+2); 81 ssize_t rd; 82 offset = 0; 83 do { 84 rd = read(fd, b->ptr+offset, len-offset); 85 } while (rd > 0 ? (offset += rd, len -= rd) : errno == EINTR); 86 buffer_commit(b, offset); 87 88 if (r->resp_send_chunked) 89 buffer_append_string_len(b, CONST_STR_LEN("\r\n")); 90 91 chunkqueue_append_buffer_commit(cq); 92 return (rd >= 0) ? 0 : -1; 93 } 94 95 static void http_chunk_append_file_fd_range(request_st * const r, const buffer * const fn, const int fd, const off_t offset, const off_t len) { 96 chunkqueue * const cq = r->write_queue; 97 98 if (r->resp_send_chunked) 99 http_chunk_len_append(cq, (uintmax_t)len); 100 101 chunkqueue_append_file_fd(cq, fn, fd, offset, len); 102 103 if (r->resp_send_chunked) 104 chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n")); 105 } 106 107 int http_chunk_append_file_range(request_st * const r, const buffer * const fn, const off_t offset, off_t len) { 108 struct stat st; 109 const int fd = http_chunk_append_file_open_fstat(r, fn, &st); 110 if (fd < 0) return -1; 111 112 if (-1 == len) { 113 if (offset >= st.st_size) { 114 close(fd); 115 return (offset == st.st_size) ? 0 : -1; 116 } 117 len = st.st_size - offset; 118 } 119 else if (st.st_size - offset < len) { 120 close(fd); 121 return -1; 122 } 123 124 http_chunk_append_file_fd_range(r, fn, fd, offset, len); 125 return 0; 126 } 127 128 int http_chunk_append_file(request_st * const r, const buffer * const fn) { 129 struct stat st; 130 const int fd = http_chunk_append_file_open_fstat(r, fn, &st); 131 if (fd < 0) return -1; 132 http_chunk_append_file_fd(r, fn, fd, st.st_size); 133 return 0; 134 } 135 136 int http_chunk_append_file_fd(request_st * const r, const buffer * const fn, const int fd, const off_t sz) { 137 if (sz > 32768) { 138 http_chunk_append_file_fd_range(r, fn, fd, 0, sz); 139 return 0; 140 } 141 142 /*(read small files into memory)*/ 143 int rc = (0 != sz) ? http_chunk_append_read_fd_range(r,fn,fd,0,sz) : 0; 144 close(fd); 145 return rc; 146 } 147 148 static int http_chunk_append_to_tempfile(request_st * const r, const char * const mem, const size_t len) { 149 chunkqueue * const cq = r->write_queue; 150 log_error_st * const errh = r->conf.errh; 151 152 if (r->resp_send_chunked 153 && 0 != http_chunk_len_append_tempfile(cq, len, errh)) 154 return -1; 155 156 if (0 != chunkqueue_append_mem_to_tempfile(cq, mem, len, errh)) 157 return -1; 158 159 if (r->resp_send_chunked 160 && 0 != 161 chunkqueue_append_mem_to_tempfile(cq, CONST_STR_LEN("\r\n"), errh)) 162 return -1; 163 164 return 0; 165 } 166 167 static int http_chunk_append_cq_to_tempfile(request_st * const r, chunkqueue * const src, const size_t len) { 168 chunkqueue * const cq = r->write_queue; 169 log_error_st * const errh = r->conf.errh; 170 171 if (r->resp_send_chunked 172 && 0 != http_chunk_len_append_tempfile(cq, len, errh)) 173 return -1; 174 175 if (0 != chunkqueue_steal_with_tempfiles(cq, src, len, errh)) 176 return -1; 177 178 if (r->resp_send_chunked 179 && 0 != 180 chunkqueue_append_mem_to_tempfile(cq, CONST_STR_LEN("\r\n"), errh)) 181 return -1; 182 183 return 0; 184 } 185 186 __attribute_pure__ 187 static int http_chunk_uses_tempfile(const request_st * const r, const chunkqueue * const cq, const size_t len) { 188 189 /* current usage does not append_mem or append_buffer after appending 190 * file, so not checking if users of this interface have appended large 191 * (references to) files to chunkqueue, which would not be in memory 192 * (but included in calculation for whether or not to use temp file) */ 193 194 /*(allow slightly larger mem use if FDEVENT_STREAM_RESPONSE_BUFMIN 195 * to reduce creation of temp files when backend producer will be 196 * blocked until more data is sent to network to client)*/ 197 198 const chunk * const c = cq->last; 199 return 200 ((c && c->type == FILE_CHUNK && c->file.is_temp) 201 || cq->bytes_in - cq->bytes_out + len 202 > ((r->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) 203 ? 128*1024 204 : 64*1024)); 205 } 206 207 int http_chunk_append_buffer(request_st * const r, buffer * const mem) { 208 size_t len = buffer_string_length(mem); 209 if (0 == len) return 0; 210 211 chunkqueue * const cq = r->write_queue; 212 213 if (http_chunk_uses_tempfile(r, cq, len)) 214 return http_chunk_append_to_tempfile(r, mem->ptr, len); 215 216 if (r->resp_send_chunked) 217 http_chunk_len_append(cq, len); 218 219 /*(chunkqueue_append_buffer() might steal buffer contents)*/ 220 chunkqueue_append_buffer(cq, mem); 221 222 if (r->resp_send_chunked) 223 chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n")); 224 225 return 0; 226 } 227 228 int http_chunk_append_mem(request_st * const r, const char * const mem, const size_t len) { 229 if (0 == len) return 0; 230 force_assert(NULL != mem); 231 232 chunkqueue * const cq = r->write_queue; 233 234 if (http_chunk_uses_tempfile(r, cq, len)) 235 return http_chunk_append_to_tempfile(r, mem, len); 236 237 if (r->resp_send_chunked) 238 http_chunk_len_append(cq, len); 239 240 chunkqueue_append_mem(cq, mem, len); 241 242 if (r->resp_send_chunked) 243 chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n")); 244 245 return 0; 246 } 247 248 int http_chunk_transfer_cqlen(request_st * const r, chunkqueue * const src, const size_t len) { 249 if (0 == len) return 0; 250 251 chunkqueue * const cq = r->write_queue; 252 253 if (http_chunk_uses_tempfile(r, cq, len)) 254 return http_chunk_append_cq_to_tempfile(r, src, len); 255 256 if (r->resp_send_chunked) 257 http_chunk_len_append(cq, len); 258 259 chunkqueue_steal(cq, src, len); 260 261 if (r->resp_send_chunked) 262 chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n")); 263 264 return 0; 265 } 266 267 void http_chunk_close(request_st * const r) { 268 if (r->resp_send_chunked) 269 chunkqueue_append_mem(r->write_queue, CONST_STR_LEN("0\r\n\r\n")); 270 } 271