ruby_wasm_ic_rack 0.1.1 → 0.2.0
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/README.md +32 -8
- data/lib/ruby_wasm_ic_rack/version.rb +1 -1
- data/lib/ruby_wasm_ic_rack.rb +49 -8
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1b639709a4fc8e3f3442bfa1f728c28e587de6e86a53a42822701caf5e04400f
|
|
4
|
+
data.tar.gz: aa52b133447adf38e52b08fe961fd7e69663f09025e2bb1227bf3b9cba53a4d1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 971c30a3c71884232c94f796935848cdc2bd17b3c3292a8996a4a455b63a24276eb95007d804f38eede538ccf4e2ebb88275d1839b8a6e72dbfc0e8fad3bd27c
|
|
7
|
+
data.tar.gz: 594a490e95660dd38ab99f62ad7eb1be8d0cd7df41dd7f626d88f5b7629cbde339311201ce885cdf57d4c720a40f90def6e4e3fee54a02aa936a66c45e8330db
|
data/README.md
CHANGED
|
@@ -61,10 +61,27 @@ def http_request_update(req) = RubyWasmIcRack.update(APP, req)
|
|
|
61
61
|
|
|
62
62
|
## What it does
|
|
63
63
|
|
|
64
|
-
- **The env:** `REQUEST_METHOD`, `PATH_INFO`, `QUERY_STRING`,
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
64
|
+
- **The env:** `REQUEST_METHOD`, `PATH_INFO`, `QUERY_STRING`, every header as
|
|
65
|
+
`HTTP_*` (`CONTENT_TYPE` as Rack names it), a repeated header joined with
|
|
66
|
+
`, `, and `rack.input` over the body. The rest is built from what the
|
|
67
|
+
gateway actually hands the canister, not from what a client could write:
|
|
68
|
+
- `CONTENT_LENGTH` is the body's size in bytes, whatever `content-length`
|
|
69
|
+
the client sent or didn't send. A body arrives whole, and the gateway
|
|
70
|
+
passes `content-length` on only if the client set it. It's unset for an
|
|
71
|
+
empty body.
|
|
72
|
+
- `SERVER_NAME`, `SERVER_PORT` and `rack.url_scheme` come from `host`.
|
|
73
|
+
`localhost`, `*.localhost` and loopback (`127.0.0.1`, `[::1]`) give
|
|
74
|
+
`http` on the port written there, so `<id>.localhost:8000` builds full
|
|
75
|
+
URLs that work locally. Any other host gives `https` on 443.
|
|
76
|
+
`x-forwarded-proto` and `x-forwarded-port` are never read: no gateway
|
|
77
|
+
sets them, and the gateway's cache doesn't key on them.
|
|
78
|
+
- `REMOTE_ADDR` is `0.0.0.0`. No client address reaches a canister.
|
|
79
|
+
- **Client-written headers.** The gateway passes the client's headers on
|
|
80
|
+
unchanged, and adds no `x-forwarded-for`, `x-real-ip` or `forwarded`
|
|
81
|
+
of its own. If one arrives, the client wrote it. They reach the app as
|
|
82
|
+
`HTTP_*`, unchecked, so `request.remote_ip` in Rails (which prefers
|
|
83
|
+
`X-Forwarded-For`) is whatever the client says, or `0.0.0.0`. Don't key a
|
|
84
|
+
rate limit or an audit trail on it.
|
|
68
85
|
- **The response:** a header whose value is an Array, or lines joined by
|
|
69
86
|
`"\n"` (Rack 3), goes out as one header each, so two `set-cookie` headers
|
|
70
87
|
arrive as two. The body is joined and closed.
|
|
@@ -107,9 +124,16 @@ traps the install. Change that word to `ascii-8bit`.
|
|
|
107
124
|
|
|
108
125
|
## Development
|
|
109
126
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
127
|
+
```bash
|
|
128
|
+
bundle install
|
|
129
|
+
bundle exec rake # the specs, on MRI, with no canister
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
The specs build envs and responses from gateway records on MRI. What only a
|
|
133
|
+
canister behind a real gateway can show is played by
|
|
134
|
+
[ruby_wasm_builder](https://gitlab.com/phrug/ruby_wasm_builder)'s suite,
|
|
135
|
+
`spec/gems/ruby_wasm_ic_rack_spec.rb`. That spec installs the published gem
|
|
136
|
+
and plays the 0.7.0 `rack_adapter` and `roda_on_minimal` Scripts with its
|
|
137
|
+
`lib/` in place of the Challenges' own copies.
|
|
114
138
|
|
|
115
139
|
MIT licensed.
|
data/lib/ruby_wasm_ic_rack.rb
CHANGED
|
@@ -13,11 +13,24 @@
|
|
|
13
13
|
# http_request_update. An app whose GET writes says so with the response
|
|
14
14
|
# header `x-ic-upgrade`, and the query upgrades that too; the header is never
|
|
15
15
|
# sent on.
|
|
16
|
+
#
|
|
17
|
+
# The env is built from what the gateway hands over and nothing it doesn't
|
|
18
|
+
# vouch for (0.2.0). CONTENT_LENGTH is the body's own size, never a client's
|
|
19
|
+
# header. REMOTE_ADDR is "0.0.0.0", since no client address reaches a
|
|
20
|
+
# canister. The scheme and port come from `host` alone: http on the port
|
|
21
|
+
# written there (80 if none) for localhost, *.localhost and loopback; https on
|
|
22
|
+
# 443 otherwise, since a public gateway listens there and nowhere else. A client's x-forwarded-* is passed
|
|
23
|
+
# on as HTTP_X_FORWARDED_* but never read, because the gateway's cache keys on
|
|
24
|
+
# authority, path and query only.
|
|
16
25
|
require_relative "ruby_wasm_ic_rack/version"
|
|
17
26
|
|
|
18
27
|
module RubyWasmIcRack
|
|
19
28
|
SAFE = %w[GET HEAD OPTIONS].freeze
|
|
20
29
|
UPGRADE = "x-ic-upgrade"
|
|
30
|
+
# No client address reaches a canister: a valid address, trusted by no proxy
|
|
31
|
+
# list, that reads as unknown.
|
|
32
|
+
REMOTE_ADDR = "0.0.0.0"
|
|
33
|
+
LOOPBACK = %w[localhost 127.0.0.1 [::1]].freeze
|
|
21
34
|
|
|
22
35
|
# rack.input over the body's bytes: what Rack 3 asks of an input stream.
|
|
23
36
|
class Input
|
|
@@ -62,27 +75,55 @@ module RubyWasmIcRack
|
|
|
62
75
|
|
|
63
76
|
def env(req)
|
|
64
77
|
path, query = req[:url].split("?", 2)
|
|
65
|
-
|
|
78
|
+
name, port = authority(header(req, "host").to_s)
|
|
79
|
+
local = local?(name)
|
|
80
|
+
scheme = local ? "http" : "https"
|
|
81
|
+
body = req[:body]
|
|
66
82
|
env = {
|
|
67
83
|
"REQUEST_METHOD" => req[:method],
|
|
68
84
|
"SCRIPT_NAME" => "",
|
|
69
85
|
"PATH_INFO" => path.to_s.empty? ? "/" : path,
|
|
70
86
|
"QUERY_STRING" => query.to_s,
|
|
71
|
-
"SERVER_NAME" =>
|
|
72
|
-
"SERVER_PORT" => "443",
|
|
87
|
+
"SERVER_NAME" => name,
|
|
88
|
+
"SERVER_PORT" => local ? port || "80" : "443",
|
|
73
89
|
"SERVER_PROTOCOL" => "HTTP/1.1",
|
|
74
|
-
"
|
|
75
|
-
"rack.
|
|
90
|
+
"REMOTE_ADDR" => REMOTE_ADDR,
|
|
91
|
+
"rack.url_scheme" => scheme,
|
|
92
|
+
"rack.input" => input(body),
|
|
76
93
|
"rack.errors" => $stderr
|
|
77
94
|
}
|
|
78
|
-
req[:headers].each do |
|
|
79
|
-
key =
|
|
80
|
-
key
|
|
95
|
+
req[:headers].each do |hname, value|
|
|
96
|
+
key = hname.upcase.tr("-", "_")
|
|
97
|
+
next if key == "CONTENT_LENGTH" # the body's bytes say, below
|
|
98
|
+
|
|
99
|
+
key = "HTTP_#{key}" unless key == "CONTENT_TYPE"
|
|
81
100
|
env[key] = env.key?(key) ? "#{env[key]}, #{value}" : value
|
|
82
101
|
end
|
|
102
|
+
env["CONTENT_LENGTH"] = body.bytesize.to_s unless body.empty?
|
|
83
103
|
env
|
|
84
104
|
end
|
|
85
105
|
|
|
106
|
+
# The name and the port (nil if none, or not digits) written in a `host`
|
|
107
|
+
# header. An IPv6 address keeps its brackets.
|
|
108
|
+
def authority(host)
|
|
109
|
+
if host.start_with?("[")
|
|
110
|
+
close = host.index("]") || (host.length - 1)
|
|
111
|
+
name = host[0..close]
|
|
112
|
+
rest = host[(close + 1)..].to_s
|
|
113
|
+
port = rest.start_with?(":") ? rest[1..] : nil
|
|
114
|
+
else
|
|
115
|
+
name, port = host.split(":", 2)
|
|
116
|
+
end
|
|
117
|
+
port = nil unless port && !port.empty? && port.each_char.all? { |c| c >= "0" && c <= "9" }
|
|
118
|
+
[name.to_s, port]
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# A gateway on this machine (PocketIC's, or a local network's) speaks http.
|
|
122
|
+
def local?(name)
|
|
123
|
+
name = name.downcase
|
|
124
|
+
LOOPBACK.include?(name) || name.end_with?(".localhost")
|
|
125
|
+
end
|
|
126
|
+
|
|
86
127
|
def header(req, name) = req[:headers].find { |n, _| n.downcase == name }&.last
|
|
87
128
|
|
|
88
129
|
# The gateway's response record from a Rack response. A header's value may
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ruby_wasm_ic_rack
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- graial
|
|
@@ -39,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
40
|
version: '0'
|
|
41
41
|
requirements: []
|
|
42
|
-
rubygems_version:
|
|
42
|
+
rubygems_version: 4.0.20
|
|
43
43
|
specification_version: 4
|
|
44
44
|
summary: Run rack and roda on the ruby.wasm build for IC!
|
|
45
45
|
test_files: []
|