Skip to content

Commit a1259db

Browse files
Classify header parse errors as bad requests.
1 parent 5cbb622 commit a1259db

18 files changed

Lines changed: 58 additions & 8 deletions

‎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_encoding.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-encoding` header represents a list of encodings that the client can accept.
1414
class AcceptEncoding < 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.1
1820
QVALUE = /0(\.[0-9]{0,3})?|1(\.[0]{0,3})?/

‎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/digest.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ module Header
2323
# # => "sha-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=, md5=9bb58f26192e4ba00f01e2e7b136bbd8"
2424
# ```
2525
class Digest < Split
26-
ParseError = Class.new(Error)
26+
ParseError = Class.new(Error) do
27+
include BadRequest
28+
end
2729

2830
# https://tools.ietf.org/html/rfc3230#section-4.3.2
2931
ENTRY = /\A(?<algorithm>[a-zA-Z0-9][a-zA-Z0-9\-]*)\s*=\s*(?<value>.*)\z/

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ module HTTP
1010
module Header
1111
# Represents a `range` request header.
1212
class Range
13-
ParseError = Class.new(Error)
13+
ParseError = Class.new(Error) do
14+
include BadRequest
15+
end
1416

1517
TOKEN = /[!#$%&'*+\-.0-9A-Z^_`a-z|~]+/
1618
HEADER = /\A(?<unit>#{TOKEN})=(?<ranges>.*)\z/

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ module Header
2323
# # => "db;dur=53.2, cache;dur=12.1;desc=\"Redis lookup\""
2424
# ```
2525
class ServerTiming < Split
26-
ParseError = Class.new(Error)
26+
ParseError = Class.new(Error) do
27+
include BadRequest
28+
end
2729

2830
# https://www.w3.org/TR/server-timing/
2931
METRIC = /\A(?<name>[a-zA-Z0-9][a-zA-Z0-9_\-]*)(;(?<parameters>.*))?\z/

‎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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Please see the [project releases](https://socketry.github.io/protocol-http/relea
3333
### Unreleased
3434

3535
- Parse and resolve HTTP `Range` header values according to the default headers policy.
36+
- Classify malformed header values as bad requests.
3637

3738
### v0.66.0
3839

‎releases.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Parse and resolve HTTP `Range` header values according to the default headers policy.
6+
- Classify malformed header values as bad requests.
67

78
## v0.66.0
89

0 commit comments

Comments
 (0)