added default localhost server path with _directoryIndex if it is empty

This commit is contained in:
Lorenzo Pichilli 2022-10-13 20:35:34 +02:00
parent 3dc5ae3038
commit b6db461c70
3 changed files with 17 additions and 3 deletions

View File

@ -13,6 +13,9 @@
- Merged "Android - Load client certificate from local storage" [#1241](https://github.com/pichillilorenzo/flutter_inappwebview/pull/1241) (thanks to [akioyamamoto1977](https://github.com/akioyamamoto1977))
- Merged "fix Theme_AppCompat_Dialog_Alert not found" [#1262](https://github.com/pichillilorenzo/flutter_inappwebview/pull/1262) (thanks to [mohenaxiba](https://github.com/mohenaxiba))
- 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

View File

@ -11,7 +11,7 @@ import 'package:flutter_inappwebview_example/in_app_browser_example.screen.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();

View File

@ -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`.
class InAppLocalhostServer {
bool _started = false;
HttpServer? _server;
@ -14,6 +15,11 @@ class InAppLocalhostServer {
String _directoryIndex = 'index.html';
String _documentRoot = './';
///- [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',
@ -26,7 +32,8 @@ class InAppLocalhostServer {
///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
///<key>NSAppTransportSecurity</key>
///<dict>
@ -55,6 +62,10 @@ class InAppLocalhostServer {
var path = request.requestedUri.path;
path = (path.startsWith('/')) ? path.substring(1) : path;
path += (path.endsWith('/')) ? _directoryIndex : '';
if (path == '') {
// if the path still empty, try to load the index file
path = _directoryIndex;
}
path = _documentRoot + path;
try {