lowloop 0.8.16 → 0.8.18

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: 53d6cf9b4dc4c2d7da0fafc75ea0a012f9c665de1a9707fc4f5cd0a6819eb0e2
4
- data.tar.gz: 242c11146c4d2b3eda2f201a3181aa6b07bd0e683612eeb687e3d018236f76e9
3
+ metadata.gz: 3e739e10de732f6298ffae12a935253c7ff3e0518f357b63ae3c61bdd206221d
4
+ data.tar.gz: 1358e039b62ccc0ee6adf45361f2f3eaf373e21508e41841b538be3d173e1ada
5
5
  SHA512:
6
- metadata.gz: 80f3afccac6eef15e81c970681f09e147fbca0344f52decdc362bed8b69590fcf4e54e4ff34691742f6d5def18680cfb2c7f101795cb63677cc0277c64074406
7
- data.tar.gz: 71349ca0269cb29052e40b077e6edd930d64f503c1ee7256e65878e2a830cb2b5977ac8f98baada742b117467bac93db8892f153c0bbd9bc8f4137104ed9a43b
6
+ metadata.gz: f2d69af7058f21b65e3bd2e1385286a9e8ad25afda15865535341de58a116aeb432eb11887a6089c8600ef9d6f45cc7fc5930ac6eef48774d1ae1935f96a93a8
7
+ data.tar.gz: 5c721bebf66027bd6b3c8df513ab2bb2f1fe2ef1713221e24096ac528d6ea738680eef8f46e032416247643d9c6369ba4ec8a356064941d5598028ac4c09bbd6
@@ -74,15 +74,14 @@ 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
78
  def keep_alive?(request)
78
- tokens = (request.headers['connection'] || []).flat_map do |value|
79
- value.split(',').map { |token| token.strip.downcase }
80
- end
79
+ tokens = request.headers['connection']
81
80
 
82
- if request.version.to_s.downcase.include?('1.0')
83
- tokens.include?('keep-alive')
81
+ if request.version.to_s.include?('1.0')
82
+ tokens&.include?('keep-alive') || false
84
83
  else
85
- !tokens.include?('close')
84
+ !tokens&.include?('close')
86
85
  end
87
86
  end
88
87
 
@@ -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]]
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..]
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
@@ -13,6 +13,8 @@ module Low
13
13
  # Interval[every_minute: 3, at_hour: 2]
14
14
  class Interval < Low::Event
15
15
  UNITS = { every_second: 1, every_minute: 60, every_hour: 3600, every_day: 86_400 }.freeze
16
+ # Enough recent turns' outcomes for a dial of up to 60 marks like each second of a minute.
17
+ KEPT_OUTCOMES = 64
16
18
 
17
19
  attr_reader :seconds, :at_hour, :triggered_at
18
20
 
@@ -37,6 +39,7 @@ module Low
37
39
  @seconds = seconds
38
40
  @at_hour = at_hour
39
41
  @triggered_at = nil
42
+ @outcomes = {}
40
43
  end
41
44
 
42
45
  # Each tick starts a tree of its own so a job's events are let go like a request's.
@@ -53,16 +56,32 @@ module Low
53
56
  # the next. It's not due outside its hour.
54
57
  def due?(now)
55
58
  @turn ||= turn_at(now)
59
+ @first_turn ||= @turn
56
60
  return false if at_hour && now.hour != at_hour
57
61
 
58
62
  turn_at(now) > @turn
59
63
  end
60
64
 
61
- # Triggers its observers, marking the turn it's in as run.
65
+ # Triggers its observers, marking the turn it's in as run and noting whether it ran or failed.
62
66
  def tick(now = Time.now)
63
67
  @triggered_at = now
64
68
  @turn = turn_at(now)
69
+ note_outcome(:running)
65
70
  trigger
71
+ note_outcome(:ran)
72
+ rescue StandardError
73
+ note_outcome(:failed)
74
+ raise
75
+ end
76
+
77
+ # What happened on the turn under way at that time: running or ran or failed. It's skipped when the turn passed
78
+ # without running since the app started and nil for a turn still to come or from before.
79
+ def outcome_at(time, now: Time.now)
80
+ turn = turn_at(time)
81
+ return @outcomes[turn] if @outcomes.key?(turn)
82
+ return unless @first_turn && turn > @first_turn && turn < turn_at(now)
83
+
84
+ :skipped
66
85
  end
67
86
 
68
87
  # Whether one of its turns starts at that second.
@@ -77,6 +96,11 @@ module Low
77
96
 
78
97
  private
79
98
 
99
+ def note_outcome(outcome)
100
+ @outcomes[@turn] = outcome
101
+ @outcomes.shift while @outcomes.size > KEPT_OUTCOMES
102
+ end
103
+
80
104
  # How many turns have passed since the epoch on the server's clock.
81
105
  def turn_at(time)
82
106
  local_seconds(time) / seconds
@@ -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.16'
5
+ VERSION = '0.8.18'
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.16
4
+ version: 0.8.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi