Skip to content

Commit ca811d3

Browse files
Classify request header parse errors as bad requests.
1 parent 5a09a3b commit ca811d3

10 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎lib/protocol/http/header/accept.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ class Accept < Split
2323
(?=,|\z) # Match until a comma or end of string
2424
/x
2525

26-
ParseError = Class.new(Error)
26+
ParseError = Class.new(Error) do
27+
include BadRequest
28+
end
2729

2830
MEDIA_RANGE = /\A(?<type>#{TOKEN})\/(?<subtype>#{TOKEN})(?<parameters>.*)\z/
2931

‎lib/protocol/http/header/accept_charset.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ module HTTP
1212
module Header
1313
# The `accept-charset` header represents a list of character sets that the client can accept.
1414
class AcceptCharset < Split
15-
ParseError = Class.new(Error)
15+
ParseError = Class.new(Error) do
16+
include BadRequest
17+
end
1618

1719
# https://tools.ietf.org/html/rfc7231#section-5.3.3
1820
CHARSET = /\A(?<name>#{TOKEN})(;q=(?<q>#{QVALUE}))?\z/

‎lib/protocol/http/header/accept_language.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ module HTTP
1212
module Header
1313
# The `accept-language` header represents a list of languages that the client can accept.
1414
class AcceptLanguage < Split
15-
ParseError = Class.new(Error)
15+
ParseError = Class.new(Error) do
16+
include BadRequest
17+
end
1618

1719
# https://tools.ietf.org/html/rfc3066#section-2.1
1820
NAME = /\*|[A-Z]{1,8}(-[A-Z0-9]{1,8})*/i

‎lib/protocol/http/header/te.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ module Header
1414
#
1515
# The `te` header allows a client to indicate which transfer encodings it can handle, and in what order of preference using quality factors.
1616
class TE < Split
17-
ParseError = Class.new(Error)
17+
ParseError = Class.new(Error) do
18+
include BadRequest
19+
end
1820

1921
# Transfer encoding token pattern
2022
TOKEN = /[!#$%&'*+\-.0-9A-Z^_`a-z|~]+/

‎readme.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Please see the [project documentation](https://socketry.github.io/protocol-http/
3030

3131
Please see the [project releases](https://socketry.github.io/protocol-http/releases/index) for all releases.
3232

33+
### Unreleased
34+
35+
- Classify malformed request header values as bad requests.
36+
3337
### v0.66.0
3438

3539
- Introduce `Protocol::HTTP::RemoteError` for remote endpoint failures where application processing may have occurred.

‎releases.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Classify malformed request header values as bad requests.
6+
37
## v0.66.0
48

59
- Introduce `Protocol::HTTP::RemoteError` for remote endpoint failures where application processing may have occurred.

‎test/protocol/http/header/accept.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
end
2121

2222
describe Protocol::HTTP::Header::Accept do
23+
it "classifies parse errors as bad requests" do
24+
expect(subject::ParseError.new).to be_a(Protocol::HTTP::BadRequest)
25+
end
26+
2327
let(:header) {subject.parse(description)}
2428
let(:media_ranges) {header.media_ranges.sort}
2529

‎test/protocol/http/header/accept_charset.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
end
1414

1515
describe Protocol::HTTP::Header::AcceptCharset do
16+
it "classifies parse errors as bad requests" do
17+
expect(subject::ParseError.new).to be_a(Protocol::HTTP::BadRequest)
18+
end
19+
1620
let(:header) {subject.parse(description)}
1721
let(:charsets) {header.charsets.sort}
1822

‎test/protocol/http/header/accept_language.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
end
1414

1515
describe Protocol::HTTP::Header::AcceptLanguage do
16+
it "classifies parse errors as bad requests" do
17+
expect(subject::ParseError.new).to be_a(Protocol::HTTP::BadRequest)
18+
end
19+
1620
let(:header) {subject.parse(description)}
1721
let(:languages) {header.languages.sort}
1822

‎test/protocol/http/header/te.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
require "protocol/http/header/te"
77

88
describe Protocol::HTTP::Header::TE do
9+
it "classifies parse errors as bad requests" do
10+
expect(subject::ParseError.new).to be_a(Protocol::HTTP::BadRequest)
11+
end
12+
913
let(:header) {subject.parse(description)}
1014

1115
with "chunked" do

0 commit comments

Comments
 (0)