1 # -*- encoding: binary -*-
4 # See the README for usage instructions
7 # the version of Clogger, currently 0.7.0
22 # support nginx variables that are less customizable than our own
24 '$request_time' => '$request_time{3}',
25 '$time_local' => '$time_local{%d/%b/%Y:%H:%M:%S %z}',
26 '$time_utc' => '$time_utc{%d/%b/%Y:%H:%M:%S %z}',
27 '$msec' => '$time{3}',
28 '$usec' => '$time{6}',
29 '$http_content_length' => '$content_length',
30 '$http_content_type' => '$content_type',
34 :body_bytes_sent => 0,
36 :request => 2, # REQUEST_METHOD PATH_INFO?QUERY_STRING HTTP_VERSION
37 :request_length => 3, # env['rack.input'].size
38 :response_length => 4, # like body_bytes_sent, except "-" instead of "0"
39 :ip => 5, # HTTP_X_FORWARDED_FOR || REMOTE_ADDR || -
46 CGI_ENV = Regexp.new('\A\$(' <<
47 %w(request_method content_length content_type
48 remote_addr remote_ident remote_user
49 path_info query_string script_name
50 server_name server_port
51 auth_type gateway_interface server_software path_translated
54 SCAN = /([^$]*)(\$+(?:env\{\w+(?:\.[\w\.]+)?\}|
56 (?:request_)?time\{\d+\}|
57 time_(?:utc|local)\{[^\}]+\}|
60 def compile_format(str, opt = {})
61 longest_day = Time.at(26265600) # "Saturday, November 01, 1970 00:00:00"
64 str.scan(SCAN).each do |pre,tok,post|
65 rv << [ OP_LITERAL, pre ] if pre && pre != ""
68 if tok.sub!(/\A(\$+)\$/, '$')
69 rv << [ OP_LITERAL, $1 ]
72 compat = ALIASES[tok] and tok = compat
76 rv << [ OP_LITERAL, $1 ]
77 when /\A\$env\{(\w+(?:\.[\w\.]+))\}\z/
78 rv << [ OP_REQUEST, $1 ]
79 when /\A\$e\{([^\}]+)\}\z/
81 when /\A\$cookie_(\w+)\z/
82 rv << [ OP_COOKIE, $1 ]
83 when CGI_ENV, /\A\$(http_\w+)\z/
84 rv << [ OP_REQUEST, $1.upcase ]
85 when /\A\$sent_http_(\w+)\z/
86 rv << [ OP_RESPONSE, $1.downcase.tr('_','-') ]
87 when /\A\$time_local\{([^\}]+)\}\z/
89 rv << [ OP_TIME_LOCAL, fmt, longest_day.strftime(fmt) ]
90 when /\A\$time_utc\{([^\}]+)\}\z/
92 rv << [ OP_TIME_UTC, fmt, longest_day.strftime(fmt) ]
93 when /\A\$time\{(\d+)\}\z/
94 rv << [ OP_TIME, *usec_conv_pair(tok, $1.to_i) ]
95 when /\A\$request_time\{(\d+)\}\z/
96 rv << [ OP_REQUEST_TIME, *usec_conv_pair(tok, $1.to_i) ]
98 tok_sym = tok[1..-1].to_sym
99 if special_code = SPECIAL_VARS[tok_sym]
100 rv << [ OP_SPECIAL, special_code ]
102 raise ArgumentError, "unable to make sense of token: #{tok}"
107 rv << [ OP_LITERAL, post ] if post && post != ""
110 # auto-append a newline
111 last = rv.last or return rv
113 ors = opt[:ORS] || "\n"
114 if (op == OP_LITERAL && /#{ors}\z/ !~ last.last) || op != OP_LITERAL
115 rv << [ OP_LITERAL, ors ] if ors.size > 0
121 def usec_conv_pair(tok, prec)
123 [ "%d", 1 ] # stupid...
125 raise ArgumentError, "#{tok}: too high precision: #{prec} (max=6)"
127 [ "%d.%0#{prec}d", 10 ** (6 - prec) ]
131 def need_response_headers?(fmt_ops)
132 fmt_ops.any? { |op| OP_RESPONSE == op[0] }
135 def need_wrap_body?(fmt_ops)
137 (OP_REQUEST_TIME == op[0]) || (OP_SPECIAL == op[0] &&
138 (SPECIAL_VARS[:body_bytes_sent] == op[1] ||
139 SPECIAL_VARS[:response_length] == op[1]))
146 require 'clogger/format'
149 raise LoadError if ENV['CLOGGER_PURE'].to_i != 0
150 require 'clogger_ext'
152 require 'clogger/pure'