2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "response.h"
|
2020-01-20 04:43:36 +00:00
|
|
|
#include "request.h"
|
2021-06-09 08:17:40 +00:00
|
|
|
#include "reqpool.h"
|
2018-03-25 07:45:05 +00:00
|
|
|
#include "base.h"
|
2017-05-06 05:23:37 +00:00
|
|
|
#include "fdevent.h"
|
2018-09-09 05:50:33 +00:00
|
|
|
#include "http_header.h"
|
2018-04-22 00:21:54 +00:00
|
|
|
#include "http_kv.h"
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "stat_cache.h"
|
|
|
|
#include "chunk.h"
|
2020-08-13 02:38:59 +00:00
|
|
|
#include "http_chunk.h"
|
2020-11-22 07:41:11 +00:00
|
|
|
#include "http_date.h"
|
2021-02-26 02:42:59 +00:00
|
|
|
#include "http_range.h"
|
2009-10-11 14:31:42 +00:00
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2021-04-28 17:18:37 +00:00
|
|
|
#include "sys-time.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-08-25 10:34:47 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
http_response_omit_header (request_st * const r, const data_string * const ds)
|
|
|
|
{
|
[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
|
|
|
const size_t klen = buffer_clen(&ds->key);
|
2019-06-06 04:24:05 +00:00
|
|
|
if (klen == sizeof("X-Sendfile")-1
|
2019-10-13 07:37:59 +00:00
|
|
|
&& buffer_eq_icase_ssn(ds->key.ptr, CONST_STR_LEN("X-Sendfile")))
|
2019-06-06 04:24:05 +00:00
|
|
|
return 1;
|
|
|
|
if (klen >= sizeof("X-LIGHTTPD-")-1
|
2019-10-13 07:37:59 +00:00
|
|
|
&& buffer_eq_icase_ssn(ds->key.ptr, CONST_STR_LEN("X-LIGHTTPD-"))) {
|
2019-06-06 04:24:05 +00:00
|
|
|
if (klen == sizeof("X-LIGHTTPD-KBytes-per-second")-1
|
2019-10-13 07:37:59 +00:00
|
|
|
&& buffer_eq_icase_ssn(ds->key.ptr+sizeof("X-LIGHTTPD-")-1,
|
2019-06-06 04:24:05 +00:00
|
|
|
CONST_STR_LEN("KBytes-per-second"))) {
|
|
|
|
/* "X-LIGHTTPD-KBytes-per-second" */
|
2019-11-16 01:26:54 +00:00
|
|
|
off_t limit = strtol(ds->value.ptr, NULL, 10) << 10; /*(*=1024)*/
|
2019-06-06 04:24:05 +00:00
|
|
|
if (limit > 0
|
2020-01-13 02:51:12 +00:00
|
|
|
&& (limit < r->conf.bytes_per_second
|
|
|
|
|| 0 == r->conf.bytes_per_second)) {
|
|
|
|
r->conf.bytes_per_second = limit;
|
2019-06-06 04:24:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-13 02:38:59 +00:00
|
|
|
|
2020-09-18 17:24:39 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static void
|
|
|
|
http_response_write_header_partial_1xx (request_st * const r, buffer * const b)
|
|
|
|
{
|
2020-09-23 14:05:04 +00:00
|
|
|
/* take data in con->write_queue and move into b
|
|
|
|
* (to be sent prior to final response headers in r->write_queue) */
|
2020-09-18 17:24:39 +00:00
|
|
|
connection * const con = r->con;
|
2020-09-29 20:50:39 +00:00
|
|
|
/*assert(&r->write_queue != con->write_queue);*/
|
2020-09-18 17:24:39 +00:00
|
|
|
chunkqueue * const cq = con->write_queue;
|
2020-09-29 20:50:39 +00:00
|
|
|
con->write_queue = &r->write_queue;
|
2020-09-23 14:05:04 +00:00
|
|
|
|
[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
|
|
|
/*assert(0 == buffer_clen(b));*//*expect empty buffer from caller*/
|
2020-09-23 14:05:04 +00:00
|
|
|
uint32_t len = (uint32_t)chunkqueue_length(cq);
|
|
|
|
/*(expecting MEM_CHUNK(s), so not expecting error reading files)*/
|
|
|
|
if (chunkqueue_read_data(cq, buffer_string_prepare_append(b, len),
|
|
|
|
len, r->conf.errh) < 0)
|
|
|
|
len = 0;
|
[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(b, len);/*expect initial empty buffer from caller*/
|
2020-09-23 14:05:04 +00:00
|
|
|
chunkqueue_free(cq);
|
2020-09-18 17:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-13 02:38:59 +00:00
|
|
|
void
|
|
|
|
http_response_write_header (request_st * const r)
|
|
|
|
{
|
2009-06-11 14:09:25 +00:00
|
|
|
/* disable keep-alive if requested */
|
2019-11-16 01:26:54 +00:00
|
|
|
|
2021-06-04 09:18:21 +00:00
|
|
|
r->con->keep_alive_idle = r->conf.max_keep_alive_idle;
|
|
|
|
if (__builtin_expect( (0 == r->conf.max_keep_alive_idle), 0)
|
|
|
|
|| r->con->request_count > r->conf.max_keep_alive_requests) {
|
2020-01-13 02:51:12 +00:00
|
|
|
r->keep_alive = 0;
|
|
|
|
} else if (0 != r->reqbody_length
|
2020-09-29 20:50:39 +00:00
|
|
|
&& r->reqbody_length != r->reqbody_queue.bytes_in
|
2020-06-01 07:52:18 +00:00
|
|
|
&& (NULL == r->handler_module
|
|
|
|
|| 0 == (r->conf.stream_request_body
|
|
|
|
& (FDEVENT_STREAM_REQUEST
|
|
|
|
| FDEVENT_STREAM_REQUEST_BUFMIN)))) {
|
2020-01-13 02:51:12 +00:00
|
|
|
r->keep_alive = 0;
|
2009-06-11 14:09:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 01:02:18 +00:00
|
|
|
if (light_btst(r->resp_htags, HTTP_HEADER_UPGRADE)
|
|
|
|
&& r->http_version == HTTP_VERSION_1_1) {
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_response_set(r, HTTP_HEADER_CONNECTION, CONST_STR_LEN("Connection"), CONST_STR_LEN("upgrade"));
|
2021-02-06 13:29:41 +00:00
|
|
|
} else if (r->keep_alive <= 0) {
|
2020-08-25 10:34:47 +00:00
|
|
|
http_header_response_set(r, HTTP_HEADER_CONNECTION, CONST_STR_LEN("Connection"), CONST_STR_LEN("close"));
|
2021-02-06 13:29:41 +00:00
|
|
|
} else if (r->http_version == HTTP_VERSION_1_0) {/*(&& r->keep_alive > 0)*/
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_response_set(r, HTTP_HEADER_CONNECTION, CONST_STR_LEN("Connection"), CONST_STR_LEN("keep-alive"));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-09-11 01:02:18 +00:00
|
|
|
if (304 == r->http_status
|
|
|
|
&& light_btst(r->resp_htags, HTTP_HEADER_CONTENT_ENCODING)) {
|
2020-01-13 02:51:12 +00:00
|
|
|
http_header_response_unset(r, HTTP_HEADER_CONTENT_ENCODING, CONST_STR_LEN("Content-Encoding"));
|
2018-09-09 05:50:33 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2021-06-04 09:18:21 +00:00
|
|
|
chunkqueue * const cq = &r->write_queue;
|
|
|
|
buffer * const b = chunkqueue_prepend_buffer_open(cq);
|
|
|
|
|
|
|
|
if (cq != r->con->write_queue)
|
|
|
|
http_response_write_header_partial_1xx(r, b);
|
|
|
|
|
|
|
|
buffer_append_string_len(b,
|
|
|
|
(r->http_version == HTTP_VERSION_1_1)
|
|
|
|
? "HTTP/1.1 "
|
|
|
|
: "HTTP/1.0 ",
|
|
|
|
sizeof("HTTP/1.1 ")-1);
|
|
|
|
http_status_append(b, r->http_status);
|
|
|
|
|
2018-09-09 05:50:33 +00:00
|
|
|
/* add all headers */
|
2021-06-04 09:18:21 +00:00
|
|
|
for (size_t i = 0, used = r->resp_headers.used; i < used; ++i) {
|
2020-01-13 02:51:12 +00:00
|
|
|
const data_string * const ds = (data_string *)r->resp_headers.data[i];
|
[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
|
|
|
const uint32_t klen = buffer_clen(&ds->key);
|
|
|
|
const uint32_t vlen = buffer_clen(&ds->value);
|
2021-06-04 09:18:21 +00:00
|
|
|
if (__builtin_expect( (0 == klen), 0)) continue;
|
|
|
|
if (__builtin_expect( (0 == vlen), 0)) continue;
|
2020-01-13 02:51:12 +00:00
|
|
|
if ((ds->key.ptr[0] & 0xdf) == 'X' && http_response_omit_header(r, ds))
|
2019-06-06 04:24:05 +00:00
|
|
|
continue;
|
2021-03-16 05:05:47 +00:00
|
|
|
char * restrict s = buffer_extend(b, klen+vlen+4);
|
|
|
|
s[0] = '\r';
|
|
|
|
s[1] = '\n';
|
|
|
|
memcpy(s+2, ds->key.ptr, klen);
|
|
|
|
s += 2+klen;
|
|
|
|
s[0] = ':';
|
|
|
|
s[1] = ' ';
|
|
|
|
memcpy(s+2, ds->value.ptr, vlen);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-09-11 01:02:18 +00:00
|
|
|
if (!light_btst(r->resp_htags, HTTP_HEADER_DATE)) {
|
2020-11-22 07:41:11 +00:00
|
|
|
/* HTTP/1.1 and later requires a Date: header */
|
2021-03-23 15:52:21 +00:00
|
|
|
/* "\r\nDate: " 8-chars + 30-chars "%a, %d %b %Y %T GMT" + '\0' */
|
2021-07-12 18:46:49 +00:00
|
|
|
static unix_time64_t tlast = 0;
|
2020-11-22 07:41:11 +00:00
|
|
|
static char tstr[40] = "\r\nDate: ";
|
2019-12-04 06:35:27 +00:00
|
|
|
|
2005-11-18 12:38:18 +00:00
|
|
|
/* cache the generated timestamp */
|
2021-07-12 18:46:49 +00:00
|
|
|
const unix_time64_t cur_ts = log_epoch_secs;
|
2020-11-22 07:41:11 +00:00
|
|
|
if (__builtin_expect ( (tlast != cur_ts), 0))
|
|
|
|
http_date_time_to_str(tstr+8, sizeof(tstr)-8, (tlast = cur_ts));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-11-22 07:41:11 +00:00
|
|
|
buffer_append_string_len(b, tstr, 37);
|
2005-11-18 12:38:18 +00:00
|
|
|
}
|
|
|
|
|
[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 (!light_btst(r->resp_htags, HTTP_HEADER_SERVER) && r->conf.server_tag)
|
2021-03-25 01:39:08 +00:00
|
|
|
buffer_append_str2(b, CONST_STR_LEN("\r\nServer: "),
|
[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
|
|
|
BUF_PTR_LEN(r->conf.server_tag));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN("\r\n\r\n"));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
[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
|
|
|
r->resp_header_len = buffer_clen(b);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->conf.log_response_header) {
|
2021-08-25 22:41:52 +00:00
|
|
|
log_error_multiline(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
BUF_PTR_LEN(b), "fd:%d resp: ", r->con->fd);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
chunkqueue_prepend_buffer_commit(cq);
|
2020-09-03 05:58:22 +00:00
|
|
|
|
|
|
|
/*(optimization to use fewer syscalls to send a small response)*/
|
|
|
|
off_t cqlen;
|
2020-09-11 01:02:18 +00:00
|
|
|
if (r->resp_body_finished
|
|
|
|
&& light_btst(r->resp_htags, HTTP_HEADER_CONTENT_LENGTH)
|
2020-09-03 05:58:22 +00:00
|
|
|
&& (cqlen = chunkqueue_length(cq) - r->resp_header_len) > 0
|
2021-10-27 03:27:51 +00:00
|
|
|
&& cqlen < 16384)
|
2020-09-03 05:58:22 +00:00
|
|
|
chunkqueue_small_resp_optim(cq);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-13 02:38:59 +00:00
|
|
|
|
2021-03-15 11:44:46 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static handler_t
|
|
|
|
http_response_physical_path_error (request_st * const r, const int code, const char * const msg)
|
|
|
|
{
|
|
|
|
r->http_status = code;
|
|
|
|
if ((code == 404 && r->conf.log_file_not_found)
|
|
|
|
|| r->conf.log_request_handling) {
|
|
|
|
if (NULL == msg)
|
|
|
|
log_perror(r->conf.errh, __FILE__, __LINE__, "-- ");
|
|
|
|
else
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__, "%s", msg);
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"Path : %s", r->physical.path.ptr);
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"URI : %s", r->uri.path.ptr);
|
|
|
|
}
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static handler_t http_response_physical_path_check(request_st * const r) {
|
2021-05-04 02:18:20 +00:00
|
|
|
stat_cache_entry *sce = stat_cache_get_entry(&r->physical.path);
|
2018-01-08 06:06:16 +00:00
|
|
|
|
2021-05-04 02:18:20 +00:00
|
|
|
if (__builtin_expect( (sce != NULL), 1)) {
|
2018-01-08 06:06:16 +00:00
|
|
|
/* file exists */
|
|
|
|
} else {
|
|
|
|
switch (errno) {
|
2021-03-15 11:44:46 +00:00
|
|
|
case ENOTDIR:
|
|
|
|
/* PATH_INFO ! :) */
|
|
|
|
break;
|
2018-01-08 06:06:16 +00:00
|
|
|
case EACCES:
|
2021-03-15 11:44:46 +00:00
|
|
|
return http_response_physical_path_error(r, 403, NULL);
|
2018-01-08 06:06:16 +00:00
|
|
|
case ENAMETOOLONG:
|
|
|
|
/* file name to be read was too long. return 404 */
|
|
|
|
case ENOENT:
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->http_method == HTTP_METHOD_OPTIONS
|
2020-10-09 06:58:41 +00:00
|
|
|
&& light_btst(r->resp_htags, HTTP_HEADER_ALLOW)) {
|
2020-01-13 02:51:12 +00:00
|
|
|
r->http_status = 200;
|
2019-03-26 02:37:31 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
2021-03-15 11:44:46 +00:00
|
|
|
return http_response_physical_path_error(r, 404, NULL);
|
2018-01-08 06:06:16 +00:00
|
|
|
default:
|
2020-02-22 22:24:12 +00:00
|
|
|
/* we have no idea what happened. let's tell the user so. */
|
2021-03-15 11:44:46 +00:00
|
|
|
return http_response_physical_path_error(r, 500, NULL);
|
2018-01-08 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* not found, perhaps PATHINFO */
|
|
|
|
|
2021-03-15 20:23:56 +00:00
|
|
|
char *pathinfo;
|
2018-01-08 06:06:16 +00:00
|
|
|
{
|
|
|
|
/*(might check at startup that s->document_root does not end in '/')*/
|
[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 = buffer_clen(&r->physical.basedir)
|
2021-03-15 20:23:56 +00:00
|
|
|
- (buffer_has_pathsep_suffix(&r->physical.basedir));
|
2020-01-13 02:51:12 +00:00
|
|
|
pathinfo = r->physical.path.ptr + len;
|
2018-10-05 03:32:41 +00:00
|
|
|
if ('/' != *pathinfo) {
|
|
|
|
pathinfo = NULL;
|
|
|
|
}
|
2020-01-13 02:51:12 +00:00
|
|
|
else if (pathinfo == r->physical.path.ptr) { /*(basedir is "/")*/
|
2018-10-05 03:32:41 +00:00
|
|
|
pathinfo = strchr(pathinfo+1, '/');
|
|
|
|
}
|
2018-01-08 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 21:26:37 +00:00
|
|
|
const uint32_t pathused = r->physical.path.used;
|
2018-02-02 11:10:24 +00:00
|
|
|
for (char *pprev = pathinfo; pathinfo; pprev = pathinfo, pathinfo = strchr(pathinfo+1, '/')) {
|
2021-03-15 21:26:37 +00:00
|
|
|
/*(temporarily modify r->physical.path in-place)*/
|
|
|
|
r->physical.path.used = pathinfo - r->physical.path.ptr + 1;
|
|
|
|
*pathinfo = '\0';
|
2021-05-04 02:18:20 +00:00
|
|
|
stat_cache_entry * const nsce = stat_cache_get_entry(&r->physical.path);
|
2021-03-15 21:26:37 +00:00
|
|
|
*pathinfo = '/';
|
|
|
|
r->physical.path.used = pathused;
|
2021-05-04 02:18:20 +00:00
|
|
|
if (NULL == nsce) {
|
2018-02-02 11:10:24 +00:00
|
|
|
pathinfo = pathinfo != pprev ? pprev : NULL;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-04 02:18:20 +00:00
|
|
|
sce = nsce;
|
|
|
|
if (!S_ISDIR(sce->st.st_mode)) break;
|
2018-01-08 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 02:18:20 +00:00
|
|
|
if (NULL == pathinfo || !S_ISREG(sce->st.st_mode)) {
|
2018-01-08 06:06:16 +00:00
|
|
|
/* no it really doesn't exists */
|
2021-03-15 11:44:46 +00:00
|
|
|
return http_response_physical_path_error(r, 404, "-- file not found");
|
2018-01-08 06:06:16 +00:00
|
|
|
}
|
2021-05-04 02:18:20 +00:00
|
|
|
/* note: historical behavior checks S_ISREG() above, permitting
|
|
|
|
* path-info only on regular files, not dirs or special files */
|
2018-01-08 06:06:16 +00:00
|
|
|
|
|
|
|
/* we have a PATHINFO */
|
|
|
|
if (pathinfo) {
|
2021-03-15 21:26:37 +00:00
|
|
|
size_t len = r->physical.path.ptr+pathused-1-pathinfo, reqlen;
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->conf.force_lowercase_filenames
|
[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
|
|
|
&& len <= (reqlen = buffer_clen(&r->target))
|
2020-01-13 02:51:12 +00:00
|
|
|
&& buffer_eq_icase_ssn(r->target.ptr + reqlen - len, pathinfo, len)) {
|
2018-01-08 06:06:16 +00:00
|
|
|
/* attempt to preserve case-insensitive PATH_INFO
|
|
|
|
* (works in common case where mod_alias, mod_magnet, and other modules
|
|
|
|
* have not modified the PATH_INFO portion of request URI, or did so
|
|
|
|
* with exactly the PATH_INFO desired) */
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_copy_string_len(&r->pathinfo, r->target.ptr + reqlen - len, len);
|
2018-01-08 06:06:16 +00:00
|
|
|
} else {
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_copy_string_len(&r->pathinfo, pathinfo, len);
|
2018-01-08 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* shorten uri.path
|
|
|
|
*/
|
|
|
|
|
[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(&r->uri.path, buffer_clen(&r->uri.path) - len);
|
|
|
|
buffer_truncate(&r->physical.path, (size_t)(pathinfo - r->physical.path.ptr));
|
2018-01-08 06:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (!r->conf.follow_symlink
|
|
|
|
&& 0 != stat_cache_path_contains_symlink(&r->physical.path, r->conf.errh)) {
|
2021-03-15 11:44:46 +00:00
|
|
|
return http_response_physical_path_error(r, 403, "-- access denied due to symlink restriction");
|
2019-04-29 05:50:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 02:18:20 +00:00
|
|
|
/* r->tmp_sce is valid in handle_subrequest_start callback --
|
|
|
|
* handle_subrquest_start callbacks should not change r->physical.path
|
|
|
|
* (or should invalidate r->tmp_sce). r->tmp_sce is not reset between
|
|
|
|
* requests and is valid only for sequential code after this func succeeds*/
|
|
|
|
r->tmp_sce = sce;
|
|
|
|
|
|
|
|
if (S_ISREG(sce->st.st_mode)) /*(common case)*/
|
2020-09-29 17:38:29 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
|
2021-05-04 02:18:20 +00:00
|
|
|
if (S_ISDIR(sce->st.st_mode)) {
|
2021-03-15 20:23:56 +00:00
|
|
|
if (!buffer_has_slash_suffix(&r->uri.path)) {
|
2020-01-13 02:51:12 +00:00
|
|
|
http_response_redirect_to_directory(r, 301);
|
2018-01-08 06:06:16 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
2020-09-29 17:38:29 +00:00
|
|
|
} else {
|
|
|
|
/* any special handling of other non-reg files ?*/
|
2018-01-08 06:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
2019-12-08 13:20:34 +00:00
|
|
|
__attribute_cold__
|
|
|
|
__attribute_noinline__
|
2020-01-13 02:51:12 +00:00
|
|
|
static handler_t http_status_set_error_close (request_st * const r, int status) {
|
|
|
|
r->keep_alive = 0;
|
|
|
|
r->resp_body_finished = 1;
|
|
|
|
r->handler_module = NULL;
|
|
|
|
r->http_status = status;
|
2019-12-08 13:20:34 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
|
2021-05-07 23:42:08 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static handler_t http_response_prepare_options_star (request_st * const r) {
|
|
|
|
r->http_status = 200;
|
|
|
|
r->resp_body_finished = 1;
|
|
|
|
http_header_response_append(r, HTTP_HEADER_ALLOW, CONST_STR_LEN("Allow"),
|
|
|
|
CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
__attribute_cold__
|
|
|
|
static handler_t http_response_prepare_connect (request_st * const r) {
|
|
|
|
return (r->handler_module)
|
|
|
|
? HANDLER_GO_ON
|
|
|
|
: http_status_set_error_close(r, 405);/* 405 Method Not Allowed */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-21 07:29:54 +00:00
|
|
|
static handler_t http_response_config (request_st * const r) {
|
|
|
|
config_cond_cache_reset(r);
|
|
|
|
config_patch_config(r);
|
|
|
|
|
[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
|
|
|
r->server_name = r->conf.server_name
|
|
|
|
? r->conf.server_name
|
|
|
|
: &r->uri.authority;
|
|
|
|
|
2020-07-21 18:15:58 +00:00
|
|
|
/* do we have to downgrade from 1.1 to 1.0 ? (ignore for HTTP/2) */
|
2021-05-07 20:14:14 +00:00
|
|
|
if (__builtin_expect( (!r->conf.allow_http11), 0)
|
|
|
|
&& r->http_version == HTTP_VERSION_1_1)
|
2020-01-21 07:29:54 +00:00
|
|
|
r->http_version = HTTP_VERSION_1_0;
|
|
|
|
|
2021-05-07 20:14:14 +00:00
|
|
|
if (__builtin_expect( (r->reqbody_length > 0), 0)
|
|
|
|
&& 0 != r->conf.max_request_size /* r->conf.max_request_size in kB */
|
|
|
|
&& (off_t)r->reqbody_length > ((off_t)r->conf.max_request_size << 10)) {
|
2020-01-21 07:29:54 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"request-size too long: %lld -> 413", (long long) r->reqbody_length);
|
|
|
|
return /* 413 Payload Too Large */
|
|
|
|
http_status_set_error_close(r, 413);
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
2020-08-13 02:38:59 +00:00
|
|
|
|
|
|
|
__attribute_cold__
|
|
|
|
static handler_t http_response_comeback (request_st * const r);
|
|
|
|
|
|
|
|
|
|
|
|
static handler_t
|
|
|
|
http_response_prepare (request_st * const r)
|
|
|
|
{
|
2020-01-21 02:05:56 +00:00
|
|
|
handler_t rc;
|
|
|
|
|
|
|
|
do {
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-20 04:43:36 +00:00
|
|
|
/* looks like someone has already made a decision */
|
2020-01-20 04:41:57 +00:00
|
|
|
if (r->http_status != 0 && r->http_status != 200) {
|
2020-01-20 04:43:36 +00:00
|
|
|
if (0 == r->resp_body_finished)
|
2020-01-13 02:51:12 +00:00
|
|
|
http_response_body_clear(r, 0);
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* no decision yet, build conf->filename */
|
[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(&r->physical.path)) {
|
2018-01-01 22:06:05 +00:00
|
|
|
|
2021-05-07 23:42:08 +00:00
|
|
|
if (__builtin_expect( (!r->async_callback), 1)) {
|
2020-01-21 07:29:54 +00:00
|
|
|
rc = http_response_config(r);
|
|
|
|
if (HANDLER_GO_ON != rc) continue;
|
|
|
|
}
|
2021-05-07 23:42:08 +00:00
|
|
|
else
|
|
|
|
r->async_callback = 0; /* reset */
|
2020-01-21 07:29:54 +00:00
|
|
|
|
2006-01-14 18:37:16 +00:00
|
|
|
/* we only come here when we have the parse the full request again
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
* a HANDLER_COMEBACK from mod_rewrite and mod_fastcgi might be a
|
2006-01-14 18:37:16 +00:00
|
|
|
* problem here as mod_setenv might get called multiple times
|
|
|
|
*
|
|
|
|
* fastcgi-auth might lead to a COMEBACK too
|
|
|
|
* fastcgi again dead server too
|
2020-01-20 04:43:36 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->conf.log_request_handling) {
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2020-01-21 07:29:54 +00:00
|
|
|
"-- parsed Request-URI");
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"Request-URI : %s", r->target.ptr);
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"URI-scheme : %s", r->uri.scheme.ptr);
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"URI-authority : %s", r->uri.authority.ptr);
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"URI-path (clean): %s", r->uri.path.ptr);
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"URI-query : %.*s",
|
2020-01-13 02:51:12 +00:00
|
|
|
BUFFER_INTLEN_PTR(&r->uri.query));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-01-01 22:06:05 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
rc = plugins_call_handle_uri_clean(r);
|
2020-01-21 02:05:56 +00:00
|
|
|
if (HANDLER_GO_ON != rc) continue;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2021-05-07 23:42:08 +00:00
|
|
|
if (__builtin_expect( (r->http_method == HTTP_METHOD_OPTIONS), 0)
|
|
|
|
&& r->uri.path.ptr[0] == '*' && r->uri.path.ptr[1] == '\0')
|
|
|
|
return http_response_prepare_options_star(r);
|
2005-08-19 00:05:52 +00:00
|
|
|
|
2021-05-07 23:42:08 +00:00
|
|
|
if (__builtin_expect( (r->http_method == HTTP_METHOD_CONNECT), 0))
|
|
|
|
return http_response_prepare_connect(r);
|
2018-04-16 01:38:37 +00:00
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2021-06-17 13:08:16 +00:00
|
|
|
/*
|
|
|
|
* border between logical and physical
|
|
|
|
* logical path (URI) becomes a physical filename
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
|
2021-06-17 13:08:16 +00:00
|
|
|
/* docroot: set r->physical.doc_root and might set r->server_name */
|
|
|
|
buffer_clear(&r->physical.doc_root);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2021-06-17 13:08:16 +00:00
|
|
|
rc = plugins_call_handle_docroot(r);
|
|
|
|
if (HANDLER_GO_ON != rc) continue;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
|
2021-06-17 13:08:16 +00:00
|
|
|
/* transform r->uri.path to r->physical.rel_path (relative file path) */
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_copy_buffer(&r->physical.rel_path, &r->uri.path);
|
2006-03-04 14:53:29 +00:00
|
|
|
#if defined(__WIN32) || defined(__CYGWIN__)
|
2006-02-22 12:50:56 +00:00
|
|
|
/* strip dots from the end and spaces
|
|
|
|
*
|
|
|
|
* windows/dos handle those filenames as the same file
|
|
|
|
*
|
|
|
|
* foo == foo. == foo..... == "foo... " == "foo.. ./"
|
|
|
|
*
|
|
|
|
* This will affect in some cases PATHINFO
|
|
|
|
*
|
2006-03-04 14:53:29 +00:00
|
|
|
* on native windows we could prepend the filename with \\?\ to circumvent
|
|
|
|
* this behaviour. I have no idea how to push this through cygwin
|
|
|
|
*
|
2006-02-22 12:50:56 +00:00
|
|
|
* */
|
2021-06-17 13:08:16 +00:00
|
|
|
{
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer *b = &r->physical.rel_path;
|
[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 = buffer_clen(b);
|
2006-02-22 12:50:56 +00:00
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
/* strip trailing " /" or "./" once */
|
|
|
|
if (len > 1 &&
|
|
|
|
b->ptr[len - 1] == '/' &&
|
|
|
|
(b->ptr[len - 2] == ' ' || b->ptr[len - 2] == '.')) {
|
|
|
|
len -= 2;
|
2006-02-22 12:50:56 +00:00
|
|
|
}
|
2015-02-08 19:10:44 +00:00
|
|
|
/* strip all trailing " " and "." */
|
|
|
|
while (len > 0 && ( ' ' == b->ptr[len-1] || '.' == b->ptr[len-1] ) ) --len;
|
[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(b, len);
|
2006-02-22 12:50:56 +00:00
|
|
|
}
|
2006-03-04 14:53:29 +00:00
|
|
|
#endif
|
2021-06-17 13:08:16 +00:00
|
|
|
/* MacOS X and Windows (typically) case-insensitive filesystems */
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->conf.force_lowercase_filenames) {
|
|
|
|
buffer_to_lower(&r->physical.rel_path);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2005-08-08 09:42:27 +00:00
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2021-06-17 13:08:16 +00:00
|
|
|
/* compose physical filename: physical.path = doc_root + rel_path */
|
|
|
|
if (buffer_is_unset(&r->physical.doc_root))
|
|
|
|
buffer_copy_buffer(&r->physical.doc_root, r->conf.document_root);
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_copy_buffer(&r->physical.basedir, &r->physical.doc_root);
|
2021-03-25 17:29:18 +00:00
|
|
|
buffer_copy_path_len2(&r->physical.path,
|
[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
|
|
|
BUF_PTR_LEN(&r->physical.doc_root),
|
|
|
|
BUF_PTR_LEN(&r->physical.rel_path));
|
2005-08-08 09:42:27 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
rc = plugins_call_handle_physical(r);
|
2020-01-21 02:05:56 +00:00
|
|
|
if (HANDLER_GO_ON != rc) continue;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->conf.log_request_handling) {
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"-- logical -> physical");
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"Doc-Root : %s", r->physical.doc_root.ptr);
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"Basedir : %s", r->physical.basedir.ptr);
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"Rel-Path : %s", r->physical.rel_path.ptr);
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"Path : %s", r->physical.path.ptr);
|
2019-11-25 06:54:08 +00:00
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-20 04:41:57 +00:00
|
|
|
if (NULL != r->handler_module) return HANDLER_GO_ON;
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
/*
|
2020-02-22 22:24:12 +00:00
|
|
|
* No module grabbed the request yet (like mod_access)
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2021-05-04 02:18:20 +00:00
|
|
|
* Go on and check if the file exists at all
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
rc = http_response_physical_path_check(r);
|
2020-01-21 02:05:56 +00:00
|
|
|
if (HANDLER_GO_ON != rc) continue;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2021-05-04 02:18:20 +00:00
|
|
|
/* r->physical.path is non-empty and exists in the filesystem */
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->conf.log_request_handling) {
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|