clogger.git  about / heads / tags
configurable request logging for Rack
blob cea4072e9f577cb7d86499f85cb948549359177e 27229 bytes (raw)
$ git show HEAD:ext/clogger_ext/clogger.c	# shows this blob on the CLI

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
 
/* quiet deprecation for _BSD_SOURCE and _SVID_SOURCE: */
#define _DEFAULT_SOURCE 1

#include <ruby.h>
#ifdef HAVE_RUBY_IO_H
#  include <ruby/io.h>
#else
#  include <rubyio.h>
#endif
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>
#ifdef HAVE_FCNTL_H
#  include <fcntl.h>
#endif
#ifndef _POSIX_C_SOURCE
#  define _POSIX_C_SOURCE 200809L
#endif
#include <time.h>
#include <stdlib.h>
#include <stdio.h>		/* snprintf */
#include "ruby_1_9_compat.h"
#include "broken_system_compat.h"
#include "blocking_helpers.h"

/*
 * Availability of a monotonic clock needs to be detected at runtime
 * since we could've been built on a different system than we're run
 * under.
 */
static clockid_t hopefully_CLOCK_MONOTONIC;
static VALUE mark_ary;

static void check_clock(void)
{
	struct timespec now;

	hopefully_CLOCK_MONOTONIC = CLOCK_MONOTONIC;

	/* we can't check this reliably at compile time */
	if (clock_gettime(CLOCK_MONOTONIC, &now) == 0)
		return;

	if (clock_gettime(CLOCK_REALTIME, &now) == 0) {
		hopefully_CLOCK_MONOTONIC = CLOCK_REALTIME;
		rb_warn("CLOCK_MONOTONIC not available, "
			"falling back to CLOCK_REALTIME");
	}
	rb_warn("clock_gettime() totally broken, " \
	        "falling back to pure Ruby Clogger");
	rb_raise(rb_eLoadError, "clock_gettime() broken");
}

static void clock_diff(struct timespec *a, const struct timespec *b)
{
	a->tv_sec -= b->tv_sec;
	a->tv_nsec -= b->tv_nsec;
	if (a->tv_nsec < 0) {
		--a->tv_sec;
		a->tv_nsec += 1000000000;
	}
}

/* give GCC hints for better branch prediction
 * (we layout branches so that ASCII characters are handled faster) */
#if defined(__GNUC__) && (__GNUC__ >= 3)
#  define likely(x)		__builtin_expect (!!(x), 1)
#  define unlikely(x)		__builtin_expect (!!(x), 0)
#else
#  define unlikely(x)		(x)
#  define likely(x)		(x)
#endif

enum clogger_opcode {
	CL_OP_LITERAL = 0,
	CL_OP_REQUEST,
	CL_OP_RESPONSE,
	CL_OP_SPECIAL,
	CL_OP_EVAL,
	CL_OP_TIME_LOCAL,
	CL_OP_TIME_UTC,
	CL_OP_REQUEST_TIME,
	CL_OP_TIME,
	CL_OP_COOKIE
};

enum clogger_special {
	CL_SP_body_bytes_sent = 0,
	CL_SP_status,
	CL_SP_request,
	CL_SP_request_length,
	CL_SP_response_length,
	CL_SP_ip,
	CL_SP_pid,
	CL_SP_request_uri,
	CL_SP_time_iso8601,
	CL_SP_time_local,
	CL_SP_time_utc
};

struct clogger {
	VALUE app;

	VALUE fmt_ops;
	VALUE logger;
	VALUE log_buf;

	VALUE env;
	VALUE cookies;
	VALUE status;
	VALUE headers;
	VALUE body;

	off_t body_bytes_sent;
	struct timespec ts_start;

	int fd;
	int wrap_body;
	int need_resp;
	int reentrant; /* tri-state, -1:auto, 1/0 true/false */
};

static ID write_id;
static ID ltlt_id;
static ID call_id;
static ID close_id;
static ID to_i_id;
static ID to_s_id;
static ID size_id;
static ID sq_brace_id;
static ID new_id;
static ID to_path_id;
static ID respond_to_id;
static VALUE cClogger;
static VALUE mFormat;
static VALUE cRackHeaders;

/* common hash lookup keys */
static VALUE g_HTTP_X_FORWARDED_FOR;
static VALUE g_REMOTE_ADDR;
static VALUE g_REQUEST_METHOD;
static VALUE g_PATH_INFO;
static VALUE g_REQUEST_URI;
static VALUE g_QUERY_STRING;
static VALUE g_HTTP_VERSION;
static VALUE g_rack_errors;
static VALUE g_rack_input;
static VALUE g_rack_multithread;
static VALUE g_dash;
static VALUE g_space;
static VALUE g_question_mark;
static VALUE g_rack_request_cookie_hash;

