Created
January 26, 2011 11:51
-
-
Save Imomoi/796596 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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