Browse Source

let mod_compress prepare etag, last-modified and content-type

git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-merge-1.4.x@569 152afb58-edef-0310-8abb-c4023f1b3aa9
svn/tags/lighttpd-1.4.2
Jan Kneschke 17 years ago
parent
commit
172340e22f
  1. 29
      src/http-header-glue.c
  2. 53
      src/mod_compress.c
  3. 69
      src/mod_staticfile.c

29
src/http-header-glue.c

@ -1,5 +1,6 @@
#include <string.h>
#include <errno.h>
#include <time.h>
#include "base.h"
#include "array.h"
@ -196,3 +197,31 @@ int http_response_redirect_to_directory(server *srv, connection *con) {
return 0;
}
buffer * strftime_cache_get(server *srv, time_t last_mod) {
struct tm *tm;
size_t i;
for (i = 0; i < FILE_CACHE_MAX; i++) {
/* found cache-entry */
if (srv->mtime_cache[i].mtime == last_mod) return srv->mtime_cache[i].str;
/* found empty slot */
if (srv->mtime_cache[i].mtime == 0) break;
}
if (i == FILE_CACHE_MAX) {
i = 0;
}
srv->mtime_cache[i].mtime = last_mod;
buffer_prepare_copy(srv->mtime_cache[i].str, 1024);
tm = gmtime(&(srv->mtime_cache[i].mtime));
srv->mtime_cache[i].str->used = strftime(srv->mtime_cache[i].str->ptr,
srv->mtime_cache[i].str->size - 1,
"%a, %d %b %Y %H:%M:%S GMT", tm);
srv->mtime_cache[i].str->used++;
return srv->mtime_cache[i].str;
}

53
src/mod_compress.c

@ -18,6 +18,7 @@
#include "plugin.h"
#include "crc32.h"
#include "etag.h"
#include "config.h"
@ -564,10 +565,9 @@ static int mod_compress_patch_connection(server *srv, connection *con, plugin_da
PHYSICALPATH_FUNC(mod_compress_physical) {
plugin_data *p = p_d;
size_t m, i;
size_t m;
off_t max_fsize;
stat_cache_entry *sce = NULL;
buffer *content_type;
/* only GET and POST can get compressed */
if (con->request.http_method != HTTP_METHOD_GET &&
@ -650,52 +650,25 @@ PHYSICALPATH_FUNC(mod_compress_physical) {
if (p->conf.compress_cache_dir->used) {
if (0 == deflate_file_to_file(srv, con, p,
con->physical.path, sce, compression_type)) {
struct tm *tm;
time_t last_mod;
buffer *mtime;
response_header_insert(srv, con, CONST_STR_LEN("Content-Encoding"), compression_name, strlen(compression_name));
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Encoding"), compression_name, strlen(compression_name));
/* Set Last-Modified of ORIGINAL file */
last_mod = sce->st.st_mtime;
for (i = 0; i < FILE_CACHE_MAX; i++) {
if (srv->mtime_cache[i].mtime == last_mod) break;
if (srv->mtime_cache[i].mtime == 0) {
srv->mtime_cache[i].mtime = last_mod;
buffer_prepare_copy(srv->mtime_cache[i].str, 1024);
tm = gmtime(&(srv->mtime_cache[i].mtime));
srv->mtime_cache[i].str->used = strftime(srv->mtime_cache[i].str->ptr,
srv->mtime_cache[i].str->size - 1,
"%a, %d %b %Y %H:%M:%S GMT", tm);
srv->mtime_cache[i].str->used++;
break;
}
}
if (i == FILE_CACHE_MAX) {
i = 0;
srv->mtime_cache[i].mtime = last_mod;
buffer_prepare_copy(srv->mtime_cache[i].str, 1024);
tm = gmtime(&(srv->mtime_cache[i].mtime));
srv->mtime_cache[i].str->used = strftime(srv->mtime_cache[i].str->ptr,
srv->mtime_cache[i].str->size - 1,
"%a, %d %b %Y %H:%M:%S GMT", tm);
srv->mtime_cache[i].str->used++;
}
response_header_insert(srv, con, CONST_STR_LEN("Last-Modified"), CONST_BUF_LEN(srv->mtime_cache[i].str));
mtime = strftime_cache_get(srv, sce->st.st_mtime);
response_header_overwrite(srv, con, CONST_STR_LEN("Last-Modified"), CONST_BUF_LEN(mtime));
etag_mutate(con->physical.etag, sce->etag);
response_header_overwrite(srv, con, CONST_STR_LEN("ETag"), CONST_BUF_LEN(con->physical.etag));
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type));
return HANDLER_GO_ON;
}
} else if (0 == deflate_file_to_buffer(srv, con, p,
con->physical.path, sce, compression_type)) {
response_header_insert(srv, con, CONST_STR_LEN("Content-Encoding"), compression_name, strlen(compression_name));
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Encoding"), compression_name, strlen(compression_name));
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type));
return HANDLER_GO_ON;
}

