[core] Add liTimeFunc (gmtime_r, localtime_r) parameter to li_worker_current_timestamp()

personal/stbuehler/wip
Thomas Porzelt 14 years ago
parent 75237dda62
commit e838c9b3cf

@ -15,6 +15,9 @@ typedef enum {
typedef enum { TRI_FALSE, TRI_MAYBE, TRI_TRUE } tristate_t;
typedef enum { LI_GMTIME, LI_LOCALTIME } liTimeFunc;
typedef enum { LI_TS_FORMAT_DEFAULT, LI_TS_FORMAT_HEADER } liTSFormat;
/* structs from headers, in alphabetic order */
/* actions.h */

@ -77,7 +77,8 @@ struct liWorker {
guint connection_load; /** incremented by server_accept_cb, decremented by worker_con_put. use atomic access */
GArray *timestamps; /** array of (worker_ts), use only from local worker context and through li_worker_current_timestamp(wrk, ndx) */
GArray *timestamps_gmt; /** array of (worker_ts), use only from local worker context and through li_worker_current_timestamp(wrk, LI_GMTIME, ndx) */
GArray *timestamps_local;
/* incoming queues */
/* - new connections (after accept) */
@ -111,7 +112,7 @@ LI_API void li_worker_new_con(liWorker *ctx, liWorker *wrk, liSocketAddress remo
LI_API void li_worker_check_keepalive(liWorker *wrk);
LI_API GString* li_worker_current_timestamp(liWorker *wrk, guint format_ndx);
LI_API GString* li_worker_current_timestamp(liWorker *wrk, liTimeFunc, guint format_ndx);
/* shutdown write and wait for eof before shutdown read and close */
LI_API void li_worker_add_closing_socket(liWorker *wrk, int fd);

@ -108,7 +108,7 @@ void li_response_send_headers(liConnection *con) {
}
if (!have_date) {
GString *d = li_worker_current_timestamp(con->wrk, 0);
GString *d = li_worker_current_timestamp(con->wrk, LI_GMTIME, LI_TS_FORMAT_HEADER);
/* HTTP/1.1 requires a Date: header */
g_string_append_len(head, CONST_STR_LEN("Date: "));
g_string_append_len(head, GSTR_LEN(d));

@ -108,6 +108,8 @@ liServer* li_server_new(const gchar *module_dir) {
srv->ts_formats = g_array_new(FALSE, TRUE, sizeof(GString*));
/* error log ts format */
li_server_ts_format_add(srv, g_string_new("%a, %d %b %Y %H:%M:%S %Z"));
/* http header ts format */
li_server_ts_format_add(srv, g_string_new("%a, %d %b %Y %H:%M:%S GMT"));
srv->throttle_pools = g_array_new(FALSE, TRUE, sizeof(liThrottlePool*));
@ -342,7 +344,7 @@ void li_server_start(liServer *srv) {
srv->started = ev_now(srv->main_worker->loop);
{
GString *str = li_worker_current_timestamp(srv->main_worker, 0);
GString *str = li_worker_current_timestamp(srv->main_worker, LI_LOCALTIME, LI_TS_FORMAT_DEFAULT);
srv->started = ev_now(srv->main_worker->loop);
srv->started_str = g_string_new_len(GSTR_LEN(str));
}

@ -154,25 +154,37 @@ static void worker_job_async_queue_cb(struct ev_loop *loop, ev_async *w, int rev
/* cache timestamp */
GString *li_worker_current_timestamp(liWorker *wrk, guint format_ndx) {
GString *li_worker_current_timestamp(liWorker *wrk, liTimeFunc timefunc, guint format_ndx) {
gsize len;
struct tm tm;
liWorkerTS *wts = &g_array_index(wrk->timestamps, liWorkerTS, format_ndx);
liWorkerTS *wts;
time_t now = (time_t)CUR_TS(wrk);
if (timefunc == LI_GMTIME)
wts = &g_array_index(wrk->timestamps_gmt, liWorkerTS, format_ndx);
else
wts = &g_array_index(wrk->timestamps_local, liWorkerTS, format_ndx);
/* cache hit */
if (now == wts->last_generated)
return wts->str;
g_string_set_size(wts->str, 255);
if (!gmtime_r(&now, &tm))
if (timefunc == LI_GMTIME) {
if (!gmtime_r(&now, &tm))
return NULL;
} else if (!localtime_r(&now, &tm))
return NULL;
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, &tm);
if (len == 0)
return NULL;
g_string_set_size(wts->str, len);
wts->last_generated = now;
return wts->str;
}
@ -292,12 +304,19 @@ liWorker* li_worker_new(liServer *srv, struct ev_loop *loop) {
wrk->tmp_str = g_string_sized_new(255);
wrk->timestamps = g_array_sized_new(FALSE, TRUE, sizeof(liWorkerTS), srv->ts_formats->len);
g_array_set_size(wrk->timestamps, srv->ts_formats->len);
wrk->timestamps_gmt = g_array_sized_new(FALSE, TRUE, sizeof(liWorkerTS), srv->ts_formats->len);
g_array_set_size(wrk->timestamps_gmt, srv->ts_formats->len);
{
guint i;
for (i = 0; i < srv->ts_formats->len; i++)
g_array_index(wrk->timestamps_gmt, liWorkerTS, i).str = g_string_sized_new(255);
}
wrk->timestamps_local = g_array_sized_new(FALSE, TRUE, sizeof(liWorkerTS), srv->ts_formats->len);
g_array_set_size(wrk->timestamps_local, srv->ts_formats->len);
{
guint i;
for (i = 0; i < srv->ts_formats->len; i++)
g_array_index(wrk->timestamps, liWorkerTS, i).str = g_string_sized_new(255);
g_array_index(wrk->timestamps_local, liWorkerTS, i).str = g_string_sized_new(255);
}
ev_init(&wrk->li_worker_exit_watcher, li_worker_exit_cb);
@ -378,9 +397,12 @@ void li_worker_free(liWorker *wrk) {
{ /* free timestamps */
guint i;
for (i = 0; i < wrk->timestamps->len; i++)
g_string_free(g_array_index(wrk->timestamps, liWorkerTS, i).str, TRUE);
g_array_free(wrk->timestamps, TRUE);
for (i = 0; i < wrk->timestamps_gmt->len; i++) {
g_string_free(g_array_index(wrk->timestamps_gmt, liWorkerTS, i).str, TRUE);
g_string_free(g_array_index(wrk->timestamps_local, liWorkerTS, i).str, TRUE);
}
g_array_free(wrk->timestamps_gmt, TRUE);
g_array_free(wrk->timestamps_local, TRUE);
}
ev_ref(wrk->loop);

@ -36,7 +36,7 @@ LI_API gboolean mod_accesslog_init(liModules *mods, liModule *mod);
LI_API gboolean mod_accesslog_free(liModules *mods, liModule *mod);
struct al_data {
guint ts_ndx_gmtime;
guint ts_ndx;
};
typedef struct al_data al_data;
@ -304,7 +304,7 @@ static GString *al_format_log(liConnection *con, al_data *ald, GArray *format) {
break;
case AL_FORMAT_TIME:
/* todo: implement format string */
tmp_gstr2 = li_worker_current_timestamp(con->wrk, ald->ts_ndx_gmtime);
tmp_gstr2 = li_worker_current_timestamp(con->wrk, LI_LOCALTIME, ald->ts_ndx);
g_string_append_len(str, GSTR_LEN(tmp_gstr2));
break;
case AL_FORMAT_AUTHED_USER:
@ -487,7 +487,7 @@ static void plugin_accesslog_init(liServer *srv, liPlugin *p) {
p->handle_close = al_handle_close;
ald = g_slice_new0(al_data);
ald->ts_ndx_gmtime = li_server_ts_format_add(srv, g_string_new_len(CONST_STR_LEN("[%d/%b/%Y:%H:%M:%S +0000]")));
ald->ts_ndx = li_server_ts_format_add(srv, g_string_new_len(CONST_STR_LEN("[%d/%b/%Y:%H:%M:%S +%z]")));
p->data = ald;
}

Loading…
Cancel
Save