lowloop 0.8.17 → 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 +4 -4
- data/lib/connections/connection_manager.rb +5 -6
- data/lib/connections/request_parser.rb +28 -10
- data/lib/responses/response_builder.rb +14 -6
- data/lib/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3e739e10de732f6298ffae12a935253c7ff3e0518f357b63ae3c61bdd206221d
|
|
4
|
+
data.tar.gz: 1358e039b62ccc0ee6adf45361f2f3eaf373e21508e41841b538be3d173e1ada
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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 =
|
|
79
|
-
value.split(',').map { |token| token.strip.downcase }
|
|
80
|
-
end
|
|
79
|
+
tokens = request.headers['connection']
|
|
81
80
|
|
|
82
|
-
if request.version.to_s.
|
|
83
|
-
tokens
|
|
81
|
+
if request.version.to_s.include?('1.0')
|
|
82
|
+
tokens&.include?('keep-alive') || false
|
|
84
83
|
else
|
|
85
|
-
!tokens
|
|
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
|
|
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',
|
|
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
|
|
56
|
+
request_line.strip!
|
|
43
57
|
return nil if request_line.empty?
|
|
44
58
|
|
|
45
|
-
|
|
46
|
-
|
|
59
|
+
method_end = request_line.index(' ') || request_line.length
|
|
60
|
+
path_end = request_line.index(' ', method_end + 1) || request_line.length
|
|
47
61
|
|
|
48
|
-
[
|
|
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
|
|
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
|
-
|
|
61
|
-
fields << [
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
58
|
+
key = key.to_s
|
|
59
|
+
next if FINAL_HEADERS.any? { key.casecmp?(it) }
|
|
51
60
|
|
|
52
|
-
head <<
|
|
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 <<
|
|
58
|
-
head <<
|
|
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