lowloop 0.8.5 → 0.8.7

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: a2560511646de0f4456ed7f395847af7c588f8093cebd669c2e0920845fc95bb
4
- data.tar.gz: ee11a83dad0dce1c414a0e4251f4e17cd708f95acea2e1f8e492c43a7f0434f1
3
+ metadata.gz: 38d73ae8ac834cc72db7520604634e6e9aa75284b82022b88efe6134ac257cc8
4
+ data.tar.gz: ad5d76b91c7778a1fbbb5a107ad43ca6d166654450e8e6a5540803e99556ed9b
5
5
  SHA512:
6
- metadata.gz: 01a65f0e4569f0c1b6fc2e61f5c05b68fa27e06aa958ee0445fde2cdbdf513d7d3e26094093795d3c139562c6062fd49673d0ef54c08f4862cb4fea0a2e1c11f
7
- data.tar.gz: b6c9b778c647d7730a255a887198ffe025051764eba9be5afd56af9b5e73a051797fa1268865da849a73a8acbee80ea84bde343fa0ceeceba3deecf680dfb6b2
6
+ metadata.gz: ae22bc76a568d7e60b366107c2b5a6f3cb77ab0807c8f09272b5bff3c3ff09b3a52524c808cb7f1b9feeca81bfc18875b99c815cca06ed91530503fe7e64a507
7
+ data.tar.gz: fef53772f9f81a193f01e64718caddaa43014eb001c4dc3157bf053dbdda84277b05fde7294eed2bb8a4446eafb07396a525dd0eff11c0f773494ae23e54b30c
@@ -36,6 +36,11 @@ module Low
36
36
  break if request.nil?
37
37
 
38
38
  version ||= request.version
39
+ unless headers_valid?(request)
40
+ ResponseBuilder.respond(config:, socket:, response: ResponseFactory.status(code: 400), keep_alive: false)
41
+ break
42
+ end
43
+
39
44
  keep_alive = keep_alive?(request)
40
45
 
41
46
  response = respond_to(request:)
@@ -64,6 +69,14 @@ module Low
64
69
  config.request_timeout || DEFAULT_REQUEST_TIMEOUT
65
70
  end
66
71
 
72
+ # Headers there can only be one of, like Referer, raise when read if a request repeats them, so it's a bad request.
73
+ def headers_valid?(request)
74
+ request.headers.to_h
75
+ true
76
+ rescue Protocol::HTTP::InvalidHeaderError
77
+ false
78
+ end
79
+
67
80
  private
68
81
 
69
82
  attr_reader :config
@@ -56,8 +56,9 @@ module Low
56
56
  line = line.strip
57
57
  break if line.empty?
58
58
 
59
- key, value = line.split(/:\s/, 2)
60
- fields << [key, value]
59
+ # 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]
61
62
  end
62
63
 
63
64
  ::Protocol::HTTP::Headers.new(fields)
data/lib/lowloop.rb CHANGED
@@ -23,7 +23,7 @@ class LowLoop
23
23
  @frame = LowFrame.new(renderer: (renderer if config.matrix_mode), fps: 10, show_output:)
24
24
 
25
25
  Low::Events::RequestEvent.define do |observers|
26
- observers << Low::FileServer.new(web_root: config.web_root, content_types: config.content_types)
26
+ observers << Low::FileServer.new(web_root: config.web_root, content_types: config.content_types, cache_control: config.cache_control)
27
27
  observers << router if router
28
28
  observers.push(self, action: :mirror) if config.mirror_mode
29
29
  end
@@ -64,7 +64,9 @@ class LowLoop
64
64
 
65
65
  private
66
66
 
67
+ # Output is written as it happens, so a log like journald's shows when each error was.
67
68
  def start_server
69
+ $stdout.sync = true
68
70
  puts "Starting server @ #{config.host}:#{config.port}" unless config.matrix_mode
69
71
 
70
72
  server = TCPServer.new(config.host, config.port)
@@ -7,13 +7,28 @@ module Low
7
7
  class << self
8
8
  def request(event:)
9
9
  file = event.file
10
+ return nil unless File.exist?(file.path)
10
11
 
11
- if File.exist?(file.path)
12
- response = ResponseFactory.file(path: file.path, content_type: file.content_type)
13
- return Events::ResponseEvent.new(response:).tap(&:branch)
14
- end
12
+ path = file.path
13
+ cache_control = file.cache_control
14
+ return respond(ResponseFactory.not_modified(path:, cache_control:)) if unchanged?(path, event.request)
15
15
 
