Browse Source

[multiple] reduce direct use of srv->cur_ts

personal/stbuehler/ci-build
Glenn Strauss 2 years ago
parent
commit
409bba80b1
  1. 3
      src/base.h
  2. 42
      src/connections.c
  3. 7
      src/gw_backend.c
  4. 18
      src/log.c
  5. 4
      src/log.h
  6. 5
      src/mod_accesslog.c
  7. 13
      src/mod_auth.c
  8. 32
      src/mod_expire.c
  9. 25
      src/mod_secdownload.c
  10. 17
      src/mod_status.c
  11. 22
      src/mod_trigger_b4_dl.c
  12. 16
      src/response.c
  13. 4
      src/server.c
  14. 49
      src/stat_cache.c
  15. 7
      src/t/test_keyvalue.c
  16. 2
      src/t/test_request.c

3
src/base.h

@ -350,10 +350,7 @@ struct server {
/* Timestamps */
time_t cur_ts;
time_t last_generated_date_ts;
time_t last_generated_debug_ts;
buffer *ts_date_str;
log_error_st *errh;
server_config srvconf;

42
src/connections.c

@ -152,7 +152,7 @@ static int connection_close(server *srv, connection *con) {
return 0;
}
static void connection_read_for_eos_plain(server *srv, connection *con) {
static void connection_read_for_eos_plain(connection * const con, const time_t cur_ts) {
/* we have to do the linger_on_close stuff regardless
* of con->keep_alive; even non-keepalive sockets may
* still have unread data, and closing before reading
@ -172,34 +172,36 @@ static void connection_read_for_eos_plain(server *srv, connection *con) {
#endif
/* 0 == len || (len < 0 && (errno is a non-recoverable error)) */
con->close_timeout_ts = srv->cur_ts - (HTTP_LINGER_TIMEOUT+1);
con->close_timeout_ts = cur_ts - (HTTP_LINGER_TIMEOUT+1);
}
static void connection_read_for_eos_ssl(server *srv, connection *con) {
static void connection_read_for_eos_ssl(connection * const con, const time_t cur_ts) {
if (con->network_read(con, con->read_queue, MAX_READ_LIMIT) < 0)
con->close_timeout_ts = srv->cur_ts - (HTTP_LINGER_TIMEOUT+1);
con->close_timeout_ts = cur_ts - (HTTP_LINGER_TIMEOUT+1);
chunkqueue_reset(con->read_queue);
}
static void connection_read_for_eos(server *srv, connection *con) {
static void connection_read_for_eos(connection * const con, const time_t cur_ts) {
!con->is_ssl_sock
? connection_read_for_eos_plain(srv, con)
: connection_read_for_eos_ssl(srv, con);
? connection_read_for_eos_plain(con, cur_ts)
: connection_read_for_eos_ssl(con, cur_ts);
}
static void connection_handle_close_state(server *srv, connection *con) {
connection_read_for_eos(srv, con);
const time_t cur_ts = srv->cur_ts;
connection_read_for_eos(con, cur_ts);
if (srv->cur_ts - con->close_timeout_ts > HTTP_LINGER_TIMEOUT) {
if (cur_ts - con->close_timeout_ts > HTTP_LINGER_TIMEOUT) {
connection_close(srv, con);
}
}
static void connection_handle_shutdown(server *srv, connection *con) {
static void connection_handle_shutdown(connection *con) {
plugins_call_handle_connection_shut_wr(con);
srv->con_closed++;
connection_reset(con);
server * const srv = con->srv;
++srv->con_closed;
/* close the connection */
if (con->fd >= 0
@ -228,9 +230,7 @@ static void connection_handle_response_end_state(connection *con) {
plugins_call_handle_request_done(con);
}
server * const srv = con->srv;
if (con->state != CON_STATE_ERROR) srv->con_written++;
if (con->state != CON_STATE_ERROR) ++con->srv->con_written;
if (con->request.content_length != con->request_content_queue->bytes_in
|| con->state == CON_STATE_ERROR) {
@ -241,12 +241,12 @@ static void connection_handle_response_end_state(connection *con) {
if (con->keep_alive) {
connection_reset(con);
#if 0
con->request_start = srv->cur_ts;
con->read_idle_ts = srv->cur_ts;
con->request_start = con->srv->cur_ts;
con->read_idle_ts = con->srv->cur_ts;
#endif
connection_set_state(con, CON_STATE_REQUEST_START);
} else {
connection_handle_shutdown(srv, con);
connection_handle_shutdown(con);
}
}
@ -719,8 +719,7 @@ static void connection_discard_blank_line(connection *con, const char * const s,
static chunk * connection_read_header_more(connection *con, chunkqueue *cq, chunk *c, const size_t olen) {
if ((NULL == c || NULL == c->next) && con->is_readable) {
server * const srv = con->srv;
con->read_idle_ts = srv->cur_ts;
con->read_idle_ts = con->srv->cur_ts;
if (0 != con->network_read(con, cq, MAX_READ_LIMIT))
connection_set_state(con, CON_STATE_ERROR);
}
@ -905,7 +904,7 @@ static handler_t connection_handle_fdevent(server *srv, void *context, int reven
if (con->state == CON_STATE_CLOSE) {
/* flush the read buffers */
connection_read_for_eos(srv, con);
connection_read_for_eos(con, srv->cur_ts);
}
@ -1266,8 +1265,7 @@ int connection_state_machine(server *srv, connection *con) {
switch ((ostate = con->state)) {
case CON_STATE_REQUEST_START: /* transient */
con->request_start = srv->cur_ts;
con->read_idle_ts = srv->cur_ts;
con->request_start = con->read_idle_ts = srv->cur_ts;
if (con->conf.high_precision_timestamps)
log_clock_gettime_realtime(&con->request_start_hp);

7
src/gw_backend.c

@ -2676,6 +2676,7 @@ handler_t gw_handle_waitpid_cb(server *srv, void *p_d, pid_t pid, int status) {
* or global scope (for convenience))
* (unable to use p->defaults.debug since gw_plugin_config
* might be part of a larger plugin_config) */
const time_t cur_ts = srv->cur_ts;
gw_exts *exts = conf->exts;
for (uint32_t j = 0; j < exts->used; ++j) {
gw_extension *ex = exts->exts+j;
@ -2690,9 +2691,9 @@ handler_t gw_handle_waitpid_cb(server *srv, void *p_d, pid_t pid, int status) {
proc->pid = 0;
/* restart, but avoid spinning if child exits too quickly */
if (proc->disabled_until < srv->cur_ts) {
if (proc->disabled_until < cur_ts) {
if (proc->state != PROC_STATE_KILLED)
proc->disabled_until = srv->cur_ts;
proc->disabled_until = cur_ts;
if (gw_spawn_connection(srv, host, proc, debug)) {
log_error(srv->errh, __FILE__, __LINE__,
"ERROR: spawning gw failed.");
@ -2706,7 +2707,7 @@ handler_t gw_handle_waitpid_cb(server *srv, void *p_d, pid_t pid, int status) {
gw_proc_waitpid_log(srv, host, proc, status);
if (proc->state != PROC_STATE_KILLED)
proc->disabled_until = srv->cur_ts;
proc->disabled_until = cur_ts;
gw_proc_set_state(host, proc, PROC_STATE_DIED);
proc->pid = 0;
return HANDLER_FINISHED;

18
src/log.c

@ -72,19 +72,22 @@ ssize_t write_all(int fd, const void* buf, size_t count) {
}
static int log_buffer_prepare(const log_error_st *errh, const char *filename, unsigned int line, buffer *b) {
static time_t tlast;
static char tstr[20]; /* 20-chars needed for "%Y-%m-%d %H:%M:%S" */
static size_t tlen;
switch(errh->errorlog_mode) {
case ERRORLOG_PIPE:
case ERRORLOG_FILE:
case ERRORLOG_FD:
if (-1 == errh->errorlog_fd) return -1;
/* cache the generated timestamp */
if (*errh->last_ts != *errh->cur_ts) {
*errh->last_ts = *errh->cur_ts;
buffer_clear(errh->tb);
buffer_append_strftime(errh->tb, "%Y-%m-%d %H:%M:%S", localtime(errh->cur_ts));
if (tlast != *errh->cur_ts) {
tlast = *errh->cur_ts;
tlen = strftime(tstr, sizeof(tstr),
"%Y-%m-%d %H:%M:%S", localtime(&tlast));
}
buffer_copy_buffer(b, errh->tb);
buffer_copy_string_len(b, tstr, tlen);
buffer_append_string_len(b, CONST_STR_LEN(": ("));
break;
case ERRORLOG_SYSLOG:
@ -246,16 +249,14 @@ log_error_multiline_buffer (const log_error_st * const restrict errh,
log_error_st *
log_error_st_init (time_t *cur_ts_ptr, time_t *last_ts_ptr)
log_error_st_init (time_t *cur_ts_ptr)
{
log_error_st *errh = calloc(1, sizeof(log_error_st));
force_assert(errh);
errh->errorlog_fd = STDERR_FILENO;
errh->errorlog_mode = ERRORLOG_FD;
errh->b = buffer_init();
errh->tb = buffer_init();
errh->cur_ts = cur_ts_ptr;
errh->last_ts = last_ts_ptr;
return errh;
}
@ -264,7 +265,6 @@ void
log_error_st_free (log_error_st *errh)
{
if (NULL == errh) return;
buffer_free(errh->tb);
buffer_free(errh->b);
free(errh);
}

4
src/log.h

@ -14,13 +14,11 @@ struct log_error_st {
enum { ERRORLOG_FILE, ERRORLOG_FD, ERRORLOG_SYSLOG, ERRORLOG_PIPE } errorlog_mode;
int errorlog_fd;
buffer *b;
buffer *tb;
time_t *cur_ts;
time_t *last_ts;
};
__attribute_cold__
log_error_st * log_error_st_init (time_t *cur_ts_ptr, time_t *last_ts_ptr);
log_error_st * log_error_st_init (time_t *cur_ts_ptr);
__attribute_cold__
void log_error_st_free (log_error_st *errh);

5
src/mod_accesslog.c

@ -863,11 +863,12 @@ REQUESTDONE_FUNC(log_access_write) {
#endif /* HAVE_STRUCT_TM_GMTOFF */
if (!(f->opt & FORMAT_FLAG_TIME_BEGIN)) {
if (parsed_format->last_generated_accesslog_ts == con->srv->cur_ts) {
const time_t cur_ts = con->srv->cur_ts;
if (parsed_format->last_generated_accesslog_ts == cur_ts) {
buffer_append_string_buffer(b, ts_accesslog_str);
break;
}
t = parsed_format->last_generated_accesslog_ts = con->srv->cur_ts;
t = parsed_format->last_generated_accesslog_ts = cur_ts;
flush = 1;
} else {
t = con->request_start;

13
src/mod_auth.c

@ -1171,17 +1171,20 @@ static handler_t mod_auth_check_digest(connection *con, void *p_d, const struct
for (i = 0; i < 8 && light_isxdigit(nonce_uns[i]); ++i) {
ts = (ts << 4) + hex2int(nonce_uns[i]);
}
server * const srv = con->srv;
const time_t cur_ts = con->srv->cur_ts;
if (nonce[i] != ':'
|| ts > srv->cur_ts || srv->cur_ts - ts > 600) { /*(10 mins)*/
|| ts > cur_ts || cur_ts - ts > 600) { /*(10 mins)*/
/* nonce is stale; have client regenerate digest */
buffer_free(b);
return mod_auth_send_401_unauthorized_digest(con, require, ai.dalgo);
}
else if (srv->cur_ts - ts > 540) { /*(9 mins)*/
else if (cur_ts - ts > 540) { /*(9 mins)*/
/*(send nextnonce when expiration is approaching)*/
mod_auth_digest_authentication_info(srv->tmp_buf, srv->cur_ts, ai.dalgo);
http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("Authentication-Info"), CONST_BUF_LEN(srv->tmp_buf));
buffer * const tb = con->srv->tmp_buf;
mod_auth_digest_authentication_info(tb, cur_ts, ai.dalgo);
http_header_response_set(con, HTTP_HEADER_OTHER,
CONST_STR_LEN("Authentication-Info"),
CONST_BUF_LEN(tb));
}
}

32
src/mod_expire.c

@ -32,13 +32,11 @@ INIT_FUNC(mod_expire_init) {
return calloc(1, sizeof(plugin_data));
}
static int mod_expire_get_offset(log_error_st *errh, plugin_data *p, const buffer *expire, time_t *offset) {
static int mod_expire_get_offset(log_error_st *errh, const buffer *expire, time_t *offset) {
char *ts;
int type = -1;
time_t retts = 0;
UNUSED(p);
/*
* parse
*
@ -221,7 +219,7 @@ SETDEFAULTS_FUNC(mod_expire_set_defaults) {
for (uint32_t k = 0; k < cpv->v.a->used; ++k) {
/* parse lines */
data_string *ds = (data_string *)cpv->v.a->data[k];
if (-1==mod_expire_get_offset(srv->errh,p,&ds->value,NULL)){
if (-1 == mod_expire_get_offset(srv->errh,&ds->value,NULL)){
log_error(srv->errh, __FILE__, __LINE__,
"parsing expire.url failed: %s", ds->value.ptr);
return HANDLER_ERROR;
@ -248,7 +246,7 @@ SETDEFAULTS_FUNC(mod_expire_set_defaults) {
buffer_string_set_length(&ds->key, klen-1);
/* parse lines */
if (-1==mod_expire_get_offset(srv->errh,p,&ds->value,NULL)){
if (-1 == mod_expire_get_offset(srv->errh,&ds->value,NULL)){
log_error(srv->errh, __FILE__, __LINE__,
"parsing expire.mimetypes failed: %s", ds->value.ptr);
return HANDLER_ERROR;
@ -309,13 +307,13 @@ CONNECTION_FUNC(mod_expire_handler) {
if (NULL != vb) {
time_t ts, expires;
const time_t cur_ts = con->srv->cur_ts;
stat_cache_entry *sce = NULL;
server * const srv = con->srv;
switch(mod_expire_get_offset(con->conf.errh, p, vb, &ts)) {
switch(mod_expire_get_offset(con->conf.errh, vb, &ts)) {
case 0:
/* access */
expires = (ts + srv->cur_ts);
expires = (ts + cur_ts);
break;
case 1:
/* modification */
@ -335,21 +333,21 @@ CONNECTION_FUNC(mod_expire_handler) {
return HANDLER_ERROR;
}
/* expires should be at least srv->cur_ts */
if (expires < srv->cur_ts) expires = srv->cur_ts;
/* expires should be at least cur_ts */
if (expires < cur_ts) expires = cur_ts;
buffer * const b = srv->tmp_buf;
buffer * const tb = con->srv->tmp_buf;
/* HTTP/1.0 */
buffer_clear(b);
buffer_append_strftime(b, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&(expires)));
http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("Expires"), CONST_BUF_LEN(b));
buffer_clear(tb);
buffer_append_strftime(tb, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&(expires)));
http_header_response_set(con, HTTP_HEADER_OTHER, CONST_STR_LEN("Expires"), CONST_BUF_LEN(tb));
/* HTTP/1.1 */
buffer_copy_string_len(b, CONST_STR_LEN("max-age="));
buffer_append_int(b, expires - srv->cur_ts); /* as expires >= srv->cur_ts the difference is >= 0 */
buffer_copy_string_len(tb, CONST_STR_LEN("max-age="));
buffer_append_int(tb, expires - cur_ts); /* as expires >= cur_ts the difference is >= 0 */
http_header_response_set(con, HTTP_HEADER_CACHE_CONTROL, CONST_STR_LEN("Cache-Control"), CONST_BUF_LEN(b));
http_header_response_set(con, HTTP_HEADER_CACHE_CONTROL, CONST_STR_LEN("Cache-Control"), CONST_BUF_LEN(tb));
return HANDLER_GO_ON;
}

25
src/mod_secdownload.c

@ -470,11 +470,11 @@ URIHANDLER_FUNC(mod_secdownload_uri_handler) {
ts = (ts << 4) + hex2int(ts_str[i]);
}
server * const srv = con->srv;
const time_t cur_ts = con->srv->cur_ts;
/* timed-out */
if ( (srv->cur_ts > ts && (unsigned int) (srv->cur_ts - ts) > p->conf.timeout) ||
(srv->cur_ts < ts && (unsigned int) (ts - srv->cur_ts) > p->conf.timeout) ) {
if ( (cur_ts > ts && (unsigned int) (cur_ts - ts) > p->conf.timeout) ||
(cur_ts < ts && (unsigned int) (ts - cur_ts) > p->conf.timeout) ) {
/* "Gone" as the url will never be valid again instead of "408 - Timeout" where the request may be repeated */
con->http_status = 410;
@ -483,6 +483,8 @@ URIHANDLER_FUNC(mod_secdownload_uri_handler) {
rel_uri = ts_str + 8;
buffer * const tb = con->srv->tmp_buf;
if (p->conf.path_segments) {
const char *rel_uri_end = rel_uri;
unsigned int count = p->conf.path_segments;
@ -490,21 +492,20 @@ URIHANDLER_FUNC(mod_secdownload_uri_handler) {
rel_uri_end = strchr(rel_uri_end+1, '/');
} while (rel_uri_end && --count);
if (rel_uri_end) {
buffer_copy_string_len(srv->tmp_buf, protected_path,
buffer_copy_string_len(tb, protected_path,
rel_uri_end - protected_path);
protected_path = srv->tmp_buf->ptr;
protected_path = tb->ptr;
}
}
if (p->conf.hash_querystr && !buffer_is_empty(con->uri.query)) {
buffer *b = srv->tmp_buf;
if (protected_path != b->ptr) {
buffer_copy_string(b, protected_path);
if (protected_path != tb->ptr) {
buffer_copy_string(tb, protected_path);
}
buffer_append_string_len(b, CONST_STR_LEN("?"));
buffer_append_string_buffer(b, con->uri.query);
/* assign last in case b->ptr is reallocated */
protected_path = b->ptr;
buffer_append_string_len(tb, CONST_STR_LEN("?"));
buffer_append_string_buffer(tb, con->uri.query);
/* assign last in case tb->ptr is reallocated */
protected_path = tb->ptr;
}
if (!secdl_verify_mac(&p->conf, protected_path, mac_str, mac_len,

17
src/mod_status.c

@ -179,6 +179,7 @@ static handler_t mod_status_handle_server_status_html(server *srv, connection *c
char multiplier = '\0';
char buf[32];
time_t ts;
const time_t cur_ts = srv->cur_ts;
int days, hours, mins, seconds;
@ -302,7 +303,7 @@ static handler_t mod_status_handle_server_status_html(server *srv, connection *c
buffer_append_string_len(b, CONST_STR_LEN(")</td></tr>\n"));
buffer_append_string_len(b, CONST_STR_LEN("<tr><td>Uptime</td><td class=\"string\">"));
ts = srv->cur_ts - srv->startup_ts;
ts = cur_ts - srv->startup_ts;
days = ts / (60 * 60 * 24);
ts %= (60 * 60 * 24);
@ -371,7 +372,7 @@ static handler_t mod_status_handle_server_status_html(server *srv, connection *c
buffer_append_string_len(b, CONST_STR_LEN("<tr><th colspan=\"2\">average (since start)</th></tr>\n"));
buffer_append_string_len(b, CONST_STR_LEN("<tr><td>Requests</td><td class=\"string\">"));
avg = p->abs_requests / (srv->cur_ts - srv->startup_ts);
avg = p->abs_requests / (cur_ts - srv->startup_ts);
mod_status_get_multiplier(&avg, &multiplier, 1000);
@ -381,7 +382,7 @@ static handler_t mod_status_handle_server_status_html(server *srv, connection *c
buffer_append_string_len(b, CONST_STR_LEN("req/s</td></tr>\n"));
buffer_append_string_len(b, CONST_STR_LEN("<tr><td>Traffic</td><td class=\"string\">"));
avg = p->abs_traffic_out / (srv->cur_ts - srv->startup_ts);
avg = p->abs_traffic_out / (cur_ts - srv->startup_ts);
mod_status_get_multiplier(&avg, &multiplier, 1024);
@ -516,7 +517,7 @@ static handler_t mod_status_handle_server_status_html(server *srv, connection *c
buffer_append_string_len(b, CONST_STR_LEN("</td><td class=\"int\">"));
buffer_append_int(b, srv->cur_ts - c->request_start);
buffer_append_int(b, cur_ts - c->request_start);
buffer_append_string_len(b, CONST_STR_LEN("</td><td class=\"string\">"));
@ -571,7 +572,6 @@ static handler_t mod_status_handle_server_status_html(server *srv, connection *c
static handler_t mod_status_handle_server_status_text(server *srv, connection *con, plugin_data *p) {
buffer *b = chunkqueue_append_buffer_open(con->write_queue);
double avg;
time_t ts;
char buf[32];
/* output total number of requests */
@ -590,8 +590,7 @@ static handler_t mod_status_handle_server_status_text(server *srv, connection *c
/* output uptime */
buffer_append_string_len(b, CONST_STR_LEN("Uptime: "));
ts = srv->cur_ts - srv->startup_ts;
buffer_append_int(b, ts);
buffer_append_int(b, srv->cur_ts - srv->startup_ts);
buffer_append_string_len(b, CONST_STR_LEN("\n"));
/* output busy servers */
@ -630,7 +629,6 @@ static handler_t mod_status_handle_server_status_text(server *srv, connection *c
static handler_t mod_status_handle_server_status_json(server *srv, connection *con, plugin_data *p) {
buffer *b = chunkqueue_append_buffer_open(con->write_queue);
double avg;
time_t ts;
char buf[32];
uint32_t j;
unsigned int jsonp = 0;
@ -666,8 +664,7 @@ static handler_t mod_status_handle_server_status_json(server *srv, connection *c
/* output uptime */
buffer_append_string_len(b, CONST_STR_LEN("\t\"Uptime\": "));
ts = srv->cur_ts - srv->startup_ts;
buffer_append_int(b, ts);
buffer_append_int(b, srv->cur_ts - srv->startup_ts);
buffer_append_string_len(b, CONST_STR_LEN(",\n"));
/* output busy servers */

22
src/mod_trigger_b4_dl.c

@ -397,6 +397,8 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
log_error(con->conf.errh, __FILE__, __LINE__, "(debug) remote-ip: %s", remote_ip->ptr);
}
const time_t cur_ts = con->srv->cur_ts;
/* check if URL is a trigger -> insert IP into DB */
if ((n = pcre_exec(p->conf.trigger_regex, NULL, CONST_BUF_LEN(con->uri.path), 0, 0, ovec, 3 * N)) < 0) {
if (n != PCRE_ERROR_NOMATCH) {
@ -406,7 +408,6 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
return HANDLER_ERROR;
}
} else {
server * const srv = con->srv;
# if defined(HAVE_GDBM_H)
if (p->conf.db) {
/* the trigger matched */
@ -415,8 +416,8 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
*(const char **)&key.dptr = remote_ip->ptr;
key.dsize = buffer_string_length(remote_ip);
val.dptr = (char *)&(srv->cur_ts);
val.dsize = sizeof(srv->cur_ts);
val.dptr = (char *)&cur_ts;
val.dsize = sizeof(cur_ts);
if (0 != gdbm_store(p->conf.db, key, val, GDBM_REPLACE)) {
log_error(con->conf.errh, __FILE__, __LINE__, "insert failed");
@ -425,7 +426,7 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
# endif
# if defined(USE_MEMCACHED)
if (p->conf.memc) {
buffer * const b = srv->tmp_buf;
buffer * const b = con->srv->tmp_buf;
mod_trigger_b4_dl_memcached_key(b, p, remote_ip);
if (p->conf.debug) {
@ -434,7 +435,7 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
if (MEMCACHED_SUCCESS != memcached_set(p->conf.memc,
CONST_BUF_LEN(b),
(const char *)&(srv->cur_ts), sizeof(srv->cur_ts),
(const char *)&cur_ts, sizeof(cur_ts),
p->conf.trigger_timeout, 0)) {
log_error(con->conf.errh, __FILE__, __LINE__, "insert failed");
}
@ -451,7 +452,6 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
}
} else {
/* the download uri matched */
server * const srv = con->srv;
# if defined(HAVE_GDBM_H)
if (p->conf.db) {
datum key, val;
@ -471,7 +471,7 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
free(val.dptr);
if (srv->cur_ts - last_hit > p->conf.trigger_timeout) {
if (cur_ts - last_hit > p->conf.trigger_timeout) {
/* found, but timeout, redirect */
if (p->conf.db) {
@ -483,8 +483,8 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
return mod_trigger_b4_dl_deny(con, p);
}
val.dptr = (char *)&(srv->cur_ts);
val.dsize = sizeof(srv->cur_ts);
val.dptr = (char *)&cur_ts;
val.dsize = sizeof(cur_ts);
if (0 != gdbm_store(p->conf.db, key, val, GDBM_REPLACE)) {
log_error(con->conf.errh, __FILE__, __LINE__, "insert failed");
@ -493,7 +493,7 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
# endif
# if defined(USE_MEMCACHED)
if (p->conf.memc) {
buffer * const b = srv->tmp_buf;
buffer * const b = con->srv->tmp_buf;
mod_trigger_b4_dl_memcached_key(b, p, remote_ip);
if (p->conf.debug) {
@ -513,7 +513,7 @@ URIHANDLER_FUNC(mod_trigger_b4_dl_uri_handler) {
/* set a new timeout */
if (MEMCACHED_SUCCESS != memcached_set(p->conf.memc,
CONST_BUF_LEN(b),
(const char *)&(srv->cur_ts), sizeof(srv->cur_ts),
(const char *)&cur_ts, sizeof(cur_ts),
p->conf.trigger_timeout, 0)) {
log_error(con->conf.errh, __FILE__, __LINE__, "insert failed");
}

16
src/response.c

@ -95,18 +95,22 @@ int http_response_write_header(connection *con) {
}
if (!(con->response.htags & HTTP_HEADER_DATE)) {
static time_t tlast;
static char tstr[32]; /* 30-chars for "%a, %d %b %Y %H:%M:%S GMT" */
static size_t tlen;
/* HTTP/1.1 requires a Date: header */
buffer_append_string_len(b, CONST_STR_LEN("\r\nDate: "));
/* cache the generated timestamp */
server * const srv = con->srv;
if (srv->cur_ts != srv->last_generated_date_ts) {
buffer_clear(srv->ts_date_str);
buffer_append_strftime(srv->ts_date_str, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&(srv->cur_ts)));
srv->last_generated_date_ts = srv->cur_ts;
const time_t cur_ts = con->srv->cur_ts;
if (tlast != cur_ts) {
tlast = cur_ts;
tlen = strftime(tstr, sizeof(tstr),
"%a, %d %b %Y %H:%M:%S GMT", gmtime(&tlast));
}
buffer_append_string_buffer(b, srv->ts_date_str);
buffer_append_string_len(b, tstr, tlen);
}
if (!(con->response.htags & HTTP_HEADER_SERVER)) {

4
src/server.c

@ -237,7 +237,6 @@ static server *server_init(void) {
#define CLEAN(x) \
srv->x = buffer_init();
CLEAN(ts_date_str);
CLEAN(tmp_buf);
#undef CLEAN
@ -250,7 +249,7 @@ static server *server_init(void) {
srv->cur_ts = time(NULL);
srv->startup_ts = srv->cur_ts;
srv->errh = log_error_st_init(&srv->cur_ts, &srv->last_generated_debug_ts);
srv->errh = log_error_st_init(&srv->cur_ts);
config_init(srv);
@ -276,7 +275,6 @@ static void server_free(server *srv) {
#define CLEAN(x) \
buffer_free(srv->x);
CLEAN(ts_date_str);
CLEAN(tmp_buf);
#undef CLEAN

49
src/stat_cache.c

@ -430,9 +430,10 @@ static fam_dir_entry * fam_dir_monitor(server *srv, stat_cache_fam *scf, char *f
/* directory already registered */
}
const time_t cur_ts = srv->cur_ts;
struct stat lst;
int ck_dir = fn_is_dir;
if (!fn_is_dir && (NULL==fam_dir || srv->cur_ts - fam_dir->stat_ts >= 16)) {
if (!fn_is_dir && (NULL==fam_dir || cur_ts - fam_dir->stat_ts >= 16)) {
ck_dir = 1;
/*(temporarily modify fn)*/
fn[dirlen] = '\0';
@ -473,7 +474,7 @@ static fam_dir_entry * fam_dir_monitor(server *srv, stat_cache_fam *scf, char *f
fam_dir->st_dev = st->st_dev;
fam_dir->st_ino = st->st_ino;
}
fam_dir->stat_ts = srv->cur_ts;
fam_dir->stat_ts = cur_ts;
}
if (NULL == fam_dir) {
@ -489,7 +490,7 @@ static fam_dir_entry * fam_dir_monitor(server *srv, stat_cache_fam *scf, char *f
}
scf->dirs = splaytree_insert(scf->dirs, dir_ndx, fam_dir);
fam_dir->stat_ts= srv->cur_ts;
fam_dir->stat_ts= cur_ts;
fam_dir->st_dev = st->st_dev;
fam_dir->st_ino = st->st_ino;
}
@ -893,6 +894,7 @@ handler_t stat_cache_get_entry(connection *con, buffer *name, stat_cache_entry *
*/
server * const srv = con->srv;
const time_t cur_ts = srv->cur_ts;
sc = srv->stat_cache;
@ -909,7 +911,7 @@ handler_t stat_cache_get_entry(connection *con, buffer *name, stat_cache_entry *
if (buffer_is_equal_string(sce->name, name->ptr, len)) {
if (srv->srvconf.stat_cache_engine == STAT_CACHE_ENGINE_SIMPLE) {
if (sce->stat_ts == srv->cur_ts) {
if (sce->stat_ts == cur_ts) {
if (final_slash && !S_ISDIR(sce->st.st_mode)) {
errno = ENOTDIR;
return HANDLER_ERROR;
@ -924,7 +926,7 @@ handler_t stat_cache_get_entry(connection *con, buffer *name, stat_cache_entry *
/* re-stat() periodically, even if monitoring for changes
* (due to limitations in stat_cache.c use of FAM)
* (gaps due to not continually monitoring an entire tree) */
if (srv->cur_ts - sce->stat_ts < 16) {
if (cur_ts - sce->stat_ts < 16) {
if (final_slash && !S_ISDIR(sce->st.st_mode)) {
errno = ENOTDIR;
return HANDLER_ERROR;
@ -985,13 +987,13 @@ handler_t stat_cache_get_entry(connection *con, buffer *name, stat_cache_entry *
#if 0 /*(performed below)*/
if (NULL != sce->fam_dir) {
/*(may have been invalidated by dir change)*/
sce->stat_ts = srv->cur_ts;
sce->stat_ts = cur_ts;
}
#endif
}
#endif
sce->stat_ts = srv->cur_ts;
sce->stat_ts = cur_ts;
*ret_sce = sce;
return HANDLER_GO_ON;
@ -1066,36 +1068,29 @@ int stat_cache_open_rdonly_fstat (const buffer *name, struct stat *st, int symli
* and remove them in a second loop
*/
static int stat_cache_tag_old_entries(server *srv, splay_tree *t, int *keys, size_t *ndx, time_t max_age) {
stat_cache_entry *sce;
static void stat_cache_tag_old_entries(splay_tree * const t, int * const keys, size_t * const ndx, const time_t max_age, const time_t cur_ts) {
if (!t) return;
if (!t) return 0;
stat_cache_tag_old_entries(t->left, keys, ndx, max_age, cur_ts);
stat_cache_tag_old_entries(t->right, keys, ndx, max_age, cur_ts);
stat_cache_tag_old_entries(srv, t->left, keys, ndx, max_age);
stat_cache_tag_old_entries(srv, t->right, keys, ndx, max_age);
const stat_cache_entry * const sce = t->data;
sce = t->data;
if (srv->cur_ts - sce->stat_ts > max_age) {
keys[(*ndx)++] = t->key;
}
return 0;
if (cur_ts - sce->stat_ts > max_age) {
keys[(*ndx)++] = t->key;
}
}
static int stat_cache_periodic_cleanup(server *srv, time_t max_age) {
stat_cache *sc;
static void stat_cache_periodic_cleanup(stat_cache * const sc, const time_t max_age, const time_t cur_ts) {
size_t max_ndx = 0, i;
int *keys;
sc = srv->stat_cache;
if (!sc->files) return 0;
if (!sc->files) return;
keys = calloc(1, sizeof(int) * sc->files->size);
force_assert(NULL != keys);
stat_cache_tag_old_entries(srv, sc->files, keys, &max_ndx, max_age);
stat_cache_tag_old_entries(sc->files, keys, &max_ndx, max_age, cur_ts);
for (i = 0; i < max_ndx; i++) {
int ndx = keys[i];
@ -1112,8 +1107,6 @@ static int stat_cache_periodic_cleanup(server *srv, time_t max_age) {
}
free(keys);
return 0;
}
int stat_cache_trigger_cleanup(server *srv) {
@ -1132,7 +1125,7 @@ int stat_cache_trigger_cleanup(server *srv) {
}
#endif
stat_cache_periodic_cleanup(srv, max_age);
stat_cache_periodic_cleanup(srv->stat_cache, max_age, srv->cur_ts);
return 0;
}

7
src/t/test_keyvalue.c

@ -13,13 +13,10 @@
#ifdef HAVE_PCRE_H
static pcre_keyvalue_buffer * test_keyvalue_test_kvb_init (void) {
static time_t cur_ts;
pcre_keyvalue_buffer *kvb = pcre_keyvalue_buffer_init();
static server srv;
memset(&srv, 0, sizeof(srv));
log_error_st * const errh =
log_error_st_init(&srv.cur_ts, &srv.last_generated_debug_ts);
log_error_st * const errh = log_error_st_init(&cur_ts);
/* strings must be persistent for pcre_keyvalue_buffer_append() */
static const buffer kvstr[] = {

2
src/t/test_request.c

@ -581,7 +581,7 @@ int main (void)
connection con;
memset(&srv, 0, sizeof(server));
srv.errh = log_error_st_init(&srv.cur_ts, &srv.last_generated_debug_ts);
srv.errh = log_error_st_init(&srv.cur_ts);
srv.errh->errorlog_fd = -1; /* (disable) */
memset(&con, 0, sizeof(connection));

Loading…
Cancel
Save