#define LOG_BUF_INIT_SIZE 128

static void init_buffers(struct clogger *c)
{
	c->log_buf = rb_str_buf_new(LOG_BUF_INIT_SIZE);
}

static inline int need_escape(unsigned c)
{
	assert(c <= 0xff);
	return !!(c == '\'' || c == '"' || c <= 0x1f || c >= 0x7f);
}

/* we are encoding-agnostic, clients can send us all sorts of junk */
static VALUE byte_xs(VALUE obj)
{
	static const char esc[] = "0123456789ABCDEF";
	unsigned char *new_ptr;
	if (rb_obj_is_kind_of(obj, rb_cArray)) {
		// Rack 3
		obj = rb_ary_join(obj, rb_str_new2("\n"));
	}
	VALUE from = rb_obj_as_string(obj);
	const unsigned char *ptr = (const unsigned char *)RSTRING_PTR(from);
	long len = RSTRING_LEN(from);
	long new_len = len;
	VALUE rv;

	for (; --len >= 0; ptr++) {
		unsigned c = *ptr;

		if (unlikely(need_escape(c)))
			new_len += 3; /* { '\', 'x', 'X', 'X' } */
	}

	len = RSTRING_LEN(from);
	if (new_len == len)
		return from;

	rv = rb_str_new(NULL, new_len);
	new_ptr = (unsigned char *)RSTRING_PTR(rv);
	ptr = (const unsigned char *)RSTRING_PTR(from);
	for (; --len >= 0; ptr++) {
		unsigned c = *ptr;

		if (unlikely(need_escape(c))) {
			*new_ptr++ = '\\';
			*new_ptr++ = 'x';
			*new_ptr++ = esc[c >> 4];
			*new_ptr++ = esc[c & 0xf];
		} else {
			*new_ptr++ = c;
		}
	}
	assert(RSTRING_PTR(rv)[RSTRING_LEN(rv)] == '\0');

	RB_GC_GUARD(from);
	return rv;
}

static void clogger_mark(void *ptr)
{
	struct clogger *c = ptr;

	rb_gc_mark(c->app);
	rb_gc_mark(c->fmt_ops);
	rb_gc_mark(c->logger);
	rb_gc_mark(c->log_buf);
	rb_gc_mark(c->env);
	rb_gc_mark(c->cookies);
	rb_gc_mark(c->status);
	rb_gc_mark(c->headers);
	rb_gc_mark(c->body);
}

static size_t memsize(const void *ptr)
{
	return sizeof(struct clogger);
}

static const rb_data_type_t clogger_type = {
	"clogger",
	{ clogger_mark, RUBY_TYPED_DEFAULT_FREE, memsize, /* reserved */ },
};

static VALUE clogger_alloc(VALUE klass)
{
	struct clogger *c;

	return TypedData_Make_Struct(klass, struct clogger, &clogger_type, c);
}

static struct clogger *clogger_get(VALUE self)
{
	struct clogger *c;

	TypedData_Get_Struct(self, struct clogger, &clogger_type, c);
	assert(c);
	return c;
}

/* only for writing to regular files, not stupid crap like NFS  */
static void write_full(int fd, const char *buf, size_t count)
{
	ssize_t r;

	while (count > 0) {
		r = nogvl_write(fd, buf, count);

		if ((size_t)r == count) { /* overwhelmingly likely */
			return;
		} else if (r > 0) {
			count -= r;
			buf += r;
		} else {
			if (errno == EINTR || errno == EAGAIN)
				continue; /* poor souls on NFS and like: */
			if (!errno)
				errno = ENOSPC;
			rb_sys_fail("write");
		}
	}
}

/*
 * allow us to use write_full() iff we detect a blocking file
 * descriptor that wouldn't play nicely with Ruby threading/fibers
 */
static int raw_fd(VALUE my_fd)
{
#if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
	int fd;
	int flags;

	if (NIL_P(my_fd))
		return -1;
	fd = NUM2INT(my_fd);

	flags = fcntl(fd, F_GETFL);
	if (flags < 0)
		rb_sys_fail("fcntl");

	if (flags & O_NONBLOCK) {
		struct stat sb;

		if (fstat(fd, &sb) < 0)
			return -1;

		/* O_NONBLOCK is no-op for regular files: */
		if (! S_ISREG(sb.st_mode))
			return -1;
	}
	return fd;
#else /* platforms w/o fcntl/F_GETFL/O_NONBLOCK */
	return -1;
#endif /* platforms w/o fcntl/F_GETFL/O_NONBLOCK */
}

/* :nodoc: */
static VALUE clogger_reentrant(VALUE self)
{
	return clogger_get(self)->reentrant == 0 ? Qfalse : Qtrue;
}

/* :nodoc: */
static VALUE clogger_wrap_body(VALUE self)
{
	return clogger_get(self)->wrap_body == 0 ? Qfalse : Qtrue;
}

static void append_status(struct clogger *c)
{
	char buf[sizeof("999")];
	int nr;
	VALUE status = c->status;

	if (TYPE(status) != T_FIXNUM) {
		status = rb_funcall(status, to_i_id, 0);
		/* no way it's a valid status code (at least not HTTP/1.1) */
		if (TYPE(status) != T_FIXNUM) {
			rb_str_buf_append(c->log_buf, g_dash);
			return;
		}
	}

	nr = FIX2INT(status);
	if (nr >= 100 && nr <= 999) {
		nr = snprintf(buf, sizeof(buf), "%03d", nr);
		assert(nr == 3);
		rb_str_buf_cat(c->log_buf, buf, nr);
	} else {
		/* raise?, swap for 500? */
		rb_str_buf_append(c->log_buf, g_dash);
	}
}

/* this is Rack 1.0.0-compatible, won't try to parse commas in XFF */
static void append_ip(struct clogger *c)
{
	VALUE env = c->env;
	VALUE tmp = rb_hash_aref(env, g_HTTP_X_FORWARDED_FOR);

	if (NIL_P(tmp)) {
		/* can't be faked on any real server, so no escape */
		tmp = rb_hash_aref(env, g_REMOTE_ADDR);
		if (NIL_P(tmp))
			tmp = g_dash;
	} else {
		tmp = byte_xs(tmp);
	}
	rb_str_buf_append(c->log_buf, tmp);
}

static void append_body_bytes_sent(struct clogger *c)
{
	char buf[(sizeof(off_t) * 8) / 3 + 1];
	const char *fmt = sizeof(off_t) == sizeof(long) ? "%ld" : "%lld";
	int nr = snprintf(buf, sizeof(buf), fmt, c->body_bytes_sent);

	assert(nr > 0 && nr < (int)sizeof(buf));
	rb_str_buf_cat(c->log_buf, buf, nr);
}

static void append_ts(struct clogger *c, VALUE op, struct timespec *ts)
{
	char buf[sizeof(".000000") + ((sizeof(ts->tv_sec) * 8) / 3)];
	int nr;
	char *fmt = RSTRING_PTR(rb_ary_entry(op, 1));
	int ndiv = NUM2INT(rb_ary_entry(op, 2));
	int usec = ts->tv_nsec / 1000;

	nr = snprintf(buf, sizeof(buf), fmt,
		      (int)ts->tv_sec, (int)(usec / ndiv));
	assert(nr > 0 && nr < (int)sizeof(buf));
	rb_str_buf_cat(c->log_buf, buf, nr);
}

#define NANO_PER_SEC (1000 * 1000 * 1000)
static void append_request_time_fmt(struct clogger *c, VALUE op)
{
	struct timespec now;
	unsigned long ipow = NUM2ULONG(rb_ary_entry(op, 3));

	clock_gettime(hopefully_CLOCK_MONOTONIC, &now);
	clock_diff(&now, &c->ts_start);
	if (ipow) {
		unsigned long adj = 1;
		int64_t now_sec = now.tv_sec, now_nsec = now.tv_nsec,
			prev_sec = now.tv_sec;

		do { adj *= 10; } while (--ipow);
		now_sec *= adj;
		now_nsec *= adj;
		if (now_nsec >= NANO_PER_SEC) {
			int64_t add = now_nsec / NANO_PER_SEC;
			now_sec += add;
			now_nsec %= NANO_PER_SEC;
		}
		if (now_sec < prev_sec) { /* overflowed */
			now_nsec = NANO_PER_SEC - 1;
			/*
			  * some platforms may use unsigned .tv_sec, but
			  * they're not worth supporting, so keep unsigned:
			  */
			now_sec = (time_t)(sizeof(now.tv_sec) == 4 ?
			             INT_MAX : LONG_MAX);
		}
		now.tv_sec = now_sec;
		now.tv_nsec = now_nsec;
	}
	append_ts(c, op, &now);
}

static void append_time_fmt(struct clogger *c, VALUE op)
{
	struct timespec now;
	int r = clock_gettime(CLOCK_REALTIME, &now);

	if (unlikely(r != 0))
		rb_sys_fail("clock_gettime(CLOCK_REALTIME)");
	append_ts(c, op, &now);
}

