6 static const size_t buf_capa
= sizeof("Thu, 01 Jan 1970 00:00:00 GMT");
9 static const char week
[] = "Sun\0Mon\0Tue\0Wed\0Thu\0Fri\0Sat";
10 static const char months
[] = "Jan\0Feb\0Mar\0Apr\0May\0Jun\0"
11 "Jul\0Aug\0Sep\0Oct\0Nov\0Dec";
13 /* for people on wonky systems only */
15 # warning using fake gmtime_r
16 static struct tm
* my_gmtime_r(time_t *now
, struct tm
*tm
)
18 struct tm
*global
= gmtime(now
);
23 # define gmtime_r my_gmtime_r
28 * Returns a string which represents the time as rfc1123-date of HTTP-date
29 * defined by RFC 2616:
31 * day-of-week, DD month-name CCYY hh:mm:ss GMT
33 * Note that the result is always GMT.
35 * This method is identical to Time#httpdate in the Ruby standard library,
36 * except it is implemented in C for performance. We always saw
37 * Time#httpdate at or near the top of the profiler output so we
38 * decided to rewrite this in C.
40 * Caveats: it relies on a Ruby implementation with the global VM lock,
41 * a thread-safe version will be provided when a Unix-only, GVL-free Ruby
42 * implementation becomes viable.
44 static VALUE
httpdate(VALUE self
)
51 * Favor gettimeofday(2) over time(2), as the latter can return the
52 * wrong value in the first 1 .. 2.5 ms of every second(!)
54 * https://lore.kernel.org/git/20230320230507.3932018-1-gitster@pobox.com/
55 * https://inbox.sourceware.org/libc-alpha/20230306160321.2942372-1-adhemerval.zanella@linaro.org/T/
56 * https://sourceware.org/bugzilla/show_bug.cgi?id=30200
58 if (gettimeofday(&now
, NULL
))
59 rb_sys_fail("gettimeofday");
61 if (last
== now
.tv_sec
)
64 gmtime_r(&now
.tv_sec
, &tm
);
66 /* we can make this thread-safe later if our Ruby loses the GVL */
67 snprintf(buf_ptr
, buf_capa
,
68 "%s, %02d %s %4d %02d:%02d:%02d GMT",
69 week
+ (tm
.tm_wday
* 4),
71 months
+ (tm
.tm_mon
* 4),
80 void init_unicorn_httpdate(void)
82 VALUE mod
= rb_define_module("Unicorn");
83 mod
= rb_define_module_under(mod
, "HttpResponse");
85 buf
= rb_str_new(0, buf_capa
- 1);
86 rb_gc_register_mark_object(buf
);
87 buf_ptr
= RSTRING_PTR(buf
);
90 rb_define_method(mod
, "httpdate", httpdate
, 0);