akita-har_logger 0.2.1 → 0.2.6

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: 8337c88eacb3b48aa2190bb12b2bc406d1140b6e7d7c9c5ed921b5e92fe557c3
4
- data.tar.gz: d6621fcf152e604396634a0614cb9fd66205e95be39be8e46142da90283af8a6
3
+ metadata.gz: cc8020afb47521c9d7d9ca923dca64e2a8f8364ff7207ab80b68cdc09cd0ca39
4
+ data.tar.gz: aa865b2768013560e4439d9e500df4680276af28f88294113aa1a594545938eb
5
5
  SHA512:
6
- metadata.gz: 9efe412f404f5e56abac9af2e79b7d186ab895ceccf40cf369841563f86f5b78eb9ee7fe0cb561b20b7de93a9284343ffa50a355894fe62f6cf3e5d4193ea7d0
7
- data.tar.gz: b251114f5be749a02d8d198e2dbce71ff98845085cdb48324c66e1791d3d6acd7252205cce6f5358f4e3a25fbabfecbba392094e21c56a88e8b0d89528beb408
6
+ metadata.gz: 2f6b7d249bcde1e1d4dea4c332bd1527c1176b7a138ae7dc36edf7844d043e07cea25f845783634fac9385216f746b6209b69e0d4a04eb5b17b2693d5aa9156e
7
+ data.tar.gz: b58e4e46383ab4caa08b0338cf85b2921c049c7d99df79146756fc2520e267600e1139654059c9ce43faf2807097316a9e874e386fc5c00ee7c2e31e65fb0c0b
data/Gemfile.lock CHANGED
@@ -1,14 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- akita-har_logger (0.2.1)
5
- json (~> 2.3)
4
+ akita-har_logger (0.2.6)
6
5
 
7
6
  GEM
8
7
  remote: https://rubygems.org/
9
8
  specs:
10
9
  diff-lcs (1.4.4)
11
- json (2.5.1)
12
10
  rake (13.0.3)
13
11
  rspec (3.10.0)
14
12
  rspec-core (~> 3.10.0)
@@ -33,4 +31,4 @@ DEPENDENCIES
33
31
  rspec (~> 3.10)
34
32
 
35
33
  BUNDLED WITH
36
- 2.2.16
34
+ 2.2.23
@@ -31,7 +31,5 @@ Gem::Specification.new do |spec|
31
31
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
32
  spec.require_paths = ['lib']
33
33
 
34
- spec.add_dependency 'json', '~> 2.3'
35
-
36
34
  spec.add_development_dependency 'rspec', '~> 3.10'
37
35
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
3
  require_relative 'http_request'
5
4
  require_relative 'http_response'
6
5
 
@@ -3,17 +3,57 @@
3
3
  module Akita
4
4
  module HarLogger
5
5
  class HarUtils
6
+ # Rack apparently uses 8-bit ASCII for everything, even when the string
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.
11
+ def self.fixEncoding(v)
12
+ if v == nil then
13
+ return v
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
33
+ end
34
+
6
35
  # Converts a Hash into a list of Hash objects. Each entry in the given
7
36
  # Hash will be represented in the output by a Hash object that maps
8
37
  # 'name' to the entry's key and 'value' to the entry's value.
9
38
  def self.hashToList(hash)
10
39
  hash.reduce([]) { |accum, (k, v)|
11
40
  accum.append({
12
- name: k,
13
- value: v,
41
+ name: fixEncoding(k),
42
+ value: fixEncoding(v),
14
43
  })
15
44
  }
16
45
  end
46
+
47
+ # Determines whether all values in a Hash are strings.
48
+ def self.allValuesAreStrings(hash)
49
+ hash.each do |_, value|
50
+ if !(value.is_a? String) then
51
+ return false
52
+ end
53
+ end
54
+
55
+ return true
56
+ end
17
57
  end
18
58
  end