static void append_request_uri(struct clogger *c)
{
	VALUE tmp;

	tmp = rb_hash_aref(c->env, g_REQUEST_URI);
	if (NIL_P(tmp)) {
		tmp = rb_hash_aref(c->env, g_PATH_INFO);
		if (!NIL_P(tmp))
			rb_str_buf_append(c->log_buf, byte_xs(tmp));
		tmp = rb_hash_aref(c->env, g_QUERY_STRING);
		if (!NIL_P(tmp) && RSTRING_LEN(tmp) != 0) {
			rb_str_buf_append(c->log_buf, g_question_mark);
			rb_str_buf_append(c->log_buf, byte_xs(tmp));
		}
	} else {
		rb_str_buf_append(c->log_buf, byte_xs(tmp));
	}
}

static void append_request(struct clogger *c)
{
	VALUE tmp;

	tmp = rb_hash_aref(c->env, g_REQUEST_METHOD);
	if (!NIL_P(tmp)) {
		tmp = byte_xs(tmp);
		rb_str_buf_append(c->log_buf, tmp);
	}

	rb_str_buf_append(c->log_buf, g_space);

	append_request_uri(c);

	/* HTTP_VERSION can be injected by malicious clients */
	tmp = rb_hash_aref(c->env, g_HTTP_VERSION);
	if (!NIL_P(tmp)) {
		rb_str_buf_append(c->log_buf, g_space);
		rb_str_buf_append(c->log_buf, byte_xs(tmp));
	}
}

static void append_request_length(struct clogger *c)
{
	VALUE tmp = rb_hash_aref(c->env, g_rack_input);
	if (NIL_P(tmp)) {
		rb_str_buf_append(c->log_buf, g_dash);
	} else {
		tmp = rb_funcall(tmp, size_id, 0);
		rb_str_buf_append(c->log_buf, rb_funcall(tmp, to_s_id, 0));
	}
}

/*
 * time(2) may slip backwards, so use CLOCK_REALTIME for accuracy
 * https://lore.kernel.org/git/20230319064353.686226-3-eggert@cs.ucla.edu/T/
 */
static time_t cur_time(void)
{
	struct timespec now;

	(void)clock_gettime(CLOCK_REALTIME, &now);
	return now.tv_sec;
}

static long local_gmtoffset(struct tm *tm)
{
	time_t t = cur_time();

	tzset();
	localtime_r(&t, tm);

/*
 * HAVE_STRUCT_TM_TM_GMTOFF may be defined in Ruby headers
 * HAVE_ST_TM_GMTOFF is defined ourselves.
 */
#if defined(HAVE_STRUCT_TM_TM_GMTOFF) || defined(HAVE_ST_TM_GMTOFF)
	return tm->tm_gmtoff / 60;
#else
	return -(tm->tm_isdst ? timezone - 3600 : timezone) / 60;
#endif
}

static void append_time_iso8601(struct clogger *c)
{
	char buf[sizeof("1970-01-01T00:00:00+00:00")];
	struct tm tm;
	int nr;
	long gmtoff = local_gmtoffset(&tm);

	nr = snprintf(buf, sizeof(buf),
	              "%4d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
	              tm.tm_year + 1900, tm.tm_mon + 1,
	              tm.tm_mday, tm.tm_hour,
	              tm.tm_min, tm.tm_sec,
	              gmtoff < 0 ? '-' : '+',
	              abs(gmtoff / 60), abs(gmtoff % 60));
	assert(nr == (sizeof(buf) - 1) && "snprintf fail");
	rb_str_buf_cat(c->log_buf, buf, sizeof(buf) - 1);
}

static const char months[] = "Jan\0Feb\0Mar\0Apr\0May\0Jun\0"
                             "Jul\0Aug\0Sep\0Oct\0Nov\0Dec";

static void append_time_local(struct clogger *c)
{
	char buf[sizeof("01/Jan/1970:00:00:00 +0000")];
	struct tm tm;
	int nr;
	long gmtoff = local_gmtoffset(&tm);

	nr = snprintf(buf, sizeof(buf),
	              "%02d/%s/%d:%02d:%02d:%02d %c%02d%02d",
	              tm.tm_mday, months + (tm.tm_mon * sizeof("Jan")),
	              tm.tm_year + 1900, tm.tm_hour,
	              tm.tm_min, tm.tm_sec,
	              gmtoff < 0 ? '-' : '+',
	              abs(gmtoff / 60), abs(gmtoff % 60));
	assert(nr == (sizeof(buf) - 1) && "snprintf fail");
	rb_str_buf_cat(c->log_buf, buf, sizeof(buf) - 1);
}

static void append_time_utc(struct clogger *c)
{
	char buf[sizeof("01/Jan/1970:00:00:00 +0000")];
	struct tm tm;
	int nr;
	time_t t = cur_time();

	gmtime_r(&t, &tm);
	nr = snprintf(buf, sizeof(buf),
	              "%02d/%s/%d:%02d:%02d:%02d +0000",
	              tm.tm_mday, months + (tm.tm_mon * sizeof("Jan")),
	              tm.tm_year + 1900, tm.tm_hour,
	              tm.tm_min, tm.tm_sec);
	assert(nr == (sizeof(buf) - 1) && "snprintf fail");
	rb_str_buf_cat(c->log_buf, buf, sizeof(buf) - 1);
}

