Skip to content

Commit

Permalink
Merge branch 'perf'
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed May 8, 2012
2 parents 27b9abd + 55e50b2 commit c2cc291
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 257 deletions.
14 changes: 6 additions & 8 deletions Client.php
Expand Up @@ -274,18 +274,16 @@ protected function prepareRequest(RequestInterface $request)
}
// Add any curl options that might in the config to the request
if (strpos($key, 'curl.') === 0) {
$curlOption = str_replace('curl.', '', $key);
$curlOption = substr($key, 5);
// Convert constants represented as string to constant int values
if (defined($curlOption)) {
$curlValue = is_string($value) && defined($value) ? constant($value) : $value;
$request->getCurlOptions()->set(constant($curlOption), $curlValue);
$value = is_string($value) && defined($value) ? constant($value) : $value;
$curlOption = constant($curlOption);
}
} elseif (strpos($key, 'cache.') === 0) {
// Add any cache options from the config to the request
// Add any curl options that might in the config to the request
$request->getParams()->set($key, $value);
$request->getCurlOptions()->set($curlOption, $value);
} elseif (strpos($key, 'params.') === 0) {
// Add request specific parameters to all requests (prefix with 'params.')
$request->getParams()->set(str_replace('params.', '', $key), $value);
$request->getParams()->set(substr($key, 7), $value);
}
}

Expand Down
64 changes: 42 additions & 22 deletions Curl/CurlHandle.php
Expand Up @@ -33,6 +33,14 @@ class CurlHandle
/**
* Factory method to create a new curl handle based on an HTTP request
*
* There are some helpful options you can set to enable specific behavior:
* - disabled_wire: This is a performance improvement that will disable
* some debugging functionality with cURL. The functionality
* it disabled allows you to see the exact HTTP request sent over
* the wire.
* - progress: Set to true to enable progress function callbacks. Most
* People don't need this, so it has been disabled by default.
*
* @param RequestInterface $request Request
*
* @return CurlHandle
Expand All @@ -41,7 +49,7 @@ public static function factory(RequestInterface $request)
{
$handle = curl_init();
$mediator = new RequestMediator($request);
$protocolVersion = $request->getProtocolVersion() === '1.0' ? CURL_HTTP_VERSION_1_0 : CURL_HTTP_VERSION_1_1;
$requestCurlOptions = $request->getCurlOptions();

// Array of default cURL options.
$curlOptions = array(
Expand All @@ -53,15 +61,23 @@ public static function factory(RequestInterface $request)
CURLOPT_USERAGENT => (string) $request->getHeader('User-Agent'),
CURLOPT_ENCODING => '', // Supports all encodings
CURLOPT_PORT => $request->getPort(),
CURLOPT_HTTP_VERSION => $protocolVersion,
CURLOPT_NOPROGRESS => false,
CURLOPT_STDERR => fopen('php://temp', 'r+'),
CURLOPT_VERBOSE => true,
CURLOPT_HTTP_VERSION => $request->getProtocolVersion() === '1.0' ? CURL_HTTP_VERSION_1_0 : CURL_HTTP_VERSION_1_1,
CURLOPT_HTTPHEADER => array(),
CURLOPT_HEADERFUNCTION => array($mediator, 'receiveResponseHeader'),
CURLOPT_PROGRESSFUNCTION => array($mediator, 'progress')
CURLOPT_HEADERFUNCTION => array($mediator, 'receiveResponseHeader')
);

// Enable the progress function if the 'progress' param was set
if ($requestCurlOptions->get('progress')) {
$curlOptions[CURLOPT_PROGRESSFUNCTION] = array($mediator, 'progress');
$curlOptions[CURLOPT_NOPROGRESS] = false;
}

// Enable curl debug information if the 'debug' param was set
if (!$requestCurlOptions->get('disable_wire')) {
$curlOptions[CURLOPT_STDERR] = fopen('php://temp', 'r+');
$curlOptions[CURLOPT_VERBOSE] = true;
}

// HEAD requests need no response body, everything else might
if ($request->getMethod() != 'HEAD') {
$curlOptions[CURLOPT_WRITEFUNCTION] = array($mediator, 'writeResponseBody');
Expand Down Expand Up @@ -96,7 +112,6 @@ public static function factory(RequestInterface $request)
unset($headers['Content-Length']);
$curlOptions[CURLOPT_INFILESIZE] = (int) (string) $request->getHeader('Content-Length');
}

break;
}

Expand All @@ -123,22 +138,27 @@ public static function factory(RequestInterface $request)
}

// Set custom cURL options
foreach ($request->getCurlOptions() as $key => $value) {
$curlOptions[$key] = $value;
foreach ($requestCurlOptions as $key => $value) {
if (is_numeric($key)) {
$curlOptions[$key] = $value;
}
}

// Check if any headers or cURL options are blacklisted
$client = $request->getClient();
if ($client && $client->getConfig('curl.blacklist')) {
foreach ($client->getConfig('curl.blacklist') as $value) {
if (strpos($value, 'header.') === 0) {
$blacklistHeader = substr($value, 7);
// Remove headers that may have previously been set
// but are supposed to be blacklisted
unset($headers[$blacklistHeader]);
$headers[$blacklistHeader] = '';
} else {
unset($curlOptions[$value]);
if ($client) {
$blacklist = $client->getConfig('curl.blacklist');
if ($blacklist) {
foreach ($blacklist as $value) {
if (strpos($value, 'header.') === 0) {
$blacklistHeader = substr($value, 7);
// Remove headers that may have previously been set
// but are supposed to be blacklisted
unset($headers[$blacklistHeader]);
$headers[$blacklistHeader] = '';
} else {
unset($curlOptions[$value]);
}
}
}
}
Expand Down Expand Up @@ -207,7 +227,7 @@ public function close()
*/
public function isAvailable()
{
return is_resource($this->handle) && false != curl_getinfo($this->handle, CURLINFO_EFFECTIVE_URL);
return is_resource($this->handle);
}

/**
Expand Down Expand Up @@ -311,7 +331,7 @@ public function getUrl()
*/
public function getHandle()
{
return $this->handle && $this->isAvailable() ? $this->handle : null;
return $this->isAvailable() ? $this->handle : null;
}

/**
Expand Down

0 comments on commit c2cc291

Please sign in to comment.