lowloop 0.8.17 → 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: a1222163a4798e1a2508a0a049a3345d49ecac4693b2d4a326afea93bc0ca103
4
- data.tar.gz: cf9372d768bf6ed5f025a7fb3a15e8e74f89b2d54c2c91e226572b59a12251de
3
+ metadata.gz: bfa90ecaa4270aadf325053b4e2e50c20a6c93339ec59e58a6739fd7752c9486
4
+ data.tar.gz: 8254d17ed1aca9301aa8216bd61897f0bf634297284002f820f1dbcbf367c0cb
5
5
  SHA512:
6
- metadata.gz: 308c54b96ada7d81d2db9725dd3974e8a7c68297b76c4e48b9b3cc4361031995e517ca56e9045f421f8dc96b13a9653f381893eebf71aa4db2fc66a3e8f258ad
7
- data.tar.gz: a0fa638b481431f9742577698659ef1854905c8dda44f306de51986805657855ba1e051f64970b2080696e5e7d2506d763632a0de8fea55cfb0bdb22aa66b3b1
6
+ metadata.gz: 6eb1fa0d79dc9a290f16035f9c13466195c473188084de9c8f15f12d9fa63fb4baec8447d175c52305c561294b17fdf54dee92f1d1cd4df5451e6698ab4f81d8
7
+ data.tar.gz: 7665d750b778779fb72ebfce139a8cc30e3e844ab92f009b6f05cf0c8cb5703c8138f84203b6ed75cc33ac282c857b676730e698d986a4ec46f93ef9ee4649d0
@@ -74,18 +74,36 @@ module Low
74
74
  end
75
75
  end
76
76
 
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.
77
79
  def keep_alive?(request)
78
- tokens = (request.headers['connection'] || []).flat_map do |value|
79
- value.split(',').map { |token| token.strip.downcase }
80
+ if request.version.to_s.include?('1.0')
81
+ connection?(request, KEEP_ALIVE_TOKEN)
82
+ else
83
+ !connection?(request, CLOSE_TOKEN)
80
84
  end
85
+ end
81
86
 
82
- if request.version.to_s.downcase.include?('1.0')
83
- tokens.include?('keep-alive')
84
- else
85
- !tokens.include?('close')
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
86
95
  end
87
96
  end
88
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
+
89
107
  def keep_alive_timeout
90
108
  config.keep_alive_timeout || DEFAULT_KEEP_ALIVE_TIMEOUT
91
109
  end
@@ -95,11 +113,20 @@ module Low
95
113
  end
96
114
 
97
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.
98
118
  def headers_valid?(request)
99
- 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
100
129
  true
101
- rescue Protocol::HTTP::InvalidHeaderError
102
- false
103
130
  end
104
131
 
105
132
  private
@@ -8,6 +8,9 @@ module Low
8
8
  class RequestParser
9
9
  include LowType
10
10
 
11
+ # The methods with a body to read. A constant, as a %w[] literal makes a new array of new strings every time.
12
+ BODY_METHODS = %w[POST PUT PATCH].freeze
13
+
11
14
  class << self
12
15
  def create_stream(socket: TCPSocket) -> { IO::Stream::Generic }
13
16
  IO::Stream(socket)
@@ -19,11 +22,21 @@ module Low
19
22
  result = parse_request(stream:)
20
23
  return nil unless result
21
24
 
22
- method, full_path, = result
25
+ method, full_path = result
23
26
  headers = parse_headers(stream:)
24
27
  body = parse_body(stream:, method:, headers:)
25
28
 
26
- ::Protocol::HTTP::Request.new('http', "#{host}:#{port}", method, full_path, version, headers, body)
29
+ ::Protocol::HTTP::Request.new('http', authority(host, port), method, full_path, version, headers, body)
30
+ end
31
+
32
+ # Like "127.0.0.1:4133", made once, as a server's host and port don't change.
33
+ def authority(host, port)
34
+ last_host, last_port, authority = @authority
35
+ return authority if last_host == host && last_port == port
36
+
37
+ authority = "#{host}:#{port}".freeze
38
+ @authority = [host, port, authority].freeze
39
+ authority
27
40
  end
28
41
 
29
42
  private
@@ -36,16 +49,17 @@ module Low
36
49
  # :header_3\r\n
37
50
  # \r\n