static void
append_time(struct clogger *c, enum clogger_opcode op, VALUE fmt, VALUE buf)
{
	char *buf_ptr = RSTRING_PTR(buf);
	size_t buf_size = RSTRING_LEN(buf) + 1; /* "\0" */
	size_t nr;
	struct tm tmp;
	time_t t = cur_time();

	if (op == CL_OP_TIME_LOCAL)
		localtime_r(&t, &tmp);
	else if (op == CL_OP_TIME_UTC)
		gmtime_r(&t, &tmp);
	else
		assert(0 && "unknown op");

	nr = strftime(buf_ptr, buf_size, RSTRING_PTR(fmt), &tmp);
	assert(nr < buf_size && "time format too small!");
	rb_str_buf_cat(c->log_buf, buf_ptr, nr);
}

static void append_pid(struct clogger *c)
{
	char buf[(sizeof(pid_t) * 8) / 3 + 1];
	int nr = snprintf(buf, sizeof(buf), "%d", (int)getpid());

	assert(nr > 0 && nr < (int)sizeof(buf));
	rb_str_buf_cat(c->log_buf, buf, nr);
}

static void append_eval(struct clogger *c, VALUE str)
{
	int state = -1;
	VALUE rv = rb_eval_string_protect(RSTRING_PTR(str), &state);

	rv = state == 0 ? rb_obj_as_string(rv) : g_dash;
	rb_str_buf_append(c->log_buf, rv);
}

static void append_cookie(struct clogger *c, VALUE key)
{
	VALUE cookie;

	if (c->cookies == Qfalse)
		c->cookies = rb_hash_aref(c->env, g_rack_request_cookie_hash);

	if (NIL_P(c->cookies)) {
		cookie = g_dash;
	} else {
		cookie = rb_hash_aref(c->cookies, key);
		cookie = NIL_P(cookie) ? g_dash : byte_xs(cookie);
	}
	rb_str_buf_append(c->log_buf, cookie);
}

static void append_request_env(struct clogger *c, VALUE key)
{
	VALUE tmp = rb_hash_aref(c->env, key);

	tmp = NIL_P(tmp) ? g_dash : byte_xs(tmp);
	rb_str_buf_append(c->log_buf, tmp);
}

static void append_response(struct clogger *c, VALUE key)
{
	VALUE v;

	assert(rb_obj_is_kind_of(c->headers, cRackHeaders)
		&& "not Rack::Headers");

	v = rb_funcall(c->headers, sq_brace_id, 1, key);
	v = NIL_P(v) ? g_dash : byte_xs(v);
	rb_str_buf_append(c->log_buf, v);
}

static void special_var(struct clogger *c, enum clogger_special var)
{
	switch (var) {
	case CL_SP_body_bytes_sent:
		append_body_bytes_sent(c);
		break;
	case CL_SP_status:
		append_status(c);
		break;
	case CL_SP_request:
		append_request(c);
		break;
	case CL_SP_request_length:
		append_request_length(c);
		break;
	case CL_SP_response_length:
		if (c->body_bytes_sent == 0)
			rb_str_buf_append(c->log_buf, g_dash);
		else
			append_body_bytes_sent(c);
		break;
	case CL_SP_ip:
		append_ip(c);
		break;
	case CL_SP_pid:
		append_pid(c);
                break;
	case CL_SP_request_uri:
		append_request_uri(c);
		break;
	case CL_SP_time_iso8601:
		append_time_iso8601(c);
		break;
	case CL_SP_time_local:
		append_time_local(c);
		break;
	case CL_SP_time_utc:
		append_time_utc(c);
	}
}