16
- nil
16
+ respond(ResponseFactory.file(path:, content_type: file.content_type, cache_control:))
17
+ end
18
+
19
+ private
20
+
21
+ def respond(response)
22
+ Events::ResponseEvent.new(response:).tap(&:branch)
23
+ end
24
+
25
+ def unchanged?(path, request)
26
+ since = request&.headers&.[]('if-modified-since')
27
+ return false unless since
28
+
29
+ File.mtime(path).to_i <= Time.httpdate(Array(since).first.to_s).to_i
30
+ rescue ArgumentError
31
+ false
17
32
  end
18
33
  end
19
34
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'cgi'
4
+ require 'time'
4
5
  require 'protocol/http'
5
6
  require 'protocol/http/body/file'
6
7
 
@@ -13,13 +14,24 @@ module Low
13
14
  Protocol::HTTP::Response.new('HTTP/1.1', 200, headers, body)
14
15
  end
15
16
 
16
- def file(path:, content_type:)
17
- headers = Protocol::HTTP::Headers.new([['content-type', content_type]])
17
+ def file(path:, content_type:, cache_control: nil)
18
+ headers = Protocol::HTTP::Headers.new([['content-type', content_type], *cache_headers(path, cache_control)])
18
19
  file = File.open(path, 'rb')
19
20
  body = Protocol::HTTP::Body::File.new(file)
20
21
  Protocol::HTTP::Response.new('HTTP/1.1', 200, headers, body)
21
22
  end
22
23
 
24
+ def not_modified(path:, cache_control: nil)
25
+ headers = Protocol::HTTP::Headers.new(cache_headers(path, cache_control))
26
+ Protocol::HTTP::Response.new('HTTP/1.1', 304, headers, nil)
27
+ end
28
+
29
+ def cache_headers(path, cache_control)
30
+ headers = [['last-modified', File.mtime(path).httpdate]]
31
+ headers << ['cache-control', cache_control] if cache_control
32
+ headers
33
+ end
34
+
23
35
  # Shows the error and where it happened in debug mode (or show a 500 status).
24
36
  def error(error:, debug: false)
25
37
  headers = Protocol::HTTP::Headers.new([['content-type', 'text/html']])
@@ -11,9 +11,10 @@ module Low
11
11
  include LowType
12
12
  include Observers
13
13
 
14
- def initialize(web_root:, content_types:)
14
+ def initialize(web_root:, content_types:, cache_control: nil)
15
15
  @web_root = web_root
16
16
  @content_types = content_types.transform_keys(&:to_s)
17
+ @cache_control = cache_control
17
18
 
18
19
  observers(Events::FileEvent) << FileResponse
19
20
  end
@@ -27,6 +28,12 @@ module Low
27
28
  extension
28
29
  end
29
30
 
31
+ def cache_control(extension)
32
+ return @cache_control unless @cache_control.is_a?(Hash)
33
+
34
+ @cache_control.transform_keys(&:to_s)[extension]
35
+ end
36
+
30
37
  def request(event: Events::RequestEvent)
31
38
  file_path = event.request.path
32
39
 
@@ -37,7 +44,7 @@ module Low
37
44
  return nil if extension.nil?
38
45
 
39
46
  file_path = url.local_path(@web_root)
40
- file = States::FileState.new(path: file_path, content_type: @content_types[extension])
47
+ file = States::FileState.new(path: file_path, content_type: @content_types[extension], cache_control: cache_control(extension))
41
48
 
42
49
  Events::FileEvent.take(file:, request: event.request)
43
50
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Low
4
4
  module States
5
- FileState = Data.define(:path, :content_type)
5
+ FileState = Data.define(:path, :content_type, :cache_control) do
6
+ def initialize(path:, content_type:, cache_control: nil) = super
7
+ end
6
8
  end
7
9
  end
data/lib/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Low
4
4
  module Loop
5
- VERSION = '0.8.5'
5
+ VERSION = '0.8.7'
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.5
4
+ version: 0.8.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi
@@ -160,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
160
  - !ruby/object:Gem::Version
161
161
  version: '0'
162
162
  requirements: []
163
- rubygems_version: 4.0.6
163
+ rubygems_version: 4.0.20
164
164
  specification_version: 4
165
165
  summary: An event-driven event loop
166
166
  test_files: []