akita-har_logger 0.2.4 → 0.2.8

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: 51fcd2da35cb17ecb365988d7975f746a717f12b002d3a20f1d0c3eab245b10c
4
- data.tar.gz: 4972d841dfc7c8dcd41085137d45faedeb450836d46e6d3f14d584785d997a24
3
+ metadata.gz: 4c48c0c45ad4fc65a7dcfa32c042ff24039025111dead2a610c5b956e1cf24e9
4
+ data.tar.gz: 85becb20d70489a5d8e48d275e894b40325acf010714f1d9d8031a90ead03996
5
5
  SHA512:
6
- metadata.gz: 8fdffa25d122a573e8c72526934e5e713fbdc573c0d4ebfa13383d749ea856f9b01d2e5bc43e89b1c152df64f97cd965ad54bd5a4ebab924d222a1829bbbec60
7
- data.tar.gz: 8ca06a98ce89ec4e9c3e6ac7f3b10ec98f73c44aacab1af0bac8c81649f342ec721017ee57ff6590c93a24234cce9d40c9b341aa8277a39a59bc86a3a9c3484e
6
+ metadata.gz: 02b3c2b375f134945aa2c7e20552771bc4829c7644b3e5d04561177e4806c2f0245b8219808e4fd608bd4da379187d7d4a2bb14e3b58ca72e69b737c14c83e57
7
+ data.tar.gz: 40bdf185b1dda6a8b83b124599931597f4363f61470ab5253645a4ff3ba028470f50254faffab63b7bf1b3334a96a86c01eb7ac005c39c1b589d98c3899ebeb5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- akita-har_logger (0.2.4)
4
+ akita-har_logger (0.2.8)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -5,12 +5,31 @@ module Akita
5
5
  class HarUtils
6
6
  # Rack apparently uses 8-bit ASCII for everything, even when the string
7
7
  # is not 8-bit ASCII. This reinterprets 8-bit ASCII strings as UTF-8.
8
+ #
9
+ # If we are unable to do this reinterpretation, return the string
10
+ # unchanged, but log a warning that points to the caller.
8
11
  def self.fixEncoding(v)
9
- if v == nil || v.encoding != Encoding::ASCII_8BIT then
10
- v
11
- else
12
- String.new(v).force_encoding(Encoding::UTF_8)
12
+ if v == nil then
13
+ return v
13
14
  end
15
+
16
+ if !(v.is_a? String) then
17
+ Rails.logger.warn "[#{caller_locations(1, 1)}] fixEncoding was not given a string. This might cause JSON serialization to fail."
18
+ return v
19
+ end
20
+
21
+ # Only re-interpret 8-bit ASCII.
22
+ if v.encoding != Encoding::ASCII_8BIT then
23
+ return v
24
+ end
25
+
26
+ forced = String.new(v).force_encoding(Encoding::UTF_8)
27
+ if forced.valid_encoding? then
28
+ return forced
29
+ end
30
+
31
+ Rails.logger.warn "[#{caller_locations(1, 1)}] Unable to fix encoding: not a valid UTF-8 string. This will likely cause JSON serialization to fail."
32
+ v
14
33
  end
15
34
 
16
35
  # Converts a Hash into a list of Hash objects. Each entry in the given
@@ -20,7 +39,7 @@ module Akita
20
39
  hash.reduce([]) { |accum, (k, v)|
21
40
  accum.append({
22
41
  name: fixEncoding(k),
23
- value: fixEncoding(v),
42
+ value: fixEncoding(v.to_s),
24
43
  })
25
44
  }
26
45
  end
@@ -85,7 +85,7 @@ module Akita
85
85
  return Encoding::ISO_8859_1
86
86
  end
87
87
 
88
- Encoding.default_external
88
+ Encoding::UTF_8
89
89
  end
90
90
 
91
91
  # Obtains the posted data from an HTTP environment.
@@ -117,8 +117,27 @@ module Akita
117
117
  # body when the request specifies UTF-8. Reinterpret the content
118
118
  # body according to what the request says it is, and re-encode into
119
119
  # UTF-8.