static VALUE cwrite(struct clogger *c)
{
	const VALUE ops = c->fmt_ops;
	long i;
	long len = RARRAY_LEN(ops);
	VALUE dst = c->log_buf;

	rb_str_set_len(dst, 0);

	for (i = 0; i < len; i++) {
		VALUE op = rb_ary_entry(ops, i);
		enum clogger_opcode opcode = FIX2INT(rb_ary_entry(op, 0));
		VALUE op1 = rb_ary_entry(op, 1);

		switch (opcode) {
		case CL_OP_LITERAL:
			rb_str_buf_append(dst, op1);
			break;
		case CL_OP_REQUEST:
			append_request_env(c, op1);
			break;
		case CL_OP_RESPONSE:
			append_response(c, op1);
			break;
		case CL_OP_SPECIAL:
			special_var(c, FIX2INT(op1));
			break;
		case CL_OP_EVAL:
			append_eval(c, op1);
			break;
		case CL_OP_TIME_LOCAL:
		case CL_OP_TIME_UTC: {
			VALUE arg2 = rb_ary_entry(op, 2);
			append_time(c, opcode, op1, arg2);
		}
			break;
		case CL_OP_REQUEST_TIME:
			append_request_time_fmt(c, op);
			break;
		case CL_OP_TIME:
			append_time_fmt(c, op);
			break;
		case CL_OP_COOKIE:
			append_cookie(c, op1);
			break;
		}
	}

	if (c->fd >= 0) {
		write_full(c->fd, RSTRING_PTR(dst), RSTRING_LEN(dst));
		/* no need for RB_GC_GUARD(dst) here, marked as c->log_buf */
	} else {
		VALUE logger = c->logger;

		if (NIL_P(logger)) {
			logger = rb_hash_aref(c->env, g_rack_errors);
                        rb_funcall(logger, write_id, 1, dst);
                } else {
			rb_funcall(logger, ltlt_id, 1, dst);
		}
	}

	return Qnil;
}

static VALUE clogger_write(VALUE self)
{
	return cwrite(clogger_get(self));
}

static void init_logger(struct clogger *c, VALUE path)
{
	ID id;

	if (!NIL_P(path) && !NIL_P(c->logger))
		rb_raise(rb_eArgError, ":logger and :path are independent");
	if (!NIL_P(path)) {
		VALUE ab = rb_str_new2("ab");
		id = rb_intern("open");
		c->logger = rb_funcall(rb_cFile, id, 2, path, ab);
	}

	id = rb_intern("sync=");
	if (rb_respond_to(c->logger, id))
		rb_funcall(c->logger, id, 1, Qtrue);

	id = rb_intern("fileno");
	if (rb_respond_to(c->logger, id))
		c->fd = raw_fd(rb_funcall(c->logger, id, 0));
}

/**
 * call-seq:
 *   Clogger.new(app, :logger => $stderr, :format => string) => obj
 *
 * Creates a new Clogger object that wraps +app+.  +:logger+ may
 * be any object that responds to the "<<" method with a string argument.
 * Instead of +:logger+, +:path+ may be specified to be a :path of a File
 * that will be opened in append mode.
 */
static VALUE clogger_init(int argc, VALUE *argv, VALUE self)
{
	struct clogger *c = clogger_get(self);
	VALUE o = Qnil;
	VALUE fmt = rb_const_get(mFormat, rb_intern("Common"));

	rb_scan_args(argc, argv, "11", &c->app, &o);
	c->fd = -1;
	c->logger = Qnil;
	c->reentrant = -1; /* auto-detect */

	if (TYPE(o) == T_HASH) {
		VALUE tmp;

		tmp = rb_hash_aref(o, ID2SYM(rb_intern("path")));
		c->logger = rb_hash_aref(o, ID2SYM(rb_intern("logger")));
		init_logger(c, tmp);

		tmp = rb_hash_aref(o, ID2SYM(rb_intern("format")));
		if (!NIL_P(tmp))
			fmt = tmp;

		tmp = rb_hash_aref(o, ID2SYM(rb_intern("reentrant")));
		switch (TYPE(tmp)) {
		case T_TRUE:
			c->reentrant = 1;
			break;
		case T_FALSE:
			c->reentrant = 0;
		case T_NIL:
			break;
		default:
			rb_raise(rb_eArgError, ":reentrant must be boolean");
		}
	}

	init_buffers(c);
	c->fmt_ops = rb_funcall(self, rb_intern("compile_format"), 2, fmt, o);

	if (Qtrue == rb_funcall(self, rb_intern("need_response_headers?"),
	                        1, c->fmt_ops))
		c->need_resp = 1;
	if (Qtrue == rb_funcall(self, rb_intern("need_wrap_body?"),
	                        1, c->fmt_ops))
		c->wrap_body = 1;

	return self;
}

static VALUE body_iter_i(VALUE str, VALUE self)
{
	struct clogger *c = clogger_get(self);

	str = rb_obj_as_string(str);
	c->body_bytes_sent += RSTRING_LEN(str);

	return rb_yield(str);
}

static VALUE body_close(VALUE self)
{
	struct clogger *c = clogger_get(self);

	if (rb_respond_to(c->body, close_id))
		return rb_funcall(c->body, close_id, 0);
	return Qnil;
}

/**
 * call-seq:
 *   clogger.each { |part| socket.write(part) }
 *
 * Delegates the body#each call to the underlying +body+ object
 * while tracking the number of bytes yielded.  This will log
 * the request.
 */
