lowloop 0.8.18 → 0.8.20

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: 18b2f0ae67dc62083d5d2f241ff7ef4ce1d9b559e43c7e2ea501dc9374ce2348
4
+ data.tar.gz: 4541c743b8ceb475f5741fbea328afb87fb4e517700e8b6364974f2c6c346d3a
5
5
  SHA512:
6
- metadata.gz: f2d69af7058f21b65e3bd2e1385286a9e8ad25afda15865535341de58a116aeb432eb11887a6089c8600ef9d6f45cc7fc5930ac6eef48774d1ae1935f96a93a8
7
- data.tar.gz: 5c721bebf66027bd6b3c8df513ab2bb2f1fe2ef1713221e24096ac528d6ea738680eef8f46e032416247643d9c6369ba4ec8a356064941d5598028ac4c09bbd6
6
+ metadata.gz: 2c6c514cebe4cee697089fd971d30fa28e65ddb03f4ba4b2bd719d556d103366c69f777b0a5db3ed74564ac92a2a11bea26c4bdf8e064223463c97c7c297433c
7
+ data.tar.gz: ad5cc40e75412e724eb131be6449dca588da2b4e2e17fdf44e26aea8d1523e0fe4ce826256e173fe84217dc53324c87fcb5882d78b38644046d16d57c2f4e8fe
@@ -45,14 +45,13 @@ module Low
45
45
  def serve_requests(socket:)
46
46
  stream = RequestParser.create_stream(socket:)
47
47
  keep_alive = true
48
- version = nil
49
48
 
50
49
  while keep_alive
51
50
  break unless socket.wait_readable(keep_alive_timeout)
52
51
 
53
52
  socket.timeout = request_timeout
54
53
  begin
55
- request = RequestParser.parse(stream:, host: config.host, port: config.port, version:)
54
+ request = RequestParser.parse(stream:, host: config.host, port: config.port)
56
55
  rescue IO::TimeoutError
57
56
  break
58
57
  ensure
@@ -60,7 +59,6 @@ module Low
60
59
  end
61
60
  break if request.nil?
62
61
 
63
- version ||= request.version
64
62
  unless headers_valid?(request)
65
63
  ResponseBuilder.respond(socket:, response: ResponseFactory.status(code: 400), keep_alive: false)
66
64
  break
@@ -74,17 +72,36 @@ module Low
74
72
  end
75
73
  end
76
74
 
77
- # Protocol::HTTP reads the Connection header as its lowercase tokens, like ["keep-alive"].
75
+ # The Connection header is read from the request's fields, as reading it with "request.headers['connection']" would
76
+ # index and parse every header. Its tokens, like "keep-alive, Upgrade", are matched in any case.
78
77
  def keep_alive?(request)
79
- tokens = request.headers['connection']
80
-
81
78
  if request.version.to_s.include?('1.0')
82
- tokens&.include?('keep-alive') || false
79
+ connection?(request, KEEP_ALIVE_TOKEN)
83
80
  else
84
- !tokens&.include?('close')
81
+ !connection?(request, CLOSE_TOKEN)
82
+ end
83
+ end
84
+
85
+ KEEP_ALIVE_TOKEN = /(?:\A|,)\s*keep-alive\s*(?:,|\z)/i
86
+ CLOSE_TOKEN = /(?:\A|,)\s*close\s*(?:,|\z)/i
87
+
88
+ # Header names are ASCII, so they're compared with casecmp, as casecmp? folds Unicode case into new strings.
89
+ def connection?(request, token)
90
+ request.headers.fields.any? do |name, value|
91
+ name = name.to_s
92
+ name.length == 10 && name.casecmp('connection') == 0 && value.to_s.match?(token) # rubocop:disable Style/NumericPredicate
85
93
  end
86
94
  end
87
95
 
96
+ # Each header name in lowercase, made once for each name, as most requests send the same few. Only so many are kept,
97
+ # as a client can send any name.
98
+ DOWNCASED = {} # rubocop:disable Style/MutableConstant
99
+ DOWNCASED_LIMIT = 1000
100
+
101
+ def downcased(name)
102
+ DOWNCASED[name] || (DOWNCASED.size < DOWNCASED_LIMIT ? DOWNCASED[name.dup.freeze] = name.downcase.freeze : name.downcase)
103
+ end
104
+
88
105
  def keep_alive_timeout
89
106
  config.keep_alive_timeout || DEFAULT_KEEP_ALIVE_TIMEOUT
90
107
  end
@@ -94,11 +111,20 @@ module Low
94
111
  end
95
112
 
96
113
  # Headers there can only be one of, like Referer, raise when read if a request repeats them, so it's a bad request.
114
+ # They're those without a policy for joining them in Protocol::HTTP, which is checked by name, rather than indexing
115
+ # and parsing every header with "to_h" before anything reads them.
97
116
  def headers_valid?(request)
98
- request.headers.to_h
117
+ seen = nil
118
+ request.headers.fields.each do |field|
119
+ key = downcased(field.first.to_s)
120
+ next if Protocol::HTTP::Headers::POLICY[key]
121
+
122
+ seen ||= {}
123
+ return false if seen.key?(key)
124
+
125
+ seen[key] = true
126
+ end
99
127
  true
100
- rescue Protocol::HTTP::InvalidHeaderError
101
- false
102
128
  end
103
129
 
104
130
  private
@@ -16,13 +16,11 @@ module Low
16
16
  IO::Stream(socket)
17
17
  end
18
18
 
19
- def parse(stream: IO::Stream::Generic, host: String, port: Integer, version: nil) -> { ::Protocol::HTTP::Request | nil }
20
- version ||= Async::HTTP::Protocol::HTTP.default.protocol_for(stream)::VERSION
21
-
19
+ def parse(stream: IO::Stream::Generic, host: String, port: Integer) -> { ::Protocol::HTTP::Request | nil }
22
20
  result = parse_request(stream:)
23
21
  return nil unless result
24
22
 
25
- method, full_path = result
23
+ method, full_path, version = result
26
24
  headers = parse_headers(stream:)
27
25
  body = parse_body(stream:, method:, headers:)
28
26
 
@@ -49,7 +47,7 @@ module Low
49
47
  # :header_3\r\n
50
48
  # \r\n
51
49
  # :body
52
- # The line is read into a new string, so it's stripped in place, and only the method and path are copied out of it.
50
+ # The line is read into a new string so it's stripped in place. A line without a version is an HTTP/1.0 client's.
53
51
  def parse_request(stream: IO::Stream::Generic)
54
52
  request_line = stream.gets || return
55
53
 
@@ -59,7 +57,9 @@ module Low
59
57
  method_end = request_line.index(' ') || request_line.length
60
58
  path_end = request_line.index(' ', method_end + 1) || request_line.length
61
59
 
62
- [request_line[0, method_end], request_line[method_end + 1...path_end]]
60
+ version = request_line[path_end + 1..]
61
+ version = 'HTTP/1.0' if version.nil? || version.empty?
62
+ [request_line[0, method_end], request_line[method_end + 1, path_end - method_end - 1], version]
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.20'
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.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi