5 static const size_t buf_capa
= sizeof("Thu, 01 Jan 1970 00:00:00 GMT");
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 */
18 static struct tm
* my_gmtime_r(time_t *now
, struct tm
*tm
)
20 struct tm
*global
= gmtime(now
);
25 # define gmtime_r my_gmtime_r
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
)
49 time_t now
= time(NULL
); /* not a syscall on modern 64-bit systems */
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",
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
);
81 rb_define_method(mod
, "httpdate", httpdate
, 0);