Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

Allow for a custom BypassProxy class to override how a page source is fetched. #88

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.3

- (web) A custom BypassProxy can now also override the way a page is fetched. This includes the ability to use custom protocols or apis instead of a standard http request.

## 0.2.1

- Breaking change
Expand Down
25 changes: 16 additions & 9 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.6.1"
version: "2.8.2"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,14 +21,14 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -101,21 +101,28 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
pedantic:
dependency: transitive
description:
Expand Down Expand Up @@ -176,7 +183,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
version: "0.4.9"
typed_data:
dependency: transitive
description:
Expand All @@ -197,7 +204,7 @@ packages:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
webview_flutter:
dependency: transitive
description:
Expand All @@ -213,5 +220,5 @@ packages:
source: path
version: "0.2.1"
sdks:
dart: ">=2.13.0 <3.0.0"
dart: ">=2.14.0 <3.0.0"
flutter: ">=2.0.0"
30 changes: 27 additions & 3 deletions lib/src/utils/web_url_bypass_proxy.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import 'package:http/http.dart' as http;

/// Proxy which will be used to fetch page sources in the [SourceType.urlBypass] mode.
abstract class BypassProxy {
const BypassProxy();

/// Builds the proxied url
String buildProxyUrl(String pageUrl);

Expand All @@ -12,10 +16,30 @@ abstract class BypassProxy {
CodeTabsBypassProxy(),
WeCorsAnyWhereProxy(),
];

Future<String> fetchPageSource({
required String method,
required String url,
Map<String, String>? headers,
Object? body,
}) async {
final proxiedUri = Uri.parse(buildProxyUrl(Uri.encodeFull(url)));

Future<http.Response> request;

if (method == 'get') {
request = http.get(proxiedUri, headers: headers);
} else {
request = http.post(proxiedUri, headers: headers, body: body);
}

final response = await request;
return extractPageSource(response.body);
}
}

/// cors.bridged.cc proxy
class BridgedBypassProxy implements BypassProxy {
class BridgedBypassProxy extends BypassProxy {
const BridgedBypassProxy();

@override
Expand All @@ -30,7 +54,7 @@ class BridgedBypassProxy implements BypassProxy {
}

/// api.codetabs.com proxy
class CodeTabsBypassProxy implements BypassProxy {
class CodeTabsBypassProxy extends BypassProxy {
const CodeTabsBypassProxy();

@override
Expand All @@ -45,7 +69,7 @@ class CodeTabsBypassProxy implements BypassProxy {
}

/// we-cors-anywhere.herokuapp.com proxy
class WeCorsAnyWhereProxy implements BypassProxy {
class WeCorsAnyWhereProxy extends BypassProxy {
const WeCorsAnyWhereProxy();

@override
Expand Down
19 changes: 6 additions & 13 deletions lib/src/view/impl/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'dart:js' as js;

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:pointer_interceptor/pointer_interceptor.dart';

import 'package:webviewx/src/utils/dart_ui_fix.dart' as ui;
Expand Down Expand Up @@ -549,19 +548,13 @@ class _WebViewXState extends State<WebViewX> {
final proxy = proxyList[i];
_debugLog('Using proxy: ${proxy.runtimeType}');

final proxiedUri = Uri.parse(proxy.buildProxyUrl(Uri.encodeFull(url)));

Future<http.Response> request;

if (method == 'get') {
request = http.get(proxiedUri, headers: headers);
} else {
request = http.post(proxiedUri, headers: headers, body: body);
}

try {
final response = await request;
return proxy.extractPageSource(response.body);
return proxy.fetchPageSource(
method: method,
url: url,
headers: headers,
body: body,
);
} catch (e) {
_debugLog(
'Failed to fetch the page at $url from proxy ${proxy.runtimeType}.',
Expand Down
25 changes: 16 additions & 9 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.6.1"
version: "2.8.2"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,14 +21,14 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -94,21 +94,28 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: "direct main"
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
pedantic:
dependency: transitive
description:
Expand Down Expand Up @@ -169,7 +176,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
version: "0.4.9"
typed_data:
dependency: transitive
description:
Expand All @@ -190,7 +197,7 @@ packages:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
webview_flutter:
dependency: "direct main"
description:
Expand All @@ -199,5 +206,5 @@ packages:
source: hosted
version: "2.0.13"
sdks:
dart: ">=2.13.0 <3.0.0"
dart: ">=2.14.0 <3.0.0"
flutter: ">=2.0.0"
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webviewx
description: A feature-rich cross-platform webview using webview_flutter for
mobile and iframe for web. JS interop-ready.
homepage: https://github.com/adrianflutur/webviewx
version: 0.2.1
version: 0.2.3

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down