lowloop 0.8.18 → 0.8.19

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e739e10de732f6298ffae12a935253c7ff3e0518f357b63ae3c61bdd206221d
4
- data.tar.gz: 1358e039b62ccc0ee6adf45361f2f3eaf373e21508e41841b538be3d173e1ada
3
+ metadata.gz: bfa90ecaa4270aadf325053b4e2e50c20a6c93339ec59e58a6739fd7752c9486
4
+ data.tar.gz: 8254d17ed1aca9301aa8216bd61897f0bf634297284002f820f1dbcbf367c0cb
5
5
  SHA512:
6
- metadata.gz: f2d69af7058f21b65e3bd2e1385286a9e8ad25afda15865535341de58a116aeb432eb11887a6089c8600ef9d6f45cc7fc5930ac6eef48774d1ae1935f96a93a8
7
- data.tar.gz: 5c721bebf66027bd6b3c8df513ab2bb2f1fe2ef1713221e24096ac528d6ea738680eef8f46e032416247643d9c6369ba4ec8a356064941d5598028ac4c09bbd6
6
+ metadata.gz: 6eb1fa0d79dc9a290f16035f9c13466195c473188084de9c8f15f12d9fa63fb4baec8447d175c52305c561294b17fdf54dee92f1d1cd4df5451e6698ab4f81d8
7
+ data.tar.gz: 7665d750b778779fb72ebfce139a8cc30e3e844ab92f009b6f05cf0c8cb5703c8138f84203b6ed75cc33ac282c857b676730e698d986a4ec46f93ef9ee4649d0
@@ -74,17 +74,36 @@ module Low
74
74
  end
75
75
  end
76
76
 
77
- # Protocol::HTTP reads the Connection header as its lowercase tokens, like ["keep-alive"].
77
+ # The Connection header is read from the request's fields, as reading it with "request.headers['connection']" would
78
+ # index and parse every header. Its tokens, like "keep-alive, Upgrade", are matched in any case.
78
79
  def keep_alive?(request)
79
- tokens = request.headers['connection']
80
-
81
80
  if request.version.to_s.include?('1.0')
82
- tokens&.include?('keep-alive') || false
81
+ connection?(request, KEEP_ALIVE_TOKEN)
83
82
  else
84
- !tokens&.include?('close')
83
+ !connection?(request, CLOSE_TOKEN)
84
+ end
85
+ end
86
+
87
+ KEEP_ALIVE_TOKEN = /(?:\A|,)\s*keep-alive\s*(?:,|\z)/i
88
+ CLOSE_TOKEN = /(?:\A|,)\s*close\s*(?:,|\z)/i
89
+
90
+ # Header names are ASCII, so they're compared with casecmp, as casecmp? folds Unicode case into new strings.
91
+ def connection?(request, token)
92
+ request.headers.fields.any? do |name, value|
93
+ name = name.to_s
94
+ name.length == 10 && name.casecmp('connection') == 0 && value.to_s.match?(token) # rubocop:disable Style/NumericPredicate
85
95
  end
86
96
  end
87
97
 
98
+ # Each header name in lowercase, made once for each name, as most requests send the same few. Only so many are kept,
99
+ # as a client can send any name.
100
+ DOWNCASED = {} # rubocop:disable Style/MutableConstant
101
+ DOWNCASED_LIMIT = 1000
102
+
103
+ def downcased(name)
104
+ DOWNCASED[name] || (DOWNCASED.size < DOWNCASED_LIMIT ? DOWNCASED[name.dup.freeze] = name.downcase.freeze : name.downcase)
105
+ end
106
+
88
107
  def keep_alive_timeout
89
108
  config.keep_alive_timeout || DEFAULT_KEEP_ALIVE_TIMEOUT
90
109
  end
@@ -94,11 +113,20 @@ module Low
94
113
  end
95
114
 
96
115
  # Headers there can only be one of, like Referer, raise when read if a request repeats them, so it's a bad request.
116
+ # They're those without a policy for joining them in Protocol::HTTP, which is checked by name, rather than indexing
117
+ # and parsing every header with "to_h" before anything reads them.
97
118
  def headers_valid?(request)
98
- request.headers.to_h
119
+ seen = nil
120
+ request.headers.fields.each do |field|
121
+ key = downcased(field.first.to_s)
122
+ next if Protocol::HTTP::Headers::POLICY[key]
123
+
124
+ seen ||= {}
125
+ return false if seen.key?(key)
126
+
127
+ seen[key] = true
128
+ end
99
129
  true
100
- rescue Protocol::HTTP::InvalidHeaderError
101
- false
102
130
  end
103
131
 
104
132
  private
@@ -59,7 +59,7 @@ module Low
59
59
  method_end = request_line.index(' ') || request_line.length
60
60
  path_end = request_line.index(' ', method_end + 1) || request_line.length
61
61
 
62
- [request_line[0, method_end], request_line[method_end + 1...path_end]]
62
+ [request_line[0, method_end], request_line[method_end + 1, path_end - method_end - 1]]
63
63
  end
64
64
 
65
65
  def parse_headers(stream: IO::Stream::Generic) -> { ::Protocol::HTTP::Headers }
@@ -74,7 +74,7 @@ module Low
74
74
  colon = line.index(':')
75
75
  next fields << [line, +''] unless colon
76
76
 
77
- value = line[colon + 1..]
77
+ value = line[colon + 1, line.length]
78
78
  value.strip!
79
79
  fields << [line[0, colon], value]
80
80
  end
data/lib/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Low
4
4
  module Loop
5
- VERSION = '0.8.18'
5
+ VERSION = '0.8.19'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lowloop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.18
4
+ version: 0.8.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi