16 #ifndef _POSIX_C_SOURCE
17 # define _POSIX_C_SOURCE 200112L
21 #include "ruby_1_9_compat.h"
22 #include "broken_system_compat.h"
23 #include "blocking_helpers.h"
26 * Availability of a monotonic clock needs to be detected at runtime
27 * since we could've been built on a different system than we're run
30 static clockid_t hopefully_CLOCK_MONOTONIC
;
32 static void check_clock(void)
36 hopefully_CLOCK_MONOTONIC
= CLOCK_MONOTONIC
;
38 /* we can't check this reliably at compile time */
39 if (clock_gettime(CLOCK_MONOTONIC
, &now
) == 0)
42 if (clock_gettime(CLOCK_REALTIME
, &now
) == 0) {
43 hopefully_CLOCK_MONOTONIC
= CLOCK_REALTIME
;
44 rb_warn("CLOCK_MONOTONIC not available, "
45 "falling back to CLOCK_REALTIME");
47 rb_warn("clock_gettime() totally broken, " \
48 "falling back to pure Ruby Clogger");
49 rb_raise(rb_eLoadError
, "clock_gettime() broken");
52 static void clock_diff(struct timespec
*a
, const struct timespec
*b
)
54 a
->tv_sec
-= b
->tv_sec
;
55 a
->tv_nsec
-= b
->tv_nsec
;
58 a
->tv_nsec
+= 1000000000;
62 /* give GCC hints for better branch prediction
63 * (we layout branches so that ASCII characters are handled faster) */
64 #if defined(__GNUC__) && (__GNUC__ >= 3)
65 # define likely(x) __builtin_expect (!!(x), 1)
66 # define unlikely(x) __builtin_expect (!!(x), 0)
68 # define unlikely(x) (x)
69 # define likely(x) (x)
85 enum clogger_special
{
86 CL_SP_body_bytes_sent
= 0,
90 CL_SP_response_length
,
112 off_t body_bytes_sent
;
113 struct timespec ts_start
;
118 int reentrant
; /* tri-state, -1:auto, 1/0 true/false */
128 static ID sq_brace_id
;
130 static ID to_path_id
;
132 static ID respond_to_id
;
133 static VALUE cClogger
;
134 static VALUE mFormat
;
135 static VALUE cHeaderHash
;
137 /* common hash lookup keys */
138 static VALUE g_HTTP_X_FORWARDED_FOR
;
139 static VALUE g_REMOTE_ADDR
;
140 static VALUE g_REQUEST_METHOD
;
141 static VALUE g_PATH_INFO
;
142 static VALUE g_REQUEST_URI
;
143 static VALUE g_QUERY_STRING
;
144 static VALUE g_HTTP_VERSION
;
145 static VALUE g_rack_errors
;
146 static VALUE g_rack_input
;
147 static VALUE g_rack_multithread
;
149 static VALUE g_space
;
150 static VALUE g_question_mark
;
151 static VALUE g_rack_request_cookie_hash
;
153 #define LOG_BUF_INIT_SIZE 128
155 static void init_buffers(struct clogger
*c
)
157 c
->log_buf
= rb_str_buf_new(LOG_BUF_INIT_SIZE
);
160 static inline int need_escape(unsigned c
)
163 return !!(c
== '\'' || c
== '"' || c
<= 0x1f);
166 /* we are encoding-agnostic, clients can send us all sorts of junk */
167 static VALUE
byte_xs_str(VALUE from
)
169 static const char esc
[] = "0123456789ABCDEF";
170 unsigned char *new_ptr
;
171 unsigned char *ptr
= (unsigned char *)RSTRING_PTR(from
);
172 long len
= RSTRING_LEN(from
);
176 for (; --len
>= 0; ptr
++) {
179 if (unlikely(need_escape(c
)))
180 new_len
+= 3; /* { '\', 'x', 'X', 'X' } */
183 len
= RSTRING_LEN(from
);
187 rv
= rb_str_new(NULL
, new_len
);
188 new_ptr
= (unsigned char *)RSTRING_PTR(rv
);
189 ptr
= (unsigned char *)RSTRING_PTR(from
);
190 for (; --len
>= 0; ptr
++) {
193 if (unlikely(need_escape(c
))) {
196 *new_ptr
++ = esc
[c
>> 4];
197 *new_ptr
++ = esc
[c
& 0xf];
202 assert(RSTRING_PTR(rv
)[RSTRING_LEN(rv
)] == '\0');
207 static VALUE
byte_xs(VALUE from
)
209 return byte_xs_str(rb_obj_as_string(from
));
212 static void clogger_mark(void *ptr
)
214 struct clogger
*c
= ptr
;
217 rb_gc_mark(c
->fmt_ops
);
218 rb_gc_mark(c
->logger
);
219 rb_gc_mark(c
->log_buf
);
221 rb_gc_mark(c
->cookies
);
222 rb_gc_mark(c
->status
);
223 rb_gc_mark(c
->headers
);
227 static VALUE
clogger_alloc(VALUE klass
)
231 return Data_Make_Struct(klass
, struct clogger
, clogger_mark
, -1, c
);
234 static struct clogger
*clogger_get(VALUE self
)
238 Data_Get_Struct(self
, struct clogger
, c
);
243 /* only for writing to regular files, not stupid crap like NFS */
244 static void write_full(int fd
, const void *buf
, size_t count
)
247 unsigned long ubuf
= (unsigned long)buf
;
250 r
= write(fd
, (void *)ubuf
, count
);
252 if ((size_t)r
== count
) { /* overwhelmingly likely */
258 if (errno
== EINTR
|| errno
== EAGAIN
)
259 continue; /* poor souls on NFS and like: */
262 rb_sys_fail("write");
268 * allow us to use write_full() iff we detect a blocking file
269 * descriptor that wouldn't play nicely with Ruby threading/fibers
271 static int raw_fd(VALUE my_fd
)
273 #if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
281 flags
= fcntl(fd
, F_GETFL
);
283 rb_sys_fail("fcntl");
285 if (flags
& O_NONBLOCK
) {
288 if (fstat(fd
, &sb
) < 0)
291 /* O_NONBLOCK is no-op for regular files: */
292 if (! S_ISREG(sb
.st_mode
))
296 #else /* platforms w/o fcntl/F_GETFL/O_NONBLOCK */
298 #endif /* platforms w/o fcntl/F_GETFL/O_NONBLOCK */
302 static VALUE
clogger_reentrant(VALUE self
)
304 return clogger_get(self
)->reentrant
== 0 ? Qfalse
: Qtrue
;
308 static VALUE
clogger_wrap_body(VALUE self
)
310 return clogger_get(self
)->wrap_body
== 0 ? Qfalse
: Qtrue
;
313 static void append_status(struct clogger
*c
)
315 char buf
[sizeof("999")];
317 VALUE status
= c
->status
;
319 if (TYPE(status
) != T_FIXNUM
) {
320 status
= rb_funcall(status
, to_i_id
, 0);
321 /* no way it's a valid status code (at least not HTTP/1.1) */
322 if (TYPE(status
) != T_FIXNUM
) {
323 rb_str_buf_append(c
->log_buf
, g_dash
);
328 nr
= FIX2INT(status
);
329 if (nr
>= 100 && nr
<= 999) {
330 nr
= snprintf(buf
, sizeof(buf
), "%03d", nr
);
332 rb_str_buf_cat(c
->log_buf
, buf
, nr
);
334 /* raise?, swap for 500? */
335 rb_str_buf_append(c
->log_buf
, g_dash
);
339 /* this is Rack 1.0.0-compatible, won't try to parse commas in XFF */
340 static void append_ip(struct clogger
*c
)
343 VALUE tmp
= rb_hash_aref(env
, g_HTTP_X_FORWARDED_FOR
);
346 /* can't be faked on any real server, so no escape */
347 tmp
= rb_hash_aref(env
, g_REMOTE_ADDR
);
353 rb_str_buf_append(c
->log_buf
, tmp
);
356 static void append_body_bytes_sent(struct clogger
*c
)
358 char buf
[(sizeof(off_t
) * 8) / 3 + 1];
359 const char *fmt
= sizeof(off_t
) == sizeof(long) ? "%ld" : "%lld";
360 int nr
= snprintf(buf
, sizeof(buf
), fmt
, c
->body_bytes_sent
);
362 assert(nr
> 0 && nr
< (int)sizeof(buf
));
363 rb_str_buf_cat(c
->log_buf
, buf
, nr
);
366 static void append_ts(struct clogger
*c
, const VALUE
*op
, struct timespec
*ts
)
368 char buf
[sizeof(".000000") + ((sizeof(ts
->tv_sec
) * 8) / 3)];
370 char *fmt
= RSTRING_PTR(op
[1]);
371 int ndiv
= NUM2INT(op
[2]);
372 int usec
= ts
->tv_nsec
/ 1000;
374 nr
= snprintf(buf
, sizeof(buf
), fmt
,
375 (int)ts
->tv_sec
, (int)(usec
/ ndiv
));
376 assert(nr
> 0 && nr
< (int)sizeof(buf
));
377 rb_str_buf_cat(c
->log_buf
, buf
, nr
);
380 static void append_request_time_fmt(struct clogger
*c
, const VALUE
*op
)
384 clock_gettime(hopefully_CLOCK_MONOTONIC
, &now
);
385 clock_diff(&now
, &c
->ts_start
);
386 append_ts(c
, op
, &now
);
389 static void append_time_fmt(struct clogger
*c
, const VALUE
*op
)
392 int r
= clock_gettime(CLOCK_REALTIME
, &now
);
394 if (unlikely(r
!= 0))
395 rb_sys_fail("clock_gettime(CLOCK_REALTIME)");
396 append_ts(c
, op
, &now
);
399 static void append_request_uri(struct clogger
*c
)
403 tmp
= rb_hash_aref(c
->env
, g_REQUEST_URI
);
405 tmp
= rb_hash_aref(c
->env
, g_PATH_INFO
);
407 rb_str_buf_append(c
->log_buf
, byte_xs(tmp
));
408 tmp
= rb_hash_aref(c
->env
, g_QUERY_STRING
);
409 if (!NIL_P(tmp
) && RSTRING_LEN(tmp
) != 0) {
410 rb_str_buf_append(c
->log_buf
, g_question_mark
);
411 rb_str_buf_append(c
->log_buf
, byte_xs(tmp
));
414 rb_str_buf_append(c
->log_buf
, byte_xs(tmp
));
418 static void append_request(struct clogger
*c
)
422 /* REQUEST_METHOD doesn't need escaping, Rack::Lint governs it */
423 tmp
= rb_hash_aref(c
->env
, g_REQUEST_METHOD
);
425 rb_str_buf_append(c
->log_buf
, tmp
);
427 rb_str_buf_append(c
->log_buf
, g_space
);
429 append_request_uri(c
);
431 /* HTTP_VERSION can be injected by malicious clients */
432 tmp
= rb_hash_aref(c
->env
, g_HTTP_VERSION
);
434 rb_str_buf_append(c
->log_buf
, g_space
);
435 rb_str_buf_append(c
->log_buf
, byte_xs(tmp
));
439 static void append_request_length(struct clogger
*c
)
441 VALUE tmp
= rb_hash_aref(c
->env
, g_rack_input
);
443 rb_str_buf_append(c
->log_buf
, g_dash
);
445 tmp
= rb_funcall(tmp
, size_id
, 0);
446 rb_str_buf_append(c
->log_buf
, rb_funcall(tmp
, to_s_id
, 0));
450 static long local_gmtoffset(struct tm
*tm
)
452 time_t t
= time(NULL
);
456 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
457 return tm
->tm_gmtoff
/ 60;
459 return -(tm
->tm_isdst
? timezone
- 3600 : timezone
) / 60;
463 static void append_time_iso8601(struct clogger
*c
)
465 char buf
[sizeof("1970-01-01T00:00:00+00:00")];
468 long gmtoff
= local_gmtoffset(&tm
);
470 nr
= snprintf(buf
, sizeof(buf
),
471 "%4d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
472 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1,
473 tm
.tm_mday
, tm
.tm_hour
,
474 tm
.tm_min
, tm
.tm_sec
,
475 gmtoff
< 0 ? '-' : '+',
476 abs(gmtoff
/ 60), abs(gmtoff
% 60));
477 assert(nr
== (sizeof(buf
) - 1) && "snprintf fail");
478 rb_str_buf_cat(c
->log_buf
, buf
, sizeof(buf
) - 1);
481 static const char months
[] = "Jan\0Feb\0Mar\0Apr\0May\0Jun\0"
482 "Jul\0Aug\0Sep\0Oct\0Nov\0Dec";
484 static void append_time_local(struct clogger
*c
)
486 char buf
[sizeof("01/Jan/1970:00:00:00 +0000")];
489 long gmtoff
= local_gmtoffset(&tm
);
491 nr
= snprintf(buf
, sizeof(buf
),
492 "%02d/%s/%d:%02d:%02d:%02d %c%02d%02d",
493 tm
.tm_mday
, months
+ (tm
.tm_mon
* sizeof("Jan")),
494 tm
.tm_year
+ 1900, tm
.tm_hour
,
495 tm
.tm_min
, tm
.tm_sec
,
496 gmtoff
< 0 ? '-' : '+',
497 abs(gmtoff
/ 60), abs(gmtoff
% 60));
498 assert(nr
== (sizeof(buf
) - 1) && "snprintf fail");
499 rb_str_buf_cat(c
->log_buf
, buf
, sizeof(buf
) - 1);
502 static void append_time_utc(struct clogger
*c
)
504 char buf
[sizeof("01/Jan/1970:00:00:00 +0000")];
507 time_t t
= time(NULL
);
510 nr
= snprintf(buf
, sizeof(buf
),
511 "%02d/%s/%d:%02d:%02d:%02d +0000",
512 tm
.tm_mday
, months
+ (tm
.tm_mon
* sizeof("Jan")),
513 tm
.tm_year
+ 1900, tm
.tm_hour
,
514 tm
.tm_min
, tm
.tm_sec
);
515 assert(nr
== (sizeof(buf
) - 1) && "snprintf fail");
516 rb_str_buf_cat(c
->log_buf
, buf
, sizeof(buf
) - 1);
520 append_time(struct clogger
*c
, enum clogger_opcode op
, VALUE fmt
, VALUE buf
)
522 char *buf_ptr
= RSTRING_PTR(buf
);
523 size_t buf_size
= RSTRING_LEN(buf
) + 1; /* "\0" */
526 time_t t
= time(NULL
);
528 if (op
== CL_OP_TIME_LOCAL
)
529 localtime_r(&t
, &tmp
);
530 else if (op
== CL_OP_TIME_UTC
)
533 assert(0 && "unknown op");
535 nr
= strftime(buf_ptr
, buf_size
, RSTRING_PTR(fmt
), &tmp
);
536 assert(nr
< buf_size
&& "time format too small!");
537 rb_str_buf_cat(c
->log_buf
, buf_ptr
, nr
);
540 static void append_pid(struct clogger
*c
)
542 char buf
[(sizeof(pid_t
) * 8) / 3 + 1];
543 int nr
= snprintf(buf
, sizeof(buf
), "%d", (int)getpid());
545 assert(nr
> 0 && nr
< (int)sizeof(buf
));
546 rb_str_buf_cat(c
->log_buf
, buf
, nr
);
549 static void append_eval(struct clogger
*c
, VALUE str
)
552 VALUE rv
= rb_eval_string_protect(RSTRING_PTR(str
), &state
);
554 rv
= state
== 0 ? rb_obj_as_string(rv
) : g_dash
;
555 rb_str_buf_append(c
->log_buf
, rv
);
558 static void append_cookie(struct clogger
*c
, VALUE key
)
562 if (c
->cookies
== Qfalse
)
563 c
->cookies
= rb_hash_aref(c
->env
, g_rack_request_cookie_hash
);
565 if (NIL_P(c
->cookies
)) {
568 cookie
= rb_hash_aref(c
->cookies
, key
);
572 rb_str_buf_append(c
->log_buf
, cookie
);
575 static void append_request_env(struct clogger
*c
, VALUE key
)
577 VALUE tmp
= rb_hash_aref(c
->env
, key
);
579 tmp
= NIL_P(tmp
) ? g_dash
: byte_xs(tmp
);
580 rb_str_buf_append(c
->log_buf
, tmp
);
583 static void append_response(struct clogger
*c
, VALUE key
)
587 assert(rb_obj_is_kind_of(c
->headers
, cHeaderHash
) && "not HeaderHash");
589 v
= rb_funcall(c
->headers
, sq_brace_id
, 1, key
);
590 v
= NIL_P(v
) ? g_dash
: byte_xs(v
);
591 rb_str_buf_append(c
->log_buf
, v
);
594 static void special_var(struct clogger
*c
, enum clogger_special var
)
597 case CL_SP_body_bytes_sent
:
598 append_body_bytes_sent(c
);
606 case CL_SP_request_length
:
607 append_request_length(c
);
609 case CL_SP_response_length
:
610 if (c
->body_bytes_sent
== 0)
611 rb_str_buf_append(c
->log_buf
, g_dash
);
613 append_body_bytes_sent(c
);
621 case CL_SP_request_uri
:
622 append_request_uri(c
);
624 case CL_SP_time_iso8601
:
625 append_time_iso8601(c
);
627 case CL_SP_time_local
:
628 append_time_local(c
);
635 static VALUE
cwrite(struct clogger
*c
)
637 const VALUE ops
= c
->fmt_ops
;
638 const VALUE
*ary
= RARRAY_PTR(ops
);
639 long i
= RARRAY_LEN(ops
);
640 VALUE dst
= c
->log_buf
;
642 rb_str_set_len(dst
, 0);
644 for (; --i
>= 0; ary
++) {
645 const VALUE
*op
= RARRAY_PTR(*ary
);
646 enum clogger_opcode opcode
= FIX2INT(op
[0]);
650 rb_str_buf_append(dst
, op
[1]);
653 append_request_env(c
, op
[1]);
656 append_response(c
, op
[1]);
659 special_var(c
, FIX2INT(op
[1]));
662 append_eval(c
, op
[1]);
664 case CL_OP_TIME_LOCAL
:
666 append_time(c
, opcode
, op
[1], op
[2]);
668 case CL_OP_REQUEST_TIME
:
669 append_request_time_fmt(c
, op
);
672 append_time_fmt(c
, op
);
675 append_cookie(c
, op
[1]);
681 write_full(c
->fd
, RSTRING_PTR(dst
), RSTRING_LEN(dst
));
683 VALUE logger
= c
->logger
;
686 logger
= rb_hash_aref(c
->env
, g_rack_errors
);
687 rb_funcall(logger
, ltlt_id
, 1, dst
);
693 static VALUE
clogger_write(VALUE self
)
695 return cwrite(clogger_get(self
));
698 static void init_logger(struct clogger
*c
, VALUE path
)
702 if (!NIL_P(path
) && !NIL_P(c
->logger
))
703 rb_raise(rb_eArgError
, ":logger and :path are independent");
705 VALUE ab
= rb_str_new2("ab");
706 id
= rb_intern("open");
707 c
->logger
= rb_funcall(rb_cFile
, id
, 2, path
, ab
);
710 id
= rb_intern("sync=");
711 if (rb_respond_to(c
->logger
, id
))
712 rb_funcall(c
->logger
, id
, 1, Qtrue
);
714 id
= rb_intern("fileno");
715 if (rb_respond_to(c
->logger
, id
))
716 c
->fd
= raw_fd(rb_funcall(c
->logger
, id
, 0));
721 * Clogger.new(app, :logger => $stderr, :format => string) => obj
723 * Creates a new Clogger object that wraps +app+. +:logger+ may
724 * be any object that responds to the "<<" method with a string argument.
725 * Instead of +:logger+, +:path+ may be specified to be a :path of a File
726 * that will be opened in append mode.
728 static VALUE
clogger_init(int argc
, VALUE
*argv
, VALUE self
)
730 struct clogger
*c
= clogger_get(self
);
732 VALUE fmt
= rb_const_get(mFormat
, rb_intern("Common"));
734 rb_scan_args(argc
, argv
, "11", &c
->app
, &o
);
737 c
->reentrant
= -1; /* auto-detect */
739 if (TYPE(o
) == T_HASH
) {
742 tmp
= rb_hash_aref(o
, ID2SYM(rb_intern("path")));
743 c
->logger
= rb_hash_aref(o
, ID2SYM(rb_intern("logger")));
746 tmp
= rb_hash_aref(o
, ID2SYM(rb_intern("format")));
750 tmp
= rb_hash_aref(o
, ID2SYM(rb_intern("reentrant")));
760 rb_raise(rb_eArgError
, ":reentrant must be boolean");
765 c
->fmt_ops
= rb_funcall(self
, rb_intern("compile_format"), 2, fmt
, o
);
767 if (Qtrue
== rb_funcall(self
, rb_intern("need_response_headers?"),
770 if (Qtrue
== rb_funcall(self
, rb_intern("need_wrap_body?"),
777 static VALUE
body_iter_i(VALUE str
, VALUE self
)
779 struct clogger
*c
= clogger_get(self
);
781 str
= rb_obj_as_string(str
);
782 c
->body_bytes_sent
+= RSTRING_LEN(str
);
784 return rb_yield(str
);
787 static VALUE
body_close(VALUE self
)
789 struct clogger
*c
= clogger_get(self
);
791 if (rb_respond_to(c
->body
, close_id
))
792 return rb_funcall(c
->body
, close_id
, 0);
798 * clogger.each { |part| socket.write(part) }
800 * Delegates the body#each call to the underlying +body+ object
801 * while tracking the number of bytes yielded. This will log
804 static VALUE
clogger_each(VALUE self
)
806 struct clogger
*c
= clogger_get(self
);
809 c
->body_bytes_sent
= 0;
810 rb_iterate(rb_each
, c
->body
, body_iter_i
, self
);
819 * Delegates the body#close call to the underlying +body+ object.
820 * This is only used when Clogger is wrapping the +body+ of a Rack
821 * response and should be automatically called by the web server.
823 static VALUE
clogger_close(VALUE self
)
826 return rb_ensure(body_close
, self
, clogger_write
, self
);
830 static VALUE
clogger_fileno(VALUE self
)
832 struct clogger
*c
= clogger_get(self
);
834 return c
->fd
< 0 ? Qnil
: INT2NUM(c
->fd
);
837 static VALUE
ccall(struct clogger
*c
, VALUE env
)
841 clock_gettime(hopefully_CLOCK_MONOTONIC
, &c
->ts_start
);
844 rv
= rb_funcall(c
->app
, call_id
, 1, env
);
845 if (TYPE(rv
) == T_ARRAY
&& RARRAY_LEN(rv
) == 3) {
846 VALUE
*tmp
= RARRAY_PTR(rv
);
852 rv
= rb_ary_new4(3, tmp
);
854 ! rb_obj_is_kind_of(tmp
[1], cHeaderHash
)) {
855 c
->headers
= rb_funcall(cHeaderHash
, new_id
, 1, tmp
[1]);
856 rb_ary_store(rv
, 1, c
->headers
);
859 volatile VALUE tmp
= rb_inspect(rv
);
861 c
->status
= INT2FIX(500);
862 c
->headers
= c
->body
= rb_ary_new();
864 rb_raise(rb_eTypeError
,
865 "app response not a 3 element Array: %s",
874 * clogger.call(env) => [ status, headers, body ]
876 * calls the wrapped Rack application with +env+, returns the
877 * [status, headers, body ] tuplet required by Rack.
879 static VALUE
clogger_call(VALUE self
, VALUE env
)
881 struct clogger
*c
= clogger_get(self
);
884 env
= rb_check_convert_type(env
, T_HASH
, "Hash", "to_hash");
887 if (c
->reentrant
< 0) {
888 VALUE tmp
= rb_hash_aref(env
, g_rack_multithread
);
889 c
->reentrant
= Qfalse
== tmp
? 0 : 1;
892 self
= rb_obj_dup(self
);
893 c
= clogger_get(self
);
897 assert(!OBJ_FROZEN(rv
) && "frozen response array");
898 rb_ary_store(rv
, 2, self
);
909 static void duplicate_buffers(VALUE ops
)
911 long i
= RARRAY_LEN(ops
);
912 VALUE
*ary
= RARRAY_PTR(ops
);
914 for ( ; --i
>= 0; ary
++) {
915 VALUE
*op
= RARRAY_PTR(*ary
);
916 enum clogger_opcode opcode
= FIX2INT(op
[0]);
918 if (opcode
== CL_OP_TIME_LOCAL
|| opcode
== CL_OP_TIME_UTC
) {
919 Check_Type(op
[2], T_STRING
);
920 op
[2] = rb_str_dup(op
[2]);
921 rb_str_modify(op
[2]); /* trigger copy-on-write */
927 static VALUE
clogger_init_copy(VALUE clone
, VALUE orig
)
929 struct clogger
*a
= clogger_get(orig
);
930 struct clogger
*b
= clogger_get(clone
);
932 memcpy(b
, a
, sizeof(struct clogger
));
934 duplicate_buffers(b
->fmt_ops
);
939 #define CONST_GLOBAL_STR2(var, val) do { \
940 g_##var = rb_obj_freeze(rb_str_new(val, sizeof(val) - 1)); \
941 rb_global_variable(&g_##var); \
944 #define CONST_GLOBAL_STR(val) CONST_GLOBAL_STR2(val, #val)
948 * clogger.respond_to?(:to_path) => true or false
949 * clogger.respond_to?(:close) => true
951 * used to delegate +:to_path+ checks for Rack webservers that optimize
952 * static file serving
954 static VALUE
respond_to(VALUE self
, VALUE method
)
956 struct clogger
*c
= clogger_get(self
);
957 ID id
= rb_to_id(method
);
961 return rb_respond_to(c
->body
, id
);
968 * used to proxy +:to_path+ method calls to the wrapped response body.
970 static VALUE
to_path(VALUE self
)
972 struct clogger
*c
= clogger_get(self
);
975 VALUE path
= rb_funcall(c
->body
, to_path_id
, 0);
977 /* try to avoid an extra path lookup */
978 if (rb_respond_to(c
->body
, to_io_id
)) {
979 rv
= fstat(my_fileno(c
->body
), &sb
);
981 const char *cpath
= StringValueCStr(path
);
984 * Rainbows! can use "/dev/fd/%u" in to_path output to avoid
985 * extra open() syscalls, too.
987 if (sscanf(cpath
, "/dev/fd/%u", &devfd
) == 1)
988 rv
= fstat((int)devfd
, &sb
);
990 rv
= stat(cpath
, &sb
);
994 * calling this method implies the web server will bypass
995 * the each method where body_bytes_sent is calculated,
996 * so we stat and set that value here.
998 c
->body_bytes_sent
= rv
== 0 ? sb
.st_size
: 0;
1006 * used to proxy +:to_io+ method calls to the wrapped response body.
1008 static VALUE
to_io(VALUE self
)
1010 struct clogger
*c
= clogger_get(self
);
1012 VALUE io
= rb_convert_type(c
->body
, T_FILE
, "IO", "to_io");
1014 if (fstat(my_fileno(io
), &sb
) == 0)
1015 c
->body_bytes_sent
= sb
.st_size
;
1021 static VALUE
body(VALUE self
)
1023 return clogger_get(self
)->body
;
1026 void Init_clogger_ext(void)
1032 ltlt_id
= rb_intern("<<");
1033 call_id
= rb_intern("call");
1034 each_id
= rb_intern("each");
1035 close_id
= rb_intern("close");
1036 to_i_id
= rb_intern("to_i");
1037 to_s_id
= rb_intern("to_s");
1038 size_id
= rb_intern("size");
1039 sq_brace_id
= rb_intern("[]");
1040 new_id
= rb_intern("new");
1041 to_path_id
= rb_intern("to_path");
1042 to_io_id
= rb_intern("to_io");
1043 respond_to_id
= rb_intern("respond_to?");
1044 cClogger
= rb_define_class("Clogger", rb_cObject
);
1045 mFormat
= rb_define_module_under(cClogger
, "Format");
1046 rb_define_alloc_func(cClogger
, clogger_alloc
);
1047 rb_define_method(cClogger
, "initialize", clogger_init
, -1);
1048 rb_define_method(cClogger
, "initialize_copy", clogger_init_copy
, 1);
1049 rb_define_method(cClogger
, "call", clogger_call
, 1);
1050 rb_define_method(cClogger
, "each", clogger_each
, 0);
1051 rb_define_method(cClogger
, "close", clogger_close
, 0);
1052 rb_define_method(cClogger
, "fileno", clogger_fileno
, 0);
1053 rb_define_method(cClogger
, "wrap_body?", clogger_wrap_body
, 0);
1054 rb_define_method(cClogger
, "reentrant?", clogger_reentrant
, 0);
1055 rb_define_method(cClogger
, "to_path", to_path
, 0);
1056 rb_define_method(cClogger
, "to_io", to_io
, 0);
1057 rb_define_method(cClogger
, "respond_to?", respond_to
, 1);
1058 rb_define_method(cClogger
, "body", body
, 0);
1059 CONST_GLOBAL_STR(REMOTE_ADDR
);
1060 CONST_GLOBAL_STR(HTTP_X_FORWARDED_FOR
);
1061 CONST_GLOBAL_STR(REQUEST_METHOD
);
1062 CONST_GLOBAL_STR(PATH_INFO
);
1063 CONST_GLOBAL_STR(QUERY_STRING
);
1064 CONST_GLOBAL_STR(REQUEST_URI
);
1065 CONST_GLOBAL_STR(HTTP_VERSION
);
1066 CONST_GLOBAL_STR2(rack_errors
, "rack.errors");
1067 CONST_GLOBAL_STR2(rack_input
, "rack.input");
1068 CONST_GLOBAL_STR2(rack_multithread
, "rack.multithread");
1069 CONST_GLOBAL_STR2(dash
, "-");
1070 CONST_GLOBAL_STR2(space
, " ");
1071 CONST_GLOBAL_STR2(question_mark
, "?");
1072 CONST_GLOBAL_STR2(rack_request_cookie_hash
, "rack.request.cookie_hash");
1074 tmp
= rb_const_get(rb_cObject
, rb_intern("Rack"));
1075 tmp
= rb_const_get(tmp
, rb_intern("Utils"));
1076 cHeaderHash
= rb_const_get(tmp
, rb_intern("HeaderHash"));