19
59
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
3
  require_relative 'har_utils'
5
4
 
6
5
  module Akita
@@ -12,7 +11,7 @@ module Akita
12
11
 
13
12
  @self = {
14
13
  method: getMethod(env),
15
- url: req.url,
14
+ url: HarUtils.fixEncoding(req.url),
16
15
  httpVersion: getHttpVersion(env),
17
16
  cookies: getCookies(env),
18
17
  headers: getHeaders(env),
@@ -34,7 +33,7 @@ module Akita
34
33
 
35
34
  # Obtains the client's request method from an HTTP environment.
36
35
  def getMethod(env)
37
- (Rack::Request.new env).request_method
36
+ HarUtils.fixEncoding (Rack::Request.new env).request_method
38
37
  end
39
38
 
40
39
  # Obtains the client-requested HTTP version from an HTTP environment.
@@ -42,7 +41,9 @@ module Akita
42
41
  # The environment doesn't have HTTP_VERSION when running with `rspec`;
43
42
  # assume HTTP/1.1 when this happens. We don't return nil, so we can
44
43
  # calculate the size of the headers.
45
- env.key?('HTTP_VERSION') ? env['HTTP_VERSION'] : 'HTTP/1.1'
44
+ env.key?('HTTP_VERSION') ?
45
+ HarUtils.fixEncoding(env['HTTP_VERSION']) :
46
+ 'HTTP/1.1'
46
47
  end
47
48
 
48
49
  # Builds a list of cookie objects from an HTTP environment.
@@ -72,23 +73,71 @@ module Akita
72
73
  HarUtils.hashToList paramMap
73
74
  end
74
75
 
76
+ # Obtains the character set of the posted data from an HTTP environment.
77
+ def getPostDataCharSet(env)
78
+ req = Rack::Request.new env
79
+ if req.content_charset != nil then
80
+ return req.content_charset
81
+ end
82
+
83
+ # RFC 2616 says that "text/*" defaults to ISO-8859-1.
84
+ if env['CONTENT_TYPE'].start_with?('text/') then
85
+ return Encoding::ISO_8859_1
86
+ end
87
+
88
+ Encoding::UTF_8
89
+ end
90
+
75
91
  # Obtains the posted data from an HTTP environment.
76
92
  def getPostData(env)
77
93
  if env.key?('CONTENT_TYPE') && env['CONTENT_TYPE'] then
78
94
  result = { mimeType: env['CONTENT_TYPE'] }
79
95
 
80
96
  # Populate 'params' if we have URL-encoded parameters. Otherwise,
81
- # populate 'text.
97
+ # populate 'text'.
82
98
  req = Rack::Request.new env
83
99
  if env['CONTENT_TYPE'] == 'application/x-www-form-urlencoded' then
84
- # Decoded parameters can be found as a map in req.params. Convert
85
- # this map into an array.
100
+ # Decoded parameters can be found as a map in req.params.
86
101
  #
87
- # XXX Spec has space for files, but are file uploads ever
88
- # URL-encoded?
89
- result[:params] = HarUtils.hashToList req.params
102
+ # Requests originating from specs can be malformed: the values in
103
+ # req.params are not necessarily strings. Encode all of req.params
104
+ # in JSON and pretend the content type was "application/json".
105
+ if HarUtils.allValuesAreStrings req.params then
106
+ # Convert req.params into an array.
107
+ #
108
+ # XXX Spec has space for files, but are file uploads ever
109
+ # URL-encoded?
110
+ result[:params] = HarUtils.hashToList req.params
111
+ else
112
+ result[:mimeType] = 'application/json'
113
+ result[:text] = req.params.to_json
114
+ end
90
115
  else
91
- result[:text] = req.body.string
116
+ # Rack has been observed to use ASCII-8BIT encoding for the request
117
+ # body when the request specifies UTF-8. Reinterpret the content
118
+ # body according to what the request says it is, and re-encode into
119
+ # UTF-8.
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
92
141
  end
93
142
 
94
143
  result
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
3
  require_relative 'har_utils'
5
4
 
6
5
  module Akita
@@ -26,7 +25,7 @@ module Akita
26
25
 
27
26
  # Obtains the status text corresponding to a status code.
28
27
  def getStatusText(status)
29
- Rack::Utils::HTTP_STATUS_CODES[status]
28
+ HarUtils.fixEncoding(Rack::Utils::HTTP_STATUS_CODES[status])
30
29
  end
31
30
 
32
31
  # Obtains the HTTP version in the response.
@@ -37,7 +36,9 @@ module Akita
37
36
  # The environment doesn't have HTTP_VERSION when running with `rspec`;
38
37
  # assume HTTP/1.1 when this happens. We don't return nil, so we can
39
38
  # calculate the size of the headers.
40
- env.key?('HTTP_VERSION') ? env['HTTP_VERSION'] : 'HTTP/1.1'
39
+ env.key?('HTTP_VERSION') ?
40
+ HarUtils.fixEncoding(env['HTTP_VERSION']) :
41
+ 'HTTP/1.1'
41
42
  end
42
43
 
43
44
  def getCookies(headers)
@@ -64,8 +65,8 @@ module Akita
64
65
  if match then cookie_value = match[1] end
65
66
 
66
67
  result << {
67
- name: cookie_name,
68
- value: cookie_value,
68
+ name: HarUtils.fixEncoding(cookie_name),
69
+ value: HarUtils.fixEncoding(cookie_value),
69
70
  }
70
71
  }
