Browse Source

add additional paramter format_ndx to worker_current_timestamp()

personal/stbuehler/wip
Thomas Porzelt 14 years ago
parent
commit
93bd5e2a6a
  1. 3
      include/lighttpd/server.h
  2. 12
      include/lighttpd/worker.h
  3. 2
      src/response.c
  4. 25
      src/server.c
  5. 41
      src/worker.c

3
include/lighttpd/server.h

@ -26,6 +26,7 @@ struct server {
struct worker *main_worker;
guint worker_count;
GArray *workers;
GArray *ts_formats; /** array of (GString*), add with server_ts_format_add() */
guint loop_flags;
ev_signal
@ -94,4 +95,6 @@ LI_API GString *server_current_timestamp();
LI_API void server_out_of_fds(server *srv);
LI_API guint server_ts_format_add(server *srv, GString* format);
#endif

12
include/lighttpd/worker.h

@ -41,6 +41,12 @@ struct statistics_t {
#define WORKER_UNLOCK(srv, lock) \
if ((srv)->worker_count > 1) g_static_rec_mutex_unlock(lock)
struct worker_ts {
ev_tstamp last_generated;
GString *str;
};
typedef struct worker_ts worker_ts;
struct worker {
struct server *srv;
@ -71,8 +77,8 @@ struct worker {
guint connection_load; /** incremented by server_accept_cb, decremented by worker_con_put. use atomic access */
ev_tstamp last_generated_date_ts;
GString *ts_date_str; /**< use worker_current_timestamp(wrk) */
GArray *timestamps; /** array of (worker_ts), use only from local worker context and through worker_current_timestamp(wrk, ndx) */
/* incoming queues */
/* - new connections (after accept) */
@ -101,7 +107,7 @@ LI_API void worker_new_con(worker *ctx, worker *wrk, sock_addr *remote_addr, int
LI_API void worker_check_keepalive(worker *wrk);
LI_API GString *worker_current_timestamp(worker *wrk);
LI_API GString* worker_current_timestamp(worker *wrk, guint format_ndx);
/* shutdown write and wait for eof before shutdown read and close */
LI_API void worker_add_closing_socket(worker *wrk, int fd);

2
src/response.c

@ -105,7 +105,7 @@ void response_send_headers(connection *con) {
}
if (!have_date) {
GString *d = worker_current_timestamp(con->wrk);
GString *d = worker_current_timestamp(con->wrk, 0);
/* HTTP/1.1 requires a Date: header */
g_string_append_len(head, CONST_STR_LEN("Date: "));
g_string_append_len(head, GSTR_LEN(d));

25
src/server.c

@ -74,6 +74,10 @@ server* server_new(const gchar *module_dir) {
srv->exiting = FALSE;
srv->ts_formats = g_array_new(FALSE, TRUE, sizeof(GString*));
/* error log ts format */
server_ts_format_add(srv, g_string_new("%a, %d %b %Y %H:%M:%S GMT"));
log_init(srv);
srv->io_timeout = 30; /* default I/O timeout */
@ -134,6 +138,13 @@ void server_free(server* srv) {
g_array_free(srv->sockets, TRUE);
}
{
guint i;
for (i = 0; i < srv->ts_formats->len; i++)
g_string_free(g_array_index(srv->ts_formats, GString*, i), TRUE);
g_array_free(srv->ts_formats, TRUE);
}
g_array_free(srv->option_def_values, TRUE);
server_plugins_free(srv);
g_array_free(srv->plugins_handle_close, TRUE);
@ -276,7 +287,7 @@ void server_start(server *srv) {
srv->started = ev_now(srv->main_worker->loop);
{
GString *str = worker_current_timestamp(srv->main_worker);
GString *str = worker_current_timestamp(srv->main_worker, 0);
srv->started = ev_now(srv->main_worker->loop);
srv->started_str = g_string_new_len(GSTR_LEN(str));
}
@ -360,3 +371,15 @@ GString *server_current_timestamp() {
void server_out_of_fds(server *srv) {
ERROR(srv, "%s", "Too many open files. Either raise your fd limit or use a lower connection limit.");
}
guint server_ts_format_add(server *srv, GString* format) {
/* check if not already registered */
guint i;
for (i = 0; i < srv->ts_formats->len; i++) {
if (g_string_equal(g_array_index(srv->ts_formats, GString*, i), format))
return i;
}
g_array_append_val(srv->ts_formats, format);
return i;
}

41
src/worker.c

@ -151,19 +151,25 @@ static void worker_job_queue_cb(struct ev_loop *loop, ev_timer *w, int revents)
}
}
/* cache timestamp */
GString *worker_current_timestamp(worker *wrk) {
time_t cur_ts = (time_t)CUR_TS(wrk);
if (cur_ts != wrk->last_generated_date_ts) {
g_string_set_size(wrk->ts_date_str, 255);
strftime(wrk->ts_date_str->str, wrk->ts_date_str->allocated_len,
"%a, %d %b %Y %H:%M:%S GMT", gmtime(&(cur_ts)));
GString *worker_current_timestamp(worker *wrk, guint format_ndx) {
gsize len;
worker_ts *wts = &g_array_index(wrk->timestamps, worker_ts, format_ndx);
ev_tstamp now = CUR_TS(wrk);
g_string_set_size(wrk->ts_date_str, strlen(wrk->ts_date_str->str));
/* cache hit */
if ((now - wts->last_generated) > 1.0)
return wts->str;
wrk->last_generated_date_ts = cur_ts;
}
return wrk->ts_date_str;
g_string_set_size(wts->str, 255);
len = strftime(wts->str->str, wts->str->allocated_len, g_array_index(wrk->srv->ts_formats, GString*, format_ndx)->str, gmtime((time_t*)(&wts->last_generated)));
if (len == 0)
return NULL;
g_string_set_size(wts->str, len);
wts->last_generated = now;
return wts->str;
}
/* stop worker watcher */
@ -280,8 +286,12 @@ worker* worker_new(struct server *srv, struct ev_loop *loop) {
wrk->tmp_str = g_string_sized_new(255);
wrk->last_generated_date_ts = 0;
wrk->ts_date_str = g_string_sized_new(255);
wrk->timestamps = g_array_sized_new(FALSE, TRUE, sizeof(worker_ts), srv->ts_formats->len);
{
guint i;
for (i = 0; i < srv->ts_formats->len; i++)
g_array_index(wrk->timestamps, worker_ts, i).str = g_string_sized_new(255);
}
ev_init(&wrk->worker_exit_watcher, worker_exit_cb);
wrk->worker_exit_watcher.data = wrk;
@ -353,8 +363,11 @@ void worker_free(worker *wrk) {
ev_ref(wrk->loop);
ev_async_stop(wrk->loop, &wrk->worker_exit_watcher);
g_string_free(wrk->tmp_str, TRUE);
g_string_free(wrk->ts_date_str, TRUE);
{ /* free timestamps */
guint i;
for (i = 0; i < wrk->timestamps->len; i++)
g_string_free(g_array_index(wrk->timestamps, worker_ts, i).str, TRUE);
}
g_async_queue_unref(wrk->new_con_queue);

Loading…
Cancel
Save