Skip to content

Commit

Permalink
Refactor proxy functions
Browse files Browse the repository at this point in the history
Easier testing.
  • Loading branch information
ozh committed May 25, 2015
1 parent faf481f commit 18cc1a4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 29 deletions.
13 changes: 13 additions & 0 deletions includes/functions-deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,16 @@ function yourls_has_interface() {
return false;
return true;
}

/**
* Check if a proxy is defined for HTTP requests
*
* @uses YOURLS_PROXY
* @since 1.7
* @deprecated 1.7.1
* @return bool true if a proxy is defined, false otherwise
*/
function yourls_http_proxy_is_defined() {
yourls_deprecated_function( __FUNCTION__, '1.7.1', 'yourls_http_get_proxy' );
return yourls_apply_filter( 'http_proxy_is_defined', defined( 'YOURLS_PROXY' ) );
}
81 changes: 52 additions & 29 deletions includes/functions-http.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,44 @@ function yourls_http_post_body( $url, $headers = array(), $data = array(), $opti
}

/**
* Check if a proxy is defined for HTTP requests
* Get proxy information
*
* @uses YOURLS_PROXY
* @since 1.7
* @return bool true if a proxy is defined, false otherwise
* @uses YOURLS_PROXY YOURLS_PROXY_USERNAME YOURLS_PROXY_PASSWORD
* @since 1.7.1
* @return mixed false if no proxy is defined, or string like '10.0.0.201:3128' or array like ('10.0.0.201:3128', 'username', 'password')
*/
function yourls_http_get_proxy() {
$proxy = false;

if( defined( 'YOURLS_PROXY' ) && !empty( 'YOURLS_PROXY' ) ) {
$proxy = YOURLS_PROXY;
// Username (?) and password can be defined as an empty string : no check for empty()
if( defined( 'YOURLS_PROXY_USERNAME' ) && defined( 'YOURLS_PROXY_PASSWORD' ) ) {
$proxy = array( YOURLS_PROXY, YOURLS_PROXY_USERNAME, YOURLS_PROXY_PASSWORD );
}
}

return yourls_apply_filter( 'http_get_proxy', $proxy );
}

/**
* Get list of hosts that should bypass the proxy
*
* @uses YOURLS_PROXY_BYPASS_HOSTS
* @since 1.7.1
* @return mixed false if no host defined, or string like "example.com, *.mycorp.com"
*/
function yourls_http_proxy_is_defined() {
return yourls_apply_filter( 'http_proxy_is_defined', defined( 'YOURLS_PROXY' ) );
function yourls_http_get_proxy_bypass_host() {
$hosts = defined( 'YOURLS_PROXY_BYPASS_HOSTS' ) ? YOURLS_PROXY_BYPASS_HOSTS : false;

return yourls_apply_filter( 'http_get_proxy_bypass_host', $hosts );
}

/**
* Default HTTP requests options for YOURLS
*
* For a list of all available options, see function request() in /includes/Requests/Requests.php
*
* @uses YOURLS_PROXY
* @uses YOURLS_PROXY_USERNAME
* @uses YOURLS_PROXY_PASSWORD
* @since 1.7
* @return array Options
*/
Expand All @@ -95,13 +115,9 @@ function yourls_http_default_options() {
'follow_redirects' => true,
'redirects' => 3,
);

if( yourls_http_proxy_is_defined() ) {
if( defined( 'YOURLS_PROXY_USERNAME' ) && defined( 'YOURLS_PROXY_PASSWORD' ) ) {
$options['proxy'] = array( YOURLS_PROXY, YOURLS_PROXY_USERNAME, YOURLS_PROXY_PASSWORD );
} else {
$options['proxy'] = YOURLS_PROXY;
}

if( yourls_http_get_proxy() ) {
$options['proxy'] = yourls_http_get_proxy();
}

return yourls_apply_filter( 'http_default_options', $options );
Expand Down Expand Up @@ -143,21 +159,28 @@ function yourls_send_through_proxy( $url ) {
if( in_array( $check['host'], $local ) )
return false;

if ( !defined( 'YOURLS_PROXY_BYPASS_HOSTS' ) )
return true;

// Check YOURLS_PROXY_BYPASS_HOSTS
$bypass = yourls_http_get_proxy_bypass_host();

if( $bypass === false OR $bypass === '' ) {
return true;
}

// Build array of hosts to bypass
static $bypass_hosts;
static $wildcard_regex = false;
if ( null == $bypass_hosts ) {
$bypass_hosts = preg_split( '|,\s*|', YOURLS_PROXY_BYPASS_HOSTS );

if ( false !== strpos( YOURLS_PROXY_BYPASS_HOSTS, '*' ) ) {
$wildcard_regex = array();
foreach ( $bypass_hosts as $host )
$wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) );
$wildcard_regex = '/^(' . implode( '|', $wildcard_regex ) . ')$/i';
}
$bypass_hosts = preg_split( '|\s*,\s*|', $bypass );

if ( false !== strpos( $bypass, '*' ) ) {
$wildcard_regex = array();
foreach ( $bypass_hosts as $host ) {
$wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) );
if ( false !== strpos( $host, '*' ) ) {
$wildcard_regex[] = str_replace( '\*\.', '', preg_quote( $host, '/' ) );
}
}
$wildcard_regex = '/^(' . implode( '|', $wildcard_regex ) . ')$/i';
}
}

if ( !empty( $wildcard_regex ) )
Expand All @@ -182,7 +205,7 @@ function yourls_http_request( $type, $url, $headers, $data, $options ) {

$options = array_merge( yourls_http_default_options(), $options );

if( yourls_http_proxy_is_defined() && !yourls_send_through_proxy( $url ) )
if( yourls_http_get_proxy() && !yourls_send_through_proxy( $url ) )
unset( $options['proxy'] );

try {
Expand Down

0 comments on commit 18cc1a4

Please sign in to comment.