bfa11ca276feaa0d0332a7b650efbccda8ff5ba4
[unicorn.git] / ext / unicorn_http / httpdate.c
blobbfa11ca276feaa0d0332a7b650efbccda8ff5ba4
1 #include <ruby.h>
2 #include <time.h>
3 #include <stdio.h>
5 static const size_t buf_capa = sizeof("Thu, 01 Jan 1970 00:00:00 GMT");
6 static VALUE buf;
7 static char *buf_ptr;
8 static const char *const week[] = {
9 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
11 static const char *const months[] = {
12 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
13 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
16 /* for people on wonky systems only */
17 #ifndef HAVE_GMTIME_R
18 static struct tm * my_gmtime_r(time_t *now, struct tm *tm)
20 struct tm *global = gmtime(now);
21 if (global)
22 *tm = *global;
23 return tm;
25 # define gmtime_r my_gmtime_r
26 #endif
30 * Returns a string which represents the time as rfc1123-date of HTTP-date
31 * defined by RFC 2616:
33 * day-of-week, DD month-name CCYY hh:mm:ss GMT
35 * Note that the result is always GMT.
37 * This method is identical to Time#httpdate in the Ruby standard library,
38 * except it is implemented in C for performance. We always saw
39 * Time#httpdate at or near the top of the profiler output so we
40 * decided to rewrite this in C.
42 * Caveats: it relies on a Ruby implementation with the global VM lock,
43 * a thread-safe version will be provided when a Unix-only, GVL-free Ruby
44 * implementation becomes viable.
46 static VALUE httpdate(VALUE self)
48 static time_t last;
49 time_t now = time(NULL); /* not a syscall on modern 64-bit systems */
50 struct tm tm;
52 if (last == now)
53 return buf;
54 last = now;
55 gmtime_r(&now, &tm);
57 /* we can make this thread-safe later if our Ruby loses the GVL */
58 snprintf(buf_ptr, buf_capa,
59 "%s, %02d %s %4d %02d:%02d:%02d GMT",
60 week[tm.tm_wday],
61 tm.tm_mday,
62 months[tm.tm_mon],
63 tm.tm_year + 1900,
64 tm.tm_hour,
65 tm.tm_min,
66 tm.tm_sec);
68 return buf;
71 void init_unicorn_httpdate(void)
73 VALUE mod = rb_const_get(rb_cObject, rb_intern("Unicorn"));
74 mod = rb_define_module_under(mod, "HttpResponse");
76 buf = rb_str_new(0, buf_capa - 1);
77 rb_global_variable(&buf);
78 buf_ptr = RSTRING_PTR(buf);
79 httpdate(Qnil);
81 rb_define_method(mod, "httpdate", httpdate, 0);