dommy-rack 0.12.0 → 0.14.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/CHANGELOG.md +31 -0
- data/lib/dommy/rack/cookie_jar.rb +7 -8
- data/lib/dommy/rack/event_source_transport.rb +160 -0
- data/lib/dommy/rack/file_upload.rb +2 -18
- data/lib/dommy/rack/http_exchange.rb +2 -1
- data/lib/dommy/rack/navigation.rb +28 -36
- data/lib/dommy/rack/network_bridge.rb +9 -3
- data/lib/dommy/rack/request_builder.rb +43 -23
- data/lib/dommy/rack/resources.rb +7 -15
- data/lib/dommy/rack/session.rb +183 -89
- data/lib/dommy/rack/session_runtime.rb +59 -12
- data/lib/dommy/rack/subresource_policy.rb +71 -0
- data/lib/dommy/rack/trace.rb +1 -5
- data/lib/dommy/rack/url.rb +36 -44
- data/lib/dommy/rack/version.rb +1 -1
- data/lib/dommy/rack/visibility.rb +41 -29
- data/lib/dommy/rack/web_socket_transport.rb +22 -22
- data/lib/dommy/rack.rb +2 -0
- metadata +6 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 46d0531520bb02901c84d106d4432605c28e90f62b297a2e4e05a218e6b33b23
|
|
4
|
+
data.tar.gz: faefa539223853aaa9ed820f71d73f7eb921fdb3aafbd6ab8d7405069c421b32
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c241789310d1c709b026338f29252b6ccb199d78f75ea33621e3491c2fbf2e1ee994006d9a3b5fa0dc48be9d2797432f4b72b99e56c4a0d66844c9a1221d2a88
|
|
7
|
+
data.tar.gz: 8ad0d556089fd463bdf45c5d3fcf106406ef7ec533679d066d223f2e39451adb288a11453a94b30779ff82701c3f149f15c0b2d9d2d697d6b1305ffed1358e04
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,36 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.14.0 — 2026-10-01
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- A same-origin `new EventSource(url)` on a page connects to the Rack app in-process and delivers its `text/event-stream` response as `message` events. A cross-origin URL keeps the in-memory stub.
|
|
8
|
+
- `Session#execute_script(script, *args)` / `#evaluate_script(script, *args)` pass their arguments to the script, and raise when the JS runtime cannot.
|
|
9
|
+
- `NetworkBridge.install` takes `resources:`, `executor:` and `scheduler:`, and is the one way the session installs its fetch handler.
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- A form submission sends the body its `enctype` declares: a `text/plain` form sends plain text and a `multipart/form-data` form a multipart body even without a file, where both were sent urlencoded.
|
|
14
|
+
- URLs go through Dommy's WHATWG URL parser rather than stdlib `URI`. `Dommy::Rack::Url` is rebuilt on it and loses its `URI` helpers (`encode_iri`, `http_host`, `origin` and the rest).
|
|
15
|
+
- `Dommy::Rack.visible?` is the one public visibility check; `hidden_node?` and `hidden_by_closed_details?` are private.
|
|
16
|
+
|
|
17
|
+
### Fixed
|
|
18
|
+
|
|
19
|
+
- A GET form with no entries still ends its URL in `?`, as a browser's does.
|
|
20
|
+
- A multipart part's name or filename percent-encodes CR and LF instead of dropping them, and a filename with a null byte no longer raises.
|
|
21
|
+
|
|
22
|
+
## 0.13.0 — 2026-09-23
|
|
23
|
+
|
|
24
|
+
### Added
|
|
25
|
+
|
|
26
|
+
- `Dommy::Rack::Session.new(app, strict_js_errors: true)` fails on JavaScript the page left unhandled, at the next point the page was allowed to run, so the failure lands on the line that caused it. Off by default.
|
|
27
|
+
- `Session#allow_js_errors { ... }` suppresses that failure for a block.
|
|
28
|
+
|
|
29
|
+
### Changed
|
|
30
|
+
|
|
31
|
+
- `js_errors` holds only what the page left unhandled: an error it cancels in `window.onerror` or in an `unhandledrejection` listener never reaches the log.
|
|
32
|
+
- The errors live in a shared `Dommy::Js::ErrorLog`, so an error the outgoing page left unreported survives the clear that navigation performs.
|
|
33
|
+
|
|
3
34
|
## 0.12.0 — 2026-09-22
|
|
4
35
|
|
|
5
36
|
Versioned in lockstep with [`dommy`](https://github.com/takahashim/dommy) 0.12.0.
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "uri"
|
|
4
3
|
require "time"
|
|
5
4
|
|
|
6
5
|
module Dommy
|
|
@@ -26,7 +25,7 @@ module Dommy
|
|
|
26
25
|
|
|
27
26
|
# Parse a single Set-Cookie header value and store the result.
|
|
28
27
|
def store_from_header(set_cookie_string, request_url)
|
|
29
|
-
uri =
|
|
28
|
+
uri = Dommy::URL.new(request_url)
|
|
30
29
|
entry = parse_set_cookie(set_cookie_string, uri)
|
|
31
30
|
return unless entry
|
|
32
31
|
|
|
@@ -92,10 +91,10 @@ module Dommy
|
|
|
92
91
|
|
|
93
92
|
# Build the Cookie request header value for the given URL, or "".
|
|
94
93
|
def cookies_for(request_url)
|
|
95
|
-
uri =
|
|
96
|
-
secure_request = uri.
|
|
97
|
-
host = uri.
|
|
98
|
-
path = uri.
|
|
94
|
+
uri = Dommy::URL.new(request_url)
|
|
95
|
+
secure_request = uri.protocol == "https:"
|
|
96
|
+
host = uri.hostname.downcase
|
|
97
|
+
path = uri.pathname.empty? ? "/" : uri.pathname
|
|
99
98
|
|
|
100
99
|
matches = @mutex.synchronize do
|
|
101
100
|
@entries.reject { |e| expired?(e) }.select do |e|
|
|
@@ -132,7 +131,7 @@ module Dommy
|
|
|
132
131
|
return nil if name.empty?
|
|
133
132
|
|
|
134
133
|
attrs = parse_attributes(segments)
|
|
135
|
-
request_host = request_uri.
|
|
134
|
+
request_host = request_uri.hostname.downcase
|
|
136
135
|
|
|
137
136
|
domain = attrs["domain"]
|
|
138
137
|
host_only = domain.nil? || domain.empty?
|
|
@@ -169,7 +168,7 @@ module Dommy
|
|
|
169
168
|
|
|
170
169
|
# RFC 6265 default-path: directory portion of the request path.
|
|
171
170
|
def default_path(uri)
|
|
172
|
-
path = uri.
|
|
171
|
+
path = uri.pathname
|
|
173
172
|
return "/" if path.empty? || !path.start_with?("/")
|
|
174
173
|
|
|
175
174
|
idx = path.rindex("/")
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "stringio"
|
|
4
|
+
|
|
5
|
+
module Dommy
|
|
6
|
+
module Rack
|
|
7
|
+
# In-process Server-Sent Events transport: connects a page's
|
|
8
|
+
# `new EventSource(url)` to the Rack app ITSELF, the same way fetch/XHR and
|
|
9
|
+
# the WebSocket transport resolve through the app. The stream is a normal
|
|
10
|
+
# same-origin GET whose (possibly streaming) response body is read on a
|
|
11
|
+
# reader thread and parsed as `text/event-stream`; every dispatched event is
|
|
12
|
+
# marshalled onto the page thread via `scheduler.post_external`, where it
|
|
13
|
+
# fires the EventSource's open / message / error events.
|
|
14
|
+
#
|
|
15
|
+
# Lifetime: a transport belongs to the page (realm) that opened it; the
|
|
16
|
+
# session closes all live transports on dispose. Unlike WebSockets, SSE
|
|
17
|
+
# has no client-to-server channel, so EventSource#close is the only write.
|
|
18
|
+
class EventSourceTransport
|
|
19
|
+
# Resolve `url` for the connector: an absolute http(s) URL that is
|
|
20
|
+
# same-origin with `base`, or nil (the EventSource then falls back to the
|
|
21
|
+
# in-memory stub).
|
|
22
|
+
def self.rack_target(url, base:)
|
|
23
|
+
target = Dommy::URL.parse(url.to_s, base.to_s)
|
|
24
|
+
return nil unless target
|
|
25
|
+
return nil unless %w[http: https:].include?(target.protocol)
|
|
26
|
+
|
|
27
|
+
b = Dommy::URL.parse(base.to_s)
|
|
28
|
+
return nil unless b && b.hostname == target.hostname && b.port == target.port
|
|
29
|
+
|
|
30
|
+
target
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def initialize(app:, es:, scheduler:, url:, origin:, cookie_string: "")
|
|
34
|
+
@app = app
|
|
35
|
+
@es = es
|
|
36
|
+
@scheduler = scheduler
|
|
37
|
+
@url = url
|
|
38
|
+
@origin = origin
|
|
39
|
+
@cookie_string = cookie_string
|
|
40
|
+
@closed = false
|
|
41
|
+
@buffer = +""
|
|
42
|
+
@data = nil
|
|
43
|
+
@event = nil
|
|
44
|
+
@id = nil
|
|
45
|
+
|
|
46
|
+
@reader = Thread.new { run }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Called from the page thread when the EventSource is closed: stop the
|
|
50
|
+
# reader and release the response body.
|
|
51
|
+
def close
|
|
52
|
+
@closed = true
|
|
53
|
+
close_body
|
|
54
|
+
nil
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Hard teardown (session dispose): drop the stream; the reader exits on
|
|
58
|
+
# the closed body or at the next chunk.
|
|
59
|
+
def dispose
|
|
60
|
+
close
|
|
61
|
+
@reader&.join(1)
|
|
62
|
+
rescue IOError
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
def run
|
|
69
|
+
status, _headers, body = @app.call(env)
|
|
70
|
+
@body = body
|
|
71
|
+
if status >= 400
|
|
72
|
+
post { @es.__transport_error__ }
|
|
73
|
+
return
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
post { @es.__transport_open__ }
|
|
77
|
+
body.each { |chunk| feed(chunk) unless @closed }
|
|
78
|
+
post { @es.__transport_closed__ } unless @closed
|
|
79
|
+
rescue IOError, Errno::EPIPE, Errno::ECONNRESET
|
|
80
|
+
nil
|
|
81
|
+
rescue StandardError
|
|
82
|
+
post { @es.__transport_error__ } unless @closed
|
|
83
|
+
ensure
|
|
84
|
+
close_body
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def env
|
|
88
|
+
url = @url
|
|
89
|
+
env = {
|
|
90
|
+
"REQUEST_METHOD" => "GET",
|
|
91
|
+
"SCRIPT_NAME" => "",
|
|
92
|
+
"PATH_INFO" => url.pathname.empty? ? "/" : url.pathname,
|
|
93
|
+
"QUERY_STRING" => url.search.delete_prefix("?"),
|
|
94
|
+
"SERVER_NAME" => url.hostname,
|
|
95
|
+
"SERVER_PORT" => Url.server_port(url),
|
|
96
|
+
"HTTP_HOST" => url.host,
|
|
97
|
+
"HTTP_ACCEPT" => "text/event-stream",
|
|
98
|
+
"HTTP_ORIGIN" => @origin,
|
|
99
|
+
"REMOTE_ADDR" => "127.0.0.1",
|
|
100
|
+
"rack.url_scheme" => url.protocol.delete_suffix(":"),
|
|
101
|
+
"rack.input" => StringIO.new(""),
|
|
102
|
+
"rack.errors" => $stderr,
|
|
103
|
+
"rack.multithread" => true,
|
|
104
|
+
"rack.multiprocess" => false,
|
|
105
|
+
"rack.run_once" => false,
|
|
106
|
+
}
|
|
107
|
+
env["HTTP_COOKIE"] = @cookie_string unless @cookie_string.to_s.empty?
|
|
108
|
+
env
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# --- SSE parsing (the "event stream interpretation" algorithm) ---
|
|
112
|
+
|
|
113
|
+
def feed(chunk)
|
|
114
|
+
@buffer << chunk.to_s
|
|
115
|
+
while (newline = @buffer.index("\n"))
|
|
116
|
+
line = @buffer.slice!(0, newline + 1).chomp("\n").chomp("\r")
|
|
117
|
+
if line.empty?
|
|
118
|
+
dispatch_event
|
|
119
|
+
elsif !line.start_with?(":")
|
|
120
|
+
field, value = line.split(":", 2)
|
|
121
|
+
store_field(field, value&.sub(/\A /, ""))
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def store_field(field, value)
|
|
127
|
+
case field
|
|
128
|
+
when "data" then (@data ||= []) << value.to_s
|
|
129
|
+
when "event" then @event = value
|
|
130
|
+
when "id" then @id = value unless value.to_s.include?("\0")
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# A blank line dispatches the buffered block, if it carried data; an
|
|
135
|
+
# id-only / event-only block updates state without firing a message.
|
|
136
|
+
# The values are snapshotted into locals because the post runs later,
|
|
137
|
+
# after the block's own state has been reset (event is per-block).
|
|
138
|
+
def dispatch_event
|
|
139
|
+
return unless @data
|
|
140
|
+
|
|
141
|
+
data = @data.join("\n")
|
|
142
|
+
event = @event || "message"
|
|
143
|
+
id = @id
|
|
144
|
+
@data = nil
|
|
145
|
+
@event = nil
|
|
146
|
+
post { @es.__transport_message__(data, event: event, id: id) }
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def close_body
|
|
150
|
+
@body&.close if @body.respond_to?(:close)
|
|
151
|
+
rescue IOError, Errno::EPIPE
|
|
152
|
+
nil
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def post(&block)
|
|
156
|
+
@scheduler.post_external(&block)
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
@@ -9,26 +9,10 @@ module Dommy
|
|
|
9
9
|
# values become text parts. Owns the multipart serialization so the HTTP
|
|
10
10
|
# layer (not Dommy::FormData) is responsible for it.
|
|
11
11
|
module FileUpload
|
|
12
|
-
MIME_TYPES = {
|
|
13
|
-
".txt" => "text/plain",
|
|
14
|
-
".html" => "text/html",
|
|
15
|
-
".htm" => "text/html",
|
|
16
|
-
".json" => "application/json",
|
|
17
|
-
".csv" => "text/csv",
|
|
18
|
-
".xml" => "application/xml",
|
|
19
|
-
".png" => "image/png",
|
|
20
|
-
".jpg" => "image/jpeg",
|
|
21
|
-
".jpeg" => "image/jpeg",
|
|
22
|
-
".gif" => "image/gif",
|
|
23
|
-
".pdf" => "application/pdf"
|
|
24
|
-
}.freeze
|
|
25
|
-
|
|
26
12
|
module_function
|
|
27
13
|
|
|
28
14
|
# Guess a MIME type from a file path's extension.
|
|
29
|
-
def mime_type_for(path)
|
|
30
|
-
MIME_TYPES.fetch(::File.extname(path).downcase, "application/octet-stream")
|
|
31
|
-
end
|
|
15
|
+
def mime_type_for(path) = Dommy::Interaction::MimeTypes.for(path)
|
|
32
16
|
|
|
33
17
|
# True when any pair value is a File/Blob.
|
|
34
18
|
def multipart?(pairs)
|
|
@@ -66,7 +50,7 @@ module Dommy
|
|
|
66
50
|
end
|
|
67
51
|
|
|
68
52
|
def escape(str)
|
|
69
|
-
str.to_s.gsub('"', "%22").gsub(
|
|
53
|
+
str.to_s.gsub('"', "%22").gsub("\r", "%0D").gsub("\n", "%0A")
|
|
70
54
|
end
|
|
71
55
|
end
|
|
72
56
|
end
|
|
@@ -46,12 +46,13 @@ module Dommy
|
|
|
46
46
|
|
|
47
47
|
# Perform one request and return its Response. `headers` are per-request
|
|
48
48
|
# overrides merged over the persistent header source.
|
|
49
|
-
def request(method, absolute_url, params: nil, body: nil, headers: {})
|
|
49
|
+
def request(method, absolute_url, params: nil, body: nil, enctype: nil, headers: {})
|
|
50
50
|
env = RequestBuilder.new(@config).build(
|
|
51
51
|
method: method,
|
|
52
52
|
url: absolute_url,
|
|
53
53
|
params: params,
|
|
54
54
|
body: body,
|
|
55
|
+
enctype: enctype,
|
|
55
56
|
headers: @headers.merge(headers),
|
|
56
57
|
cookie_string: @cookie_jar.cookies_for(absolute_url)
|
|
57
58
|
)
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "uri"
|
|
4
|
-
|
|
5
3
|
module Dommy
|
|
6
4
|
module Rack
|
|
7
5
|
# URL resolution, redirect following, same-origin enforcement, and
|
|
@@ -21,36 +19,36 @@ module Dommy
|
|
|
21
19
|
# Resolve a possibly-relative URL against a base (current URL or host).
|
|
22
20
|
def resolve_url(url_or_path, base_url)
|
|
23
21
|
base = base_url || @config.default_host
|
|
24
|
-
# A link/redirect target may carry raw UTF-8 (e.g. /hashtag/応援)
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
# that
|
|
28
|
-
|
|
29
|
-
rescue URI::InvalidURIError
|
|
30
|
-
url_or_path.to_s
|
|
22
|
+
# A link/redirect target may carry raw UTF-8 (e.g. /hashtag/応援) —
|
|
23
|
+
# Dommy::URL (a WHATWG parser, unlike stdlib URI) percent-encodes it
|
|
24
|
+
# like a browser would rather than rejecting it, so a genuinely
|
|
25
|
+
# malformed target is the only thing that falls through to nil here.
|
|
26
|
+
Url.resolve(base, url_or_path) || url_or_path.to_s
|
|
31
27
|
end
|
|
32
28
|
|
|
33
29
|
# Merge ordered [name, value] params into `url`'s query, preserving any
|
|
34
30
|
# existing query and keeping a fragment last (browser address-bar form).
|
|
31
|
+
# A GET form submission always has a (possibly empty) query part, so an
|
|
32
|
+
# empty entry list still leaves a trailing "?".
|
|
35
33
|
def append_query(url, params)
|
|
36
|
-
encoded =
|
|
37
|
-
return url if encoded.empty?
|
|
38
|
-
|
|
34
|
+
encoded = Dommy::URLSearchParams.new(params).to_s
|
|
39
35
|
base, hash, fragment = url.to_s.partition("#")
|
|
40
|
-
sep = base.include?("?") ? "&" : "?"
|
|
36
|
+
sep = base.include?("?") ? (encoded.empty? ? "" : "&") : "?"
|
|
37
|
+
return url if encoded.empty? && sep.empty?
|
|
38
|
+
|
|
41
39
|
"#{base}#{sep}#{encoded}#{hash}#{fragment}"
|
|
42
40
|
end
|
|
43
41
|
|
|
44
42
|
def check_same_origin!(url)
|
|
45
43
|
return unless @config.enforce_same_origin
|
|
46
|
-
return if same_origin?(url, @config.default_host)
|
|
44
|
+
return if Url.same_origin?(url, @config.default_host)
|
|
47
45
|
|
|
48
46
|
raise CrossOriginError, "cross-origin request to #{url} is not allowed"
|
|
49
47
|
end
|
|
50
48
|
|
|
51
49
|
# Perform a navigation, following redirects per session policy, then
|
|
52
50
|
# apply the final response to the session (updating document + history).
|
|
53
|
-
def navigate(method:, url:, params: nil, body: nil, headers: {}, replace: false)
|
|
51
|
+
def navigate(method:, url:, params: nil, body: nil, enctype: nil, headers: {}, replace: false)
|
|
54
52
|
return navigate_about(url.to_s) if url.to_s.start_with?("about:")
|
|
55
53
|
|
|
56
54
|
verb = method.to_s.upcase
|
|
@@ -66,7 +64,7 @@ module Dommy
|
|
|
66
64
|
end
|
|
67
65
|
check_same_origin!(target)
|
|
68
66
|
|
|
69
|
-
response, final_url = run(method: verb, url: target, params: params, body: body, headers: headers)
|
|
67
|
+
response, final_url = run(method: verb, url: target, params: params, body: body, enctype: enctype, headers: headers)
|
|
70
68
|
# replace: a location.replace() / reload() / redirect updates the current
|
|
71
69
|
# history entry in place rather than pushing a new one.
|
|
72
70
|
@session.apply_navigation_response(response, final_url, replace: replace)
|
|
@@ -75,11 +73,11 @@ module Dommy
|
|
|
75
73
|
|
|
76
74
|
# Fetch-style request: resolves and enforces origin, runs the redirect
|
|
77
75
|
# loop per mode, and returns the Response without touching session state.
|
|
78
|
-
def fetch(url, method: "GET", params: nil, body: nil, headers: {}, redirect: :follow)
|
|
76
|
+
def fetch(url, method: "GET", params: nil, body: nil, enctype: nil, headers: {}, redirect: :follow)
|
|
79
77
|
verb = method.to_s.upcase
|
|
80
78
|
target = resolve_url(url, @session.current_url)
|
|
81
79
|
check_same_origin!(target)
|
|
82
|
-
run_fetch(verb, target, params: params, body: body, headers: headers, redirect: redirect)
|
|
80
|
+
run_fetch(verb, target, params: params, body: body, enctype: enctype, headers: headers, redirect: redirect)
|
|
83
81
|
end
|
|
84
82
|
|
|
85
83
|
# Worker-safe variant of #fetch: `target` is already absolute and origin-
|
|
@@ -87,8 +85,8 @@ module Dommy
|
|
|
87
85
|
# in the redirect loop is issued through `exchange` (which touches only
|
|
88
86
|
# thread-safe state) instead of the session. Returns the Response. This is
|
|
89
87
|
# the primitive a network worker runs for the async-network path.
|
|
90
|
-
def fetch_resolved(exchange, method, target, params: nil, body: nil, headers: {}, redirect: :follow)
|
|
91
|
-
run_fetch(method.to_s.upcase, target, params: params, body: body, headers: headers,
|
|
88
|
+
def fetch_resolved(exchange, method, target, params: nil, body: nil, enctype: nil, headers: {}, redirect: :follow)
|
|
89
|
+
run_fetch(method.to_s.upcase, target, params: params, body: body, enctype: enctype, headers: headers,
|
|
92
90
|
redirect: redirect, exchange: exchange)
|
|
93
91
|
end
|
|
94
92
|
|
|
@@ -102,7 +100,7 @@ module Dommy
|
|
|
102
100
|
|
|
103
101
|
# Run the request/redirect loop. Returns [response, final_url].
|
|
104
102
|
# Public so Session#fetch can reuse it without applying navigation state.
|
|
105
|
-
def run(method:, url:, params: nil, body: nil, headers: {}, follow: true, exchange: nil)
|
|
103
|
+
def run(method:, url:, params: nil, body: nil, enctype: nil, headers: {}, follow: true, exchange: nil)
|
|
106
104
|
verb = method
|
|
107
105
|
target = url
|
|
108
106
|
# Carry the fragment across redirects: a redirect Location without its
|
|
@@ -116,9 +114,9 @@ module Dommy
|
|
|
116
114
|
# `exchange` is injected, the very same loop runs on a network worker.
|
|
117
115
|
response =
|
|
118
116
|
if exchange
|
|
119
|
-
exchange.request(verb, target, params: params, body: body, headers: headers)
|
|
117
|
+
exchange.request(verb, target, params: params, body: body, enctype: enctype, headers: headers)
|
|
120
118
|
else
|
|
121
|
-
@session.raw_request(verb, target, params: params, body: body, headers: headers)
|
|
119
|
+
@session.raw_request(verb, target, params: params, body: body, enctype: enctype, headers: headers)
|
|
122
120
|
end
|
|
123
121
|
|
|
124
122
|
unless follow && redirect_to_follow?(response)
|
|
@@ -150,8 +148,8 @@ module Dommy
|
|
|
150
148
|
# Run the redirect loop per fetch `redirect` mode and return the Response.
|
|
151
149
|
# Shared by the page-thread #fetch and the worker-safe #fetch_resolved
|
|
152
150
|
# (which passes an `exchange`); the only difference is where requests issue.
|
|
153
|
-
def run_fetch(verb, target, params:, body:, headers:, redirect:, exchange: nil)
|
|
154
|
-
args = {method: verb, url: target, params: params, body: body, headers: headers, exchange: exchange}
|
|
151
|
+
def run_fetch(verb, target, params:, body:, enctype:, headers:, redirect:, exchange: nil)
|
|
152
|
+
args = {method: verb, url: target, params: params, body: body, enctype: enctype, headers: headers, exchange: exchange}
|
|
155
153
|
case redirect
|
|
156
154
|
when :follow
|
|
157
155
|
run(**args, follow: true).first
|
|
@@ -199,10 +197,12 @@ module Dommy
|
|
|
199
197
|
maybe_follow_meta_refresh(next_response, depth + 1) || next_response
|
|
200
198
|
end
|
|
201
199
|
|
|
200
|
+
# The bare fragment (no leading "#"), or "" for none/empty, or nil if
|
|
201
|
+
# `url` doesn't parse. Callers already fold nil and "" together, so
|
|
202
|
+
# Dommy::URL#hash never distinguishing "no fragment" from "empty
|
|
203
|
+
# fragment" (both "") doesn't change behavior here.
|
|
202
204
|
def uri_fragment(url)
|
|
203
|
-
|
|
204
|
-
rescue URI::InvalidURIError
|
|
205
|
-
nil
|
|
205
|
+
Dommy::URL.parse(url.to_s)&.hash&.delete_prefix("#")
|
|
206
206
|
end
|
|
207
207
|
|
|
208
208
|
def with_fragment(url, fragment)
|
|
@@ -225,14 +225,6 @@ module Dommy
|
|
|
225
225
|
else original # 307, 308 keep the method
|
|
226
226
|
end
|
|
227
227
|
end
|
|
228
|
-
|
|
229
|
-
def same_origin?(url_a, url_b)
|
|
230
|
-
a = URI.parse(url_a)
|
|
231
|
-
b = URI.parse(url_b)
|
|
232
|
-
a.scheme == b.scheme && a.host == b.host && a.port == b.port
|
|
233
|
-
rescue URI::InvalidURIError
|
|
234
|
-
false
|
|
235
|
-
end
|
|
236
228
|
end
|
|
237
229
|
end
|
|
238
230
|
end
|
|
@@ -14,9 +14,15 @@ module Dommy
|
|
|
14
14
|
# `Dommy::Resources::FetchHandler` backed by `Dommy::Rack::Resources`, so
|
|
15
15
|
# `fetch` / XHR and `<script src>` loads resolve through the same adapter.
|
|
16
16
|
module NetworkBridge
|
|
17
|
-
# Wire a bridge for `session` into `window`.
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
# Wire a bridge for `session` into `window`. `resources` defaults to a
|
|
18
|
+
# fresh Dommy::Rack::Resources for the session; pass one in when the
|
|
19
|
+
# caller needs to keep using it afterward (SessionRuntime does, for
|
|
20
|
+
# script prefetch/boot). `executor:`/`scheduler:` opt the handler into
|
|
21
|
+
# off-thread fetch/XHR resolution (see Dommy::Resources::FetchHandler);
|
|
22
|
+
# nil (the default) keeps it synchronous. Returns the fetch handler.
|
|
23
|
+
def self.install(session, window, resources: nil, executor: nil, scheduler: nil)
|
|
24
|
+
resources ||= Resources.new(session)
|
|
25
|
+
handler = Dommy::Resources::FetchHandler.new(resources, executor: executor, scheduler: scheduler)
|
|
20
26
|
window.globals["__fetch_handler__"] = handler
|
|
21
27
|
handler
|
|
22
28
|
end
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "uri"
|
|
4
3
|
require "stringio"
|
|
5
4
|
require "rack/utils"
|
|
6
5
|
|
|
@@ -10,21 +9,23 @@ module Dommy
|
|
|
10
9
|
# Stateless aside from the session config it reads defaults from.
|
|
11
10
|
class RequestBuilder
|
|
12
11
|
FORM_URLENCODED = "application/x-www-form-urlencoded"
|
|
12
|
+
MULTIPART = "multipart/form-data"
|
|
13
|
+
TEXT_PLAIN = "text/plain"
|
|
13
14
|
BODY_METHODS = %w[POST PUT PATCH DELETE].freeze
|
|
14
15
|
|
|
15
16
|
def initialize(config)
|
|
16
17
|
@config = config
|
|
17
18
|
end
|
|
18
19
|
|
|
19
|
-
def build(method:, url:, headers: {}, body: nil, params: nil, cookie_string: "")
|
|
20
|
+
def build(method:, url:, headers: {}, body: nil, params: nil, enctype: nil, cookie_string: "")
|
|
20
21
|
raise ArgumentError, "pass either :params or :body, not both" if params && body
|
|
21
22
|
|
|
22
23
|
verb = method.to_s.upcase
|
|
23
|
-
uri =
|
|
24
|
+
uri = Dommy::URL.new(url)
|
|
24
25
|
env_headers = normalize_headers(headers)
|
|
25
26
|
|
|
26
|
-
body_string, query_extra, content_type = encode_payload(verb, params, body, env_headers)
|
|
27
|
-
query = merge_query(uri.
|
|
27
|
+
body_string, query_extra, content_type = encode_payload(verb, params, body, env_headers, enctype)
|
|
28
|
+
query = merge_query(uri.search.delete_prefix("?"), query_extra)
|
|
28
29
|
|
|
29
30
|
env = base_env(verb, uri, query, body_string)
|
|
30
31
|
apply_default_headers(env, env_headers, cookie_string)
|
|
@@ -39,14 +40,14 @@ module Dommy
|
|
|
39
40
|
{
|
|
40
41
|
"REQUEST_METHOD" => verb,
|
|
41
42
|
"SCRIPT_NAME" => "",
|
|
42
|
-
"PATH_INFO" => uri.
|
|
43
|
+
"PATH_INFO" => uri.pathname.empty? ? "/" : uri.pathname,
|
|
43
44
|
"QUERY_STRING" => query,
|
|
44
|
-
"SERVER_NAME" => uri.
|
|
45
|
-
"SERVER_PORT" => uri
|
|
45
|
+
"SERVER_NAME" => uri.hostname,
|
|
46
|
+
"SERVER_PORT" => Url.server_port(uri),
|
|
46
47
|
"SERVER_PROTOCOL" => "HTTP/1.1",
|
|
47
|
-
"HTTP_HOST" =>
|
|
48
|
+
"HTTP_HOST" => uri.host,
|
|
48
49
|
"CONTENT_LENGTH" => body_string.bytesize.to_s,
|
|
49
|
-
"rack.url_scheme" => uri.
|
|
50
|
+
"rack.url_scheme" => uri.protocol.delete_suffix(":"),
|
|
50
51
|
"rack.input" => StringIO.new(body_string),
|
|
51
52
|
"rack.errors" => $stderr
|
|
52
53
|
}
|
|
@@ -59,17 +60,14 @@ module Dommy
|
|
|
59
60
|
end
|
|
60
61
|
|
|
61
62
|
# Returns [body_string, query_extra, content_type]. For GET-style verbs,
|
|
62
|
-
# params go into the query string and the body stays empty.
|
|
63
|
-
|
|
63
|
+
# params go into the query string and the body stays empty. For body verbs
|
|
64
|
+
# the declared form `enctype` picks the serialization; with none declared
|
|
65
|
+
# (a programmatic post) a File/Blob forces multipart, else urlencoded.
|
|
66
|
+
def encode_payload(verb, params, body, env_headers, enctype)
|
|
64
67
|
if params
|
|
65
68
|
pairs = to_pairs(params)
|
|
66
69
|
if BODY_METHODS.include?(verb)
|
|
67
|
-
|
|
68
|
-
multipart_body, content_type = FileUpload.encode(pairs)
|
|
69
|
-
[multipart_body, nil, content_type]
|
|
70
|
-
else
|
|
71
|
-
[encode_query(pairs), nil, env_headers["CONTENT_TYPE"] || FORM_URLENCODED]
|
|
72
|
-
end
|
|
70
|
+
encode_form_body(pairs, enctype, env_headers)
|
|
73
71
|
else
|
|
74
72
|
["", encode_query(pairs), nil]
|
|
75
73
|
end
|
|
@@ -82,6 +80,33 @@ module Dommy
|
|
|
82
80
|
end
|
|
83
81
|
end
|
|
84
82
|
|
|
83
|
+
def encode_form_body(pairs, enctype, env_headers)
|
|
84
|
+
case effective_enctype(pairs, enctype)
|
|
85
|
+
when MULTIPART
|
|
86
|
+
multipart_body, content_type = FileUpload.encode(pairs)
|
|
87
|
+
[multipart_body, nil, content_type]
|
|
88
|
+
when TEXT_PLAIN
|
|
89
|
+
[plain_text_body(pairs), nil, "#{TEXT_PLAIN};charset=UTF-8"]
|
|
90
|
+
else
|
|
91
|
+
[encode_query(pairs), nil, env_headers["CONTENT_TYPE"] || FORM_URLENCODED]
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# A form that declares an enctype uses it verbatim. A programmatic
|
|
96
|
+
# request with no declaration is urlencoded unless a File value forces
|
|
97
|
+
# multipart (the historical inference).
|
|
98
|
+
def effective_enctype(pairs, enctype)
|
|
99
|
+
declared = enctype.to_s.downcase
|
|
100
|
+
return declared if [FORM_URLENCODED, MULTIPART, TEXT_PLAIN].include?(declared)
|
|
101
|
+
|
|
102
|
+
FileUpload.multipart?(pairs) ? MULTIPART : FORM_URLENCODED
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# WHATWG plain-text form data: `name=value` per entry, CRLF-terminated.
|
|
106
|
+
def plain_text_body(pairs)
|
|
107
|
+
pairs.map { |name, value| "#{name}=#{scalar(value)}" }.join("\r\n") + "\r\n"
|
|
108
|
+
end
|
|
109
|
+
|
|
85
110
|
def merge_query(existing, extra)
|
|
86
111
|
[existing, extra].compact.reject(&:empty?).join("&")
|
|
87
112
|
end
|
|
@@ -124,11 +149,6 @@ module Dommy
|
|
|
124
149
|
else "HTTP_#{key.upcase.tr("-", "_")}"
|
|
125
150
|
end
|
|
126
151
|
end
|
|
127
|
-
|
|
128
|
-
def host_header(uri)
|
|
129
|
-
default = uri.scheme == "https" ? 443 : 80
|
|
130
|
-
uri.port && uri.port != default ? "#{uri.host}:#{uri.port}" : uri.host.to_s
|
|
131
|
-
end
|
|
132
152
|
end
|
|
133
153
|
end
|
|
134
154
|
end
|
data/lib/dommy/rack/resources.rb
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "uri"
|
|
4
|
-
|
|
5
3
|
module Dommy
|
|
6
4
|
module Rack
|
|
7
5
|
# `Dommy::Resources` adapter backed by a Rack session: serves same-origin
|
|
@@ -155,19 +153,11 @@ module Dommy
|
|
|
155
153
|
end
|
|
156
154
|
|
|
157
155
|
def absolute_url(url)
|
|
158
|
-
|
|
159
|
-
# src has non-ASCII path) so the ASCII-only parser accepts it.
|
|
160
|
-
URI.join(base_url, Url.encode_iri(url)).to_s
|
|
161
|
-
rescue URI::InvalidURIError
|
|
162
|
-
nil
|
|
156
|
+
Url.resolve(base_url, url)
|
|
163
157
|
end
|
|
164
158
|
|
|
165
159
|
def same_origin?(target)
|
|
166
|
-
|
|
167
|
-
b = URI.parse(base_url)
|
|
168
|
-
t.scheme == b.scheme && t.host == b.host && t.port == b.port
|
|
169
|
-
rescue URI::InvalidURIError
|
|
170
|
-
false
|
|
160
|
+
Url.same_origin?(target, base_url)
|
|
171
161
|
end
|
|
172
162
|
|
|
173
163
|
def allowed_cross_origin?(target)
|
|
@@ -185,10 +175,12 @@ module Dommy
|
|
|
185
175
|
@session.__internal_record_dropped_subresource(host) if host
|
|
186
176
|
end
|
|
187
177
|
|
|
178
|
+
# nil (not "") when `target` parses but has no host — Dommy::URL#hostname
|
|
179
|
+
# returns "" rather than nil for a host-less (e.g. opaque-path) URL, and
|
|
180
|
+
# every caller here treats "no host" as "skip this".
|
|
188
181
|
def host_of(target)
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
nil
|
|
182
|
+
host = Dommy::URL.parse(target)&.hostname
|
|
183
|
+
host unless host.nil? || host.empty?
|
|
192
184
|
end
|
|
193
185
|
end
|
|
194
186
|
end
|