apachelogregex 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,110 @@
1
+ #
2
+ # = Apache Log Regex
3
+ #
4
+ # Ruby parser for Apache log files based on regular expressions.
5
+ #
6
+ # Category::
7
+ # Package:: ApacheLogRegex
8
+ # Author:: Simone Carletti <weppos@weppos.net>
9
+ # License::
10
+ #
11
+ #--
12
+ # SVN: $Id$
13
+ #++
14
+
15
+
16
+ require 'test_helper'
17
+
18
+
19
+ class ApacheLogRegexTest < Test::Unit::TestCase
20
+
21
+ def setup
22
+ @format = '%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"'
23
+ @parser = ApacheLogRegex.new(@format)
24
+ end
25
+
26
+ def test_regexp
27
+ regexp = '(?-mix:^(\\S*) (\\S*) (\\S*) (\\[[^\\]]+\\]) (?-mix:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)") (\\S*) (\\S*) (?-mix:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)") (?-mix:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)")$)'
28
+ assert_equal(regexp, @parser.regexp.to_s)
29
+ end
30
+
31
+ def test_parse_line
32
+ expected = { '%h' => '212.74.15.68',
33
+ '%l' => '-',
34
+ '%u' => '-',
35
+ '%t' => '[23/Jan/2004:11:36:20 +0000]',
36
+ '%r' => 'GET /images/previous.png HTTP/1.1',
37
+ '%>s' => '200',
38
+ '%b' => '2607',
39
+ '%{Referer}i' => 'http://peterhi.dyndns.org/bandwidth/index.html',
40
+ '%{User-Agent}i' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2) Gecko/20021202' }
41
+ results = @parser.parse(read_testcase('line.log'))
42
+ assert_kind_of(Hash, results)
43
+ assert_match_expected_hash(expected, results)
44
+ end
45
+
46
+ def test_parse_line_with_slash_quote_in_request
47
+ expected = { '%h' => '212.74.15.68',
48
+ '%l' => '-',
49
+ '%u' => '-',
50
+ '%t' => '[23/Jan/2004:11:36:20 +0000]',
51
+ '%r' => 'GET /images/previous.png=\" HTTP/1.1',
52
+ '%>s' => '200',
53
+ '%b' => '2607',
54
+ '%{Referer}i' => 'http://peterhi.dyndns.org/bandwidth/index.html',
55
+ '%{User-Agent}i' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2) Gecko/20021202' }
56
+ results = @parser.parse(read_testcase('line-with-slash-quote-in-request.log'))
57
+ assert_kind_of(Hash, results)
58
+ assert_match_expected_hash(expected, results)
59
+ end
60
+
61
+ def test_parse_line_with_slash_quote_in_referer
62
+ expected = { '%h' => '4.224.234.46',
63
+ '%l' => '-',
64
+ '%u' => '-',
65
+ '%t' => '[20/Jul/2004:13:18:55 -0700]',
66
+ '%r' => 'GET /core/listing/pl_boat_detail.jsp?&units=Feet&checked_boats=1176818&slim=broker&&hosturl=giffordmarine&&ywo=giffordmarine& HTTP/1.1',
67
+ '%>s' => '200',
68
+ '%b' => '2888',
69
+ '%{Referer}i' => 'http://search.yahoo.com/bin/search?p=\"grady%20white%20306%20bimini\"',
70
+ '%{User-Agent}i' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; YPC 3.0.3; yplus 4.0.00d)' }
71
+ results = @parser.parse(read_testcase('line-with-slash-quote-in-referer.log'))
72
+ assert_kind_of(Hash, results)
73
+ assert_match_expected_hash(expected, results)
74
+ end
75
+
76
+ def test_parse_line_should_return_nil_on_invalid_format
77
+ results = @parser.parse('foobar')
78
+ assert_nil(results)
79
+ end
80
+
81
+ def test_parse_log
82
+ results = File.open("#{TESTCASES_PATH}/log.log").readlines.map { |line| @parser.parse(line) }.compact # skip last line
83
+ assert_equal(5, results.length)
84
+ assert_equal(%w(87.18.183.252 79.28.16.191 79.28.16.191 69.150.40.169 217.220.110.75), results.map { |r| r['%h'] })
85
+ end
86
+
87
+ def test_stricparse_line
88
+ testcase = read_testcase('line.log')
89
+ assert_equal(@parser.parse(testcase), @parser.parse!(testcase))
90
+ end
91
+
92
+ def test_stricparse_line_should_raise_on_invalid_format
93
+ error = assert_raise(ApacheLogRegex::ParseError) { results = @parser.parse!('foobar') }
94
+ assert_match(/Invalid format/, error.message)
95
+ end
96
+
97
+
98
+ protected
99
+
100
+ def read_testcase(filename)
101
+ File.read("#{TESTCASES_PATH}/#{filename}")
102
+ end
103
+
104
+ def assert_match_expected_hash(expected, current)
105
+ expected.each do |key, value|
106
+ assert_equal(value, current[key], "Expected `#{key}` to match value `#{value}` but `#{current[:key]}`")
107
+ end
108
+ end
109
+
110
+ end
@@ -0,0 +1,18 @@
1
+ #
2
+ # = Apache Log Regex
3
+ #
4
+ # Ruby parser for Apache log files based on regular expressions.
5
+ #
6
+ # Category::
7
+ # Package:: ApacheLogRegex
8
+ # Author:: Simone Carletti <weppos@weppos.net>
9
+ # License::
10
+ #
11
+ #--
12
+ # SVN: $Id$
13
+ #++
14
+
15
+
16
+ require 'test_helper'
17
+
18
+ Dir.glob(File.dirname(__FILE__) + '/**/*_test.rb').sort.each { |unit| require unit }
@@ -0,0 +1,24 @@
1
+ #
2
+ # = Apache Log Regex
3
+ #
4
+ # Ruby parser for Apache log files based on regular expressions.
5
+ #
6
+ # Category::
7
+ # Package:: ApacheLogRegex
8
+ # Author:: Simone Carletti <weppos@weppos.net>
9
+ # License::
10
+ #
11
+ #--
12
+ # SVN: $Id$
13
+ #++
14
+
15
+
16
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
17
+
18
+ require 'rubygems'
19
+ require 'test/unit'
20
+ require 'apache_log_regex'
21
+
22
+ # testcase file path
23
+ TESTCASES_PATH = File.dirname(__FILE__) + '/testcases' unless defined?(TESTCASES_PATH)
24
+ FIXTURES_PATH = File.dirname(__FILE__) + '/fixtures' unless defined?(FIXTURES_PATH)
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apachelogregex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Simone Carletti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-13 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0.8"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: echoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "3.1"
34
+ version:
35
+ description: Apache Log Regex is a Ruby port of Peter Hickman's Apache::LogRegex 1.4 Perl module. It provides functionalities to parse a line from an Apache log file into a hash.
36
+ email: weppos@weppos.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - CHANGELOG.rdoc
43
+ - lib/apache_log_regex/version.rb
44
+ - lib/apache_log_regex.rb
45
+ - lib/apachelogregex.rb
46
+ - README.rdoc
47
+ files:
48
+ - CHANGELOG.rdoc
49
+ - lib/apache_log_regex/version.rb
50
+ - lib/apache_log_regex.rb
51
+ - lib/apachelogregex.rb
52
+ - LICENSE.rdoc
53
+ - Manifest
54
+ - Rakefile
55
+ - README.rdoc
56
+ - setup.rb
57
+ - test/apache_log_regex_test.rb
58
+ - test/test_all.rb
59
+ - test/test_helper.rb
60
+ - apachelogregex.gemspec
61
+ has_rdoc: true
62
+ homepage: http://code.simonecarletti.com/apachelogregex
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --line-numbers
66
+ - --inline-source
67
+ - --title
68
+ - Apachelogregex
69
+ - --main
70
+ - README.rdoc
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "1.2"
84
+ version:
85
+ requirements: []
86
+
87
+ rubyforge_project: apachelogregex
88
+ rubygems_version: 1.3.1
89
+ signing_key:
90
+ specification_version: 2
91
+ summary: Ruby parser for Apache log files based on regular expressions.
92
+ test_files:
93
+ - test/test_all.rb