version 1.1, 2013/10/14 10:32:47
|
version 1.1.1.3, 2016/11/02 10:35:00
|
Line 1
|
Line 1
|
|
#include "first.h" |
|
|
#include "base.h" |
#include "base.h" |
#include "log.h" |
#include "log.h" |
#include "buffer.h" |
#include "buffer.h" |
Line 43 INIT_FUNC(mod_expire_init) {
|
Line 45 INIT_FUNC(mod_expire_init) {
|
|
|
p->expire_tstmp = buffer_init(); |
p->expire_tstmp = buffer_init(); |
|
|
buffer_prepare_copy(p->expire_tstmp, 255); | buffer_string_prepare_copy(p->expire_tstmp, 255); |
|
|
return p; |
return p; |
} |
} |
Line 62 FREE_FUNC(mod_expire_free) {
|
Line 64 FREE_FUNC(mod_expire_free) {
|
size_t i; |
size_t i; |
for (i = 0; i < srv->config_context->used; i++) { |
for (i = 0; i < srv->config_context->used; i++) { |
plugin_config *s = p->config_storage[i]; |
plugin_config *s = p->config_storage[i]; |
if (!s) continue; |
|
|
|
|
if (NULL == s) continue; |
|
|
array_free(s->expire_url); |
array_free(s->expire_url); |
free(s); |
free(s); |
} |
} |
Line 90 static int mod_expire_get_offset(server *srv, plugin_d
|
Line 93 static int mod_expire_get_offset(server *srv, plugin_d
|
* e.g. 'access 1 years' |
* e.g. 'access 1 years' |
*/ |
*/ |
|
|
if (expire->used == 0) { | if (buffer_string_is_empty(expire)) { |
log_error_write(srv, __FILE__, __LINE__, "s", |
log_error_write(srv, __FILE__, __LINE__, "s", |
"empty:"); |
"empty:"); |
return -1; |
return -1; |
Line 221 SETDEFAULTS_FUNC(mod_expire_set_defaults) {
|
Line 224 SETDEFAULTS_FUNC(mod_expire_set_defaults) {
|
|
|
if (!p) return HANDLER_ERROR; |
if (!p) return HANDLER_ERROR; |
|
|
p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *)); | p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *)); |
|
|
for (i = 0; i < srv->config_context->used; i++) { |
for (i = 0; i < srv->config_context->used; i++) { |
|
data_config const* config = (data_config const*)srv->config_context->data[i]; |
plugin_config *s; |
plugin_config *s; |
|
|
s = calloc(1, sizeof(plugin_config)); |
s = calloc(1, sizeof(plugin_config)); |
Line 233 SETDEFAULTS_FUNC(mod_expire_set_defaults) {
|
Line 237 SETDEFAULTS_FUNC(mod_expire_set_defaults) {
|
|
|
p->config_storage[i] = s; |
p->config_storage[i] = s; |
|
|
if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv)) { | if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) { |
return HANDLER_ERROR; |
return HANDLER_ERROR; |
} |
} |
|
|
Line 288 URIHANDLER_FUNC(mod_expire_path_handler) {
|
Line 292 URIHANDLER_FUNC(mod_expire_path_handler) {
|
int s_len; |
int s_len; |
size_t k; |
size_t k; |
|
|
if (con->uri.path->used == 0) return HANDLER_GO_ON; | if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON; |
|
|
mod_expire_patch_connection(srv, con, p); |
mod_expire_patch_connection(srv, con, p); |
|
|
s_len = con->uri.path->used - 1; | s_len = buffer_string_length(con->uri.path); |
|
|
for (k = 0; k < p->conf.expire_url->used; k++) { |
for (k = 0; k < p->conf.expire_url->used; k++) { |
data_string *ds = (data_string *)p->conf.expire_url->data[k]; |
data_string *ds = (data_string *)p->conf.expire_url->data[k]; |
int ct_len = ds->key->used - 1; | int ct_len = buffer_string_length(ds->key); |
|
|
if (ct_len > s_len) continue; |
if (ct_len > s_len) continue; |
if (ds->key->used == 0) continue; | if (buffer_is_empty(ds->key)) continue; |
|
|
if (0 == strncmp(con->uri.path->ptr, ds->key->ptr, ct_len)) { |
if (0 == strncmp(con->uri.path->ptr, ds->key->ptr, ct_len)) { |
time_t ts, expires; |
time_t ts, expires; |
size_t len; |
|
stat_cache_entry *sce = NULL; |
stat_cache_entry *sce = NULL; |
|
|
stat_cache_get_entry(srv, con, con->physical.path, &sce); | /* if stat fails => sce == NULL, ignore return value */ |
| (void) stat_cache_get_entry(srv, con, con->physical.path, &sce); |
|
|
switch(mod_expire_get_offset(srv, p, ds->value, &ts)) { |
switch(mod_expire_get_offset(srv, p, ds->value, &ts)) { |
case 0: |
case 0: |
Line 316 URIHANDLER_FUNC(mod_expire_path_handler) {
|
Line 320 URIHANDLER_FUNC(mod_expire_path_handler) {
|
case 1: |
case 1: |
/* modification */ |
/* modification */ |
|
|
|
/* can't set modification based expire header if |
|
* mtime is not available |
|
*/ |
|
if (NULL == sce) return HANDLER_GO_ON; |
|
|
expires = (ts + sce->st.st_mtime); |
expires = (ts + sce->st.st_mtime); |
break; |
break; |
default: |
default: |
/* -1 is handled at parse-time */ |
/* -1 is handled at parse-time */ |
break; | return HANDLER_ERROR; |
} |
} |
|
|
/* expires should be at least srv->cur_ts */ |
/* expires should be at least srv->cur_ts */ |
if (expires < srv->cur_ts) expires = srv->cur_ts; |
if (expires < srv->cur_ts) expires = srv->cur_ts; |
|
|
if (0 == (len = strftime(p->expire_tstmp->ptr, p->expire_tstmp->size - 1, | buffer_string_prepare_copy(p->expire_tstmp, 255); |
"%a, %d %b %Y %H:%M:%S GMT", gmtime(&(expires))))) { | buffer_append_strftime(p->expire_tstmp, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&(expires))); |
/* could not set expire header, out of mem */ | |
|
|
return HANDLER_GO_ON; |
|
} |
|
|
|
p->expire_tstmp->used = len + 1; |
|
|
|
/* HTTP/1.0 */ |
/* HTTP/1.0 */ |
response_header_overwrite(srv, con, CONST_STR_LEN("Expires"), CONST_BUF_LEN(p->expire_tstmp)); |
response_header_overwrite(srv, con, CONST_STR_LEN("Expires"), CONST_BUF_LEN(p->expire_tstmp)); |
|
|
/* HTTP/1.1 */ |
/* HTTP/1.1 */ |
buffer_copy_string_len(p->expire_tstmp, CONST_STR_LEN("max-age=")); |
buffer_copy_string_len(p->expire_tstmp, CONST_STR_LEN("max-age=")); |
buffer_append_long(p->expire_tstmp, expires - srv->cur_ts); /* as expires >= srv->cur_ts the difference is >= 0 */ | buffer_append_int(p->expire_tstmp, expires - srv->cur_ts); /* as expires >= srv->cur_ts the difference is >= 0 */ |
|
|
response_header_append(srv, con, CONST_STR_LEN("Cache-Control"), CONST_BUF_LEN(p->expire_tstmp)); |
response_header_append(srv, con, CONST_STR_LEN("Cache-Control"), CONST_BUF_LEN(p->expire_tstmp)); |
|
|