Skip to content

Commit acf43c7

Browse files
DanTupCommit Queue
authored andcommitted
[dds] Build DevTools URL using querystring, not fragment
DevTools now uses page URLs so we don't need to put the querystring as a fragment. The original code did work, but it complicates things for other code (such as code injecting `?wasm=true`) so should be updated. Fixes Dart-Code/Dart-Code#5365 Change-Id: I80ff4d78a3df1f77740a9b8f61ef5499bd008eda Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/399681 Commit-Queue: Ben Konyi <[email protected]> Reviewed-by: Kenzie Davisson <[email protected]> Reviewed-by: Ben Konyi <[email protected]>
1 parent d3ecb22 commit acf43c7

File tree

2 files changed

+84
-14
lines changed

2 files changed

+84
-14
lines changed

pkg/dds/lib/devtools_server.dart

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,11 @@ class DevToolsServer {
647647
uriParams['uri'] = vmServiceUri.toString();
648648

649649
final devToolsUri = Uri.parse(devToolsUrl);
650-
final uriToLaunch = _buildUriToLaunch(uriParams, page, devToolsUri);
650+
final uriToLaunch = buildUriToLaunch(
651+
devToolsUri,
652+
page,
653+
uriParams,
654+
);
651655

652656
// TODO(dantup): When ChromeOS has support for tunneling all ports we can
653657
// change this to always use the native browser for ChromeOS and may wish to
@@ -746,23 +750,22 @@ class DevToolsServer {
746750
return false;
747751
}
748752

749-
String _buildUriToLaunch(
750-
Map<String, dynamic> uriParams,
751-
String? page,
753+
static String buildUriToLaunch(
752754
Uri devToolsUri,
755+
String? page,
756+
Map<String, dynamic>? params,
753757
) {
754-
final queryStringNameValues = [];
755-
uriParams.forEach((key, value) => queryStringNameValues.add(
756-
'${Uri.encodeQueryComponent(key)}=${Uri.encodeQueryComponent(value)}'));
757-
758-
if (page != null) {
759-
queryStringNameValues.add('page=${Uri.encodeQueryComponent(page)}');
760-
}
761-
758+
page ??= '';
759+
var pathSep = devToolsUri.path.endsWith('/') ? '' : '/';
760+
var newPath = '${devToolsUri.path}$pathSep$page';
761+
var newParams = {
762+
...devToolsUri.queryParameters,
763+
...?params,
764+
};
762765
return devToolsUri
763766
.replace(
764-
path: devToolsUri.path.isEmpty ? '/' : devToolsUri.path,
765-
fragment: '?${queryStringNameValues.join('&')}')
767+
path: newPath,
768+
queryParameters: newParams.isNotEmpty ? newParams : null)
766769
.toString();
767770
}
768771

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:dds/devtools_server.dart';
6+
import 'package:test/test.dart';
7+
8+
void main() {
9+
group('DevToolsServer.buildUriToLaunch', () {
10+
var build = DevToolsServer.buildUriToLaunch;
11+
12+
test('with no trailing slash', () async {
13+
var base = Uri.parse('http://localhost:1235');
14+
expect(build(base, null, {}), 'http://localhost:1235/');
15+
expect(build(base, 'inspector', {}), 'http://localhost:1235/inspector');
16+
expect(build(base, null, {'a': 'a'}), 'http://localhost:1235/?a=a');
17+
expect(build(base, 'inspector', {'a': 'a'}),
18+
'http://localhost:1235/inspector?a=a');
19+
});
20+
21+
test('with trailing slash', () async {
22+
var base = Uri.parse('http://localhost:1235/');
23+
24+
expect(build(base, null, {}), 'http://localhost:1235/');
25+
expect(build(base, 'inspector', {}), 'http://localhost:1235/inspector');
26+
expect(build(base, null, {'a': 'a'}), 'http://localhost:1235/?a=a');
27+
expect(build(base, 'inspector', {'a': 'a'}),
28+
'http://localhost:1235/inspector?a=a');
29+
});
30+
31+
test('with folder and no trailing slash', () async {
32+
var base = Uri.parse('http://localhost:1235/devtools');
33+
expect(build(base, null, {}), 'http://localhost:1235/devtools/');
34+
expect(build(base, 'inspector', {}),
35+
'http://localhost:1235/devtools/inspector');
36+
expect(
37+
build(base, null, {'a': 'a'}), 'http://localhost:1235/devtools/?a=a');
38+
expect(build(base, 'inspector', {'a': 'a'}),
39+
'http://localhost:1235/devtools/inspector?a=a');
40+
});
41+
42+
test('with folder and trailing slash', () async {
43+
var base = Uri.parse('http://localhost:1235/devtools/');
44+
45+
expect(build(base, null, {}), 'http://localhost:1235/devtools/');
46+
expect(build(base, 'inspector', {}),
47+
'http://localhost:1235/devtools/inspector');
48+
expect(
49+
build(base, null, {'a': 'a'}), 'http://localhost:1235/devtools/?a=a');
50+
expect(build(base, 'inspector', {'a': 'a'}),
51+
'http://localhost:1235/devtools/inspector?a=a');
52+
});
53+
54+
test('with existing query params', () async {
55+
var base = Uri.parse('http://localhost:1235/devtools/?a=orig&b=b');
56+
57+
expect(
58+
build(base, null, {}), 'http://localhost:1235/devtools/?a=orig&b=b');
59+
expect(build(base, 'inspector', {}),
60+
'http://localhost:1235/devtools/inspector?a=orig&b=b');
61+
expect(build(base, null, {'a': 'a', 'c': 'c'}),
62+
'http://localhost:1235/devtools/?a=a&b=b&c=c');
63+
expect(build(base, 'inspector', {'a': 'a', 'c': 'c'}),
64+
'http://localhost:1235/devtools/inspector?a=a&b=b&c=c');
65+
});
66+
});
67+
}

0 commit comments

Comments
 (0)