static VALUE clogger_each(VALUE self)
{
	struct clogger *c = clogger_get(self);

	rb_need_block();
	c->body_bytes_sent = 0;
	rb_iterate(rb_each, c->body, body_iter_i, self);

	return self;
}

/**
 * call-seq:
 *   clogger.close
 *
 * Delegates the body#close call to the underlying +body+ object.
 * This is only used when Clogger is wrapping the +body+ of a Rack
 * response and should be automatically called by the web server.
 */
static VALUE clogger_close(VALUE self)
{

	return rb_ensure(body_close, self, clogger_write, self);
}

/* :nodoc: */
static VALUE clogger_fileno(VALUE self)
{
	struct clogger *c = clogger_get(self);

	return c->fd < 0 ? Qnil : INT2NUM(c->fd);
}

static VALUE ccall(struct clogger *c, VALUE env)
{
	VALUE rv;

	clock_gettime(hopefully_CLOCK_MONOTONIC, &c->ts_start);
	c->env = env;
	c->cookies = Qfalse;
	rv = rb_funcall(c->app, call_id, 1, env);
	if (TYPE(rv) == T_ARRAY && RARRAY_LEN(rv) == 3) {
		c->status = rb_ary_entry(rv, 0);
		c->headers = rb_ary_entry(rv, 1);
		c->body = rb_ary_entry(rv, 2);

		rv = rb_ary_dup(rv);
		if (c->need_resp &&
                    ! rb_obj_is_kind_of(c->headers, cRackHeaders)) {
			c->headers = rb_funcall(cRackHeaders, sq_brace_id,
			                        1, c->headers);
		}
	} else {
		VALUE tmp = rb_inspect(rv);

		c->status = INT2FIX(500);
		c->headers = c->body = rb_ary_new();
		cwrite(c);
		rb_raise(rb_eTypeError,
		         "app response not a 3 element Array: %s",
			 RSTRING_PTR(tmp));
		RB_GC_GUARD(tmp);
	}

	return rv;
}

/*
 * call-seq:
 *   clogger.call(env) => [ status, headers, body ]
 *
 * calls the wrapped Rack application with +env+, returns the
 * [status, headers, body ] tuplet required by Rack.
 */
static VALUE clogger_call(VALUE self, VALUE env)
{
	struct clogger *c = clogger_get(self);
	VALUE rv;

	env = rb_check_convert_type(env, T_HASH, "Hash", "to_hash");

	if (c->wrap_body) {
		/* XXX: we assume the existence of the GVL here: */
		if (c->reentrant < 0) {
			VALUE tmp = rb_hash_aref(env, g_rack_multithread);
			c->reentrant = Qfalse == tmp ? 0 : 1;
		}

		if (c->reentrant) {
			self = rb_obj_dup(self);
			c = clogger_get(self);
		}

		rv = ccall(c, env);
		assert(!OBJ_FROZEN(rv) && "frozen response array");
		rb_ary_store(rv, 2, self);

		return rv;
	}

	rv = ccall(c, env);
	cwrite(c);

	return rv;
}

static void duplicate_buffers(VALUE ops)
{
	long i;
	long len = RARRAY_LEN(ops);

	for (i = 0; i < len; i++) {
		VALUE op = rb_ary_entry(ops, i);
		enum clogger_opcode opcode = FIX2INT(rb_ary_entry(op, 0));

		if (opcode == CL_OP_TIME_LOCAL || opcode == CL_OP_TIME_UTC) {
			VALUE buf = rb_ary_entry(op, 2);
			Check_Type(buf, T_STRING);
			buf = rb_str_dup(buf);
			rb_str_modify(buf); /* trigger copy-on-write */
			rb_ary_store(op, 2, buf);
		}
	}
}

/* :nodoc: */
static VALUE clogger_init_copy(VALUE clone, VALUE orig)
{
	struct clogger *a = clogger_get(orig);
	struct clogger *b = clogger_get(clone);

	memcpy(b, a, sizeof(struct clogger));
	init_buffers(b);
	duplicate_buffers(b->fmt_ops);

	return clone;
}

#define CONST_GLOBAL_STR2(var, val) do { \
	g_##var = rb_obj_freeze(rb_str_new(val, sizeof(val) - 1)); \
	rb_ary_push(mark_ary, g_##var); \
} while (0)

#define CONST_GLOBAL_STR(val) CONST_GLOBAL_STR2(val, #val)

/*
 * call-seq:
 *   clogger.respond_to?(:to_path) => true or false
 *   clogger.respond_to?(:close)  => true
 *
 * used to delegate +:to_path+ checks for Rack webservers that optimize
 * static file serving
 */
