[web] Change the web server to support path url strategy (#66606)
This commit is contained in:
@@ -190,6 +190,7 @@ class WebAssetServer implements AssetReader {
|
||||
platform: globals.platform,
|
||||
flutterRoot: Cache.flutterRoot,
|
||||
webBuildDirectory: getWebBuildDirectory(),
|
||||
basePath: server.basePath,
|
||||
);
|
||||
shelf.serveRequests(httpServer, releaseAssetServer.handle);
|
||||
return server;
|
||||
@@ -316,30 +317,28 @@ class WebAssetServer implements AssetReader {
|
||||
@visibleForTesting
|
||||
Uint8List getMetadata(String path) => _metadataFiles[path];
|
||||
|
||||
@visibleForTesting
|
||||
/// The base path to serve from.
|
||||
///
|
||||
/// It should have no leading or trailing slashes.
|
||||
String basePath = '';
|
||||
|
||||
// handle requests for JavaScript source, dart sources maps, or asset files.
|
||||
@visibleForTesting
|
||||
Future<shelf.Response> handleRequest(shelf.Request request) async {
|
||||
String requestPath = request.url.path;
|
||||
while (requestPath.startsWith('/')) {
|
||||
requestPath = requestPath.substring(1);
|
||||
final String requestPath = _stripBasePath(request.url.path, basePath);
|
||||
|
||||
if (requestPath == null) {
|
||||
return shelf.Response.notFound('');
|
||||
}
|
||||
final Map<String, String> headers = <String, String>{};
|
||||
|
||||
// If the response is `/`, then we are requesting the index file.
|
||||
if (request.url.path == '/' || request.url.path.isEmpty) {
|
||||
final File indexFile = globals.fs.currentDirectory
|
||||
.childDirectory('web')
|
||||
.childFile('index.html');
|
||||
if (indexFile.existsSync()) {
|
||||
headers[HttpHeaders.contentTypeHeader] = 'text/html';
|
||||
headers[HttpHeaders.contentLengthHeader] = indexFile.lengthSync().toString();
|
||||
return shelf.Response.ok(indexFile.openRead(), headers: headers);
|
||||
} else {
|
||||
headers[HttpHeaders.contentTypeHeader] = 'text/html';
|
||||
headers[HttpHeaders.contentLengthHeader] = _kDefaultIndex.length.toString();
|
||||
return shelf.Response.ok(_kDefaultIndex, headers: headers);
|
||||
}
|
||||
if (requestPath == '/' || requestPath.isEmpty) {
|
||||
return _serveIndex();
|
||||
}
|
||||
|
||||
final Map<String, String> headers = <String, String>{};
|
||||
|
||||
// Track etag headers for better caching of resources.
|
||||
final String ifNoneMatch = request.headers[HttpHeaders.ifNoneMatchHeader];
|
||||
headers[HttpHeaders.cacheControlHeader] = 'max-age=0, must-revalidate';
|
||||
@@ -407,7 +406,7 @@ class WebAssetServer implements AssetReader {
|
||||
}
|
||||
|
||||
if (!file.existsSync()) {
|
||||
return shelf.Response.notFound('');
|
||||
return _serveIndex();
|
||||
}
|
||||
|
||||
// For real files, use a serialized file stat plus path as a revision.
|
||||
@@ -536,6 +535,23 @@ class WebAssetServer implements AssetReader {
|
||||
/// Whether to use the cavaskit SDK for rendering.
|
||||
bool canvasKitRendering = false;
|
||||
|
||||
shelf.Response _serveIndex() {
|
||||
final Map<String, String> headers = <String, String>{
|
||||
HttpHeaders.contentTypeHeader: 'text/html',
|
||||
};
|
||||
final File indexFile = globals.fs.currentDirectory
|
||||
.childDirectory('web')
|
||||
.childFile('index.html');
|
||||
|
||||
if (indexFile.existsSync()) {
|
||||
headers[HttpHeaders.contentLengthHeader] = indexFile.lengthSync().toString();
|
||||
return shelf.Response.ok(indexFile.openRead(), headers: headers);
|
||||
}
|
||||
|
||||
headers[HttpHeaders.contentLengthHeader] = _kDefaultIndex.length.toString();
|
||||
return shelf.Response.ok(_kDefaultIndex, headers: headers);
|
||||
}
|
||||
|
||||
// Attempt to resolve `path` to a dart file.
|
||||
File _resolveDartFile(String path) {
|
||||
// Return the actual file objects so that local engine changes are automatically picked up.
|
||||
@@ -907,6 +923,7 @@ class ReleaseAssetServer {
|
||||
@required String webBuildDirectory,
|
||||
@required String flutterRoot,
|
||||
@required Platform platform,
|
||||
this.basePath = '',
|
||||
}) : _fileSystem = fileSystem,
|
||||
_platform = platform,
|
||||
_flutterRoot = flutterRoot,
|
||||
@@ -920,6 +937,12 @@ class ReleaseAssetServer {
|
||||
final FileSystemUtils _fileSystemUtils;
|
||||
final Platform _platform;
|
||||
|
||||
@visibleForTesting
|
||||
/// The base path to serve from.
|
||||
///
|
||||
/// It should have no leading or trailing slashes.
|
||||
final String basePath;
|
||||
|
||||
// Locations where source files, assets, or source maps may be located.
|
||||
List<Uri> _searchPaths() => <Uri>[
|
||||
_fileSystem.directory(_webBuildDirectory).uri,
|
||||
@@ -931,11 +954,17 @@ class ReleaseAssetServer {
|
||||
|
||||
Future<shelf.Response> handle(shelf.Request request) async {
|
||||
Uri fileUri;
|
||||
final String requestPath = _stripBasePath(request.url.path, basePath);
|
||||
|
||||
if (requestPath == null) {
|
||||
return shelf.Response.notFound('');
|
||||
}
|
||||
|
||||
if (request.url.toString() == 'main.dart') {
|
||||
fileUri = entrypoint;
|
||||
} else {
|
||||
for (final Uri uri in _searchPaths()) {
|
||||
final Uri potential = uri.resolve(request.url.path);
|
||||
final Uri potential = uri.resolve(requestPath);
|
||||
if (potential == null || !_fileSystem.isFileSync(
|
||||
potential.toFilePath(windows: _platform.isWindows))) {
|
||||
continue;
|
||||
@@ -955,13 +984,11 @@ class ReleaseAssetServer {
|
||||
'Content-Type': mimeType,
|
||||
});
|
||||
}
|
||||
if (request.url.path == '') {
|
||||
final File file = _fileSystem.file(_fileSystem.path.join(_webBuildDirectory, 'index.html'));
|
||||
return shelf.Response.ok(file.readAsBytesSync(), headers: <String, String>{
|
||||
'Content-Type': 'text/html',
|
||||
});
|
||||
}
|
||||
return shelf.Response.notFound('');
|
||||
|
||||
final File file = _fileSystem.file(_fileSystem.path.join(_webBuildDirectory, 'index.html'));
|
||||
return shelf.Response.ok(file.readAsBytesSync(), headers: <String, String>{
|
||||
'Content-Type': 'text/html',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -975,3 +1002,19 @@ Future<Directory> _loadDwdsDirectory(FileSystem fileSystem, Logger logger) async
|
||||
);
|
||||
return fileSystem.directory(packageConfig['dwds'].packageUriRoot);
|
||||
}
|
||||
|
||||
String _stripBasePath(String path, String basePath) {
|
||||
while (path.startsWith('/')) {
|
||||
path = path.substring(1);
|
||||
}
|
||||
if (path.startsWith(basePath)) {
|
||||
path = path.substring(basePath.length);
|
||||
} else {
|
||||
// The given path isn't under base path, return null to indicate that.
|
||||
return null;
|
||||
}
|
||||
while (path.startsWith('/')) {
|
||||
path = path.substring(1);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user