2020-05-20 05:06:13 +00:00
|
|
|
/*
|
|
|
|
* http_chunk - append response to chunkqueue, possibly in "chunked" encoding
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2020-05-20 05:06:13 +00:00
|
|
|
* Fully-rewritten from original
|
|
|
|
* Copyright(c) 2019 Glenn Strauss gstrauss()gluelogic.com All rights reserved
|
|
|
|
* License: BSD 3-clause (same as lighttpd)
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
2020-05-20 05:06:13 +00:00
|
|
|
#include "first.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2019-11-26 03:38:16 +00:00
|
|
|
#include "http_chunk.h"
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "chunk.h"
|
2016-03-30 10:39:33 +00:00
|
|
|
#include "stat_cache.h"
|
2017-03-28 04:04:31 +00:00
|
|
|
#include "fdevent.h"
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "log.h"
|
2021-08-02 09:11:36 +00:00
|
|
|
#include "request.h"
|
2009-10-11 14:31:42 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-06-19 06:17:34 +00:00
|
|
|
__attribute_noinline__
|
2019-11-26 03:38:16 +00:00
|
|
|
static void http_chunk_len_append(chunkqueue * const cq, uintmax_t len) {
|
|
|
|
char buf[24]; /* 64-bit (8 bytes) is 16 hex chars (+2 \r\n, +1 \0 = 19) */
|
|
|
|
#if 0
|
|
|
|
buffer b = { buf, 0, sizeof(buf) };
|
|
|
|
buffer_append_uint_hex(&b, len);
|
|
|
|
buffer_append_string_len(&b, CONST_STR_LEN("\r\n"));
|
|
|
|
chunkqueue_append_mem(cq, b.ptr, b.used-1);
|
|
|
|
#else
|
|
|
|
int i = (int)(sizeof(buf));
|
|
|
|
buf[--i] = '\n';
|
|
|
|
buf[--i] = '\r';
|
|
|
|
do { buf[--i] = "0123456789abcdef"[len & 0x0F]; } while (len >>= 4);
|
|
|
|
chunkqueue_append_mem(cq, buf+i, sizeof(buf)-i);
|
|
|
|
#endif
|
2018-11-06 00:39:03 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2021-06-19 06:17:34 +00:00
|
|
|
__attribute_noinline__
|
2019-11-26 03:38:16 +00:00
|
|
|
static int http_chunk_len_append_tempfile(chunkqueue * const cq, uintmax_t len, log_error_st * const errh) {
|
|
|
|
char buf[24]; /* 64-bit (8 bytes) is 16 hex chars (+2 \r\n, +1 \0 = 19) */
|
|
|
|
#if 0
|
|
|
|
buffer b = { buf, 0, sizeof(buf) };
|
|
|
|
buffer_append_uint_hex(&b, len);
|
|
|
|
buffer_append_string_len(&b, CONST_STR_LEN("\r\n"));
|
|
|
|
return chunkqueue_append_mem_to_tempfile(cq, b.ptr, b.used-1, errh);
|
|
|
|
#else
|
|
|
|
int i = (int)(sizeof(buf));
|
|
|
|
buf[--i] = '\n';
|
|
|
|
buf[--i] = '\r';
|
|
|
|
do { buf[--i] = "0123456789abcdef"[len & 0x0F]; } while (len >>= 4);
|
|
|
|
return chunkqueue_append_mem_to_tempfile(cq, buf+i, sizeof(buf)-i, errh);
|
|
|
|
#endif
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-19 06:17:34 +00:00
|
|
|
__attribute_noinline__
|
2020-01-13 02:51:12 +00:00
|
|
|
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) {
|
2019-05-12 22:31:43 +00:00
|
|
|
/* note: this routine should not be used for range requests
|
|
|
|
* unless the total size of ranges requested is small */
|
|
|
|
/* note: future: could read into existing MEM_CHUNK in cq->last if
|
|
|
|
* there is sufficient space, but would need to adjust for existing
|
|
|
|
* offset in for cq->bytes_in in chunkqueue_append_buffer_commit() */
|
|
|
|
UNUSED(fn);
|
|
|
|
|
2020-09-29 20:50:39 +00:00
|
|
|
chunkqueue * const cq = &r->write_queue;
|
2019-11-26 03:38:16 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_send_chunked)
|
2019-11-26 03:38:16 +00:00
|
|
|
http_chunk_len_append(cq, (uintmax_t)len);
|
2019-05-12 22:31:43 +00:00
|
|
|
|
2021-09-28 01:12:41 +00:00
|
|
|
#ifndef HAVE_PREAD
|
2020-10-20 14:55:08 +00:00
|
|
|
if (-1 == lseek(fd, offset, SEEK_SET)) return -1;
|
2021-09-28 01:12:41 +00:00
|
|
|
#endif
|
2020-10-11 16:23:21 +00:00
|
|
|
buffer * const b = chunkqueue_append_buffer_open_sz(cq, len+2+1);
|
2019-05-12 22:31:43 +00:00
|
|
|
ssize_t rd;
|
2021-09-28 01:12:41 +00:00
|
|
|
#ifdef HAVE_PREAD
|
|
|
|
const off_t foff = offset;
|
|
|
|
#endif
|
2019-05-12 22:31:43 +00:00
|
|
|
offset = 0;
|
|
|
|
do {
|
2021-09-28 01:12:41 +00:00
|
|
|
#ifdef HAVE_PREAD
|
|
|
|
rd =pread(fd, b->ptr+offset, (size_t)(len-offset), foff+offset);
|
|
|
|
#else
|
2021-03-13 20:55:28 +00:00
|
|
|
rd = read(fd, b->ptr+offset, (size_t)(len-offset));
|
2021-09-28 01:12:41 +00:00
|
|
|
#endif
|
2021-03-13 20:55:28 +00:00
|
|
|
} while (rd > 0 ? (offset += rd) != len : errno == EINTR);
|
2019-05-12 22:31:43 +00:00
|
|
|
buffer_commit(b, offset);
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_send_chunked)
|
2019-05-12 22:31:43 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
|
|
|
|
|
|
|
|
chunkqueue_append_buffer_commit(cq);
|
|
|
|
return (rd >= 0) ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2021-06-19 06:17:34 +00:00
|
|
|
__attribute_noinline__
|
2021-05-14 06:40:22 +00:00
|
|
|
void http_chunk_append_file_ref_range(request_st * const r, stat_cache_entry * const sce, const off_t offset, off_t len) {
|
2020-10-20 03:11:18 +00:00
|
|
|
chunkqueue * const cq = &r->write_queue;
|
|
|
|
|
2021-05-14 06:40:22 +00:00
|
|
|
if (sce->st.st_size - offset < len)
|
|
|
|
len = sce->st.st_size - offset;
|
|
|
|
if (len <= 0)
|
|
|
|
return;
|
|
|
|
|
2020-10-20 03:11:18 +00:00
|
|
|
if (r->resp_send_chunked)
|
|
|
|
http_chunk_len_append(cq, (uintmax_t)len);
|
|
|
|
|
|
|
|
const buffer * const fn = &sce->name;
|
|
|
|
const int fd = sce->fd;
|
|
|
|
chunkqueue_append_file_fd(cq, fn, fd, offset, len);
|
|
|
|
if (fd >= 0) {
|
|
|
|
chunk * const d = cq->last;
|
|
|
|
d->file.ref = sce;
|
|
|
|
d->file.refchg = stat_cache_entry_refchg;
|
|
|
|
stat_cache_entry_refchg(sce, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r->resp_send_chunked)
|
|
|
|
chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
|
|
|
|
}
|
|
|
|
|
2021-06-19 06:17:34 +00:00
|
|
|
__attribute_noinline__
|
2020-10-10 10:01:02 +00:00
|
|
|
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) {
|
2020-09-29 20:50:39 +00:00
|
|
|
chunkqueue * const cq = &r->write_queue;
|
2015-02-08 12:37:10 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_send_chunked)
|
2019-11-26 03:38:16 +00:00
|
|
|
http_chunk_len_append(cq, (uintmax_t)len);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-26 03:38:16 +00:00
|
|
|
chunkqueue_append_file_fd(cq, fn, fd, offset, len);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_send_chunked)
|
2019-11-26 03:38:16 +00:00
|
|
|
chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
int http_chunk_append_file_fd(request_st * const r, const buffer * const fn, const int fd, const off_t sz) {
|
2020-09-03 05:58:22 +00:00
|
|
|
if (sz > 32768 || !r->resp_send_chunked) {
|
2020-01-13 02:51:12 +00:00
|
|
|
http_chunk_append_file_fd_range(r, fn, fd, 0, sz);
|
2019-11-26 03:38:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-05-25 20:45:09 +00:00
|
|
|
|
2019-11-26 03:38:16 +00:00
|
|
|
/*(read small files into memory)*/
|
2020-01-13 02:51:12 +00:00
|
|
|
int rc = (0 != sz) ? http_chunk_append_read_fd_range(r,fn,fd,0,sz) : 0;
|
2019-11-26 03:38:16 +00:00
|
|
|
close(fd);
|
|
|
|
return rc;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 03:11:18 +00:00
|
|
|
int http_chunk_append_file_ref(request_st * const r, stat_cache_entry * const sce) {
|
|
|
|
const off_t sz = sce->st.st_size;
|
|
|
|
if (sz > 32768 || !r->resp_send_chunked) {
|
|
|
|
http_chunk_append_file_ref_range(r, sce, 0, sz);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*(read small files into memory)*/
|
|
|
|
const buffer * const fn = &sce->name;
|
|
|
|
const int fd = sce->fd;
|
|
|
|
int rc = (0 != sz) ? http_chunk_append_read_fd_range(r,fn,fd,0,sz) : 0;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-06-19 06:17:34 +00:00
|
|
|
__attribute_noinline__
|
2020-01-13 02:51:12 +00:00
|
|
|
static int http_chunk_append_to_tempfile(request_st * const r, const char * const mem, const size_t len) {
|
2020-09-29 20:50:39 +00:00
|
|
|
chunkqueue * const cq = &r->write_queue;
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error_st * const errh = r->conf.errh;
|
2018-11-06 00:39:03 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_send_chunked
|
2019-11-26 03:38:16 +00:00
|
|
|
&& 0 != http_chunk_len_append_tempfile(cq, len, errh))
|
|
|
|
return -1;
|
2018-11-06 00:39:03 +00:00
|
|
|
|
2019-11-26 03:38:16 +00:00
|
|
|
if (0 != chunkqueue_append_mem_to_tempfile(cq, mem, len, errh))
|
2018-11-06 00:39:03 +00:00
|
|
|
return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_send_chunked
|
2019-11-26 03:38:16 +00:00
|
|
|
&& 0 !=
|
|
|
|
chunkqueue_append_mem_to_tempfile(cq, CONST_STR_LEN("\r\n"), errh))
|
|
|
|
return -1;
|
2018-11-06 00:39:03 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-19 06:17:34 +00:00
|
|
|
__attribute_noinline__
|
2020-01-13 02:51:12 +00:00
|
|
|
static int http_chunk_append_cq_to_tempfile(request_st * const r, chunkqueue * const src, const size_t len) {
|
2020-09-29 20:50:39 +00:00
|
|
|
chunkqueue * const cq = &r->write_queue;
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error_st * const errh = r->conf.errh;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_send_chunked
|
2019-11-26 03:38:16 +00:00
|
|
|
&& 0 != http_chunk_len_append_tempfile(cq, len, errh))
|
|
|
|
return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-26 03:38:16 +00:00
|
|
|
if (0 != chunkqueue_steal_with_tempfiles(cq, src, len, errh))
|
|
|
|
return -1;
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_send_chunked
|
2019-11-26 03:38:16 +00:00
|
|
|
&& 0 !=
|
|
|
|
chunkqueue_append_mem_to_tempfile(cq, CONST_STR_LEN("\r\n"), errh))
|
|
|
|
return -1;
|
[core] option to stream response body to client (fixes #949, #760, #1283, #1387)
Set server.stream-response-body = 1 or server.stream-response-body = 2
to have lighttpd stream response body to client as it arrives from the
backend (CGI, FastCGI, SCGI, proxy).
default: buffer entire response body before sending response to client.
(This preserves existing behavior for now, but may in the future be
changed to stream response to client, which is the behavior more
commonly expected.)
x-ref:
"fastcgi, cgi, flush, php5 problem."
https://redmine.lighttpd.net/issues/949
"Random crashing on FreeBSD 6.1"
https://redmine.lighttpd.net/issues/760
"Memory usage increases when proxy+ssl+large file"
https://redmine.lighttpd.net/issues/1283
"lighttpd+fastcgi memory problem"
https://redmine.lighttpd.net/issues/1387
2016-06-11 15:04:01 +00:00
|
|
|
|
2019-11-26 03:38:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-05-25 20:45:09 +00:00
|
|
|
|
2021-06-19 06:17:34 +00:00
|
|
|
/*(inlined by compiler optimizer)*/
|
2019-11-26 03:38:16 +00:00
|
|
|
__attribute_pure__
|
2021-09-17 15:49:55 +00:00
|
|
|
static int http_chunk_uses_tempfile(const chunkqueue * const cq, const size_t len) {
|
2019-11-26 03:38:16 +00:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
* (but included in calculation for whether or not to use temp file) */
|
|
|
|
const chunk * const c = cq->last;
|
|
|
|
return
|
|
|
|
((c && c->type == FILE_CHUNK && c->file.is_temp)
|
2021-09-17 15:49:55 +00:00
|
|
|
|| chunkqueue_length(cq) + len > 65536);
|
2018-11-06 00:39:03 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2021-06-19 06:17:34 +00:00
|
|
|
__attribute_noinline__
|
2020-01-13 02:51:12 +00:00
|
|
|
int http_chunk_append_buffer(request_st * const r, buffer * const mem) {
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
size_t len = mem ? buffer_clen(mem) : 0;
|
2018-11-06 00:39:03 +00:00
|
|
|
if (0 == len) return 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-09-29 20:50:39 +00:00
|
|
|
chunkqueue * const cq = &r->write_queue;
|
2019-11-26 03:38:16 +00:00
|
|
|
|
2021-09-17 15:49:55 +00:00
|
|
|
if (http_chunk_uses_tempfile(cq, len)) {
|
2021-07-24 04:59:48 +00:00
|
|
|
int rc = http_chunk_append_to_tempfile(r, mem->ptr, len);
|
|
|
|
buffer_clear(mem);
|
|
|
|
return rc;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_send_chunked)
|
2019-11-26 03:38:16 +00:00
|
|
|
http_chunk_len_append(cq, len);
|
2016-05-25 20:45:09 +00:00
|
|
|
|
2018-11-06 00:39:03 +00:00
|
|
|
/*(chunkqueue_append_buffer() might steal buffer contents)*/
|
|
|
|
chunkqueue_append_buffer(cq, mem);
|
2016-05-25 20:45:09 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_send_chunked)
|
2018-11-06 00:39:03 +00:00
|
|
|
chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
|
2016-05-25 20:45:09 +00:00
|
|
|
|
2018-11-06 00:39:03 +00:00
|
|
|
return 0;
|
2016-05-25 20:45:09 +00:00
|
|
|
}
|
|
|
|
|
2021-06-19 06:17:34 +00:00
|
|
|
__attribute_noinline__
|
2020-01-13 02:51:12 +00:00
|
|
|
int http_chunk_append_mem(request_st * const r, const char * const mem, const size_t len) {
|
2018-11-06 00:39:03 +00:00
|
|
|
if (0 == len) return 0;
|
|
|
|
force_assert(NULL != mem);
|
|
|
|
|
2020-09-29 20:50:39 +00:00
|
|
|
chunkqueue * const cq = &r->write_queue;
|
2019-11-26 03:38:16 +00:00
|
|
|
|
2021-09-17 15:49:55 +00:00
|
|
|
if (http_chunk_uses_tempfile(cq, len))
|
2020-01-13 02:51:12 +00:00
|
|
|
return http_chunk_append_to_tempfile(r, mem, len);
|
2018-11-06 00:39:03 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_send_chunked)
|
2019-11-26 03:38:16 +00:00
|
|
|
http_chunk_len_append(cq, len);
|
2018-11-06 00:39:03 +00:00
|
|
|
|
|
|
|
chunkqueue_append_mem(cq, mem, len);
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_send_chunked)
|
2018-11-06 00:39:03 +00:00
|
|
|
chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
int http_chunk_transfer_cqlen(request_st * const r, chunkqueue * const src, const size_t len) {
|
2018-11-06 00:39:03 +00:00
|
|
|
if (0 == len) return 0;
|
|
|
|
|
2020-09-29 20:50:39 +00:00
|
|
|
chunkqueue * const cq = &r->write_queue;
|
2019-11-26 03:38:16 +00:00
|
|
|
|
2021-09-17 15:49:55 +00:00
|
|
|
if (http_chunk_uses_tempfile(cq, len))
|
2020-01-13 02:51:12 +00:00
|
|
|
return http_chunk_append_cq_to_tempfile(r, src, len);
|
2018-11-06 00:39:03 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_send_chunked)
|
2019-11-26 03:38:16 +00:00
|
|
|
http_chunk_len_append(cq, len);
|
2018-11-06 00:39:03 +00:00
|
|
|
|
|
|
|
chunkqueue_steal(cq, src, len);
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->resp_send_chunked)
|
2018-11-06 00:39:03 +00:00
|
|
|
chunkqueue_append_mem(cq, CONST_STR_LEN("\r\n"));
|
2016-05-25 20:45:09 +00:00
|
|
|
|
2018-11-06 00:39:03 +00:00
|
|
|
return 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
void http_chunk_close(request_st * const r) {
|
2020-07-28 11:32:29 +00:00
|
|
|
if (!r->resp_send_chunked) return;
|
|
|
|
|
2020-12-17 05:32:01 +00:00
|
|
|
if (r->gw_dechunk) {
|
2020-07-28 11:32:29 +00:00
|
|
|
if (!r->gw_dechunk->done)
|
|
|
|
r->keep_alive = 0;
|
|
|
|
}
|
|
|
|
else
|
2020-09-29 20:50:39 +00:00
|
|
|
chunkqueue_append_mem(&r->write_queue, CONST_STR_LEN("0\r\n\r\n"));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2020-07-28 11:32:29 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
http_chunk_decode_append_data (request_st * const r, const char *mem, off_t len)
|
|
|
|
{
|
2020-12-15 00:55:00 +00:00
|
|
|
if (r->gw_dechunk->done) return -1; /*(excess data)*/
|
2020-07-28 11:32:29 +00:00
|
|
|
|
|
|
|
buffer * const h = &r->gw_dechunk->b;
|
|
|
|
off_t te_chunked = r->gw_dechunk->gw_chunked;
|
|
|
|
while (len) {
|
|
|
|
if (0 == te_chunked) {
|
2020-12-17 05:32:01 +00:00
|
|
|
const char *p;
|
|
|
|
unsigned char *s = (unsigned char *)mem;
|
|
|
|
off_t hsz;
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (buffer_is_blank(h)) {
|
2020-12-17 05:32:01 +00:00
|
|
|
/*(short-circuit common case: complete chunked header line)*/
|
|
|
|
p = memchr(mem, '\n', (size_t)len);
|
|
|
|
if (p)
|
|
|
|
hsz = (off_t)(++p - mem);
|
|
|
|
else {
|
|
|
|
if (len >= 1024) {
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"chunked header line too long");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buffer_append_string_len(h, mem, (uint32_t)len);
|
|
|
|
break; /* incomplete HTTP chunked header line */
|
2020-07-28 11:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-17 05:32:01 +00:00
|
|
|
else {
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
uint32_t hlen = buffer_clen(h);
|
2020-12-17 05:32:01 +00:00
|
|
|
p = strchr(h->ptr, '\n');
|
|
|
|
if (p)
|
|
|
|
hsz = (off_t)(++p - h->ptr);
|
|
|
|
else {
|
|
|
|
p = memchr(mem, '\n', (size_t)len);
|
|
|
|
hsz = (p ? (off_t)(++p - mem) : len);
|
2020-07-28 11:32:29 +00:00
|
|
|
if ((off_t)(1024 - hlen) < hsz) {
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"chunked header line too long");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buffer_append_string_len(h, mem, hsz);
|
2020-12-17 05:32:01 +00:00
|
|
|
if (NULL == p) break;/*incomplete HTTP chunked header line*/
|
|
|
|
mem += hsz;
|
|
|
|
len -= hsz;
|
|
|
|
hsz = 0;
|
2020-07-28 11:32:29 +00:00
|
|
|
}
|
2020-12-17 05:32:01 +00:00
|
|
|
s = (unsigned char *)h->ptr;/*(note: read h->ptr after append)*/
|
2020-07-28 11:32:29 +00:00
|
|
|
}
|
2020-12-17 05:32:01 +00:00
|
|
|
|
2020-07-28 11:32:29 +00:00
|
|
|
for (unsigned char u; (u=(unsigned char)hex2int(*s))!=0xFF; ++s) {
|
2020-11-22 18:13:46 +00:00
|
|
|
if (te_chunked > (off_t)(1uLL<<(8*sizeof(off_t)-5))-1-2) {
|
2020-07-28 11:32:29 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"chunked data size too large");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
te_chunked <<= 4;
|
|
|
|
te_chunked |= u;
|
|
|
|
}
|
|
|
|
if ((char *)s == mem || (char *)s == h->ptr) return -1; /*(no hex)*/
|
|
|
|
while (*s == ' ' || *s == '\t') ++s;
|
|
|
|
if (*s != '\r' && *s != ';') { /*(not strictly checking \r\n)*/
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"chunked header invalid chars");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == te_chunked) {
|
|
|
|
/* do not consume final chunked header until
|
|
|
|
* (optional) trailers received along with
|
|
|
|
* request-ending blank line "\r\n" */
|
2020-12-17 05:32:01 +00:00
|
|
|
if (len - hsz >= 2 && p[0] == '\r' && p[1] == '\n') {
|
|
|
|
if (len - hsz > 2) return -1; /*(excess data)*/
|
2020-07-28 11:32:29 +00:00
|
|
|
/* common case with no trailers; final \r\n received */
|
|
|
|
#if 0 /*(avoid allocation for common case; users must check)*/
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (buffer_is_unset(h))
|
2020-07-28 11:32:29 +00:00
|
|
|
buffer_copy_string_len(h, CONST_STR_LEN("0\r\n\r\n"));
|
|
|
|
#else
|
|
|
|
buffer_clear(h);
|
|
|
|
#endif
|
|
|
|
r->gw_dechunk->done = r->http_status;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* accumulate trailers and check for end of trailers */
|
|
|
|
/* XXX: reuse r->conf.max_request_field_size
|
|
|
|
* or have separate limit? */
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
uint32_t mlen = buffer_clen(h);
|
2020-12-17 05:32:01 +00:00
|
|
|
mlen = (r->conf.max_request_field_size > mlen)
|
|
|
|
? r->conf.max_request_field_size - mlen
|
|
|
|
: 0;
|
|
|
|
if ((off_t)mlen < len) {
|
2020-07-28 11:32:29 +00:00
|
|
|
/* truncate excessively long trailers */
|
2020-12-17 05:32:01 +00:00
|
|
|
/* (not truncated; passed as-is if r->resp_send_chunked) */
|
|
|
|
if (r->resp_send_chunked) r->keep_alive = 0;
|
2020-07-28 11:32:29 +00:00
|
|
|
r->gw_dechunk->done = r->http_status;
|
2020-12-17 05:32:01 +00:00
|
|
|
buffer_append_string_len(h, mem, mlen);
|
2020-07-28 11:32:29 +00:00
|
|
|
p = strrchr(h->ptr, '\n');
|
2020-12-17 05:32:01 +00:00
|
|
|
if (NULL != p) {
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
buffer_truncate(h, p + 1 - h->ptr);
|
2020-12-17 05:32:01 +00:00
|
|
|
if (p[-1] != '\r')
|
|
|
|
buffer_append_string_len(h, CONST_STR_LEN("\r\n"));
|
|
|
|
}
|
2020-07-28 11:32:29 +00:00
|
|
|
else { /*(should not happen)*/
|
|
|
|
buffer_clear(h);
|
|
|
|
buffer_append_string_len(h, CONST_STR_LEN("0\r\n"));
|
|
|
|
}
|
|
|
|
buffer_append_string_len(h, CONST_STR_LEN("\r\n"));
|
|
|
|
break;
|
|
|
|
}
|
2020-12-17 05:32:01 +00:00
|
|
|
buffer_append_string_len(h, mem, (uint32_t)len);
|
|
|
|
if ((p = strstr(h->ptr, "\r\n\r\n"))) {
|
2020-07-28 11:32:29 +00:00
|
|
|
r->gw_dechunk->done = r->http_status;
|
2020-12-17 05:32:01 +00:00
|
|
|
if (p[4] != '\0') return -1; /*(excess data)*/
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
/*buffer_truncate(h, (uint32_t)(p+4-h->ptr));*/
|
2020-07-28 11:32:29 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mem += hsz;
|
|
|
|
len -= hsz;
|
|
|
|
|
|
|
|
te_chunked += 2; /*(for trailing "\r\n" after chunked data)*/
|
2020-12-17 05:32:01 +00:00
|
|
|
buffer_clear(h);
|
2020-12-14 15:58:36 +00:00
|
|
|
if (0 == len) break;
|
2020-07-28 11:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (te_chunked >= 2) {
|
|
|
|
off_t clen = te_chunked - 2;
|
|
|
|
if (clen > len) clen = len;
|
2020-12-15 00:57:01 +00:00
|
|
|
if (!r->resp_send_chunked
|
|
|
|
&& 0 != http_chunk_append_mem(r, mem, clen))
|
2020-07-28 11:32:29 +00:00
|
|
|
return -1;
|
|
|
|
mem += clen;
|
|
|
|
len -= clen;
|
|
|
|
te_chunked -= clen;
|
|
|
|
if (te_chunked == 2) {
|
|
|
|
if (len >= 2) {
|
|
|
|
if (mem[0] != '\r' || mem[1] != '\n') return -1;
|
|
|
|
mem += 2;
|
|
|
|
len -= 2;
|
|
|
|
te_chunked = 0;
|
|
|
|
}
|
2020-12-14 15:58:36 +00:00
|
|
|
else if (len == 1) {
|
|
|
|
if (mem[0] != '\r') return -1;
|
|
|
|
/*++mem;*/
|
|
|
|
/*--len;*/
|
|
|
|
te_chunked = 1;
|
|
|
|
break;
|
|
|
|
}
|
2020-07-28 11:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (1 == te_chunked) {
|
|
|
|
/* finish reading chunk block "\r\n" */
|
|
|
|
if (mem[0] != '\n') return -1;
|
|
|
|
++mem;
|
|
|
|
--len;
|
|
|
|
te_chunked = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-12-15 00:57:01 +00:00
|
|
|
if (r->gw_dechunk->done)
|
|
|
|
r->resp_body_finished = 1;
|
2020-07-28 11:32:29 +00:00
|
|
|
r->gw_dechunk->gw_chunked = te_chunked;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int http_chunk_decode_append_buffer(request_st * const r, buffer * const mem)
|
|
|
|
{
|
2020-12-15 00:57:01 +00:00
|
|
|
/* Note: this routine is separate from http_chunk_decode_append_mem() to
|
|
|
|
* potentially avoid copying in http_chunk_append_buffer(). Otherwise this
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
* would be: return http_chunk_decode_append_mem(r, BUF_PTR_LEN(mem)); */
|
2020-12-15 00:57:01 +00:00
|
|
|
|
2021-07-24 04:59:48 +00:00
|
|
|
/*(called by funcs receiving chunked data from backends)*/
|
2020-07-28 11:32:29 +00:00
|
|
|
/*(separate from http_chunk_append_buffer() called by numerous others)*/
|
|
|
|
|
2020-12-15 00:57:01 +00:00
|
|
|
/* might avoid copy by transferring buffer if buffer is all data that is
|
|
|
|
* part of large chunked block, but choosing to *not* expand that out here*/
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (0 != http_chunk_decode_append_data(r, BUF_PTR_LEN(mem)))
|
2020-12-15 00:57:01 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* no need to decode chunked to immediately re-encode chunked;
|
|
|
|
* pass through chunked encoding as provided by backend,
|
|
|
|
* though it is still parsed (above) to maintain state.
|
|
|
|
* XXX: consider having callers use chunk buffers for hctx->b
|
|
|
|
* for more efficient data copy avoidance and buffer reuse
|
|
|
|
* note: r->resp_send_chunked = 0 until response headers sent,
|
|
|
|
* which is when Transfer-Encoding: chunked might be chosen */
|
2020-07-28 11:32:29 +00:00
|
|
|
if (r->resp_send_chunked) {
|
|
|
|
r->resp_send_chunked = 0;
|
|
|
|
int rc = http_chunk_append_buffer(r, mem); /* might append to tmpfile */
|
|
|
|
r->resp_send_chunked = 1;
|
|
|
|
return rc;
|
|
|
|
}
|
2021-09-09 20:12:37 +00:00
|
|
|
else
|
|
|
|
buffer_clear(mem);
|
2020-07-28 11:32:29 +00:00
|
|
|
|
2020-12-15 00:57:01 +00:00
|
|
|
return 0;
|
2020-07-28 11:32:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 05:32:24 +00:00
|
|
|
int http_chunk_decode_append_mem(request_st * const r, const char * const mem, size_t len)
|
2020-07-28 11:32:29 +00:00
|
|
|
{
|
2021-07-24 04:59:48 +00:00
|
|
|
/*(called by funcs receiving chunked data from backends)*/
|
2020-07-28 11:32:29 +00:00
|
|
|
/*(separate from http_chunk_append_mem() called by numerous others)*/
|
|
|
|
|
2020-12-15 00:57:01 +00:00
|
|
|
if (0 != http_chunk_decode_append_data(r, mem, (off_t)len))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* no need to decode chunked to immediately re-encode chunked;
|
|
|
|
* pass through chunked encoding as provided by backend,
|
|
|
|
* though it is still parsed (above) to maintain state.
|
|
|
|
* note: r->resp_send_chunked = 0 until response headers sent,
|
|
|
|
* which is when Transfer-Encoding: chunked might be chosen */
|
2020-07-28 11:32:29 +00:00
|
|
|
if (r->resp_send_chunked) {
|
|
|
|
r->resp_send_chunked = 0;
|
|
|
|
int rc = http_chunk_append_mem(r, mem, len); /*might append to tmpfile*/
|
|
|
|
r->resp_send_chunked = 1;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-12-15 00:57:01 +00:00
|
|
|
return 0;
|
2020-07-28 11:32:29 +00:00
|
|
|
}
|