71
72
 
@@ -73,19 +74,56 @@ module Akita
73
74
  end
74
75
 
75
76
  def getContent(headers, body)
76
- # XXX Handle compression & encoding.
77
+ # XXX Handle compression
78
+ # XXX Figure out how to properly join together multi-part bodies.
77
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.
78
83
  text = +""
84
+ haveBinaryData = false
79
85
  body.each { |part|
80
- # XXX Figure out how to join together multi-part bodies.
81
- text << 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
82
114
  }
83
115
 
116
+ if haveBinaryData then
117
+ # TODO Encode binary body data with base64.
118
+ # XXX Omit for now.
119
+ text = ""
120
+ end
121
+
84
122
  {
85
123
  size: getBodySize(body),
86
124
 
87
125
  # XXX What to use when no Content-Type is given?
88
- mimeType: headers['Content-Type'],
126
+ mimeType: HarUtils.fixEncoding(headers['Content-Type']),
89
127
 
90
128
  text: text,
91
129
  }
@@ -94,7 +132,9 @@ module Akita
94
132
  def getRedirectUrl(headers)
95
133
  # Use the "Location" header if it exists. Otherwise, based on some HAR
96
134
  # examples found online, it looks like an empty string is used.
97
- headers.key?('Location') ? headers['Location'] : ''
135
+ headers.key?('Location') ?
136
+ HarUtils.fixEncoding(headers['Location']) :
137
+ ''
98
138
  end
99
139
 
100
140
  def getHeadersSize(env, status, headers)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Akita
4
4
  module HarLogger
5
- VERSION = "0.2.1"
5
+ VERSION = "0.2.6"
6
6
  end
7
7
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
-
5
3
  module Akita
6
4
  module HarLogger
7
5
  # A thread that consumes HarEntry objects from a queue and writes them to a
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: akita-har_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jed Liu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-21 00:00:00.000000000 Z
11
+ date: 2021-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: json
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '2.3'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '2.3'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rspec
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -83,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
69
  - !ruby/object:Gem::Version
84
70
  version: '0'
85
71
  requirements: []
86
- rubygems_version: 3.2.15
72
+ rubygems_version: 3.2.21
87
73
  signing_key:
88
74
  specification_version: 4
89
75
  summary: Rails middleware for HAR logging