http_debug_output-parser 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/http_debug_output/parser/version.rb +1 -1
- data/lib/http_debug_output/parser.rb +68 -17
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 063f0170d11d7a6afecffe9865366d314020679cb3f112aaceb928e76a3372d5
|
|
4
|
+
data.tar.gz: 9284c52632ab7981f27f0bf76fea3270e3aa63289059f069410e85e14d0de409
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5a932934697fe37ddc83275c41a28daa620c3f0acf0f79b398314099b07e9a3b80b6ea3d614308e725a2017b78e2a7a09b917d194a8b91031f626b80eed9b129
|
|
7
|
+
data.tar.gz: 1662a6879e5d92b671b42f576af0de3765fc02408c075637cf9bba4591cc615574400160671f41bc057da73dcea037fcf09da11f223c343233d572a9ebf3e106
|
|
@@ -5,8 +5,16 @@ require 'json'
|
|
|
5
5
|
|
|
6
6
|
module HttpDebugOutput
|
|
7
7
|
class Parser
|
|
8
|
+
ESCAPE_SEQUENCES = {
|
|
9
|
+
'\\\\' => '\\',
|
|
10
|
+
'\\r' => "\r",
|
|
11
|
+
'\\n' => "\n",
|
|
12
|
+
'\\t' => "\t",
|
|
13
|
+
'\\"' => '"'
|
|
14
|
+
}.freeze
|
|
15
|
+
|
|
8
16
|
def initialize(debug_output)
|
|
9
|
-
@debug_output = debug_output.split("\n")
|
|
17
|
+
@debug_output = normalize(debug_output).split("\n")
|
|
10
18
|
end
|
|
11
19
|
|
|
12
20
|
def call
|
|
@@ -18,40 +26,83 @@ module HttpDebugOutput
|
|
|
18
26
|
|
|
19
27
|
private
|
|
20
28
|
|
|
21
|
-
def parse_request
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
headers = request_lines[1..].take_while { |line| line != '' }
|
|
26
|
-
payload = request_lines[headers.count + 1..].join
|
|
29
|
+
def parse_request
|
|
30
|
+
lines = request_lines
|
|
31
|
+
method, path, protocol = lines.first.split
|
|
32
|
+
headers = headers_from(lines) { |line| line == '' }
|
|
27
33
|
|
|
28
34
|
{
|
|
29
35
|
method:,
|
|
30
36
|
path:,
|
|
31
37
|
protocol:,
|
|
32
38
|
headers:,
|
|
33
|
-
payload:
|
|
39
|
+
payload: parse_payload(lines[headers.count + 1..])
|
|
34
40
|
}
|
|
35
41
|
end
|
|
36
42
|
|
|
37
|
-
def parse_response
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
protocol, status, *message = response_lines.first.split
|
|
42
|
-
headers = response_lines[1..].take_while { |line| !line.start_with?('reading') }
|
|
43
|
-
payload_end_index = response_lines.rindex { |line| line.start_with?('read ') } || response_lines.count
|
|
44
|
-
payload = response_lines[headers.count + 2..payload_end_index - 1].join
|
|
43
|
+
def parse_response
|
|
44
|
+
lines = response_lines
|
|
45
|
+
protocol, status, *message = lines.first.split
|
|
46
|
+
headers = headers_from(lines) { |line| line.start_with?('reading') }
|
|
45
47
|
|
|
46
48
|
{
|
|
47
49
|
protocol:,
|
|
48
50
|
status:,
|
|
49
51
|
message: message.join(' '),
|
|
50
52
|
headers:,
|
|
51
|
-
payload:
|
|
53
|
+
payload: parse_payload(lines[headers.count + 2..payload_end_index(lines) - 1])
|
|
52
54
|
}
|
|
53
55
|
end
|
|
54
56
|
|
|
57
|
+
def request_lines
|
|
58
|
+
@debug_output[find_range_for_start_and_end_strings('<- ', '-> ', last_occurence_of_end: false)]
|
|
59
|
+
.map { |line| clean_line(line.gsub('<- ', '')) }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def response_lines
|
|
63
|
+
@debug_output[find_range_for_start_and_end_strings('-> ', 'read ')]
|
|
64
|
+
.map { |line| clean_line(line.gsub('-> ', '')) }
|
|
65
|
+
.filter { |line| line != '' }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def headers_from(lines)
|
|
69
|
+
lines[1..].take_while { |line| !yield(line) }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def payload_end_index(lines)
|
|
73
|
+
lines.rindex { |line| line.start_with?('read ') } || lines.count
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def parse_payload(lines)
|
|
77
|
+
payload = lines.join
|
|
78
|
+
payload.empty? ? nil : JSON.parse(payload)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def normalize(debug_output)
|
|
82
|
+
return debug_output unless escaped?(debug_output)
|
|
83
|
+
|
|
84
|
+
lines = debug_output.split("\n").reject { |line| line.strip.empty? }
|
|
85
|
+
join_markers(lines).map { |line| unescape(line) }.join("\n")
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def escaped?(debug_output)
|
|
89
|
+
!debug_output.include?("\r") && debug_output.include?('\r\n')
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def join_markers(lines)
|
|
93
|
+
lines.each_with_object([]) do |line, joined|
|
|
94
|
+
if joined.last&.match?(/\A(?:<-|->)\s*\z/)
|
|
95
|
+
joined[-1] = "#{joined.last.strip} #{line}"
|
|
96
|
+
else
|
|
97
|
+
joined << line
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def unescape(line)
|
|
103
|
+
line.gsub(/\\(?:\\|r|n|t|")/) { |sequence| ESCAPE_SEQUENCES[sequence] }
|
|
104
|
+
end
|
|
105
|
+
|
|
55
106
|
def find_range_for_start_and_end_strings(start_index_string, end_index_string, last_occurence_of_end: true)
|
|
56
107
|
start_index = @debug_output.find_index { |line| line.start_with?(start_index_string) }
|
|
57
108
|
end_index_method = last_occurence_of_end ? :rindex : :find_index
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: http_debug_output-parser
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Damian Baćkowski
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-08-05 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A gem that parses debug output from Ruby's Net::HTTP.
|
|
14
14
|
email:
|
|
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
47
47
|
- !ruby/object:Gem::Version
|
|
48
48
|
version: '0'
|
|
49
49
|
requirements: []
|
|
50
|
-
rubygems_version: 3.4.
|
|
50
|
+
rubygems_version: 3.4.19
|
|
51
51
|
signing_key:
|
|
52
52
|
specification_version: 4
|
|
53
53
|
summary: Ruby's Net::HTTP debug output parser
|