]> granicus.if.org Git - libevent/commitdiff
http: do not use local settings for Date header
authorVis Virial <visvrial@gmail.com>
Thu, 10 Nov 2016 12:58:15 +0000 (21:58 +0900)
committerAzat Khuzhin <a3at.mail@gmail.com>
Sun, 18 Dec 2016 21:55:50 +0000 (00:55 +0300)
evutil_time.c
http.c
include/event2/util.h

index 32da2d6e8bfd5e2a3905f8cb8d87268a907ec7c4..3a8424ea3c7f4ec590c088807709fe183ac57a63 100644 (file)
@@ -145,6 +145,34 @@ evutil_usleep_(const struct timeval *tv)
 #endif
 }
 
+int
+evutil_date_rfc1123(char *date, const size_t datelen, struct tm *cur_p) {
+       static const char *DAYS[] =
+               { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
+       static const char *MONTHS[] =
+               { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
+
+       time_t t = time(NULL);
+
+       /* If `cur_p` is null, set system's current time. */
+       if (cur_p == NULL) {
+#ifdef _WIN32
+               cur_p = gmtime(&t);
+#else
+               {
+                       struct tm cur;
+                       gmtime_r(&t, &cur);
+                       cur_p = &cur;
+               }
+#endif
+       }
+
+       return evutil_snprintf(
+               date, datelen, "%s, %02d %s %4d %02d:%02d:%02d GMT",
+               DAYS[cur_p->tm_wday], cur_p->tm_mday, MONTHS[cur_p->tm_mon],
+               1900+cur_p->tm_year, cur_p->tm_hour, cur_p->tm_min, cur_p->tm_sec);
+}
+
 /*
    This function assumes it's called repeatedly with a
    not-actually-so-monotonic time source whose outputs are in 'tv'. It
diff --git a/http.c b/http.c
index 3c4116de531ce64ed75ddb14d91948a71892779a..f5a2ef931a5f3b5ef605eae807a5cfbc490442ee 100644 (file)
--- a/http.c
+++ b/http.c
@@ -80,7 +80,6 @@
 #include <syslog.h>
 #endif
 #include <signal.h>
-#include <time.h>
 #ifdef EVENT__HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -491,19 +490,7 @@ evhttp_maybe_add_date_header(struct evkeyvalq *headers)
 {
        if (evhttp_find_header(headers, "Date") == NULL) {
                char date[50];
-#ifndef _WIN32
-               struct tm cur;
-#endif
-               struct tm *cur_p;
-               time_t t = time(NULL);
-#ifdef _WIN32
-               cur_p = gmtime(&t);
-#else
-               gmtime_r(&t, &cur);
-               cur_p = &cur;
-#endif
-               if (strftime(date, sizeof(date),
-                       "%a, %d %b %Y %H:%M:%S GMT", cur_p) != 0) {
+               if (sizeof(date) - evutil_date_rfc1123(date, sizeof(date), NULL) > 0) {
                        evhttp_add_header(headers, "Date", date);
                }
        }
index 2c43e30e9539a333610d42803c06203f75d15df2..defe00a3a65ed450bb3b70e6ab50176992749e3a 100644 (file)
@@ -77,6 +77,8 @@ extern "C" {
 #include <sys/socket.h>
 #endif
 
+#include <time.h>
+
 /* Some openbsd autoconf versions get the name of this macro wrong. */
 #if defined(EVENT__SIZEOF_VOID__) && !defined(EVENT__SIZEOF_VOID_P)
 #define EVENT__SIZEOF_VOID_P EVENT__SIZEOF_VOID__
@@ -331,6 +333,15 @@ struct evutil_monotonic_timer
 #define EV_MONOT_PRECISE  1
 #define EV_MONOT_FALLBACK 2
 
+/** Format a date string using RFC 1123 format (used in HTTP).
+ * If `cur_p` is NULL, current system's time will be used.
+ * The number of characters written will be returned.
+ * One should check if the return value is smaller than `datelen` to check if
+ * the result is truncated or not.
+ */
+EVENT2_EXPORT_SYMBOL
+int evutil_date_rfc1123(char *date, const size_t datelen, struct tm *cur_p);
+
 /** Allocate a new struct evutil_monotonic_timer for use with the
  * evutil_configure_monotonic_time() and evutil_gettime_monotonic()
  * functions.  You must configure the timer with