38
51
  # :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.
39
53
  def parse_request(stream: IO::Stream::Generic)
40
54
  request_line = stream.gets || return
41
55
 
42
- request_line = request_line.strip
56
+ request_line.strip!
43
57
  return nil if request_line.empty?
44
58
 
45
- method, full_path, _http_version = request_line.split(' ', 3)
46
- path, query = full_path.split('?', 2)
59
+ method_end = request_line.index(' ') || request_line.length
60
+ path_end = request_line.index(' ', method_end + 1) || request_line.length
47
61
 
48
- [method, full_path, path, query]
62
+ [request_line[0, method_end], request_line[method_end + 1, path_end - method_end - 1]]
49
63
  end
50
64
 
51
65
  def parse_headers(stream: IO::Stream::Generic) -> { ::Protocol::HTTP::Headers }
@@ -53,19 +67,23 @@ module Low
53
67
 
54
68
  # Sometimes returns nil which represents a client disconnect mid-request.
55
69
  while (line = stream.gets)
56
- line = line.strip
70
+ line.strip!
57
71
  break if line.empty?
58
72
 
59
73
  # A value can be empty, like "Accept-Language:", or follow the colon without a space.
60
- key, value = line.split(':', 2)
61
- fields << [key, value.to_s.strip]
74
+ colon = line.index(':')
75
+ next fields << [line, +''] unless colon
76
+
77
+ value = line[colon + 1, line.length]
78
+ value.strip!
79
+ fields << [line[0, colon], value]
62
80
  end
63
81
 
64
82
  ::Protocol::HTTP::Headers.new(fields)
65
83
  end
66
84
 
67
85
  def parse_body(stream: IO::Stream::Generic, method: String, headers: ::Protocol::HTTP::Headers)
68
- return nil unless %w[POST PUT PATCH].include?(method)
86
+ return nil unless BODY_METHODS.include?(method)
69
87
 
70
88
  # A single value header like content-length is a String.
71
89
  content_length = headers['content-length'].to_i
@@ -4,6 +4,12 @@ module Low
4
4
  class ResponseBuilder
5
5
  # Room for the status line and headers of most responses.
6
6
  HEAD_CAPACITY = 1024
7
+ # Headers set from the response itself, rather than from the node's headers.
8
+ FINAL_HEADERS = %w[content-length connection].freeze
9
+ KEEP_ALIVE = "Connection: keep-alive\r\n\r\n"
10
+ CLOSE = "Connection: close\r\n\r\n"
11
+ # A status code's text, like "200", made once for each code.
12
+ STATUSES = (100..599).to_h { [it, it.to_s.freeze] }.freeze
7
13
 
8
14
  class << self
9
15
  # TODO: Use Async wherever we can where it doesn't have "Task" requirement.
@@ -41,22 +47,24 @@ module Low
41
47
  [body&.join].compact
42
48
  end
43
49
 
50
+ # Each part is appended as it is, rather than interpolated into a new string first.
44
51
  def write_status_line(head, response)
45
- head << "#{response.version} #{response.status}\r\n"
52
+ status = response.status
53
+ head << response.version.to_s << ' ' << (STATUSES[status] || status.to_s) << "\r\n"
46
54
  end
47
55
 
48
56
  def write_response_headers(head, response)
49
57
  response.headers.fields.each do |key, value|
50
- next if %w[content-length connection].include?(key.to_s.downcase)
58
+ key = key.to_s
59
+ next if FINAL_HEADERS.any? { key.casecmp?(it) }
51
60
 
52
- head << "#{key}: #{value}\r\n"
61
+ head << key << ': ' << value.to_s << "\r\n"
53
62
  end
54
63
  end
55
64
 
56
65
  def write_final_headers(head, content_length, keep_alive)
57
- head << "Content-Length: #{content_length}\r\n"
58
- head << "Connection: #{keep_alive ? 'keep-alive' : 'close'}\r\n"
59
- head << "\r\n"
66
+ head << 'Content-Length: ' << content_length.to_s << "\r\n"
67
+ head << (keep_alive ? KEEP_ALIVE : CLOSE)
60
68
  end
61
69
  end
62
70
  end
data/lib/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Low
4
4
  module Loop
5
- VERSION = '0.8.17'
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.17
4
+ version: 0.8.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi