summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorValentin Bartenev <vbart@nginx.com>2018-02-12 16:58:40 +0300
committerValentin Bartenev <vbart@nginx.com>2018-02-12 16:58:40 +0300
commit9646772a00e50c437e0b3204ce6233b6414e1053 (patch)
tree07d32260bd2b109883f865fdefb350f09023740a
parentdc50773e518f6e68109384cb37c91985dbc3fe63 (diff)
downloadunit-9646772a00e50c437e0b3204ce6233b6414e1053.tar.gz
unit-9646772a00e50c437e0b3204ce6233b6414e1053.tar.bz2
HTTP: the Date response header.
-rw-r--r--src/nxt_http.h1
-rw-r--r--src/nxt_http_request.c54
-rw-r--r--src/nxt_http_response.c2
-rw-r--r--test/test_python_application.py4
4 files changed, 59 insertions, 2 deletions
diff --git a/src/nxt_http.h b/src/nxt_http.h
index 384a8ea9..604c2883 100644
--- a/src/nxt_http.h
+++ b/src/nxt_http.h
@@ -89,6 +89,7 @@ typedef union {
typedef struct {
nxt_list_t *fields;
+ nxt_http_field_t *date;
nxt_http_field_t *content_type;
nxt_http_field_t *content_length;
nxt_off_t content_length_n;
diff --git a/src/nxt_http_request.c b/src/nxt_http_request.c
index 9c7d4451..0fbcdbbc 100644
--- a/src/nxt_http_request.c
+++ b/src/nxt_http_request.c
@@ -12,6 +12,9 @@ static void nxt_http_request_start(nxt_task_t *task, void *obj, void *data);
static void nxt_http_app_request(nxt_task_t *task, void *obj, void *data);
static void nxt_http_request_done(nxt_task_t *task, void *obj, void *data);
+static u_char *nxt_http_date(u_char *buf, nxt_realtime_t *now, struct tm *tm,
+ size_t size, const char *format);
+
static const nxt_http_request_state_t nxt_http_request_init_state;
static const nxt_http_request_state_t nxt_http_request_body_state;
@@ -258,10 +261,19 @@ void
nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r)
{
u_char *p, *end;
- nxt_http_field_t *server, *content_length;
+ nxt_http_field_t *server, *date, *content_length;
+
+ static nxt_time_string_t date_cache = {
+ (nxt_atomic_uint_t) -1,
+ nxt_http_date,
+ "%s, %02d %s %4d %02d:%02d:%02d GMT",
+ sizeof("Wed, 31 Dec 1986 16:40:00 GMT") - 1,
+ NXT_THREAD_TIME_GMT,
+ NXT_THREAD_TIME_SEC,
+ };
/*
- * TODO: "Server" and "Content-Length" processing should be moved
+ * TODO: "Server", "Date", and "Content-Length" processing should be moved
* to the last header filter.
*/
@@ -272,6 +284,27 @@ nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r)
nxt_http_field_set(server, "Server", "unit/" NXT_VERSION);
+ if (r->resp.date == NULL) {
+ date = nxt_list_zero_add(r->resp.fields);
+ if (nxt_slow_path(date == NULL)) {
+ goto fail;
+ }
+
+ nxt_http_field_name_set(date, "Date");
+
+ p = nxt_mp_nget(r->mem_pool, date_cache.size);
+ if (nxt_slow_path(p == NULL)) {
+ goto fail;
+ }
+
+ (void) nxt_thread_time_string(task->thread, &date_cache, p);
+
+ date->value = p;
+ date->value_length = date_cache.size;
+
+ r->resp.date = date;
+ }
+
if (r->resp.content_length_n != -1
&& (r->resp.content_length == NULL || r->resp.content_length->skip))
{
@@ -389,3 +422,20 @@ nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
handler(task, proto);
}
}
+
+
+static u_char *
+nxt_http_date(u_char *buf, nxt_realtime_t *now, struct tm *tm, size_t size,
+ const char *format)
+{
+ static const char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
+ "Sat" };
+
+ static const char *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
+
+ return nxt_sprintf(buf, buf + size, format,
+ week[tm->tm_wday], tm->tm_mday,
+ month[tm->tm_mon], tm->tm_year + 1900,
+ tm->tm_hour, tm->tm_min, tm->tm_sec);
+}
diff --git a/src/nxt_http_response.c b/src/nxt_http_response.c
index 330890c1..755182db 100644
--- a/src/nxt_http_response.c
+++ b/src/nxt_http_response.c
@@ -21,6 +21,8 @@ nxt_lvlhsh_t nxt_response_fields_hash;
static nxt_http_field_proc_t nxt_response_fields[] = {
{ nxt_string("Status"), &nxt_http_response_status, 0 },
{ nxt_string("Server"), &nxt_http_response_skip, 0 },
+ { nxt_string("Date"), &nxt_http_response_field,
+ offsetof(nxt_http_request_t, resp.date) },
{ nxt_string("Connection"), &nxt_http_response_skip, 0 },
{ nxt_string("Content-Type"), &nxt_http_response_field,
offsetof(nxt_http_request_t, resp.content_type) },
diff --git a/test/test_python_application.py b/test/test_python_application.py
index e5f1d02c..5f9debb8 100644
--- a/test/test_python_application.py
+++ b/test/test_python_application.py
@@ -1,3 +1,4 @@
+import time
import unittest
import unit
@@ -62,6 +63,9 @@ def application(environ, start_response):
headers = resp['headers']
self.assertRegex(headers.pop('Server'), r'unit/[\d\.]+',
'server header')
+ self.assertLess(abs(time.mktime(time.gmtime()) -
+ time.mktime(time.strptime(headers.pop('Date'),
+ '%a, %d %b %Y %H:%M:%S GMT'))), 5, 'date header')
self.assertDictEqual(headers, {
'Content-Length': str(len(body)),
'Content-Type': 'text/html',