2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2011-05-09 23:22:41 +04:00
|
|
|
#--
|
2003-07-23 20:51:36 +04:00
|
|
|
# accesslog.rb -- Access log handling utilities
|
|
|
|
#
|
|
|
|
# Author: IPR -- Internet Programming with Ruby -- writers
|
|
|
|
# Copyright (c) 2002 keita yamaguchi
|
|
|
|
# Copyright (c) 2002 Internet Programming with Ruby writers
|
|
|
|
#
|
|
|
|
# $IPR: accesslog.rb,v 1.1 2002/10/01 17:16:32 gotoyuzo Exp $
|
|
|
|
|
|
|
|
module WEBrick
|
2011-05-10 04:13:58 +04:00
|
|
|
|
|
|
|
##
|
|
|
|
# AccessLog provides logging to various files in various formats.
|
|
|
|
#
|
|
|
|
# Multiple logs may be written to at the same time:
|
|
|
|
#
|
|
|
|
# access_log = [
|
|
|
|
# [$stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT],
|
|
|
|
# [$stderr, WEBrick::AccessLog::REFERER_LOG_FORMAT],
|
|
|
|
# ]
|
|
|
|
#
|
|
|
|
# server = WEBrick::HTTPServer.new :AccessLog => access_log
|
|
|
|
#
|
|
|
|
# Custom log formats may be defined. WEBrick::AccessLog provides a subset
|
|
|
|
# of the formatting from Apache's mod_log_config
|
|
|
|
# http://httpd.apache.org/docs/mod/mod_log_config.html#formats. See
|
|
|
|
# AccessLog::setup_params for a list of supported options
|
|
|
|
|
2003-07-23 20:51:36 +04:00
|
|
|
module AccessLog
|
2011-05-10 04:13:58 +04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Raised if a parameter such as %e, %i, %o or %n is used without fetching
|
|
|
|
# a specific field.
|
|
|
|
|
2003-07-23 20:51:36 +04:00
|
|
|
class AccessLogError < StandardError; end
|
|
|
|
|
2011-05-10 04:13:58 +04:00
|
|
|
##
|
|
|
|
# The Common Log Format's time format
|
|
|
|
|
2003-07-23 20:51:36 +04:00
|
|
|
CLF_TIME_FORMAT = "[%d/%b/%Y:%H:%M:%S %Z]"
|
2011-05-10 04:13:58 +04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Common Log Format
|
|
|
|
|
2003-07-23 20:51:36 +04:00
|
|
|
COMMON_LOG_FORMAT = "%h %l %u %t \"%r\" %s %b"
|
2011-05-10 04:13:58 +04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Short alias for Common Log Format
|
|
|
|
|
2003-07-23 20:51:36 +04:00
|
|
|
CLF = COMMON_LOG_FORMAT
|
2011-05-10 04:13:58 +04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Referer Log Format
|
|
|
|
|
2003-07-23 20:51:36 +04:00
|
|
|
REFERER_LOG_FORMAT = "%{Referer}i -> %U"
|
2011-05-10 04:13:58 +04:00
|
|
|
|
|
|
|
##
|
|
|
|
# User-Agent Log Format
|
|
|
|
|
2003-07-23 20:51:36 +04:00
|
|
|
AGENT_LOG_FORMAT = "%{User-Agent}i"
|
2011-05-10 04:13:58 +04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Combined Log Format
|
|
|
|
|
2003-07-23 20:51:36 +04:00
|
|
|
COMBINED_LOG_FORMAT = "#{CLF} \"%{Referer}i\" \"%{User-agent}i\""
|
|
|
|
|
|
|
|
module_function
|
|
|
|
|
2011-05-10 04:13:58 +04:00
|
|
|
# This format specification is a subset of mod_log_config of Apache:
|
|
|
|
#
|
|
|
|
# %a:: Remote IP address
|
|
|
|
# %b:: Total response size
|
|
|
|
# %e{variable}:: Given variable in ENV
|
|
|
|
# %f:: Response filename
|
|
|
|
# %h:: Remote host name
|
|
|
|
# %{header}i:: Given request header
|
|
|
|
# %l:: Remote logname, always "-"
|
|
|
|
# %m:: Request method
|
|
|
|
# %{attr}n:: Given request attribute from <tt>req.attributes</tt>
|
|
|
|
# %{header}o:: Given response header
|
|
|
|
# %p:: Server's request port
|
|
|
|
# %{format}p:: The canonical port of the server serving the request or the
|
|
|
|
# actual port or the client's actual port. Valid formats are
|
|
|
|
# canonical, local or remote.
|
|
|
|
# %q:: Request query string
|
|
|
|
# %r:: First line of the request
|
|
|
|
# %s:: Request status
|
2013-12-11 07:58:07 +04:00
|
|
|
# %t:: Time the request was received
|
2011-05-10 04:13:58 +04:00
|
|
|
# %T:: Time taken to process the request
|
|
|
|
# %u:: Remote user from auth
|
|
|
|
# %U:: Unparsed URI
|
|
|
|
# %%:: Literal %
|
|
|
|
|
2011-06-23 11:22:37 +04:00
|
|
|
def setup_params(config, req, res)
|
2003-07-23 20:51:36 +04:00
|
|
|
params = Hash.new("")
|
|
|
|
params["a"] = req.peeraddr[3]
|
|
|
|
params["b"] = res.sent_size
|
|
|
|
params["e"] = ENV
|
|
|
|
params["f"] = res.filename || ""
|
|
|
|
params["h"] = req.peeraddr[2]
|
|
|
|
params["i"] = req
|
|
|
|
params["l"] = "-"
|
|
|
|
params["m"] = req.request_method
|
2004-12-15 11:47:49 +03:00
|
|
|
params["n"] = req.attributes
|
2003-07-23 20:51:36 +04:00
|
|
|
params["o"] = res
|
2003-09-08 13:52:34 +04:00
|
|
|
params["p"] = req.port
|
2003-07-23 20:51:36 +04:00
|
|
|
params["q"] = req.query_string
|
|
|
|
params["r"] = req.request_line.sub(/\x0d?\x0a\z/o, '')
|
|
|
|
params["s"] = res.status # won't support "%>s"
|
|
|
|
params["t"] = req.request_time
|
|
|
|
params["T"] = Time.now - req.request_time
|
|
|
|
params["u"] = req.user || "-"
|
|
|
|
params["U"] = req.unparsed_uri
|
|
|
|
params["v"] = config[:ServerName]
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
2013-01-26 05:12:54 +04:00
|
|
|
##
|
|
|
|
# Formats +params+ according to +format_string+ which is described in
|
|
|
|
# setup_params.
|
|
|
|
|
2003-07-23 20:51:36 +04:00
|
|
|
def format(format_string, params)
|
2004-12-15 11:47:49 +03:00
|
|
|
format_string.gsub(/\%(?:\{(.*?)\})?>?([a-zA-Z%])/){
|
2003-07-23 20:51:36 +04:00
|
|
|
param, spec = $1, $2
|
|
|
|
case spec[0]
|
2004-12-15 11:47:49 +03:00
|
|
|
when ?e, ?i, ?n, ?o
|
2003-07-23 20:51:36 +04:00
|
|
|
raise AccessLogError,
|
|
|
|
"parameter is required for \"#{spec}\"" unless param
|
2010-01-11 07:30:37 +03:00
|
|
|
(param = params[spec][param]) ? escape(param) : "-"
|
2003-07-23 20:51:36 +04:00
|
|
|
when ?t
|
|
|
|
params[spec].strftime(param || CLF_TIME_FORMAT)
|
2010-12-09 13:16:50 +03:00
|
|
|
when ?p
|
|
|
|
case param
|
|
|
|
when 'remote'
|
|
|
|
escape(params["i"].peeraddr[1].to_s)
|
|
|
|
else
|
|
|
|
escape(params["p"].to_s)
|
|
|
|
end
|
2004-12-15 11:47:49 +03:00
|
|
|
when ?%
|
|
|
|
"%"
|
2003-07-23 20:51:36 +04:00
|
|
|
else
|
2010-01-10 12:33:47 +03:00
|
|
|
escape(params[spec].to_s)
|
2003-07-23 20:51:36 +04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2010-01-10 12:33:47 +03:00
|
|
|
|
2013-01-26 05:12:54 +04:00
|
|
|
##
|
|
|
|
# Escapes control characters in +data+
|
|
|
|
|
2010-01-10 12:33:47 +03:00
|
|
|
def escape(data)
|
|
|
|
if data.tainted?
|
|
|
|
data.gsub(/[[:cntrl:]\\]+/) {$&.dump[1...-1]}.untaint
|
|
|
|
else
|
|
|
|
data
|
|
|
|
end
|
|
|
|
end
|
2003-07-23 20:51:36 +04:00
|
|
|
end
|
|
|
|
end
|