forked from firka/flutter
Support --web-header option for flutter run (#136297)
Adds support for a new --web-header option to flutter run. Creates a workaround for https://github.com/flutter/flutter/issues/127902 This PR allows adding additional headers for the flutter run web server. This is useful to add headers like Cross-Origin-Embedder-Policy and Cross-Origin-Opener-Policy without the use of a proxy server. These headers are required enable advanced web features. This approach provides flexibility to the developer to make use of the feature as they see fit and is backward-compatible. One tradeoff is that it increases the surface area to support for future changes to the flutter web server. https://github.com/flutter/flutter/issues/127902 is not fully addressed by this change. The solution for that task will be more opinionated. This PR creates a general-purpose workaround for anyone who needs a solution sooner while the bigger solution is developed.
This commit is contained in:
@@ -59,6 +59,16 @@ abstract class DotEnvRegex {
|
||||
static final RegExp unquotedValue = RegExp(r'^([^#\n\s]*)\s*(?:\s*#\s*(.*))?$');
|
||||
}
|
||||
|
||||
abstract class _HttpRegex {
|
||||
// https://datatracker.ietf.org/doc/html/rfc7230#section-3.2
|
||||
static const String _vchar = r'\x21-\x7E';
|
||||
static const String _spaceOrTab = r'\x20\x09';
|
||||
static const String _nonDelimiterVchar = r'\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7A\x7C\x7E';
|
||||
|
||||
// --web-header is provided as key=value for consistency with --dart-define
|
||||
static final RegExp httpHeader = RegExp('^([$_nonDelimiterVchar]+)' r'\s*=\s*' '([$_vchar$_spaceOrTab]+)' r'$');
|
||||
}
|
||||
|
||||
enum ExitStatus {
|
||||
success,
|
||||
warning,
|
||||
@@ -218,6 +228,14 @@ abstract class FlutterCommand extends Command<void> {
|
||||
}
|
||||
|
||||
void usesWebOptions({ required bool verboseHelp }) {
|
||||
argParser.addMultiOption('web-header',
|
||||
help: 'Additional key-value pairs that will added by the web server '
|
||||
'as headers to all responses. Multiple headers can be passed by '
|
||||
'repeating "--web-header" multiple times.',
|
||||
valueHelp: 'X-Custom-Header=header-value',
|
||||
splitCommas: false,
|
||||
hide: !verboseHelp,
|
||||
);
|
||||
argParser.addOption('web-hostname',
|
||||
defaultsTo: 'localhost',
|
||||
help:
|
||||
@@ -1521,6 +1539,31 @@ abstract class FlutterCommand extends Command<void> {
|
||||
return dartDefinesSet.toList();
|
||||
}
|
||||
|
||||
|
||||
Map<String, String> extractWebHeaders() {
|
||||
final Map<String, String> webHeaders = <String, String>{};
|
||||
|
||||
if (argParser.options.containsKey('web-header')) {
|
||||
final List<String> candidates = stringsArg('web-header');
|
||||
final List<String> invalidHeaders = <String>[];
|
||||
for (final String candidate in candidates) {
|
||||
final Match? keyValueMatch = _HttpRegex.httpHeader.firstMatch(candidate);
|
||||
if (keyValueMatch == null) {
|
||||
invalidHeaders.add(candidate);
|
||||
continue;
|
||||
}
|
||||
|
||||
webHeaders[keyValueMatch.group(1)!] = keyValueMatch.group(2)!;
|
||||
}
|
||||
|
||||
if (invalidHeaders.isNotEmpty) {
|
||||
throwToolExit('Invalid web headers: ${invalidHeaders.join(', ')}');
|
||||
}
|
||||
}
|
||||
|
||||
return webHeaders;
|
||||
}
|
||||
|
||||
void _registerSignalHandlers(String commandPath, DateTime startTime) {
|
||||
void handler(io.ProcessSignal s) {
|
||||
globals.cache.releaseLock();
|
||||
|
||||
Reference in New Issue
Block a user