static VALUE respond_to(int argc, VALUE *argv, VALUE self)
{
	struct clogger *c = clogger_get(self);
	VALUE method, include_all;
	ID id;

	rb_scan_args(argc, argv, "11", &method, &include_all);
	id = rb_to_id(method);
	if (close_id == id)
		return Qtrue;

#ifdef HAVE_RB_OBJ_RESPOND_TO
	return rb_obj_respond_to(c->body, id, RTEST(include_all));
#endif
	if (argc == 1)
		return rb_respond_to(c->body, id);
	return rb_funcallv(c->body, respond_to_id, argc, argv);
}

/*
 * call-seq:
 *   clogger.to_path
 *
 * used to proxy +:to_path+ method calls to the wrapped response body.
 */
static VALUE to_path(VALUE self)
{
	struct clogger *c = clogger_get(self);
	struct stat sb;
	int rv;
	VALUE path = rb_funcall(c->body, to_path_id, 0);
	const char *cpath = StringValueCStr(path);
	unsigned devfd;

	/*
	 * Rainbows! can use "/dev/fd/%u" in to_path output to avoid
	 * extra open() syscalls, too.
	 */
	if (sscanf(cpath, "/dev/fd/%u", &devfd) == 1)
		rv = fstat((int)devfd, &sb);
	else
		rv = nogvl_stat(cpath, &sb);

	/*
	 * calling this method implies the web server will bypass
	 * the each method where body_bytes_sent is calculated,
	 * so we stat and set that value here.
	 */
	c->body_bytes_sent = rv == 0 ? sb.st_size : 0;
	return path;
}

/* :nodoc: */
static VALUE body(VALUE self)
{
	return clogger_get(self)->body;
}

void Init_clogger_ext(void)
{
	VALUE tmp;

	mark_ary = rb_ary_new();
	rb_global_variable(&mark_ary);

	check_clock();

	write_id = rb_intern("write");
	ltlt_id = rb_intern("<<");
	call_id = rb_intern("call");
	close_id = rb_intern("close");
	to_i_id = rb_intern("to_i");
	to_s_id = rb_intern("to_s");
	size_id = rb_intern("size");
	sq_brace_id = rb_intern("[]");
	new_id = rb_intern("new");
	to_path_id = rb_intern("to_path");
	respond_to_id = rb_intern("respond_to?");
	cClogger = rb_define_class("Clogger", rb_cObject);
	mFormat = rb_define_module_under(cClogger, "Format");
	rb_define_alloc_func(cClogger, clogger_alloc);
	rb_define_method(cClogger, "initialize", clogger_init, -1);
	rb_define_method(cClogger, "initialize_copy", clogger_init_copy, 1);
	rb_define_method(cClogger, "call", clogger_call, 1);
	rb_define_method(cClogger, "each", clogger_each, 0);
	rb_define_method(cClogger, "close", clogger_close, 0);
	rb_define_method(cClogger, "fileno", clogger_fileno, 0);
	rb_define_method(cClogger, "wrap_body?", clogger_wrap_body, 0);
	rb_define_method(cClogger, "reentrant?", clogger_reentrant, 0);
	rb_define_method(cClogger, "to_path", to_path, 0);
	rb_define_method(cClogger, "respond_to?", respond_to, -1);
	rb_define_method(cClogger, "body", body, 0);
	CONST_GLOBAL_STR(REMOTE_ADDR);
	CONST_GLOBAL_STR(HTTP_X_FORWARDED_FOR);
	CONST_GLOBAL_STR(REQUEST_METHOD);
	CONST_GLOBAL_STR(PATH_INFO);
	CONST_GLOBAL_STR(QUERY_STRING);
	CONST_GLOBAL_STR(REQUEST_URI);
	CONST_GLOBAL_STR(HTTP_VERSION);
	CONST_GLOBAL_STR2(rack_errors, "rack.errors");
	CONST_GLOBAL_STR2(rack_input, "rack.input");
	CONST_GLOBAL_STR2(rack_multithread, "rack.multithread");
	CONST_GLOBAL_STR2(dash, "-");
	CONST_GLOBAL_STR2(space, " ");
	CONST_GLOBAL_STR2(question_mark, "?");
	CONST_GLOBAL_STR2(rack_request_cookie_hash, "rack.request.cookie_hash");

	tmp = rb_const_get(rb_cObject, rb_intern("Rack"));
	if (rb_const_defined(tmp, rb_intern("Headers"))) {
		// Rack >= 3.0
		cRackHeaders = rb_const_get(tmp, rb_intern("Headers"));
	} else {
		// Rack < 3.0
		tmp = rb_const_get(tmp, rb_intern("Utils"));
		cRackHeaders = rb_const_get(tmp, rb_intern("HeaderHash"));
	}
	rb_ary_push(mark_ary, cRackHeaders);

	rb_obj_freeze(mark_ary);
}

git clone https://yhbt.net/clogger.git