1c18f442aSGlenn Strauss /*
2c18f442aSGlenn Strauss * http_chunk - append response to chunkqueue, possibly in "chunked" encoding
3bcdc6a3bSJan Kneschke *
4c18f442aSGlenn Strauss * Fully-rewritten from original
5c18f442aSGlenn Strauss * Copyright(c) 2019 Glenn Strauss gstrauss()gluelogic.com All rights reserved
6c18f442aSGlenn Strauss * License: BSD 3-clause (same as lighttpd)
7bcdc6a3bSJan Kneschke */
8c18f442aSGlenn Strauss #include "first.h"
9bcdc6a3bSJan Kneschke
100fcd5143SGlenn Strauss #include "http_chunk.h"
1122e8b456SStefan Bühler #include "chunk.h"
12a65c57a5SGlenn Strauss #include "stat_cache.h"
13ba953cdfSGlenn Strauss #include "fdevent.h"
1422e8b456SStefan Bühler #include "log.h"
1567c0b149SGlenn Strauss #include "request.h"
1622e8b456SStefan Bühler
17bcdc6a3bSJan Kneschke #include <sys/types.h>
18bcdc6a3bSJan Kneschke #include <sys/stat.h>
19bcdc6a3bSJan Kneschke
20bcdc6a3bSJan Kneschke #include <stdlib.h>
21bcdc6a3bSJan Kneschke #include <unistd.h>
22bcdc6a3bSJan Kneschke
23bcdc6a3bSJan Kneschke #include <errno.h>
24bcdc6a3bSJan Kneschke #include <string.h>
25bcdc6a3bSJan Kneschke
261ca721d4SGlenn Strauss __attribute_noinline__
http_chunk_len_append(chunkqueue * const cq,uintmax_t len)270fcd5143SGlenn Strauss static void http_chunk_len_append(chunkqueue * const cq, uintmax_t len) {
280fcd5143SGlenn Strauss char buf[24]; /* 64-bit (8 bytes) is 16 hex chars (+2 \r\n, +1 \0 = 19) */
290fcd5143SGlenn Strauss #if 0
300fcd5143SGlenn Strauss buffer b = { buf, 0, sizeof(buf) };
310fcd5143SGlenn Strauss buffer_append_uint_hex(&b, len);
320fcd5143SGlenn Strauss buffer_append_string_len(&b, CONST_STR_LEN("\r\n"));
330fcd5143SGlenn Strauss chunkqueue_append_mem(cq, b.ptr, b.used-1);
340fcd5143SGlenn Strauss #else
350fcd5143SGlenn Strauss int i = (int)(sizeof(buf));
360fcd5143SGlenn Strauss buf[--i] = '\n';
370fcd5143SGlenn Strauss buf[--i] = '\r';
380fcd5143SGlenn Strauss do { buf[--i] = "0123456789abcdef"[len & 0x0F]; } while (len >>= 4);
390fcd5143SGlenn Strauss chunkqueue_append_mem(cq, buf+i, sizeof(buf)-i);
400fcd5143SGlenn Strauss #endif
41c79bc316SGlenn Strauss }
42bcdc6a3bSJan Kneschke
431ca721d4SGlenn Strauss __attribute_noinline__
http_chunk_len_append_tempfile(chunkqueue * const cq,uintmax_t len,log_error_st * const errh)440fcd5143SGlenn Strauss static int http_chunk_len_append_tempfile(chunkqueue * const cq, uintmax_t len, log_error_st * const errh) {
450fcd5143SGlenn Strauss char buf[24]; /* 64-bit (8 bytes) is 16 hex chars (+2 \r\n, +1 \0 = 19) */
460fcd5143SGlenn Strauss #if 0
470fcd5143SGlenn Strauss buffer b = { buf, 0, sizeof(buf) };
480fcd5143SGlenn Strauss buffer_append_uint_hex(&b, len);
490fcd5143SGlenn Strauss buffer_append_string_len(&b, CONST_STR_LEN("\r\n"));
500fcd5143SGlenn Strauss return chunkqueue_append_mem_to_tempfile(cq, b.ptr, b.used-1, errh);
510fcd5143SGlenn Strauss #else
520fcd5143SGlenn Strauss int i = (int)(sizeof(buf));
530fcd5143SGlenn Strauss buf[--i] = '\n';
540fcd5143SGlenn Strauss buf[--i] = '\r';
550fcd5143SGlenn Strauss do { buf[--i] = "0123456789abcdef"[len & 0x0F]; } while (len >>= 4);
560fcd5143SGlenn Strauss return chunkqueue_append_mem_to_tempfile(cq, buf+i, sizeof(buf)-i, errh);
570fcd5143SGlenn Strauss #endif
58bcdc6a3bSJan Kneschke }
59bcdc6a3bSJan Kneschke
601ca721d4SGlenn Strauss __attribute_noinline__
http_chunk_append_read_fd_range(request_st * const r,const buffer * const fn,const int fd,off_t offset,off_t len)617c7f8c46SGlenn Strauss 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) {
62a86ea83bSGlenn Strauss /* note: this routine should not be used for range requests
63a86ea83bSGlenn Strauss * unless the total size of ranges requested is small */
64a86ea83bSGlenn Strauss /* note: future: could read into existing MEM_CHUNK in cq->last if
65a86ea83bSGlenn Strauss * there is sufficient space, but would need to adjust for existing
66a86ea83bSGlenn Strauss * offset in for cq->bytes_in in chunkqueue_append_buffer_commit() */
67a86ea83bSGlenn Strauss UNUSED(fn);
68a86ea83bSGlenn Strauss
6981029b8bSGlenn Strauss chunkqueue * const cq = &r->write_queue;
700fcd5143SGlenn Strauss
717c7f8c46SGlenn Strauss if (r->resp_send_chunked)
720fcd5143SGlenn Strauss http_chunk_len_append(cq, (uintmax_t)len);
73a86ea83bSGlenn Strauss
749c25581dSGlenn Strauss buffer * const b = chunkqueue_append_buffer_open_sz(cq, len+2+1);
75a86ea83bSGlenn Strauss ssize_t rd;
76042622c8SGlenn Strauss const off_t foff = offset;
77a86ea83bSGlenn Strauss offset = 0;
78a86ea83bSGlenn Strauss do {
79*2d1b1672SGlenn Strauss rd = chunk_file_pread(fd, b->ptr+offset, (size_t)len, foff+offset);
80*2d1b1672SGlenn Strauss } while (rd > 0 && (offset += rd, len -= rd));
81a86ea83bSGlenn Strauss buffer_commit(b, offset);
82a86ea83bSGlenn Strauss
837c7f8c46SGlenn Strauss if (r->resp_send_chunked)
84a86ea83bSGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
85a86ea83bSGlenn Strauss
86a86ea83bSGlenn Strauss chunkqueue_append_buffer_commit(cq);
87*2d1b1672SGlenn Strauss return (len == 0) ? 0 : -1;
88a86ea83bSGlenn Strauss }
89a86ea83bSGlenn Strauss
901ca721d4SGlenn Strauss __attribute_noinline__
http_chunk_append_file_ref_range(request_st * const r,stat_cache_entry * const sce,const off_t offset,off_t len)911ce82209SGlenn Strauss void http_chunk_append_file_ref_range(request_st * const r, stat_cache_entry * const sce, const off_t offset, off_t len) {
929078cc4cSGlenn Strauss chunkqueue * const cq = &r->write_queue;
939078cc4cSGlenn Strauss
941ce82209SGlenn Strauss if (sce->st.st_size - offset < len)
951ce82209SGlenn Strauss len = sce->st.st_size - offset;
961ce82209SGlenn Strauss if (len <= 0)
971ce82209SGlenn Strauss return;
981ce82209SGlenn Strauss
999078cc4cSGlenn Strauss if (r->resp_send_chunked)
1009078cc4cSGlenn Strauss http_chunk_len_append(cq, (uintmax_t)len);
1019078cc4cSGlenn Strauss
1029078cc4cSGlenn Strauss const buffer * const fn = &sce->name;
1039078cc4cSGlenn Strauss const int fd = sce->fd;
1049078cc4cSGlenn Strauss chunkqueue_append_file_fd(cq, fn, fd, offset, len);
1059078cc4cSGlenn Strauss if (fd >= 0) {
1069078cc4cSGlenn Strauss chunk * const d = cq->last;
1079078cc4cSGlenn Strauss d->file.ref = sce;
1089078cc4cSGlenn Strauss d->file.refchg = stat_cache_entry_refchg;
1099078cc4cSGlenn Strauss stat_cache_entry_refchg(sce, 1);
1109078cc4cSGlenn Strauss }
1119078cc4cSGlenn Strauss
1129078cc4cSGlenn Strauss if (r->resp_send_chunked)
1139078cc4cSGlenn Strauss chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
1149078cc4cSGlenn Strauss }
1159078cc4cSGlenn Strauss
1161ca721d4SGlenn Strauss __attribute_noinline__
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)117e9912607SGlenn Strauss 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) {
11881029b8bSGlenn Strauss chunkqueue * const cq = &r->write_queue;
119a65c57a5SGlenn Strauss
1207c7f8c46SGlenn Strauss if (r->resp_send_chunked)
1210fcd5143SGlenn Strauss http_chunk_len_append(cq, (uintmax_t)len);
122a65c57a5SGlenn Strauss
123a65c57a5SGlenn Strauss chunkqueue_append_file_fd(cq, fn, fd, offset, len);
124bcdc6a3bSJan Kneschke
1257c7f8c46SGlenn Strauss if (r->resp_send_chunked)
1266afad87dSStefan Bühler chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
1276afad87dSStefan Bühler }
128bcdc6a3bSJan Kneschke
http_chunk_append_file_fd(request_st * const r,const buffer * const fn,const int fd,const off_t sz)1297c7f8c46SGlenn Strauss int http_chunk_append_file_fd(request_st * const r, const buffer * const fn, const int fd, const off_t sz) {
1305fd8a26aSGlenn Strauss if (sz > 32768 || !r->resp_send_chunked) {
1317c7f8c46SGlenn Strauss http_chunk_append_file_fd_range(r, fn, fd, 0, sz);
132a86ea83bSGlenn Strauss return 0;
1330fcd5143SGlenn Strauss }
1340fcd5143SGlenn Strauss
1350fcd5143SGlenn Strauss /*(read small files into memory)*/
1367c7f8c46SGlenn Strauss int rc = (0 != sz) ? http_chunk_append_read_fd_range(r,fn,fd,0,sz) : 0;
137a65c57a5SGlenn Strauss close(fd);
138a86ea83bSGlenn Strauss return rc;
139a65c57a5SGlenn Strauss }
140a65c57a5SGlenn Strauss
http_chunk_append_file_ref(request_st * const r,stat_cache_entry * const sce)1419078cc4cSGlenn Strauss int http_chunk_append_file_ref(request_st * const r, stat_cache_entry * const sce) {
1429078cc4cSGlenn Strauss const off_t sz = sce->st.st_size;
1439078cc4cSGlenn Strauss if (sz > 32768 || !r->resp_send_chunked) {
1449078cc4cSGlenn Strauss http_chunk_append_file_ref_range(r, sce, 0, sz);
1459078cc4cSGlenn Strauss return 0;
1469078cc4cSGlenn Strauss }
1479078cc4cSGlenn Strauss
1489078cc4cSGlenn Strauss /*(read small files into memory)*/
1499078cc4cSGlenn Strauss const buffer * const fn = &sce->name;
1509078cc4cSGlenn Strauss const int fd = sce->fd;
1519078cc4cSGlenn Strauss int rc = (0 != sz) ? http_chunk_append_read_fd_range(r,fn,fd,0,sz) : 0;
1529078cc4cSGlenn Strauss return rc;
1539078cc4cSGlenn Strauss }
1549078cc4cSGlenn Strauss
1551ca721d4SGlenn Strauss __attribute_noinline__
http_chunk_append_to_tempfile(request_st * const r,const char * const mem,const size_t len)1567c7f8c46SGlenn Strauss static int http_chunk_append_to_tempfile(request_st * const r, const char * const mem, const size_t len) {
15781029b8bSGlenn Strauss chunkqueue * const cq = &r->write_queue;
1587c7f8c46SGlenn Strauss log_error_st * const errh = r->conf.errh;
159bcdc6a3bSJan Kneschke
1607c7f8c46SGlenn Strauss if (r->resp_send_chunked
1610fcd5143SGlenn Strauss && 0 != http_chunk_len_append_tempfile(cq, len, errh))
1625a91fd4bSGlenn Strauss return -1;
163bcdc6a3bSJan Kneschke
1640fcd5143SGlenn Strauss if (0 != chunkqueue_append_mem_to_tempfile(cq, mem, len, errh))
1655a91fd4bSGlenn Strauss return -1;
166bcdc6a3bSJan Kneschke
1677c7f8c46SGlenn Strauss if (r->resp_send_chunked
1680fcd5143SGlenn Strauss && 0 !=
1690fcd5143SGlenn Strauss chunkqueue_append_mem_to_tempfile(cq, CONST_STR_LEN("\r\n"), errh))
1705a91fd4bSGlenn Strauss return -1;
171bcdc6a3bSJan Kneschke
1725a91fd4bSGlenn Strauss return 0;
1735a91fd4bSGlenn Strauss }
174bcdc6a3bSJan Kneschke
1751ca721d4SGlenn Strauss __attribute_noinline__
http_chunk_append_cq_to_tempfile(request_st * const r,chunkqueue * const src,const size_t len)1767c7f8c46SGlenn Strauss static int http_chunk_append_cq_to_tempfile(request_st * const r, chunkqueue * const src, const size_t len) {
17781029b8bSGlenn Strauss chunkqueue * const cq = &r->write_queue;
1787c7f8c46SGlenn Strauss log_error_st * const errh = r->conf.errh;
1796afad87dSStefan Bühler
1807c7f8c46SGlenn Strauss if (r->resp_send_chunked
1810fcd5143SGlenn Strauss && 0 != http_chunk_len_append_tempfile(cq, len, errh))
182c79bc316SGlenn Strauss return -1;
183c79bc316SGlenn Strauss
1840fcd5143SGlenn Strauss if (0 != chunkqueue_steal_with_tempfiles(cq, src, len, errh))
185c79bc316SGlenn Strauss return -1;
186c79bc316SGlenn Strauss
1877c7f8c46SGlenn Strauss if (r->resp_send_chunked
1880fcd5143SGlenn Strauss && 0 !=
1890fcd5143SGlenn Strauss chunkqueue_append_mem_to_tempfile(cq, CONST_STR_LEN("\r\n"), errh))
190c79bc316SGlenn Strauss return -1;
191c79bc316SGlenn Strauss
192c79bc316SGlenn Strauss return 0;
193c79bc316SGlenn Strauss }
194c79bc316SGlenn Strauss
1951ca721d4SGlenn Strauss /*(inlined by compiler optimizer)*/
1960fcd5143SGlenn Strauss __attribute_pure__
http_chunk_uses_tempfile(const chunkqueue * const cq,const size_t len)197f19f7162SGlenn Strauss static int http_chunk_uses_tempfile(const chunkqueue * const cq, const size_t len) {
198bcdc6a3bSJan Kneschke
1995a91fd4bSGlenn Strauss /* current usage does not append_mem or append_buffer after appending
2005a91fd4bSGlenn Strauss * file, so not checking if users of this interface have appended large
201c79bc316SGlenn Strauss * (references to) files to chunkqueue, which would not be in memory
202c79bc316SGlenn Strauss * (but included in calculation for whether or not to use temp file) */
2030fcd5143SGlenn Strauss const chunk * const c = cq->last;
2040fcd5143SGlenn Strauss return
2050fcd5143SGlenn Strauss ((c && c->type == FILE_CHUNK && c->file.is_temp)
206f19f7162SGlenn Strauss || chunkqueue_length(cq) + len > 65536);
2075a91fd4bSGlenn Strauss }
2085a91fd4bSGlenn Strauss
2091ca721d4SGlenn Strauss __attribute_noinline__
http_chunk_append_buffer(request_st * const r,buffer * const mem)2107c7f8c46SGlenn Strauss int http_chunk_append_buffer(request_st * const r, buffer * const mem) {
211af3df29aSGlenn Strauss size_t len = mem ? buffer_clen(mem) : 0;
212c79bc316SGlenn Strauss if (0 == len) return 0;
213c79bc316SGlenn Strauss
21481029b8bSGlenn Strauss chunkqueue * const cq = &r->write_queue;
215bcdc6a3bSJan Kneschke
216f19f7162SGlenn Strauss if (http_chunk_uses_tempfile(cq, len)) {
217833d6587SGlenn Strauss int rc = http_chunk_append_to_tempfile(r, mem->ptr, len);
218833d6587SGlenn Strauss buffer_clear(mem);
219833d6587SGlenn Strauss return rc;
220833d6587SGlenn Strauss }
2210fcd5143SGlenn Strauss
2227c7f8c46SGlenn Strauss if (r->resp_send_chunked)
2230fcd5143SGlenn Strauss http_chunk_len_append(cq, len);
224bcdc6a3bSJan Kneschke
2255a91fd4bSGlenn Strauss /*(chunkqueue_append_buffer() might steal buffer contents)*/
226c79bc316SGlenn Strauss chunkqueue_append_buffer(cq, mem);
227bcdc6a3bSJan Kneschke
2287c7f8c46SGlenn Strauss if (r->resp_send_chunked)
2296afad87dSStefan Bühler chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
2305a91fd4bSGlenn Strauss
2315a91fd4bSGlenn Strauss return 0;
2325a91fd4bSGlenn Strauss }
2335a91fd4bSGlenn Strauss
2341ca721d4SGlenn Strauss __attribute_noinline__
http_chunk_append_mem(request_st * const r,const char * const mem,const size_t len)2357c7f8c46SGlenn Strauss int http_chunk_append_mem(request_st * const r, const char * const mem, const size_t len) {
236c79bc316SGlenn Strauss if (0 == len) return 0;
237c79bc316SGlenn Strauss force_assert(NULL != mem);
2385a91fd4bSGlenn Strauss
23981029b8bSGlenn Strauss chunkqueue * const cq = &r->write_queue;
2405a91fd4bSGlenn Strauss
241f19f7162SGlenn Strauss if (http_chunk_uses_tempfile(cq, len))
2427c7f8c46SGlenn Strauss return http_chunk_append_to_tempfile(r, mem, len);
2430fcd5143SGlenn Strauss
2447c7f8c46SGlenn Strauss if (r->resp_send_chunked)
2450fcd5143SGlenn Strauss http_chunk_len_append(cq, len);
2465a91fd4bSGlenn Strauss
247c79bc316SGlenn Strauss chunkqueue_append_mem(cq, mem, len);
248c79bc316SGlenn Strauss
2497c7f8c46SGlenn Strauss if (r->resp_send_chunked)
250c79bc316SGlenn Strauss chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
251c79bc316SGlenn Strauss
252c79bc316SGlenn Strauss return 0;
253c79bc316SGlenn Strauss }
254c79bc316SGlenn Strauss
http_chunk_transfer_cqlen(request_st * const r,chunkqueue * const src,const size_t len)2557c7f8c46SGlenn Strauss int http_chunk_transfer_cqlen(request_st * const r, chunkqueue * const src, const size_t len) {
256c79bc316SGlenn Strauss if (0 == len) return 0;
257c79bc316SGlenn Strauss
25881029b8bSGlenn Strauss chunkqueue * const cq = &r->write_queue;
259c79bc316SGlenn Strauss
260f19f7162SGlenn Strauss if (http_chunk_uses_tempfile(cq, len))
2617c7f8c46SGlenn Strauss return http_chunk_append_cq_to_tempfile(r, src, len);
2620fcd5143SGlenn Strauss
2637c7f8c46SGlenn Strauss if (r->resp_send_chunked)
2640fcd5143SGlenn Strauss http_chunk_len_append(cq, len);
265c79bc316SGlenn Strauss
266c79bc316SGlenn Strauss chunkqueue_steal(cq, src, len);
267c79bc316SGlenn Strauss
2687c7f8c46SGlenn Strauss if (r->resp_send_chunked)
269c79bc316SGlenn Strauss chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
270c79bc316SGlenn Strauss
271c79bc316SGlenn Strauss return 0;
272bcdc6a3bSJan Kneschke }
273bcdc6a3bSJan Kneschke
http_chunk_close(request_st * const r)2747c7f8c46SGlenn Strauss void http_chunk_close(request_st * const r) {
2757420526dSGlenn Strauss if (!r->resp_send_chunked) return;
2767420526dSGlenn Strauss
277cabced1fSGlenn Strauss if (r->gw_dechunk) {
2787420526dSGlenn Strauss if (!r->gw_dechunk->done)
2797420526dSGlenn Strauss r->keep_alive = 0;
2807420526dSGlenn Strauss }
2817420526dSGlenn Strauss else
28281029b8bSGlenn Strauss chunkqueue_append_mem(&r->write_queue, CONST_STR_LEN("0\r\n\r\n"));
283bcdc6a3bSJan Kneschke }
2847420526dSGlenn Strauss
2857420526dSGlenn Strauss static int
http_chunk_decode_append_data(request_st * const r,const char * mem,off_t len)2867420526dSGlenn Strauss http_chunk_decode_append_data (request_st * const r, const char *mem, off_t len)
2877420526dSGlenn Strauss {
2883230c6efSGlenn Strauss if (r->gw_dechunk->done) return -1; /*(excess data)*/
2897420526dSGlenn Strauss
2907420526dSGlenn Strauss buffer * const h = &r->gw_dechunk->b;
2917420526dSGlenn Strauss off_t te_chunked = r->gw_dechunk->gw_chunked;
2927420526dSGlenn Strauss while (len) {
2937420526dSGlenn Strauss if (0 == te_chunked) {
294cabced1fSGlenn Strauss const char *p;
295cabced1fSGlenn Strauss unsigned char *s = (unsigned char *)mem;
296cabced1fSGlenn Strauss off_t hsz;
297af3df29aSGlenn Strauss if (buffer_is_blank(h)) {
298cabced1fSGlenn Strauss /*(short-circuit common case: complete chunked header line)*/
299cabced1fSGlenn Strauss p = memchr(mem, '\n', (size_t)len);
300cabced1fSGlenn Strauss if (p)
301cabced1fSGlenn Strauss hsz = (off_t)(++p - mem);
302cabced1fSGlenn Strauss else {
303cabced1fSGlenn Strauss if (len >= 1024) {
3047420526dSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
3057420526dSGlenn Strauss "chunked header line too long");
3067420526dSGlenn Strauss return -1;
3077420526dSGlenn Strauss }
308cabced1fSGlenn Strauss buffer_append_string_len(h, mem, (uint32_t)len);
309cabced1fSGlenn Strauss break; /* incomplete HTTP chunked header line */
3107420526dSGlenn Strauss }
311cabced1fSGlenn Strauss }
312cabced1fSGlenn Strauss else {
313af3df29aSGlenn Strauss uint32_t hlen = buffer_clen(h);
314cabced1fSGlenn Strauss p = strchr(h->ptr, '\n');
315cabced1fSGlenn Strauss if (p)
316cabced1fSGlenn Strauss hsz = (off_t)(++p - h->ptr);
317cabced1fSGlenn Strauss else {
318cabced1fSGlenn Strauss p = memchr(mem, '\n', (size_t)len);
319cabced1fSGlenn Strauss hsz = (p ? (off_t)(++p - mem) : len);
3207420526dSGlenn Strauss if ((off_t)(1024 - hlen) < hsz) {
3217420526dSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
3227420526dSGlenn Strauss "chunked header line too long");
3237420526dSGlenn Strauss return -1;
3247420526dSGlenn Strauss }
3257420526dSGlenn Strauss buffer_append_string_len(h, mem, hsz);
326cabced1fSGlenn Strauss if (NULL == p) break;/*incomplete HTTP chunked header line*/
327cabced1fSGlenn Strauss mem += hsz;
328cabced1fSGlenn Strauss len -= hsz;
329cabced1fSGlenn Strauss hsz = 0;
3307420526dSGlenn Strauss }
331cabced1fSGlenn Strauss s = (unsigned char *)h->ptr;/*(note: read h->ptr after append)*/
3327420526dSGlenn Strauss }
333cabced1fSGlenn Strauss
3347420526dSGlenn Strauss for (unsigned char u; (u=(unsigned char)hex2int(*s))!=0xFF; ++s) {
335edfc5f39SGlenn Strauss if (te_chunked > (off_t)(1uLL<<(8*sizeof(off_t)-5))-1-2) {
3367420526dSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
3377420526dSGlenn Strauss "chunked data size too large");
3387420526dSGlenn Strauss return -1;
3397420526dSGlenn Strauss }
3407420526dSGlenn Strauss te_chunked <<= 4;
3417420526dSGlenn Strauss te_chunked |= u;
3427420526dSGlenn Strauss }
3437420526dSGlenn Strauss if ((char *)s == mem || (char *)s == h->ptr) return -1; /*(no hex)*/
3447420526dSGlenn Strauss while (*s == ' ' || *s == '\t') ++s;
3457420526dSGlenn Strauss if (*s != '\r' && *s != ';') { /*(not strictly checking \r\n)*/
3467420526dSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
3477420526dSGlenn Strauss "chunked header invalid chars");
3487420526dSGlenn Strauss return -1;
3497420526dSGlenn Strauss }
3507420526dSGlenn Strauss
3517420526dSGlenn Strauss if (0 == te_chunked) {
3527420526dSGlenn Strauss /* do not consume final chunked header until
3537420526dSGlenn Strauss * (optional) trailers received along with
3547420526dSGlenn Strauss * request-ending blank line "\r\n" */
355cabced1fSGlenn Strauss if (len - hsz >= 2 && p[0] == '\r' && p[1] == '\n') {
356cabced1fSGlenn Strauss if (len - hsz > 2) return -1; /*(excess data)*/
3577420526dSGlenn Strauss /* common case with no trailers; final \r\n received */
3587420526dSGlenn Strauss #if 0 /*(avoid allocation for common case; users must check)*/
359af3df29aSGlenn Strauss if (buffer_is_unset(h))
3607420526dSGlenn Strauss buffer_copy_string_len(h, CONST_STR_LEN("0\r\n\r\n"));
3617420526dSGlenn Strauss #else
3627420526dSGlenn Strauss buffer_clear(h);
3637420526dSGlenn Strauss #endif
3647420526dSGlenn Strauss r->gw_dechunk->done = r->http_status;
3657420526dSGlenn Strauss break;
3667420526dSGlenn Strauss }
3677420526dSGlenn Strauss
3687420526dSGlenn Strauss /* accumulate trailers and check for end of trailers */
3697420526dSGlenn Strauss /* XXX: reuse r->conf.max_request_field_size
3707420526dSGlenn Strauss * or have separate limit? */
371af3df29aSGlenn Strauss uint32_t mlen = buffer_clen(h);
372cabced1fSGlenn Strauss mlen = (r->conf.max_request_field_size > mlen)
373cabced1fSGlenn Strauss ? r->conf.max_request_field_size - mlen
374cabced1fSGlenn Strauss : 0;
375cabced1fSGlenn Strauss if ((off_t)mlen < len) {
3767420526dSGlenn Strauss /* truncate excessively long trailers */
377cabced1fSGlenn Strauss /* (not truncated; passed as-is if r->resp_send_chunked) */
378cabced1fSGlenn Strauss if (r->resp_send_chunked) r->keep_alive = 0;
3797420526dSGlenn Strauss r->gw_dechunk->done = r->http_status;
380cabced1fSGlenn Strauss buffer_append_string_len(h, mem, mlen);
3817420526dSGlenn Strauss p = strrchr(h->ptr, '\n');
382cabced1fSGlenn Strauss if (NULL != p) {
383af3df29aSGlenn Strauss buffer_truncate(h, p + 1 - h->ptr);
384cabced1fSGlenn Strauss if (p[-1] != '\r')
385cabced1fSGlenn Strauss buffer_append_string_len(h, CONST_STR_LEN("\r\n"));
386cabced1fSGlenn Strauss }
3877420526dSGlenn Strauss else { /*(should not happen)*/
3887420526dSGlenn Strauss buffer_clear(h);
3897420526dSGlenn Strauss buffer_append_string_len(h, CONST_STR_LEN("0\r\n"));
3907420526dSGlenn Strauss }
3917420526dSGlenn Strauss buffer_append_string_len(h, CONST_STR_LEN("\r\n"));
3927420526dSGlenn Strauss break;
3937420526dSGlenn Strauss }
394cabced1fSGlenn Strauss buffer_append_string_len(h, mem, (uint32_t)len);
395cabced1fSGlenn Strauss if ((p = strstr(h->ptr, "\r\n\r\n"))) {
3963230c6efSGlenn Strauss r->gw_dechunk->done = r->http_status;
397cabced1fSGlenn Strauss if (p[4] != '\0') return -1; /*(excess data)*/
398af3df29aSGlenn Strauss /*buffer_truncate(h, (uint32_t)(p+4-h->ptr));*/
3997420526dSGlenn Strauss }
4007420526dSGlenn Strauss break;
4017420526dSGlenn Strauss }
4027420526dSGlenn Strauss
4037420526dSGlenn Strauss mem += hsz;
4047420526dSGlenn Strauss len -= hsz;
4057420526dSGlenn Strauss
4067420526dSGlenn Strauss te_chunked += 2; /*(for trailing "\r\n" after chunked data)*/
407cabced1fSGlenn Strauss buffer_clear(h);
408163cb8beSGlenn Strauss if (0 == len) break;
4097420526dSGlenn Strauss }
4107420526dSGlenn Strauss
4117420526dSGlenn Strauss if (te_chunked >= 2) {
4127420526dSGlenn Strauss off_t clen = te_chunked - 2;
4137420526dSGlenn Strauss if (clen > len) clen = len;
414167513c8SGlenn Strauss if (!r->resp_send_chunked
415167513c8SGlenn Strauss && 0 != http_chunk_append_mem(r, mem, clen))
4167420526dSGlenn Strauss return -1;
4177420526dSGlenn Strauss mem += clen;
4187420526dSGlenn Strauss len -= clen;
4197420526dSGlenn Strauss te_chunked -= clen;
4207420526dSGlenn Strauss if (te_chunked == 2) {
4217420526dSGlenn Strauss if (len >= 2) {
4227420526dSGlenn Strauss if (mem[0] != '\r' || mem[1] != '\n') return -1;
4237420526dSGlenn Strauss mem += 2;
4247420526dSGlenn Strauss len -= 2;
4257420526dSGlenn Strauss te_chunked = 0;
4267420526dSGlenn Strauss }
427163cb8beSGlenn Strauss else if (len == 1) {
428163cb8beSGlenn Strauss if (mem[0] != '\r') return -1;
429163cb8beSGlenn Strauss /*++mem;*/
430163cb8beSGlenn Strauss /*--len;*/
431163cb8beSGlenn Strauss te_chunked = 1;
432163cb8beSGlenn Strauss break;
433163cb8beSGlenn Strauss }
4347420526dSGlenn Strauss }
4357420526dSGlenn Strauss }
4367420526dSGlenn Strauss else if (1 == te_chunked) {
4377420526dSGlenn Strauss /* finish reading chunk block "\r\n" */
4387420526dSGlenn Strauss if (mem[0] != '\n') return -1;
4397420526dSGlenn Strauss ++mem;
4407420526dSGlenn Strauss --len;
4417420526dSGlenn Strauss te_chunked = 0;
4427420526dSGlenn Strauss }
4437420526dSGlenn Strauss }
444167513c8SGlenn Strauss if (r->gw_dechunk->done)
445167513c8SGlenn Strauss r->resp_body_finished = 1;
4467420526dSGlenn Strauss r->gw_dechunk->gw_chunked = te_chunked;
4477420526dSGlenn Strauss return 0;
4487420526dSGlenn Strauss }
4497420526dSGlenn Strauss
http_chunk_decode_append_buffer(request_st * const r,buffer * const mem)4507420526dSGlenn Strauss int http_chunk_decode_append_buffer(request_st * const r, buffer * const mem)
4517420526dSGlenn Strauss {
452167513c8SGlenn Strauss /* Note: this routine is separate from http_chunk_decode_append_mem() to
453167513c8SGlenn Strauss * potentially avoid copying in http_chunk_append_buffer(). Otherwise this
454af3df29aSGlenn Strauss * would be: return http_chunk_decode_append_mem(r, BUF_PTR_LEN(mem)); */
455167513c8SGlenn Strauss
456833d6587SGlenn Strauss /*(called by funcs receiving chunked data from backends)*/
4577420526dSGlenn Strauss /*(separate from http_chunk_append_buffer() called by numerous others)*/
4587420526dSGlenn Strauss
459167513c8SGlenn Strauss /* might avoid copy by transferring buffer if buffer is all data that is
460167513c8SGlenn Strauss * part of large chunked block, but choosing to *not* expand that out here*/
461af3df29aSGlenn Strauss if (0 != http_chunk_decode_append_data(r, BUF_PTR_LEN(mem)))
462167513c8SGlenn Strauss return -1;
463167513c8SGlenn Strauss
464167513c8SGlenn Strauss /* no need to decode chunked to immediately re-encode chunked;
465167513c8SGlenn Strauss * pass through chunked encoding as provided by backend,
466167513c8SGlenn Strauss * though it is still parsed (above) to maintain state.
467167513c8SGlenn Strauss * XXX: consider having callers use chunk buffers for hctx->b
468167513c8SGlenn Strauss * for more efficient data copy avoidance and buffer reuse
469167513c8SGlenn Strauss * note: r->resp_send_chunked = 0 until response headers sent,
470167513c8SGlenn Strauss * which is when Transfer-Encoding: chunked might be chosen */
4717420526dSGlenn Strauss if (r->resp_send_chunked) {
4727420526dSGlenn Strauss r->resp_send_chunked = 0;
4737420526dSGlenn Strauss int rc = http_chunk_append_buffer(r, mem); /* might append to tmpfile */
4747420526dSGlenn Strauss r->resp_send_chunked = 1;
4757420526dSGlenn Strauss return rc;
4767420526dSGlenn Strauss }
477e97a5b7eSGlenn Strauss else
478e97a5b7eSGlenn Strauss buffer_clear(mem);
4797420526dSGlenn Strauss
480167513c8SGlenn Strauss return 0;
4817420526dSGlenn Strauss }
4827420526dSGlenn Strauss
http_chunk_decode_append_mem(request_st * const r,const char * const mem,size_t len)483903024d7SGlenn Strauss int http_chunk_decode_append_mem(request_st * const r, const char * const mem, size_t len)
4847420526dSGlenn Strauss {
485833d6587SGlenn Strauss /*(called by funcs receiving chunked data from backends)*/
4867420526dSGlenn Strauss /*(separate from http_chunk_append_mem() called by numerous others)*/
4877420526dSGlenn Strauss
488167513c8SGlenn Strauss if (0 != http_chunk_decode_append_data(r, mem, (off_t)len))
489167513c8SGlenn Strauss return -1;
490167513c8SGlenn Strauss
491167513c8SGlenn Strauss /* no need to decode chunked to immediately re-encode chunked;
492167513c8SGlenn Strauss * pass through chunked encoding as provided by backend,
493167513c8SGlenn Strauss * though it is still parsed (above) to maintain state.
494167513c8SGlenn Strauss * note: r->resp_send_chunked = 0 until response headers sent,
495167513c8SGlenn Strauss * which is when Transfer-Encoding: chunked might be chosen */
4967420526dSGlenn Strauss if (r->resp_send_chunked) {
4977420526dSGlenn Strauss r->resp_send_chunked = 0;
4987420526dSGlenn Strauss int rc = http_chunk_append_mem(r, mem, len); /*might append to tmpfile*/
4997420526dSGlenn Strauss r->resp_send_chunked = 1;
5007420526dSGlenn Strauss return rc;
5017420526dSGlenn Strauss }
5027420526dSGlenn Strauss
503167513c8SGlenn Strauss return 0;
5047420526dSGlenn Strauss }
505