10 #include <sys/types.h>
18 #include "ruby_1_9_compat.h"
20 /* in case _BSD_SOURCE doesn't give us this macro */
22 # define timersub(a, b, result) \
24 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
25 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
26 if ((result)->tv_usec < 0) { \
28 (result)->tv_usec += 1000000; \
33 /* give GCC hints for better branch prediction
34 * (we layout branches so that ASCII characters are handled faster) */
35 #if defined(__GNUC__) && (__GNUC__ >= 3)
36 # define likely(x) __builtin_expect (!!(x), 1)
37 # define unlikely(x) __builtin_expect (!!(x), 0)
39 # define unlikely(x) (x)
40 # define likely(x) (x)
56 enum clogger_special
{
57 CL_SP_body_bytes_sent
= 0,
61 CL_SP_response_length
,
80 off_t body_bytes_sent
;
81 struct timeval tv_start
;
86 int reentrant
; /* tri-state, -1:auto, 1/0 true/false */
96 static ID sq_brace_id
;
100 static VALUE cClogger
;
101 static VALUE cToPath
;
102 static VALUE mFormat
;
103 static VALUE cHeaderHash
;
105 /* common hash lookup keys */
106 static VALUE g_HTTP_X_FORWARDED_FOR
;
107 static VALUE g_REMOTE_ADDR
;
108 static VALUE g_REQUEST_METHOD
;
109 static VALUE g_PATH_INFO
;
110 static VALUE g_REQUEST_URI
;
111 static VALUE g_QUERY_STRING
;
112 static VALUE g_HTTP_VERSION
;
113 static VALUE g_rack_errors
;
114 static VALUE g_rack_input
;
115 static VALUE g_rack_multithread
;
117 static VALUE g_space
;
118 static VALUE g_question_mark
;
119 static VALUE g_rack_request_cookie_hash
;
121 #define LOG_BUF_INIT_SIZE 128
123 static void init_buffers(struct clogger
*c
)
125 c
->log_buf
= rb_str_buf_new(LOG_BUF_INIT_SIZE
);
128 static inline int need_escape(unsigned c
)
131 return !!(c
== '\'' || c
== '"' || c
<= 0x1f);
134 /* we are encoding-agnostic, clients can send us all sorts of junk */
135 static VALUE
byte_xs_str(VALUE from
)
137 static const char esc
[] = "0123456789ABCDEF";
138 unsigned char *new_ptr
;
139 unsigned char *ptr
= (unsigned char *)RSTRING_PTR(from
);
140 long len
= RSTRING_LEN(from
);
144 for (; --len
>= 0; ptr
++) {
147 if (unlikely(need_escape(c
)))
148 new_len
+= 3; /* { '\', 'x', 'X', 'X' } */
151 len
= RSTRING_LEN(from
);
155 rv
= rb_str_new(NULL
, new_len
);
156 new_ptr
= (unsigned char *)RSTRING_PTR(rv
);
157 ptr
= (unsigned char *)RSTRING_PTR(from
);
158 for (; --len
>= 0; ptr
++) {
161 if (unlikely(need_escape(c
))) {
164 *new_ptr
++ = esc
[c
>> 4];
165 *new_ptr
++ = esc
[c
& 0xf];
170 assert(RSTRING_PTR(rv
)[RSTRING_LEN(rv
)] == '\0');
175 static VALUE
byte_xs(VALUE from
)
177 return byte_xs_str(rb_obj_as_string(from
));
180 static void clogger_mark(void *ptr
)
182 struct clogger
*c
= ptr
;
185 rb_gc_mark(c
->fmt_ops
);
186 rb_gc_mark(c
->logger
);
187 rb_gc_mark(c
->log_buf
);
189 rb_gc_mark(c
->cookies
);
190 rb_gc_mark(c
->status
);
191 rb_gc_mark(c
->headers
);
195 static VALUE
clogger_alloc(VALUE klass
)
199 return Data_Make_Struct(klass
, struct clogger
, clogger_mark
, -1, c
);
202 static struct clogger
*clogger_get(VALUE self
)
206 Data_Get_Struct(self
, struct clogger
, c
);
211 static VALUE
obj_fileno(VALUE obj
)
213 return rb_funcall(obj
, rb_intern("fileno"), 0);
216 static VALUE
obj_enable_sync(VALUE obj
)
218 return rb_funcall(obj
, rb_intern("sync="), 1, Qtrue
);
221 /* only for writing to regular files, not stupid crap like NFS */
222 static void write_full(int fd
, const void *buf
, size_t count
)
225 unsigned long ubuf
= (unsigned long)buf
;
228 r
= write(fd
, (void *)ubuf
, count
);
230 if ((size_t)r
== count
) { /* overwhelmingly likely */
236 if (errno
== EINTR
|| errno
== EAGAIN
)
237 continue; /* poor souls on NFS and like: */
240 rb_sys_fail("write");
246 * allow us to use write_full() iff we detect a blocking file
247 * descriptor that wouldn't play nicely with Ruby threading/fibers
249 static int raw_fd(VALUE my_fd
)
251 #if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
259 flags
= fcntl(fd
, F_GETFL
);
261 rb_sys_fail("fcntl");
263 return (flags
& O_NONBLOCK
) ? -1 : fd
;
264 #else /* platforms w/o fcntl/F_GETFL/O_NONBLOCK */
266 #endif /* platforms w/o fcntl/F_GETFL/O_NONBLOCK */
270 static VALUE
clogger_reentrant(VALUE self
)
272 return clogger_get(self
)->reentrant
== 0 ? Qfalse
: Qtrue
;
276 static VALUE
clogger_wrap_body(VALUE self
)
278 return clogger_get(self
)->wrap_body
== 0 ? Qfalse
: Qtrue
;
281 static void append_status(struct clogger
*c
)
283 char buf
[sizeof("999")];
285 VALUE status
= c
->status
;
287 if (TYPE(status
) != T_FIXNUM
) {
288 status
= rb_funcall(status
, to_i_id
, 0);
289 /* no way it's a valid status code (at least not HTTP/1.1) */
290 if (TYPE(status
) != T_FIXNUM
) {
291 rb_str_buf_append(c
->log_buf
, g_dash
);
296 nr
= FIX2INT(status
);
297 if (nr
>= 100 && nr
<= 999) {
298 nr
= snprintf(buf
, sizeof(buf
), "%03d", nr
);
300 rb_str_buf_cat(c
->log_buf
, buf
, nr
);
302 /* raise?, swap for 500? */
303 rb_str_buf_append(c
->log_buf
, g_dash
);
307 /* this is Rack 1.0.0-compatible, won't try to parse commas in XFF */
308 static void append_ip(struct clogger
*c
)
311 VALUE tmp
= rb_hash_aref(env
, g_HTTP_X_FORWARDED_FOR
);
314 /* can't be faked on any real server, so no escape */
315 tmp
= rb_hash_aref(env
, g_REMOTE_ADDR
);
321 rb_str_buf_append(c
->log_buf
, tmp
);
324 static void append_body_bytes_sent(struct clogger
*c
)
326 char buf
[(sizeof(off_t
) * 8) / 3 + 1];
327 const char *fmt
= sizeof(off_t
) == sizeof(long) ? "%ld" : "%lld";
328 int nr
= snprintf(buf
, sizeof(buf
), fmt
, c
->body_bytes_sent
);
330 assert(nr
> 0 && nr
< (int)sizeof(buf
));
331 rb_str_buf_cat(c
->log_buf
, buf
, nr
);
334 static void append_tv(struct clogger
*c
, const VALUE
*op
, struct timeval
*tv
)
336 char buf
[sizeof(".000000") + ((sizeof(tv
->tv_sec
) * 8) / 3)];
338 char *fmt
= RSTRING_PTR(op
[1]);
339 int ndiv
= NUM2INT(op
[2]);
341 nr
= snprintf(buf
, sizeof(buf
), fmt
,
342 (int)tv
->tv_sec
, (int)(tv
->tv_usec
/ ndiv
));
343 assert(nr
> 0 && nr
< (int)sizeof(buf
));
344 rb_str_buf_cat(c
->log_buf
, buf
, nr
);
347 static void append_request_time_fmt(struct clogger
*c
, const VALUE
*op
)
349 struct timeval now
, d
;
351 gettimeofday(&now
, NULL
);
352 timersub(&now
, &c
->tv_start
, &d
);
353 append_tv(c
, op
, &d
);
356 static void append_time_fmt(struct clogger
*c
, const VALUE
*op
)
360 gettimeofday(&now
, NULL
);
361 append_tv(c
, op
, &now
);
364 static void append_request_uri(struct clogger
*c
)
368 tmp
= rb_hash_aref(c
->env
, g_REQUEST_URI
);
370 tmp
= rb_hash_aref(c
->env
, g_PATH_INFO
);
372 rb_str_buf_append(c
->log_buf
, byte_xs(tmp
));
373 tmp
= rb_hash_aref(c
->env
, g_QUERY_STRING
);
374 if (!NIL_P(tmp
) && RSTRING_LEN(tmp
) != 0) {
375 rb_str_buf_append(c
->log_buf
, g_question_mark
);
376 rb_str_buf_append(c
->log_buf
, byte_xs(tmp
));
379 rb_str_buf_append(c
->log_buf
, byte_xs(tmp
));
383 static void append_request(struct clogger
*c
)
387 /* REQUEST_METHOD doesn't need escaping, Rack::Lint governs it */
388 tmp
= rb_hash_aref(c
->env
, g_REQUEST_METHOD
);
390 rb_str_buf_append(c
->log_buf
, tmp
);
392 rb_str_buf_append(c
->log_buf
, g_space
);
394 append_request_uri(c
);
396 /* HTTP_VERSION can be injected by malicious clients */
397 tmp
= rb_hash_aref(c
->env
, g_HTTP_VERSION
);
399 rb_str_buf_append(c
->log_buf
, g_space
);
400 rb_str_buf_append(c
->log_buf
, byte_xs(tmp
));
404 static void append_request_length(struct clogger
*c
)
406 VALUE tmp
= rb_hash_aref(c
->env
, g_rack_input
);
408 rb_str_buf_append(c
->log_buf
, g_dash
);
410 tmp
= rb_funcall(tmp
, size_id
, 0);
411 rb_str_buf_append(c
->log_buf
, rb_funcall(tmp
, to_s_id
, 0));
415 static void append_time(struct clogger
*c
, enum clogger_opcode op
, VALUE fmt
)
417 /* you'd have to be a moron to use formats this big... */
418 char buf
[sizeof("Saturday, November 01, 1970, 00:00:00 PM +0000")];
421 time_t t
= time(NULL
);
423 if (op
== CL_OP_TIME_LOCAL
)
424 localtime_r(&t
, &tmp
);
425 else if (op
== CL_OP_TIME_UTC
)
428 assert(0 && "unknown op");
430 nr
= strftime(buf
, sizeof(buf
), RSTRING_PTR(fmt
), &tmp
);
431 if (nr
== 0 || nr
== sizeof(buf
))
432 rb_str_buf_append(c
->log_buf
, g_dash
);
434 rb_str_buf_cat(c
->log_buf
, buf
, nr
);
437 static void append_pid(struct clogger
*c
)
439 char buf
[(sizeof(pid_t
) * 8) / 3 + 1];
440 int nr
= snprintf(buf
, sizeof(buf
), "%d", (int)getpid());
442 assert(nr
> 0 && nr
< (int)sizeof(buf
));
443 rb_str_buf_cat(c
->log_buf
, buf
, nr
);
446 static void append_eval(struct clogger
*c
, VALUE str
)
449 VALUE rv
= rb_eval_string_protect(RSTRING_PTR(str
), &state
);
451 rv
= state
== 0 ? rb_obj_as_string(rv
) : g_dash
;
452 rb_str_buf_append(c
->log_buf
, rv
);
455 static void append_cookie(struct clogger
*c
, VALUE key
)
459 if (c
->cookies
== Qfalse
)
460 c
->cookies
= rb_hash_aref(c
->env
, g_rack_request_cookie_hash
);
462 if (NIL_P(c
->cookies
)) {
465 cookie
= rb_hash_aref(c
->cookies
, key
);
469 rb_str_buf_append(c
->log_buf
, cookie
);
472 static void append_request_env(struct clogger
*c
, VALUE key
)
474 VALUE tmp
= rb_hash_aref(c
->env
, key
);
476 tmp
= NIL_P(tmp
) ? g_dash
: byte_xs(tmp
);
477 rb_str_buf_append(c
->log_buf
, tmp
);
480 static void append_response(struct clogger
*c
, VALUE key
)
484 assert(rb_obj_is_kind_of(c
->headers
, cHeaderHash
) && "not HeaderHash");
486 v
= rb_funcall(c
->headers
, sq_brace_id
, 1, key
);
487 v
= NIL_P(v
) ? g_dash
: byte_xs(v
);
488 rb_str_buf_append(c
->log_buf
, v
);
491 static void special_var(struct clogger
*c
, enum clogger_special var
)
494 case CL_SP_body_bytes_sent
:
495 append_body_bytes_sent(c
);
503 case CL_SP_request_length
:
504 append_request_length(c
);
506 case CL_SP_response_length
:
507 if (c
->body_bytes_sent
== 0)
508 rb_str_buf_append(c
->log_buf
, g_dash
);
510 append_body_bytes_sent(c
);
518 case CL_SP_request_uri
:
519 append_request_uri(c
);
523 static VALUE
cwrite(struct clogger
*c
)
525 const VALUE ops
= c
->fmt_ops
;
526 const VALUE
*ary
= RARRAY_PTR(ops
);
527 long i
= RARRAY_LEN(ops
);
528 VALUE dst
= c
->log_buf
;
530 rb_str_set_len(dst
, 0);
532 for (; --i
>= 0; ary
++) {
533 const VALUE
*op
= RARRAY_PTR(*ary
);
534 enum clogger_opcode opcode
= FIX2INT(op
[0]);
538 rb_str_buf_append(dst
, op
[1]);
541 append_request_env(c
, op
[1]);
544 append_response(c
, op
[1]);
547 special_var(c
, FIX2INT(op
[1]));
550 append_eval(c
, op
[1]);
552 case CL_OP_TIME_LOCAL
:
554 append_time(c
, opcode
, op
[1]);
556 case CL_OP_REQUEST_TIME
:
557 append_request_time_fmt(c
, op
);
560 append_time_fmt(c
, op
);
563 append_cookie(c
, op
[1]);
569 write_full(c
->fd
, RSTRING_PTR(dst
), RSTRING_LEN(dst
));
571 VALUE logger
= c
->logger
;
574 logger
= rb_hash_aref(c
->env
, g_rack_errors
);
575 rb_funcall(logger
, ltlt_id
, 1, dst
);
583 * Clogger.new(app, :logger => $stderr, :format => string) => obj
585 * Creates a new Clogger object that wraps +app+. +:logger+ may
586 * be any object that responds to the "<<" method with a string argument.
588 static VALUE
clogger_init(int argc
, VALUE
*argv
, VALUE self
)
590 struct clogger
*c
= clogger_get(self
);
592 VALUE fmt
= rb_const_get(mFormat
, rb_intern("Common"));
594 rb_scan_args(argc
, argv
, "11", &c
->app
, &o
);
597 c
->reentrant
= -1; /* auto-detect */
599 if (TYPE(o
) == T_HASH
) {
602 c
->logger
= rb_hash_aref(o
, ID2SYM(rb_intern("logger")));
603 if (!NIL_P(c
->logger
)) {
604 rb_rescue(obj_enable_sync
, c
->logger
, NULL
, 0);
605 tmp
= rb_rescue(obj_fileno
, c
->logger
, NULL
, 0);
609 tmp
= rb_hash_aref(o
, ID2SYM(rb_intern("format")));
613 tmp
= rb_hash_aref(o
, ID2SYM(rb_intern("reentrant")));
623 rb_raise(rb_eArgError
, ":reentrant must be boolean");
628 c
->fmt_ops
= rb_funcall(self
, rb_intern("compile_format"), 2, fmt
, o
);
630 if (Qtrue
== rb_funcall(self
, rb_intern("need_response_headers?"),
633 if (Qtrue
== rb_funcall(self
, rb_intern("need_wrap_body?"),
640 static VALUE
body_iter_i(VALUE str
, VALUE memop
)
642 off_t
*len
= (off_t
*)memop
;
644 str
= rb_obj_as_string(str
);
645 *len
+= RSTRING_LEN(str
);
647 return rb_yield(str
);
650 static VALUE
body_close(struct clogger
*c
)
652 if (rb_respond_to(c
->body
, close_id
))
653 return rb_funcall(c
->body
, close_id
, 0);
659 * clogger.each { |part| socket.write(part) }
661 * Delegates the body#each call to the underlying +body+ object
662 * while tracking the number of bytes yielded. This will log
665 static VALUE
clogger_each(VALUE self
)
667 struct clogger
*c
= clogger_get(self
);
670 c
->body_bytes_sent
= 0;
671 rb_iterate(rb_each
, c
->body
, body_iter_i
, (VALUE
)&c
->body_bytes_sent
);
680 * Delegates the body#close call to the underlying +body+ object.
681 * This is only used when Clogger is wrapping the +body+ of a Rack
682 * response and should be automatically called by the web server.
684 static VALUE
clogger_close(VALUE self
)
686 struct clogger
*c
= clogger_get(self
);
688 return rb_ensure(body_close
, (VALUE
)c
, cwrite
, (VALUE
)c
);
692 static VALUE
clogger_fileno(VALUE self
)
694 struct clogger
*c
= clogger_get(self
);
696 return c
->fd
< 0 ? Qnil
: INT2NUM(c
->fd
);
699 static VALUE
ccall(struct clogger
*c
, VALUE env
)
703 gettimeofday(&c
->tv_start
, NULL
);
706 rv
= rb_funcall(c
->app
, call_id
, 1, env
);
707 if (TYPE(rv
) == T_ARRAY
&& RARRAY_LEN(rv
) == 3) {
708 VALUE
*tmp
= RARRAY_PTR(rv
);
714 rv
= rb_ary_new4(3, tmp
);
716 ! rb_obj_is_kind_of(tmp
[1], cHeaderHash
)) {
717 c
->headers
= rb_funcall(cHeaderHash
, new_id
, 1, tmp
[1]);
718 rb_ary_store(rv
, 1, c
->headers
);
721 volatile VALUE tmp
= rb_inspect(rv
);
723 c
->status
= INT2FIX(500);
724 c
->headers
= c
->body
= rb_ary_new();
726 rb_raise(rb_eTypeError
,
727 "app response not a 3 element Array: %s",
736 * clogger.call(env) => [ status, headers, body ]
738 * calls the wrapped Rack application with +env+, returns the
739 * [status, headers, body ] tuplet required by Rack.
741 static VALUE
clogger_call(VALUE self
, VALUE env
)
743 struct clogger
*c
= clogger_get(self
);
746 env
= rb_check_convert_type(env
, T_HASH
, "Hash", "to_hash");
749 if (c
->reentrant
< 0) {
750 VALUE tmp
= rb_hash_aref(env
, g_rack_multithread
);
751 c
->reentrant
= Qfalse
== tmp
? 0 : 1;
754 self
= rb_obj_dup(self
);
755 c
= clogger_get(self
);
759 assert(!OBJ_FROZEN(rv
) && "frozen response array");
761 if (rb_respond_to(c
->body
, to_path_id
))
762 self
= rb_funcall(cToPath
, new_id
, 1, self
);
763 rb_ary_store(rv
, 2, self
);
775 static VALUE
clogger_init_copy(VALUE clone
, VALUE orig
)
777 struct clogger
*a
= clogger_get(orig
);
778 struct clogger
*b
= clogger_get(clone
);
780 memcpy(b
, a
, sizeof(struct clogger
));
786 #define CONST_GLOBAL_STR2(var, val) do { \
787 g_##var = rb_obj_freeze(rb_str_new(val, sizeof(val) - 1)); \
788 rb_global_variable(&g_##var); \
791 #define CONST_GLOBAL_STR(val) CONST_GLOBAL_STR2(val, #val)
793 static VALUE
to_path(VALUE self
)
795 struct clogger
*c
= clogger_get(RSTRUCT_PTR(self
)[0]);
796 VALUE path
= rb_funcall(c
->body
, to_path_id
, 0);
801 Check_Type(path
, T_STRING
);
802 cpath
= RSTRING_PTR(path
);
804 /* try to avoid an extra path lookup */
805 if (rb_respond_to(c
->body
, to_io_id
))
806 rv
= fstat(my_fileno(c
->body
), &sb
);
808 * Rainbows! can use "/dev/fd/%d" in to_path output to avoid
809 * extra open() syscalls, too.
811 else if (sscanf(cpath
, "/dev/fd/%d", &rv
) == 1)
814 rv
= stat(cpath
, &sb
);
816 c
->body_bytes_sent
= rv
== 0 ? sb
.st_size
: 0;
820 void Init_clogger_ext(void)
824 ltlt_id
= rb_intern("<<");
825 call_id
= rb_intern("call");
826 each_id
= rb_intern("each");
827 close_id
= rb_intern("close");
828 to_i_id
= rb_intern("to_i");
829 to_s_id
= rb_intern("to_s");
830 size_id
= rb_intern("size");
831 sq_brace_id
= rb_intern("[]");
832 new_id
= rb_intern("new");
833 to_path_id
= rb_intern("to_path");
834 to_io_id
= rb_intern("to_io");
835 cClogger
= rb_define_class("Clogger", rb_cObject
);
836 mFormat
= rb_define_module_under(cClogger
, "Format");
837 rb_define_alloc_func(cClogger
, clogger_alloc
);
838 rb_define_method(cClogger
, "initialize", clogger_init
, -1);
839 rb_define_method(cClogger
, "initialize_copy", clogger_init_copy
, 1);
840 rb_define_method(cClogger
, "call", clogger_call
, 1);
841 rb_define_method(cClogger
, "each", clogger_each
, 0);
842 rb_define_method(cClogger
, "close", clogger_close
, 0);
843 rb_define_method(cClogger
, "fileno", clogger_fileno
, 0);
844 rb_define_method(cClogger
, "wrap_body?", clogger_wrap_body
, 0);
845 rb_define_method(cClogger
, "reentrant?", clogger_reentrant
, 0);
846 CONST_GLOBAL_STR(REMOTE_ADDR
);
847 CONST_GLOBAL_STR(HTTP_X_FORWARDED_FOR
);
848 CONST_GLOBAL_STR(REQUEST_METHOD
);
849 CONST_GLOBAL_STR(PATH_INFO
);
850 CONST_GLOBAL_STR(QUERY_STRING
);
851 CONST_GLOBAL_STR(REQUEST_URI
);
852 CONST_GLOBAL_STR(HTTP_VERSION
);
853 CONST_GLOBAL_STR2(rack_errors
, "rack.errors");
854 CONST_GLOBAL_STR2(rack_input
, "rack.input");
855 CONST_GLOBAL_STR2(rack_multithread
, "rack.multithread");
856 CONST_GLOBAL_STR2(dash
, "-");
857 CONST_GLOBAL_STR2(space
, " ");
858 CONST_GLOBAL_STR2(question_mark
, "?");
859 CONST_GLOBAL_STR2(rack_request_cookie_hash
, "rack.request.cookie_hash");
861 tmp
= rb_const_get(rb_cObject
, rb_intern("Rack"));
862 tmp
= rb_const_get(tmp
, rb_intern("Utils"));
863 cHeaderHash
= rb_const_get(tmp
, rb_intern("HeaderHash"));
864 cToPath
= rb_const_get(cClogger
, rb_intern("ToPath"));
865 rb_define_method(cToPath
, "to_path", to_path
, 0);