lowloop 0.8.9 → 0.8.11

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: 84fcc50ae2b18a1a82394d70439a9970283c45f246b1e3efa30d0f9b8be63f4a
4
- data.tar.gz: 691b026a9bbd0f932fdbb2fc0cd753d59125691ce0539c3cb0001170ad43de67
3
+ metadata.gz: 18c405026e9ac9963370fe8160d332a281556316ed45eefb348d527b3850f252
4
+ data.tar.gz: 9443d33e68f4d9b60202112bb0c54bfcf8baab10d0c60cb0c1fefa0226d3cab9
5
5
  SHA512:
6
- metadata.gz: 3725bd9228b2299cf31485c4fd96e10750e44a6a23afb63cd5ac9edc55896f284f7e581f1f94108e8f906a2208755580a8b083942ad4f73812625eca09a4bf97
7
- data.tar.gz: 0b1272174433cf3eb1f2f2b8991156eada221cfb474dc288ab833c387eb8011da759cd09753ae7e64e6e39e7cfcaff40f20f1658404210068b94042aa0063181
6
+ metadata.gz: 77ede7d33f4f405614ded0e87c481ba0ee6a495b88d3b8298982a1fcac58db2daed643dc39c516f34ec1d6d97cc05ff2752fb22c8c800d7c8a6a38890d83bcc1
7
+ data.tar.gz: 24ecd76e384df5a0d575720799f606b4e3ea7e0cceaf641829e24c4af754b3f0ef0ebda2be809ceeb7e16fcb3ae9e05121037c9d3a9a969ce8624ff81bada135
@@ -2,21 +2,19 @@
2
2
 
3
3
  module Low
4
4
  class ResponseBuilder
5
+ # Room for the status line and headers of most responses.
6
+ HEAD_CAPACITY = 1024
7
+
5
8
  class << self
6
9
  # TODO: Use Async wherever we can where it doesn't have "Task" requirement.
7
10
  def respond(socket:, response:, keep_alive: true)
8
11
  file_body = response.body.respond_to?(:file)
12
+ chunks = body_chunks(response.body) unless file_body
13
+ content_length = file_body ? response.body.file.size : chunks.sum(&:bytesize)
9
14
 
10
- if file_body
11
- content_length = response.body.file.size
12
- else
13
- # A response can have no body, like a node returning "Status[201]", and "join" reads every chunk of a body.
14
- body_data = response.body&.join || ''
15
- content_length = body_data.bytesize
16
- end
17
-
18
- # Build the whole head in memory then write once, as every socket write is a syscall.
19
- head = +''
15
+ # Build the whole response in one string, sized up front so it isn't copied as it grows, then write once, as
16
+ # every socket write is a syscall.
17
+ head = String.new(capacity: HEAD_CAPACITY + (file_body ? 0 : content_length))
20
18
  write_status_line(head, response)
21
19
  write_response_headers(head, response)
22
20
  write_final_headers(head, content_length, keep_alive)
@@ -25,7 +23,9 @@ module Low
25
23
  socket.write(head)
26
24
  IO.copy_stream(response.body.file, socket)
27
25
  else
28
- socket.write(head << body_data)
26
+ # As bytes, so a body in any encoding can follow headers in any other.
27
+ chunks.each { head.append_as_bytes(it) }
28
+ socket.write(head)
29
29
  end
30
30
  ensure
31
31
  response.body.close if file_body
@@ -33,6 +33,14 @@ module Low
33
33
 
34
34
  private
35
35
 
36
+ # A buffered body's chunks as they are, rather than joined into a copy. Any other body is read, and a response
37
+ # can have no body, like a node returning "Status[201]".
38
+ def body_chunks(body)
39
+ return body.chunks if body.is_a?(Protocol::HTTP::Body::Buffered)
40
+
41
+ [body&.join].compact
42
+ end
43
+
36
44
  def write_status_line(head, response)
37
45
  head << "#{response.version} #{response.status}\r\n"
38
46
  end
@@ -16,7 +16,8 @@ module Low
16
16
  @content_types = content_types.transform_keys(&:to_s)
17
17
  @cache_control = cache_control
18
18
 
19
- observers(Events::FileEvent) << FileResponse
19
+ # A second file server, like one for a framework's own files, shares the one FileResponse rather than adding another.
20
+ observers(Events::FileEvent) << FileResponse unless responding?
20
21
  end
21
22
 
22
23
  def extension(file_path:)
@@ -48,5 +49,11 @@ module Low
48
49
 
49
50
  Events::FileEvent.take(file:, request: event.request)
50
51
  end
52
+
53
+ private
54
+
55
+ def responding?
56
+ Observers::Keys[Events::FileEvent].observers.any? { |observer| observer.object == FileResponse }
57
+ end
51
58
  end
52
59
  end
data/lib/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Low
4
4
  module Loop
5
- VERSION = '0.8.9'
5
+ VERSION = '0.8.11'
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.9
4
+ version: 0.8.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi