Skip to content

Commit 890be01

Browse files
Ensure regex are linear time.
1 parent cedb025 commit 890be01

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

lib/protocol/http1/connection.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ module HTTP1
4040
WS = /[ \t]/ # Whitespace.
4141
OWS = /#{WS}*/ # Optional whitespace.
4242
VCHAR = /[!-~]/ # Match visible characters from ASCII 33 to 126.
43-
FIELD_VALUE = /(?:#{VCHAR}+(?:#{WS}+#{VCHAR})*)*/.freeze
44-
HEADER = /\A(#{FIELD_NAME}):#{OWS}(#{FIELD_VALUE})#{OWS}\z/.freeze
43+
FIELD_VALUE = /#{VCHAR}+(?:#{WS}+#{VCHAR}+)*/.freeze
44+
HEADER = /\A(#{FIELD_NAME}):#{OWS}(?:(#{FIELD_VALUE})#{OWS})?\z/.freeze
4545

4646
VALID_FIELD_NAME = /\A#{FIELD_NAME}\z/.freeze
4747
VALID_FIELD_VALUE = /\A#{FIELD_VALUE}\z/.freeze
@@ -487,7 +487,7 @@ def read_headers
487487
break if line.empty?
488488

489489
if match = line.match(HEADER)
490-
fields << [match[1], match[2]]
490+
fields << [match[1], match[2] || ""]
491491
else
492492
raise BadHeader, "Could not parse header: #{line.inspect}"
493493
end

test/protocol/http1/parser.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2025, by Samuel Williams.
5+
6+
require "protocol/http1/connection"
7+
8+
describe Protocol::HTTP1 do
9+
describe "REQUEST_LINE" do
10+
it "parses in linear time" do
11+
skip_unless_method_defined(:linear_time?, Regexp.singleton_class)
12+
13+
expect(Regexp).to be(:linear_time?, Protocol::HTTP1::REQUEST_LINE)
14+
end
15+
end
16+
17+
describe "HEADER" do
18+
it "parses in linear time" do
19+
skip_unless_method_defined(:linear_time?, Regexp.singleton_class)
20+
21+
expect(Regexp).to be(:linear_time?, Protocol::HTTP1::HEADER)
22+
end
23+
end
24+
25+
describe "VALID_FIELD_NAME" do
26+
it "parses in linear time" do
27+
skip_unless_method_defined(:linear_time?, Regexp.singleton_class)
28+
29+
expect(Regexp).to be(:linear_time?, Protocol::HTTP1::VALID_FIELD_NAME)
30+
end
31+
end
32+
33+
describe "VALID_FIELD_VALUE" do
34+
it "parses in linear time" do
35+
skip_unless_method_defined(:linear_time?, Regexp.singleton_class)
36+
37+
expect(Regexp).to be(:linear_time?, Protocol::HTTP1::VALID_FIELD_VALUE)
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)