akita-har_logger 0.2.8 → 0.2.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c48c0c45ad4fc65a7dcfa32c042ff24039025111dead2a610c5b956e1cf24e9
4
- data.tar.gz: 85becb20d70489a5d8e48d275e894b40325acf010714f1d9d8031a90ead03996
3
+ metadata.gz: 804312155d41d0e753f3b4e527fff9140fbd82ef27b9bda45a726e0ca52baea6
4
+ data.tar.gz: 8407df19c1bdbd64a432bf93b6ff0c22bf1c48a647832ede28d7fde6fb37fb0d
5
5
  SHA512:
6
- metadata.gz: 02b3c2b375f134945aa2c7e20552771bc4829c7644b3e5d04561177e4806c2f0245b8219808e4fd608bd4da379187d7d4a2bb14e3b58ca72e69b737c14c83e57
7
- data.tar.gz: 40bdf185b1dda6a8b83b124599931597f4363f61470ab5253645a4ff3ba028470f50254faffab63b7bf1b3334a96a86c01eb7ac005c39c1b589d98c3899ebeb5
6
+ metadata.gz: f4d42e38b8e5a1eb510a5d6ac6866a8455528306c063cfbfa9c97b7c38706096c38554d795fa78b723c64bf003b65ab6759645a63b7567a1f3b545df4915f283
7
+ data.tar.gz: ebe6c4aecfc66a61d8b68e807f07a51155f0b17e26054fac35aca1b5924f1bec38e9aebf74d6364fb78b4c1822ed4ce712cb41006779676cd210a60bd7ce8472
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- akita-har_logger (0.2.8)
4
+ akita-har_logger (0.2.12)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -37,18 +37,41 @@ module Akita
37
37
 
38
38
  def call(env)
39
39
  start_time = Time.now
40
+
41
+ # Read the request body here, in case there is non-Rack-compliant
42
+ # middleware in the stack that closes the request-body stream on us.
43
+ request_body = env['rack.input'].read
44
+ env['rack.input'].rewind # Be kind.
45
+
40
46
  status, headers, body = @app.call(env)
41
47
  end_time = Time.now
42
48
 
43
49
  wait_time_ms = ((end_time.to_f - start_time.to_f) * 1000).round
44
50
 
51
+ # Patch env with our saved request body.
52
+ saved_input = env['rack.input']
53
+ env['rack.input'] = StringIO.new request_body
54
+
55
+ # Buffer the response body in case it's not a rewindable stream.
56
+ body = HarLogger.bufferBody(body)
57
+
45
58
  @entry_queue << (HarEntry.new start_time, wait_time_ms, env, status,
46
59
  headers, body)
47
60
 
61
+ # Be kind and restore the original request-body stream.
62
+ env['rack.input'] = saved_input
63
+
48
64
  [ status, headers, body ]
49
65
  end
50
66
  end
51
67
 
68
+ # Reads the given body into an array and returns the result.
69
+ def self.bufferBody(body)
70
+ result = []
71
+ body.each { |part| result << part }
72
+ result
73
+ end
74
+
52
75
  # Logging filter for `ActionController`s.
53
76
  # TODO: Some amount of code duplication here. Should refactor.
54
77
  class Filter
@@ -72,6 +95,11 @@ module Akita
72
95
  def around(controller)
73
96
  start_time = Time.now
74
97
 
98
+ # Read the request body here, in case there is non-Rack-compliant
99
+ # middleware in the stack that closes the request-body stream on us.
100
+ request_body = controller.response.request.env['rack.input'].read
101
+ controller.response.request.env['rack.input'].rewind # Be kind.
102
+
75
103
  yield
76
104
 
77
105
  end_time = Time.now
@@ -80,9 +108,16 @@ module Akita
80
108
  response = controller.response
81
109
  request = response.request
82
110
 
111
+ # Patch env with our saved request body.
112
+ saved_input = request.env['rack.input']
113
+ request.env['rack.input'] = StringIO.new request_body
114
+
83
115
  @entry_queue << (HarEntry.new start_time, wait_time_ms, request.env,
84
116
  response.status, response.headers,
85
117
  [response.body])
118
+
119
+ # Be kind and restore the original request-body stream.
120
+ request.env['rack.input'] = saved_input
86
121
  end
87
122
  end
88
123
 
@@ -103,13 +138,23 @@ module Akita
103
138
  # queue.
104
139
  def self.get_queue(out_file_name)
105
140
  queue = nil
141
+ Rails.logger.debug "AKITA: About to acquire entry-queue mutex "\
142
+ "#{@@entry_queues_mutex} in #{Thread.current}. "\
143
+ "Self-owned? #{@@entry_queues_mutex.owned?}"
106
144
  @@entry_queues_mutex.synchronize {
107
- if @@entry_queues.has_key?(out_file_name) then
108
- return @@entry_queues[out_file_name]
145
+ begin
146
+ Rails.logger.debug "AKITA: Acquired entry-queue mutex "\
147
+ "#{@@entry_queues_mutex} in #{Thread.current}."
148
+ if @@entry_queues.has_key?(out_file_name) then
149
+ return @@entry_queues[out_file_name]
150
+ end
151
+
152
+ queue = Queue.new
153
+ @@entry_queues[out_file_name] = queue
154
+ ensure
155
+ Rails.logger.debug "AKITA: About to release entry-queue mutex "\
156
+ "#{@@entry_queues_mutex} in #{Thread.current}."
109
157
  end
110
-
111
- queue = Queue.new
112
- @@entry_queues[out_file_name] = queue
113
158
  }
114
159
 
115
160
  WriterThread.new out_file_name, queue
@@ -121,7 +121,8 @@ module Akita
121
121
  # Gracefully handle any characters that are invalid in the source
