xp-framework / http

HTTP protocol support for the XP Framework
2 stars 2 forks source link

Proxies with HTTPS broken #1

Closed thekid closed 9 years ago

thekid commented 9 years ago

The following tries to connect to the proxy via HTTPS. Instead, it should connect there with HTTP and issue a CONNECT verb.

$c= new HttpConnection('https://...');
$c->setProxy(new HttpProxy('proxy.local.lan', 8080));
$response= $c->get();

Result:

Uncaught exception: Exception lang.reflect.TargetInvocationException (Proxy::main)
  at peer.http.SSLSocketHttpTransport::newSocket() [line 21 of SSLSocketHttpTransport.class.php] Missing argument 2 for peer\http\SSLSocketHttpTransport::newSocket(), called in C:\cygwin\home\Timm\devel\xp\http\src\main\php\peer\http\SocketHttpTransport.class.php on line 45 and defined
  at peer.http.SSLSocketHttpTransport::newSocket() [line 22 of SSLSocketHttpTransport.class.php] Undefined variable: arg
  at lang.reflect.Method::invoke(NULL, array[1]) [line 248 of class-main.php]
Caused by Exception peer.ConnectException (Failed connecting to ********:3128 within 2 seconds [0: ])
  at peer.http.SSLSocketHttpTransport::newSocket() [line 21 of SSLSocketHttpTransport.class.php] Missing argument 2 for peer\http\SSLSocketHttpTransport::newSocket(), called in C:\cygwin\home\Timm\devel\xp\http\src\main\php\peer\http\SocketHttpTransport.class.php on line 45 and defined
  at peer.http.SSLSocketHttpTransport::newSocket() [line 22 of SSLSocketHttpTransport.class.php] Undefined variable: arg
  at <main>::stream_socket_client() [line 140 of Socket.class.php] stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
  at <main>::stream_socket_client() [line 140 of Socket.class.php] stream_socket_client(): Failed to enable crypto
  at <main>::stream_socket_client() [line 140 of Socket.class.php] stream_socket_client(): unable to connect to ssl://********:3128 (Unknown error)
  at peer.Socket::connect(2) [line 80 of SocketHttpTransport.class.php]
  at peer.http.SocketHttpTransport::send(peer.http.HttpRequest{}, 60, 2) [line 123 of HttpConnection.class.php]
  at peer.http.HttpConnection::send(peer.http.HttpRequest{}) [line 164 of HttpConnection.class.php]
  at peer.http.HttpConnection::request((0x3)'GET', NULL, array[0]) [line 175 of HttpConnection.class.php]
  at peer.http.HttpConnection::get() [line 14 of Proxy.class.php]
  at Proxy::main(array[0]) [line 0 of StackTraceElement.class.php]
  ... 2 more

See http://stackoverflow.com/questions/11697943/when-should-one-use-connect-and-get-http-methods-at-http-proxy-server

thekid commented 9 years ago

Workaround for the moment is to force HttpConnection to use the CURL extension:

HttpTransport::register('https', XPClass::forName('peer.http.CurlHttpTransport'));
$c= new HttpConnection('https://...');
// ...
thekid commented 9 years ago

Test script:

<?php

use peer\http\HttpConnection;
use peer\http\HttpProxy;
use util\log\LogCategory;
use util\log\ConsoleAppender;
use util\cmd\Console;

class Proxy extends \lang\Object {

  public static function main($args) {
    $c= new HttpConnection($args[0]);
    $c->setTrace((new LogCategory('console'))->withAppender(new ConsoleAppender()));
    if (isset($args[1])) {
      sscanf($args[1], '%[^:]:%d', $host, $port);
      $c->setProxy(new HttpProxy($host, $port ?: 3128));
    }
    $c->get();
  }
}

Works with certain proxies; we might need to use "CONNECT" though, not sure...

thekid commented 9 years ago

Now works:

vagrant@precise32:/devel/xp/http$ XP_RT=sys xp Proxy https://github.com/ localhost:3128
[13:42:18  5677  info] >>> CONNECT github.com:443 HTTP/1.1
[13:42:19  5677  info] <<< HTTP/1.0 200 Connection established

[13:42:19  5677 debug] @@@ Enabling tls:// cryptography
[13:42:19  5677  info] >>> GET / HTTP/1.1
Connection: close
Host: github.com
User-Agent: Proxy test class

[13:42:19  5677  info] <<< HTTP/1.1 200 OK
Server: GitHub.com
Date: Sun, 19 Oct 2014 11:42:19 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 200 OK
# ...

The proxy I'm using is Squid3.