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

Commit fbd6753

Browse files
authored
Follow-up to #5: also fix encodings in responses (#7)
Bump to v0.2.4. Also be less aggressive when reinterpreting strings as UTF-8. Only reinterpret 8-bit ASCII strings. This is the only encoding observed from Rack-provided strings that needs reinterpretation.
1 parent 8f9777e commit fbd6753

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
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.3)
4+
akita-har_logger (0.2.4)
55

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

lib/akita/har_logger/har_utils.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ module Akita
44
module HarLogger
55
class HarUtils
66
# Rack apparently uses 8-bit ASCII for everything, even when the string
7-
# is not 8-bit ASCII. This reinterprets the given string as UTF-8.
7+
# is not 8-bit ASCII. This reinterprets 8-bit ASCII strings as UTF-8.
88
def self.fixEncoding(v)
9-
if v == nil || v.encoding == Encoding::UTF_8 then
9+
if v == nil || v.encoding != Encoding::ASCII_8BIT then
1010
v
1111
else
1212
String.new(v).force_encoding(Encoding::UTF_8)

lib/akita/har_logger/http_response.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def to_json(*args)
2525

2626
# Obtains the status text corresponding to a status code.
2727
def getStatusText(status)
28-
Rack::Utils::HTTP_STATUS_CODES[status]
28+
HarUtils.fixEncoding(Rack::Utils::HTTP_STATUS_CODES[status])
2929
end
3030

3131
# Obtains the HTTP version in the response.
@@ -65,8 +65,8 @@ def getCookies(headers)
6565
if match then cookie_value = match[1] end
6666

6767
result << {
68-
name: cookie_name,
69-
value: cookie_value,
68+
name: HarUtils.fixEncoding(cookie_name),
69+
value: HarUtils.fixEncoding(cookie_value),
7070
}
7171
}
7272

@@ -79,14 +79,14 @@ def getContent(headers, body)
7979
text = +""
8080
body.each { |part|
8181
# XXX Figure out how to join together multi-part bodies.
82-
text << part;
82+
text << (HarUtils.fixEncoding part);
8383
}
8484

8585
{
8686
size: getBodySize(body),
8787

8888
# XXX What to use when no Content-Type is given?
89-
mimeType: headers['Content-Type'],
89+
mimeType: HarUtils.fixEncoding(headers['Content-Type']),
9090

9191
text: text,
9292
}
@@ -95,7 +95,9 @@ def getContent(headers, body)
9595
def getRedirectUrl(headers)
9696
# Use the "Location" header if it exists. Otherwise, based on some HAR
9797
# examples found online, it looks like an empty string is used.
98-
headers.key?('Location') ? headers['Location'] : ''
98+
headers.key?('Location') ?
99+
HarUtils.fixEncoding(headers['Location']) :
100+
''
99101
end
100102

101103
def getHeadersSize(env, status, headers)

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.3"
5+
VERSION = "0.2.4"
66
end
77
end

0 commit comments

Comments
 (0)