122
122
  # encoding and characters that have no UTF-8 representation by
123
123
  # replacing with '?'. Log a warning when this happens.
124
- source = req.body.string.force_encoding(getPostDataCharSet(env))
124
+ sourceCharset = getPostDataCharSet(env)
125
+ source = String.new(req.body.read).force_encoding(sourceCharset)
125
126
  utf8EncodingSuccessful = false
126
127
  if source.valid_encoding? then
127
128
  begin
@@ -159,7 +159,9 @@ module Akita
159
159
 
160
160
  def getBodySize(body)
161
161
  length = 0
162
- body.each { |part| length += part.bytesize }
162
+ # Convert each body part into a string in case we're dealing with
163
+ # non-Rack-compliant components.
164
+ body.each { |part| length += part.to_s.bytesize }
163
165
  length
164
166
  end
165
167
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Akita
4
4
  module HarLogger
5
- VERSION = "0.2.8"
5
+ VERSION = "0.2.12"
6
6
  end
7
7
  end
@@ -15,42 +15,57 @@ module Akita
15
15
  shutdown_mutex = Mutex.new
16
16
 
17
17
  Thread.new do
18
+ Rails.logger.debug "AKITA: About to acquire shutdown mutex "\
19
+ "#{shutdown_mutex} for file #{out_file_name} "\
20
+ "in writer thread #{Thread.current}. "\
21
+ "Self-owned? #{shutdown_mutex.owned?}"
18
22
  shutdown_mutex.synchronize {
19
- File.open(out_file_name, 'w') { |f|
20
- # Produce a preamble.
21
- f.write <<~EOF.chomp
22
- {
23
- "log": {
24
- "version": "1.2",
25
- "creator": {
26
- "name": "Akita HAR logger for Ruby",
27
- "version": "1.0.0"
28
- },
29
- "entries": [
30
- EOF
23
+ begin
24
+ Rails.logger.debug "AKITA: Acquired shutdown mutex "\
25
+ "#{shutdown_mutex} for file "\
26
+ "#{out_file_name} in writer thread "\
27
+ "#{Thread.current}."
28
+ File.open(out_file_name, 'w') { |f|
29
+ # Produce a preamble.
30
+ f.write <<~EOF.chomp
31
+ {
32
+ "log": {
33
+ "version": "1.2",
34
+ "creator": {
35
+ "name": "Akita HAR logger for Ruby",
36
+ "version": "1.0.0"
37
+ },
38
+ "entries": [
39
+ EOF
31
40
 
32
- first_entry = true
41
+ first_entry = true
33
42
 
34
- loop do
35
- entry = entry_queue.pop
36
- if entry == nil then break end
43
+ loop do
44
+ entry = entry_queue.pop
45
+ if entry == nil then break end
37
46
 
38
- # Emit comma separator if needed.
39
- f.puts (first_entry ? '' : ',')
40
- first_entry = false
47
+ # Emit comma separator if needed.
48
+ f.puts (first_entry ? '' : ',')
49
+ first_entry = false
41
50
 
42
- # Emit the dequeued entry.
43
- f.write JSON.generate(entry)
44
- end
51
+ # Emit the dequeued entry.
52
+ f.write JSON.generate(entry)
53
+ end
45
54
 
46
- # Produce the epilogue.
47
- f.write <<~EOF
55
+ # Produce the epilogue.
56
+ f.write <<~EOF
48
57
 
49
- ]
58
+ ]
59
+ }
50
60
  }
51
- }
52
- EOF
53
- }
61
+ EOF
62
+ }
63
+ ensure
64
+ Rails.logger.debug "AKITA: About to release shutdown mutex "\
65
+ "#{shutdown_mutex} for file "\
66
+ "#{out_file_name} in writer thread "\
67
+ "#{Thread.current}."
68
+ end
54
69
  }
55
70
  end
56
71
 
@@ -59,7 +74,18 @@ module Akita
59
74
  # Signal to the consumer that this is the end of the entry stream and
60
75
  # wait for the consumer to terminate.
61
76
  entry_queue << nil
62
- shutdown_mutex.synchronize {}
77
+ Rails.logger.debug "AKITA: About to acquire shutdown mutex "\
78
+ "#{shutdown_mutex} for file #{out_file_name} "\
79
+ "while shutting down in #{Thread.current}. "\
80
+ "Self-owned? #{shutdown_mutex.owned?}"
81
+ shutdown_mutex.synchronize {
82
+ Rails.logger.debug "AKITA: Acquired shutdown mutex "\
83
+ "#{shutdown_mutex} for file #{out_file_name} "\
84
+ "while shutting down in #{Thread.current}."
85
+ Rails.logger.debug "AKITA: About to release shutdown mutex "\
86
+ "#{shutdown_mutex} for file #{out_file_name} "\
87
+ "while shutting down in #{Thread.current}."
88
+ }
63
89
  end
64
90
  end
65
91
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: akita-har_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jed Liu
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-15 00:00:00.000000000 Z
11
+ date: 2021-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -54,7 +54,7 @@ licenses:
54
54
  metadata:
55
55
  homepage_uri: https://akitasoftware.com/
56
56
  source_code_uri: https://github.com/akitasoftware/akita-rails-har-logger
57
- post_install_message:
57
+ post_install_message:
58
58
  rdoc_options: []
59
59
  require_paths:
60
60
  - lib
@@ -69,8 +69,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
71
  requirements: []
72
- rubygems_version: 3.1.2
73
- signing_key:
72
+ rubygems_version: 3.2.21
73
+ signing_key:
74
74
  specification_version: 4
75
75
  summary: Rails middleware for HAR logging
76
76
  test_files: []