fhwang-http_log_reader 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.
Files changed (2) hide show
  1. data/lib/http_log_reader.rb +69 -0
  2. metadata +53 -0
@@ -0,0 +1,69 @@
1
+ module HttpLogReader
2
+ def self.foreach( file_path, opts = {} )
3
+ requests = read file_path, opts
4
+ requests.each do |r| yield( r ); end
5
+ end
6
+
7
+ def self.read( file_path, opts = {} )
8
+ lines = File.readlines file_path
9
+ requests = lines.
10
+ map { |line| line.chomp! }.
11
+ select { |line| line.size > 0 }.
12
+ map { |line| Request.new( line ) }
13
+ if opts[:start_time]
14
+ requests = requests.select { |req|
15
+ req.time_finished >= opts[:start_time]
16
+ }
17
+ end
18
+ requests
19
+ end
20
+
21
+ class Request
22
+ require 'parsedate'
23
+
24
+ attr_reader :ip_address, :remote_user_name, :http_auth_userid,
25
+ :time_finished, :request_line, :status_code, :bytes_returned,
26
+ :referer, :user_agent
27
+
28
+ def initialize( line )
29
+ line =~ %r|^(\S+) (\S+) (\S+) \[([^\]]+)\] "([^"]+)" (\S+) (\S+) "(\S+)" "([^"]+)"|
30
+ @ip_address = $1
31
+ @remote_user_name = $2 unless $2 == '-'
32
+ @http_auth_userid = $3 unless $3 == '-'
33
+ time_finished_raw = $4
34
+ request_line_raw = $5
35
+ @status_code = $6.to_i
36
+ @bytes_returned = $7.to_i unless $7 == '-'
37
+ @referer = $8 unless $8 == '-'
38
+ @user_agent = $9
39
+ @time_finished = parse_time_finished time_finished_raw
40
+ @request_line = RequestLine.new request_line_raw
41
+ end
42
+
43
+ def parse_time_finished( time_finished_raw )
44
+ time_finished_raw =~ %r{^(\d{2})/(\w{3})/(\d{4}):(\d{2}):(\d{2}):(\d{2}) (-|\+)(\d{2})(\d{2})}
45
+ offset_direction = $7
46
+ offset_hours = $8.to_i
47
+ offset_minutes = ( offset_hours * 60 ) + $9.to_i
48
+ offset_seconds = offset_minutes * 60
49
+ time_finished = Time.utc(
50
+ $3.to_i, $2, $1.to_i, $4.to_i, $5.to_i, $6.to_i
51
+ )
52
+ if offset_direction == '+'
53
+ time_finished = time_finished - offset_seconds
54
+ else
55
+ time_finished = time_finished + offset_seconds
56
+ end
57
+ time_finished
58
+ end
59
+
60
+ class RequestLine
61
+ attr_reader :to_s, :method, :resource, :protocol
62
+
63
+ def initialize( string )
64
+ @to_s = string
65
+ @method, @resource, @protocol = string.split /\s+/
66
+ end
67
+ end
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fhwang-http_log_reader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Francis Hwang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-05 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: sera@fhwang.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - ./lib/http_log_reader.rb
26
+ has_rdoc: false
27
+ homepage: http://github.com/fhwang/http_log_reader/tree/master
28
+ post_install_message:
29
+ rdoc_options: []
30
+
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: "0"
38
+ version:
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ requirements: []
46
+
47
+ rubyforge_project:
48
+ rubygems_version: 1.2.0
49
+ signing_key:
50
+ specification_version: 2
51
+ summary: ""
52
+ test_files: []
53
+