Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit 36a8238

Browse files
authored
Address issues with response bodies (#14)
Buffer response bodies in case they are coming from a non-rewindable stream. The [Rack specification](https://github.com/rack/rack/blob/d15dd728440710cfc35ed155d66a98dc2c07ae42/SPEC.rdoc#the-body-) says we can't count on being able to call `each` on the body more than once. When computing the response body size, convert each part into a string, in case we're dealing with non-Rack-compliant components. (The Rack specification says `each` must only yield String values.) Bumped to v0.2.11.
1 parent 6b18350 commit 36a8238

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
akita-har_logger (0.2.10)
4+
akita-har_logger (0.2.11)
55

66
GEM
77
remote: https://rubygems.org/

lib/akita/har_logger.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def call(env)
5252
saved_input = env['rack.input']
5353
env['rack.input'] = StringIO.new request_body
5454

55+
# Buffer the response body in case it's not a rewindable stream.
56+
body = HarLogger.bufferBody(body)
57+
5558
@entry_queue << (HarEntry.new start_time, wait_time_ms, env, status,
5659
headers, body)
5760

@@ -62,6 +65,13 @@ def call(env)
6265
end
6366
end
6467

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+
6575
# Logging filter for `ActionController`s.
6676
# TODO: Some amount of code duplication here. Should refactor.
6777
class Filter

lib/akita/har_logger/http_response.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ def getHeadersSize(env, status, headers)
159159

160160
def getBodySize(body)
161161
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 }
163165
length
164166
end
165167
end

lib/akita/har_logger/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Akita
44
module HarLogger
5-
VERSION = "0.2.10"
5+
VERSION = "0.2.11"
66
end
77
end

0 commit comments

Comments
 (0)