improve uri parsing for urls without scheme
This commit is contained in:
@@ -4,7 +4,8 @@ sealed class SharedContent {
|
||||
const SharedContent();
|
||||
|
||||
factory SharedContent.parse(String content) {
|
||||
if (uri_parser.tryParseUrl(content) case final Uri uri) {
|
||||
if (uri_parser.tryParseUrl(content, eagerParsing: true)
|
||||
case final Uri uri) {
|
||||
return SharedUrl(uri);
|
||||
} else {
|
||||
return SharedText(content);
|
||||
|
||||
@@ -20,6 +20,7 @@ import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
||||
import 'package:flutter_material_design_icons/flutter_material_design_icons.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
import 'package:bang_navigator/utils/uri_parser.dart' as uri_parser;
|
||||
|
||||
class LoadingWebPageDialog extends HookConsumerWidget {
|
||||
final Uri url;
|
||||
@@ -125,9 +126,10 @@ class WebPageDialog extends HookConsumerWidget {
|
||||
await webViewController!.loadUrl(
|
||||
urlRequest: URLRequest(
|
||||
url: WebUri.uri(
|
||||
Uri.parse(
|
||||
uri_parser.tryParseUrl(
|
||||
urlTextController.text,
|
||||
),
|
||||
eagerParsing: true,
|
||||
)!,
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -140,12 +142,9 @@ class WebPageDialog extends HookConsumerWidget {
|
||||
: null,
|
||||
),
|
||||
validator: (value) {
|
||||
if (value != null) {
|
||||
if (Uri.tryParse(value) case final Uri url) {
|
||||
if (url.hasScheme && url.hasAuthority) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (uri_parser.tryParseUrl(value, eagerParsing: true) !=
|
||||
null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return 'Invalid URL';
|
||||
|
||||
@@ -1,12 +1,22 @@
|
||||
Uri? tryParseUrl(String? input) {
|
||||
Uri? tryParseUrl(String? input, {bool eagerParsing = false}) {
|
||||
if (input != null) {
|
||||
final uri = Uri.tryParse(input);
|
||||
if (uri != null &&
|
||||
uri.hasAuthority &&
|
||||
(uri.isScheme('http') || uri.isScheme('https') || !uri.hasScheme)) {
|
||||
return uri;
|
||||
var uri = Uri.tryParse(input);
|
||||
if (uri != null) {
|
||||
if (uri.authority.isEmpty && eagerParsing) {
|
||||
//When there is no scheme, there will be no authority/host and all becomes a path
|
||||
//so we make sure there are at least 2 segments where the first one looks like a domain
|
||||
if (uri.pathSegments.length > 1 &&
|
||||
RegExp(r'.[a-z]{2,}$').hasMatch(uri.pathSegments.first)) {
|
||||
uri = Uri.tryParse('https://$input');
|
||||
}
|
||||
}
|
||||
|
||||
if (uri != null &&
|
||||
uri.authority.isNotEmpty &&
|
||||
(uri.isScheme('http') || uri.isScheme('https') || !uri.hasScheme)) {
|
||||
return uri;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user