Skip to content

Commit

Permalink
Merge 9b8a7c6 into dec780a
Browse files Browse the repository at this point in the history
  • Loading branch information
ozh committed Sep 7, 2013
2 parents dec780a + 9b8a7c6 commit d28aef9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
22 changes: 22 additions & 0 deletions examples/proxy.php
@@ -0,0 +1,22 @@
<?php

// First, include Requests
include('../library/Requests.php');

// Next, make sure Requests can load internal classes
Requests::register_autoloader();

// Let's make a direct request
$request = Requests::get('http://httpbin.org/ip' );

// Check what we received
var_dump( $request->body );

// Now let's make a request via a proxy.
$options = array(
'proxy' => '127.0.0.1:8080', // syntax: host:port, eg 12.13.14.14:8080 or someproxy.com:3128
);
$request = Requests::get('http://httpbin.org/ip', array(), $options );

// Compare with previous result
var_dump( $request->body );
13 changes: 12 additions & 1 deletion library/Requests/Transport/cURL.php
Expand Up @@ -84,7 +84,7 @@ public function __construct() {
*/
public function request($url, $headers = array(), $data = array(), $options = array()) {
$this->setup_handle($url, $headers, $data, $options);

$options['hooks']->dispatch('curl.before_send', array(&$this->fp));

if ($options['filename'] !== false) {
Expand All @@ -106,6 +106,17 @@ public function request($url, $headers = array(), $data = array(), $options = ar
curl_setopt($this->fp, CURLOPT_SSL_VERIFYHOST, 0);
}

// Proxy support
if ( isset( $options['proxy'] ) ) {
curl_setopt( $this->fp, CURLOPT_PROXYTYPE, CURLPROXY_HTTP );
curl_setopt( $this->fp, CURLOPT_PROXY, $options['proxy'] );

if ( isset( $options['proxy_username'] ) && isset( $options['proxy_password'] ) ) {
curl_setopt( $this->fp, CURLOPT_PROXYAUTH, CURLAUTH_ANY );
curl_setopt( $this->fp, CURLOPT_PROXYUSERPWD, $options['proxy_username'] . ':' . $options['proxy_password'] );
}
}

$response = curl_exec($this->fp);

$options['hooks']->dispatch('curl.after_send', array(&$fake_headers));
Expand Down
40 changes: 30 additions & 10 deletions library/Requests/Transport/fsockopen.php
Expand Up @@ -78,14 +78,23 @@ public function request($url, $headers = array(), $data = array(), $options = ar
else {
$remote_socket = 'tcp://' . $host;
}


$proxy = isset( $options['proxy'] );
$proxy_auth = $proxy && isset( $options['proxy_username'] ) && isset( $options['proxy_password'] );

if (!isset($url_parts['port'])) {
$url_parts['port'] = 80;
}
$remote_socket .= ':' . $url_parts['port'];

set_error_handler(array($this, 'connect_error_handler'), E_WARNING | E_NOTICE);
$fp = stream_socket_client($remote_socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $context);

if ( $proxy )
$fp = stream_socket_client( 'tcp://' . $options['proxy'], $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $context);
else
$fp = stream_socket_client( $remote_socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $context);


restore_error_handler();

if (!$fp) {
Expand All @@ -105,14 +114,18 @@ public function request($url, $headers = array(), $data = array(), $options = ar
case Requests::POST:
case Requests::PUT:
case Requests::PATCH:
if (isset($url_parts['path'])) {
$path = $url_parts['path'];
if (isset($url_parts['query'])) {
$path .= '?' . $url_parts['query'];
if( $proxy ) {
$path = $url;
} else {
if (isset($url_parts['path'])) {
$path = $url_parts['path'];
if (isset($url_parts['query'])) {
$path .= '?' . $url_parts['query'];
}
}
else {
$path = '/';
}
}
else {
$path = '/';
}
$out = $options['type'] . " $path HTTP/1.0\r\n";
if (is_array($data)) {
Expand All @@ -131,7 +144,11 @@ public function request($url, $headers = array(), $data = array(), $options = ar
case Requests::HEAD:
case Requests::GET:
case Requests::DELETE:
$get = self::format_get($url_parts, $data);
if( $proxy ) {
$get = $url;
} else {
$get = self::format_get($url_parts, $data);
}
$out = $options['type'] . " $get HTTP/1.0\r\n";
break;
}
Expand All @@ -140,6 +157,9 @@ public function request($url, $headers = array(), $data = array(), $options = ar
if ($url_parts['port'] !== 80) {
$out .= ":{$url_parts['port']}";
}

if( $proxy_auth )
$out .= 'Proxy-Authorization: Basic ' . base64_encode( $options['proxy_username'] . ':' . $options['proxy_password'] ) . "\r\n";

$out .= "\r\n";

Expand Down

0 comments on commit d28aef9

Please sign in to comment.