Skip to content

Commit 7b48b11

Browse files
committed
Normalize http resourced paths before passing it further to the app
After deploying the new flex version of pub.dartlang.org travis builds suddendly started to fail. It seems that travis is using a special PUB_HOSTED_URL with a slash at the end, which causes the travis builds's pub to make requests like: GET //api/packages/http [email protected] Review-Url: https://codereview.chromium.org/2754713003 .
1 parent 98fb556 commit 7b48b11

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

app/bin/server.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:gcloud/db.dart';
77
import 'package:gcloud/service_scope.dart';
88
import 'package:gcloud/storage.dart';
99
import 'package:googleapis_auth/auth_io.dart' as auth;
10+
import 'package:path/path.dart' as path;
1011
import 'package:shelf/shelf.dart' as shelf;
1112
import 'package:shelf/shelf_io.dart' as shelf_io;
1213

@@ -80,6 +81,7 @@ void main() {
8081

8182
logger.info('Handling request: ${request.requestedUri} '
8283
'(Using namespace "$namespace")');
84+
request = sanitizeRequestedUri(request);
8385
return appHandler(request, apiHandler).catchError((error, s) {
8486
logger.severe('Request handler failed', error, s);
8587
return new shelf.Response.internalServerError();
@@ -98,3 +100,36 @@ void main() {
98100

99101
/// Gets the namespace to use.
100102
String getCurrentNamespace() => '';
103+
104+
shelf.Request sanitizeRequestedUri(shelf.Request request) {
105+
final uri = request.requestedUri;
106+
final resource = uri.path;
107+
final normalizedResource = path.normalize(resource);
108+
109+
if (resource == normalizedResource) {
110+
return request;
111+
} else {
112+
// With the new flex VMs we will get requests from the L7 load balancer which
113+
// can contain [Uri]s with e.g. double slashes
114+
//
115+
// -> e.g. https://pub.dartlang.org//api/packages/foo
116+
//
117+
// It seems that travis builds e.g. set PUB_HOSTED_URL to a URL with
118+
// a slash at the end. The pub client will not remove it and instead
119+
// directly try to request the URI:
120+
//
121+
// GET //api/...
122+
//
123+
// :-/
124+
125+
final changedUri = uri.replace(path: normalizedResource);
126+
return new shelf.Request(
127+
request.method,
128+
changedUri,
129+
protocolVersion: request.protocolVersion,
130+
headers: request.headers,
131+
body: request.read(),
132+
encoding: request.encoding,
133+
context: request.context);
134+
}
135+
}

0 commit comments

Comments
 (0)