diff --git a/CHANGELOG.md b/CHANGELOG.md index cdaeb594..da42ceac 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ - Merged "Allow a cookie without a domain to be set on Android" [#1295](https://github.com/pichillilorenzo/flutter_inappwebview/pull/1295) (thanks to [bagedevimo](https://github.com/bagedevimo)) - Merged "Catch and ignore utf8 format exception in getFavicons()" [#1302](https://github.com/pichillilorenzo/flutter_inappwebview/pull/1302) (thanks to [Doflatango](https://github.com/Doflatango)) - Merged "Disable exporting activity definitions for Android" [#1313](https://github.com/pichillilorenzo/flutter_inappwebview/pull/1313) (thanks to [daanporon](https://github.com/daanporon)) +- Merged "Add directoryIndex and documentRoot to InAppLocalhostServer option" [#1319](https://github.com/pichillilorenzo/flutter_inappwebview/pull/1319) (thanks to [fa0311](https://github.com/fa0311)) ## 5.4.4+3 diff --git a/example/lib/main.dart b/example/lib/main.dart index e233ac4e..bab8ae07 100755 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -13,7 +13,7 @@ import 'package:pointer_interceptor/pointer_interceptor.dart'; // import 'package:path_provider/path_provider.dart'; // import 'package:permission_handler/permission_handler.dart'; -InAppLocalhostServer localhostServer = new InAppLocalhostServer(); +InAppLocalhostServer localhostServer = new InAppLocalhostServer(documentRoot: 'assets'); Future main() async { WidgetsFlutterBinding.ensureInitialized(); @@ -21,8 +21,6 @@ Future main() async { // await Permission.microphone.request(); // await Permission.storage.request(); - WebView.debugLoggingSettings.maxLogMessageLength = 500; - if (defaultTargetPlatform == TargetPlatform.android) { await InAppWebViewController.setWebContentsDebuggingEnabled(true); } diff --git a/lib/src/in_app_localhost_server.dart b/lib/src/in_app_localhost_server.dart index e30c96d5..6b5085c4 100755 --- a/lib/src/in_app_localhost_server.dart +++ b/lib/src/in_app_localhost_server.dart @@ -6,7 +6,8 @@ import 'package:flutter/services.dart' show rootBundle; import 'mime_type_resolver.dart'; -///This class allows you to create a simple server on `http://localhost:[port]/` in order to be able to load your assets file on a server. The default [port] value is `8080`. +///This class allows you to create a simple server on `http://localhost:[port]/` in order to be able to load your assets file on a server. +///The default `port` value is `8080`. /// ///**Supported Platforms/Implementations**: ///- Android native WebView @@ -15,14 +16,28 @@ class InAppLocalhostServer { bool _started = false; HttpServer? _server; int _port = 8080; + String _directoryIndex = 'index.html'; + String _documentRoot = './'; - InAppLocalhostServer({int port = 8080}) { + ///- [port] represents the port of the server. The default value is `8080`. + /// + ///- [directoryIndex] represents the index file to use. The default value is `index.html`. + /// + ///- [documentRoot] represents the document root path to serve. The default value is `./`. + InAppLocalhostServer({ + int port = 8080, + String directoryIndex = 'index.html', + String documentRoot = './', + }) { this._port = port; + this._directoryIndex = directoryIndex; + this._documentRoot = (documentRoot.endsWith('/')) ? documentRoot : '$documentRoot/'; } ///Starts the server on `http://localhost:[port]/`. /// - ///**NOTE for iOS**: For the iOS Platform, you need to add the `NSAllowsLocalNetworking` key with `true` in the `Info.plist` file (See [ATS Configuration Basics](https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW35)): + ///**NOTE for iOS**: For the iOS Platform, you need to add the `NSAllowsLocalNetworking` key with `true` in the `Info.plist` file + ///(See [ATS Configuration Basics](https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW35)): ///```xml ///NSAppTransportSecurity /// @@ -50,7 +65,12 @@ class InAppLocalhostServer { var path = request.requestedUri.path; path = (path.startsWith('/')) ? path.substring(1) : path; - path += (path.endsWith('/')) ? 'index.html' : ''; + path += (path.endsWith('/')) ? _directoryIndex : ''; + if (path == '') { + // if the path still empty, try to load the index file + path = _directoryIndex; + } + path = _documentRoot + path; try { body = (await rootBundle.load(path)).buffer.asUint8List();