dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.21k stars 1.57k forks source link

HttpException: Invalid proxy configuration HTTPS #43475

Open liudonghua123 opened 4 years ago

liudonghua123 commented 4 years ago

I have a pac script which returns HTTPS usa.cn-cloudflare.com:443 and I just want to use this https proxy in my dart code. So I write some code like this.

import 'dart:io';
import 'dart:convert';

// https://codeburst.io/quick-tip-how-to-make-http-requests-in-dart-53fc407daf31

void main() async {
  HttpClient client = new HttpClient();
  client.findProxy = (Uri uri) => "HTTPS usa.cn-cloudflare.com:443";
  // produces a request object
  var request = await client.getUrl(Uri.parse('https://jsonplaceholder.typicode.com/posts'));
  // sends the request
  var response = await request.close();
  // transforms and prints the response
  await for (var contents in response.transform(Utf8Decoder())) {
    print(contents);
  }
  client.close();
}

But it shown me the following erros.

Unhandled exception:
HttpException: Invalid proxy configuration HTTPS usa.cn-cloudflare.com:443
#0      new _ProxyConfiguration.<anonymous closure> (dart:_http/http_impl.dart:3020:11)
#1      List.forEach (dart:core-patch/growable_array.dart:302:8)
#2      new _ProxyConfiguration (dart:_http/http_impl.dart:2976:10)
#3      _HttpClient._openUrl (dart:_http/http_impl.dart:2338:25)
#4      _HttpClient.getUrl (dart:_http/http_impl.dart:2208:48)
#5      main (file:///D:/code/dart/simple_throttle_debounce/proxy.dart:11:30)
#6      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
#7      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

I also read the doc https://api.dart.dev/stable/2.9.3/dart-io/HttpClient/findProxyFromEnvironment.html, https://api.dart.dev/stable/2.9.3/dart-io/HttpClient/findProxy.html, and find it seems do not support HTTPS of pac.

liudonghua123 commented 4 years ago

I read the code implementation https://github.com/dart-lang/sdk/blob/04591ddfbcd92caf6d1155bf62906d47a559bdaa/sdk/lib/_http/http_impl.dart#L2632-L2652 it seems the proxy pac support in dart:io is not complete. It only support simple PROXY/DIRECT, other return values like SOCKS/HTTP/HTTPS/SOCKS4/SOCKS5 (https://developer.mozilla.org/en-US/docs/Web/HTTP/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_(PAC)_file) is not support.

cooolinx commented 4 years ago

Same issue here.

And depends on the official document HttpClient.findProxyFromEnvironment, https_proxy should work but it's not.

I also read the source code L2211 in sdk/lib/_http/http_impl.dart:

Future<ConnectionTask> connectionTask = (isSecure && proxy.isDirect
        ? SecureSocket.startConnect(host, port,
            context: context, onBadCertificate: callback)
        : Socket.startConnect(host, port));

it says that: only build HTTPS (SecureSocket) connection when the RAW target is secure and without proxy, with proxy never HTTPS.

So @liudonghua123 , they don't even implement https proxy feature, not only the HTTPS directive in PAC.

hacker1024 commented 2 years ago

43876

hacker1024 commented 2 years ago

This may also be able to be set up manually once #43277 is resolved.