120
- result[:text] = req.body.string.encode(Encoding::UTF_8,
121
- getPostDataCharSet(env))
120
+ #
121
+ # Gracefully handle any characters that are invalid in the source
122
+ # encoding and characters that have no UTF-8 representation by
123
+ # replacing with '?'. Log a warning when this happens.
124
+ source = req.body.string.force_encoding(getPostDataCharSet(env))
125
+ utf8EncodingSuccessful = false
126
+ if source.valid_encoding? then
127
+ begin
128
+ result[:text] = source.encode(Encoding::UTF_8)
129
+ utf8EncodingSuccessful = true
130
+ rescue Encoding::UndefinedConversionError
131
+ Rails.logger.warn "[#{caller_locations(0, 1)}] Unable to losslessly convert request body from #{source.encoding} to UTF-8. Characters undefined in UTF-8 will be replaced with '?'."
132
+ end
133
+ else
134
+ Rails.logger.warn "[#{caller_locations(0, 1)}] Request body is not valid #{source.encoding}. Invalid characters and characters undefined in UTF-8 will be replaced with '?'."
135
+ end
136
+
137
+ if !utf8EncodingSuccessful then
138
+ result[:text] = source.encode(Encoding::UTF_8,
139
+ invalid: :replace, undef: :replace, replace: '?')
140
+ end
122
141
  end
123
142
 
124
143
  result
@@ -74,14 +74,51 @@ module Akita
74
74
  end
75
75
 
76
76
  def getContent(headers, body)
77
- # XXX Handle compression & encoding.
77
+ # XXX Handle compression
78
+ # XXX Figure out how to properly join together multi-part bodies.
78
79
 
80
+ # Try to convert the body into UTF-8. If this fails, assume the body is
81
+ # binary data.
82
+ # XXX TODO Take charset part of Content-Type header into account.
79
83
  text = +""
84
+ haveBinaryData = false
80
85
  body.each { |part|
81
- # XXX Figure out how to join together multi-part bodies.
82
- text << (HarUtils.fixEncoding part);
86
+ partStr = part.to_s
87
+
88
+ if partStr.encoding == Encoding::ASCII_8BIT then
89
+ # Have 8-bit ASCII data. Try to interpret as UTF-8. If this fails,
90
+ # treat as binary data.
91
+ forced = String.new(partStr).force_encoding(Encoding::UTF_8)
92
+ if forced.valid_encoding? then
93
+ text << forced
94
+ next
95
+ end
96
+
97
+ haveBinaryData = true
98
+ break
99
+ end
100
+
101
+ if !partStr.valid_encoding? then
102
+ # Source encoding is not valid. Treat as binary data.
103
+ haveBinaryData = true
104
+ break
105
+ end
106
+
107
+ # Try to re-encode as UTF-8. If this fails, treat as binary data.
108
+ begin
109
+ text << partStr.encode(Encoding::UTF_8)
110
+ rescue Encoding::UndefinedConversionError
111
+ haveBinaryData = true
112
+ break
113
+ end
83
114
  }
84
115
 
116
+ if haveBinaryData then
117
+ # TODO Encode binary body data with base64.
118
+ # XXX Omit for now.
119
+ text = ""
120
+ end
121
+
85
122
  {
86
123
  size: getBodySize(body),
87
124
 
@@ -116,7 +153,7 @@ module Akita
116
153
  # for the CRLF on the blank line.
117
154
  headers.reduce(status_length + 2) { |accum, (k, v)|
118
155
  # Header-Name: header value<CR><LF>
119
- accum + k.length + 2 + v.length + 2
156
+ accum + k.length + 2 + v.to_s.length + 2
120
157
  }
121
158
  end
122
159
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Akita
4
4
  module HarLogger
5
- VERSION = "0.2.4"
5
+ VERSION = "0.2.8"
6
6
  end
7
7
  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.4
4
+ version: 0.2.8
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-11 00:00:00.000000000 Z
11
+ date: 2021-07-15 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.2.21
73
- signing_key:
72
+ rubygems_version: 3.1.2
73
+ signing_key:
74
74
  specification_version: 4
75
75
  summary: Rails middleware for HAR logging
76
76
  test_files: []