69
src/mod_staticfile.c

@ -340,39 +340,13 @@ static int http_response_parse_range(server *srv, connection *con, plugin_data *
return 0;
}
static buffer * strftime_cache_get(server *srv, time_t last_mod) {
struct tm *tm;
size_t i;
for (i = 0; i < FILE_CACHE_MAX; i++) {
/* found cache-entry */
if (srv->mtime_cache[i].mtime == last_mod) return srv->mtime_cache[i].str;
/* found empty slot */
if (srv->mtime_cache[i].mtime == 0) break;
}
if (i == FILE_CACHE_MAX) {
i = 0;
}
srv->mtime_cache[i].mtime = last_mod;
buffer_prepare_copy(srv->mtime_cache[i].str, 1024);
tm = gmtime(&(srv->mtime_cache[i].mtime));
srv->mtime_cache[i].str->used = strftime(srv->mtime_cache[i].str->ptr,
srv->mtime_cache[i].str->size - 1,
"%a, %d %b %Y %H:%M:%S GMT", tm);
srv->mtime_cache[i].str->used++;
return srv->mtime_cache[i].str;
}
URIHANDLER_FUNC(mod_staticfile_subrequest) {
plugin_data *p = p_d;
size_t k;
int s_len;
buffer *mtime;
stat_cache_entry *sce = NULL;
buffer *mtime;
data_string *ds;
/* someone else has done a decision for us */
if (con->http_status != 0) return HANDLER_GO_ON;
@ -398,8 +372,11 @@ URIHANDLER_FUNC(mod_staticfile_subrequest) {
/* ignore certain extensions */
for (k = 0; k < p->conf.exclude_ext->used; k++) {
data_string *ds = (data_string *)p->conf.exclude_ext->data[k];
int ct_len = ds->value->used - 1;
int ct_len;
ds = (data_string *)p->conf.exclude_ext->data[k];
ct_len = ds->value->used - 1;
if (ct_len > s_len) continue;
@ -437,24 +414,34 @@ URIHANDLER_FUNC(mod_staticfile_subrequest) {
return HANDLER_FINISHED;
}
/* mod_compress might set several data directly, don't overwrite them */
/* set response content-type */
if (buffer_is_empty(sce->content_type)) {
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("application/octet-stream"));
} else {
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type));
/* set response content-type, if not set already */
if (NULL == array_get_element(con->response.headers, "Content-Type")) {
if (buffer_is_empty(sce->content_type)) {
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("application/octet-stream"));
} else {
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type));
}
}
/* generate e-tag */
etag_mutate(con->physical.etag, sce->etag);
if (NULL == array_get_element(con->response.headers, "ETag")) {
/* generate e-tag */
etag_mutate(con->physical.etag, sce->etag);
response_header_overwrite(srv, con, CONST_STR_LEN("ETag"), CONST_BUF_LEN(con->physical.etag));
response_header_overwrite(srv, con, CONST_STR_LEN("ETag"), CONST_BUF_LEN(con->physical.etag));
}
response_header_overwrite(srv, con, CONST_STR_LEN("Accept-Ranges"), CONST_STR_LEN("bytes"));
/* prepare header */
mtime = strftime_cache_get(srv, sce->st.st_mtime);
response_header_overwrite(srv, con, CONST_STR_LEN("Last-Modified"), CONST_BUF_LEN(mtime));
if (NULL == (ds = (data_string *)array_get_element(con->response.headers, "Last-Modified"))) {
mtime = strftime_cache_get(srv, sce->st.st_mtime);
response_header_overwrite(srv, con, CONST_STR_LEN("Last-Modified"), CONST_BUF_LEN(mtime));
} else {
mtime = ds->value;
}
/*
* 14.26 If-None-Match

Loading…
Cancel
Save