Skip to content

Instantly share code, notes, and snippets.

@Imomoi
Created January 26, 2011 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Imomoi/796596 to your computer and use it in GitHub Desktop.
Save Imomoi/796596 to your computer and use it in GitHub Desktop.
def request_reg_exp
return @_reg_exp if @_reg_exp
request_type = 'GET'
request_protocol = '(http:\/\/)?'
server_name = '([\d\.\w-]+)?'
server_port = ':?(\d+)?'
request_address = '([\w\d\.\/]+)'
request_parameters = '\??(.*)?'
http_version = 'HTTP\/1\.1'
reg_exp = '"' +
request_type + '\s' + request_protocol + server_name + server_port +
request_address + request_parameters + '\s' + http_version + '"'
@_reg_exp = Regexp.new(reg_exp, 'i')
end
class Request
attr_reader :protocol, :hostname, :port, :address, :parameters
def initialize(matches)
@protocol = matches[1]
@hostname = matches[2]
@port = matches[3]
@address = matches[4]
@parameters = matches[5] || ''
end
def protocol?
return @_protocol_ if @_protocol_
@_protocol_ = self.protocol.blank?
end
def hostname?
return @_hostname_ if @_hostname_
@_hostname_ = self.hostname.blank?
end
def port?
return @_port_ if @_hport_
@_port_ = self.port.blank?
end
end
def analyze_line_with_regexp(log_line)
matches = request_reg_exp.match(log_line)
return [] if matches.nil? || matches.length == 1
request = Request.new(matches)
return [] if request.protocol? && !request.hostname?
return [] if !request.protocol? && request.hostname?
return [] if !request.port? && request.hostname?
[
request.address,
request.parameters
]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment