class LogLineParser::Query
Constants
- ALLOWABLE_METHODS
- IMAGE_FILE_RE
- SLASH
- TAIL_SLASH_RE
Public Class Methods
# File lib/log_line_parser/query.rb, line 61 def self.access_by_bots?(record, bots_re=Bots::DEFAULT_RE) bots_re =~ record.user_agent end
# File lib/log_line_parser/query.rb, line 65 def self.access_to_image?(record) IMAGE_FILE_RE =~ record.resource end
# File lib/log_line_parser/query.rb, line 81 def self.access_to_resources?(record, resources=[]) resources.include?(record.resource) end
# File lib/log_line_parser/query.rb, line 85 def self.access_to_under?(record, path) record.resource.start_with?(path) end
# File lib/log_line_parser/query.rb, line 175 def initialize(domain: nil, resources: [], bots_re: Bots::DEFAULT_RE) @domain = domain @resources = normalize_resources(resources) @bots_re = bots_re @normalized_resources = normalize_resources(resources) @normalized_dirs = @normalized_resources - @resources end
# File lib/log_line_parser/query.rb, line 89 def self.referred_from_host?(record, host_name) record.referer_host == host_name end
Returns true if the path+query part of the value of %{Referer}i matchs one of resources.
# File lib/log_line_parser/query.rb, line 73 def self.referred_from_resources?(record, resources=[]) resources.include?(record.referer_resource) end
# File lib/log_line_parser/query.rb, line 77 def self.referred_from_under?(record, path) record.referer_resource.start_with?(path) end
# File lib/log_line_parser/query.rb, line 94 def register_query_to_log(option, logs, bots_re=Bots::DEFAULT_RE) query = Query.new(domain: option[ConfigFields::HOST_NAME], resources: option[ConfigFields::RESOURCES], bots_re: bots_re) queries = option[ConfigFields::MATCH] reject_unacceptable_queries(queries) log = logs[option[ConfigFields::OUTPUT_LOG_NAME]] match_type = option[ConfigFields::MATCH_TYPE] ignore_match = option[ConfigFields::IGNORE_MATCH] reject_unacceptable_queries(ignore_match) if ignore_match compile_query(match_type, log, query, queries, ignore_match) end
Private Class Methods
# File lib/log_line_parser/query.rb, line 160 def compile_query(match_type, log, query, queries, ignore_match) if match_type == "all".freeze if ignore_match return log_if_all_match_but(log, query, queries, ignore_match) end log_if_all_match(log, query, queries) else if ignore_match return log_if_any_match_but(log, query, queries, ignore_match) end log_if_any_match(log, query, queries) end end
# File lib/log_line_parser/query.rb, line 117 def error_message_for_unacceptable_queries(unacceptable_queries) query_names = unacceptable_queries.join(", ") if unacceptable_queries.length == 1 "An unacceptable query is set: #{query_names}" else "Unacceptable queries are set: #{query_names}" end end
# File lib/log_line_parser/query.rb, line 126 def log_if_all_match(log, query, queries) proc do |line, record| if queries.all? {|method| query.send(method, record) } log.print line end end end
# File lib/log_line_parser/query.rb, line 142 def log_if_all_match_but(log, query, queries, ignore_match) proc do |line, record| if queries.all? {|method| query.send(method, record) } and not ignore_match.any? {|method| query.send(method, record) } log.print line end end end
# File lib/log_line_parser/query.rb, line 134 def log_if_any_match(log, query, queries) proc do |line, record| if queries.any? {|method| query.send(method, record) } log.print line end end end
# File lib/log_line_parser/query.rb, line 151 def log_if_any_match_but(log, query, queries, ignore_match) proc do |line, record| if queries.any? {|method| query.send(method, record) } and not ignore_match.any? {|method| query.send(method, record) } log.print line end end end
# File lib/log_line_parser/query.rb, line 109 def reject_unacceptable_queries(queries) unacceptable_queries = queries - ALLOWABLE_METHODS unless unacceptable_queries.empty? message = error_message_for_unacceptable_queries(unacceptable_queries) raise NotAllowableMethodError.new(message) end end
Public Instance Methods
# File lib/log_line_parser/query.rb, line 183 def access_by_bots?(record) @bots_re =~ record.user_agent end
# File lib/log_line_parser/query.rb, line 187 def access_to_image?(record) IMAGE_FILE_RE =~ record.resource end
Returns true if the value of %U%q in record
matches one of the resources that are passed as the second argument when you create an instance of Query
.
When you give a directory as one of resources, you should append a “/” at the end of the directory, otherwise records whose %U%q value points to the same directory but without trailing “/” will return false.
For example, when you create queries as follows,
query_with_slash = Query.new("www.example.org", ["/dir/subdir/"]) query_without_slash = Query.new("www.example.org", ["/dir/subdir"])
query_with_slash.access_to_resources?(record)
returns true for both of records whose %U%q value is “/dir/subdir/” and “/dir/subdir” respectively.
But query_without_slash.access_to_resources?(record)
returns false for a record whose %U%q value is “/dir/subdir/”
# File lib/log_line_parser/query.rb, line 264 def access_to_resources?(record) @normalized_resources.include?(record.resource) end
Returns true if the value of %U%q in record
begins with one of the resources that are passed as the second argument when you create an instance of Query
.
When a given resource is a directory, you should append a “/” at the end of it, otherwise you would get a wrong result. For example, suppose you define the following queries:
correct_query = Query.new("www.example.org", ["/dir/subdir/"]) wrong_query = Query.new("www.example.org", ["/dir/subdir"])
wrong_query.access_to_under_resources?(record)
returns true even when the value of %U%q in record is “/subdir_for_images/a_file_name”, while correct_query.access_to_under_resources?(record)
returns true when the value of %U%q is “/subdir/a_filename” or “/subdir”, and returns false for “/subdir_for_images”.
# File lib/log_line_parser/query.rb, line 287 def access_to_under_resources?(record) resource = record.resource @normalized_dirs.include?(resource) or @resources.any? {|target| resource.start_with?(target) } end
# File lib/log_line_parser/query.rb, line 342 def connect_method?(record) record.method == HttpMethods::CONNECT end
# File lib/log_line_parser/query.rb, line 334 def delete_method?(record) record.method == HttpMethods::DELETE end
# File lib/log_line_parser/query.rb, line 318 def get_method?(record) record.method == HttpMethods::GET end
# File lib/log_line_parser/query.rb, line 322 def head_method?(record) record.method == HttpMethods::HEAD end
# File lib/log_line_parser/query.rb, line 314 def options_method?(record) record.method == HttpMethods::OPTIONS end
# File lib/log_line_parser/query.rb, line 346 def patch_method?(record) record.method == HttpMethods::PATCH end
# File lib/log_line_parser/query.rb, line 326 def post_method?(record) record.method == HttpMethods::POST end
# File lib/log_line_parser/query.rb, line 330 def put_method?(record) record.method == HttpMethods::PUT end
Returns true if the path+query part of the value of %{Referer}i matches one of the resources that are passed as the second argument when you create an instance of Query
.
When a given resource is a directory, you should append a “/” at the end of it, otherwise you would get a wrong result. For example, suppose you define the following queries:
correct_query = Query.new("www.example.org", ["/dir/subdir/"]) wrong_query = Query.new("www.example.org", ["/dir/subdir"])
correct_query.referred_from_resources?(record)
returns true when the value of %{Referer}i is “www.example.org/subdir” or “www.example.org/subdir/”, but wrong_query.referred_from_resources?(record)
returns false for “www.example.org/subdir/”
# File lib/log_line_parser/query.rb, line 209 def referred_from_resources?(record) if_matching_domain(record) and @normalized_resources.include?(record.referer_resource) end
Returns true if the path+query part of the value of %{Referer}i begins with one of the resources that are passed as the second argument when you create an instance of Query
.
When a given resource is a directory, you should append a “/” at the end of it, otherwise you would get a wrong result. For example, suppose you define the following queries:
correct_query = Query.new("www.example.org", ["/dir/subdir/"]) wrong_query = Query.new("www.example.org", ["/dir/subdir"])
wrong_query.referred_from_under_resources?(record)
returns true even when the value of %{Referer}i in record is “www.example.org/subdir_for_images/a_file_name”, while correct_query.referred_from_under_resources?(record)
returns true when the value of %{Referer}i is “www.example.org/subdir/a_filename” or “www.example.org/subdir”, and returns false for “www.example.org/subdir_for_images”.
# File lib/log_line_parser/query.rb, line 235 def referred_from_under_resources?(record) referer_resource = record.referer_resource if_matching_domain(record) and @normalized_dirs.include?(referer_resource) or @resources.any?{|target| referer_resource.start_with?(target) } end
# File lib/log_line_parser/query.rb, line 293 def status_code_206?(record) record.last_request_status == 206 end
# File lib/log_line_parser/query.rb, line 297 def status_code_301?(record) record.last_request_status == 301 end
# File lib/log_line_parser/query.rb, line 301 def status_code_304?(record) record.last_request_status == 304 end
# File lib/log_line_parser/query.rb, line 305 def status_code_404?(record) record.last_request_status == 404 end
# File lib/log_line_parser/query.rb, line 338 def trace_method?(record) record.method == HttpMethods::TRACE end
Private Instance Methods
# File lib/log_line_parser/query.rb, line 352 def if_matching_domain(record) # When @domain is not set, it should be ignored. not @domain or @domain == record.referer_host end
# File lib/log_line_parser/query.rb, line 357 def normalize_resources(resources) [].tap do |normalized| resources.each do |resource| # record.referer_resource is expected to return '/' # even when the value of record.referer doesn't end # with a slash (e.g. 'http://www.example.org'). # So in the normalized result, you don't have to include # an empty string that corresponds to the root of a given # domain. if TAIL_SLASH_RE =~ resource and SLASH != resource normalized.push resource.sub(TAIL_SLASH_RE, "".freeze) end normalized.push resource end end end