Skip to content

Commit

Permalink
Merge pull request #26 from maxmind/greg/reuse-curl
Browse files Browse the repository at this point in the history
Reuse curl handles
  • Loading branch information
horgh authored Dec 9, 2019
2 parents bad3762 + e2f1885 commit ceee26d
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 31 deletions.
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ language: php
dist: trusty
matrix:
include:
- php: '5.4'
- php: '5.5'
- php: '5.6'
- php: '7.0'
- php: '7.1'
- php: '7.2'
- php: '7.3'
- php: '7.4'
env:
- RUN_LINTER=1
- RUN_SNYK=1
- php: nightly
allow_failures:
- php: nightly
cache:
directories:
- $HOME/.composer/cache
before_install:
- composer self-update
- composer install --dev -n --prefer-source
- composer install --no-interaction
- phpenv rehash
- "if [[ $RUN_SNYK && $SNYK_TOKEN ]]; then sudo apt-get install -y nodejs; npm install -g snyk; fi"
install:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

0.6.0
------------------

* Curl handles are now reused across requests. Pull request by Willem
Stuursma-Ruwen. GitHub #24.

0.5.0 (2018-02-12)
------------------

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ shared code between MaxMind's various web service client APIs.

## Requirements ##

The library requires PHP 5.4 or greater.
The library requires PHP 5.6 or greater.

There are several other dependencies as defined in the `composer.json` file.

Expand All @@ -20,6 +20,6 @@ This API uses [Semantic Versioning](http://semver.org/).

## Copyright and License ##

This software is Copyright (c) 2015-2018 by MaxMind, Inc.
This software is Copyright (c) 2015-2019 by MaxMind, Inc.

This is free software, licensed under the Apache License, Version 2.0.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"php": ">=5.4",
"php": ">=5.6",
"composer/ca-bundle": "^1.0.3",
"ext-curl": "*",
"ext-json": "*"
Expand Down
2 changes: 1 addition & 1 deletion src/WebService/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ private function getCaBundle()
// On OS X, when the SSL version is "SecureTransport", the system's
// keychain will be used.
if ($curlVersion['ssl_version'] === 'SecureTransport') {
return;
return null;
}
$cert = CaBundle::getSystemCaRootBundlePath();

Expand Down
40 changes: 33 additions & 7 deletions src/WebService/Http/CurlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,36 @@
*/
class CurlRequest implements Request
{
/**
* @var resource
*/
private $ch;

/**
* @var string
*/
private $url;

/**
* @var array
*/
private $options;

/**
* @param $url
* @param $options
* @param string $url
* @param array $options
*/
public function __construct($url, $options)
{
$this->url = $url;
$this->options = $options;
$this->ch = $options['curlHandle'];
}

/**
* @param $body
* @param string $body
*
* @throws HttpException
*
* @return array
*/
Expand Down Expand Up @@ -53,11 +68,16 @@ public function get()
*/
private function createCurl()
{
$curl = curl_init($this->url);
curl_reset($this->ch);

$opts = [];
$opts[CURLOPT_URL] = $this->url;

if (!empty($this->options['caBundle'])) {
$opts[CURLOPT_CAINFO] = $this->options['caBundle'];
}

$opts[CURLOPT_ENCODING] = '';
$opts[CURLOPT_SSL_VERIFYHOST] = 2;
$opts[CURLOPT_FOLLOWLOCATION] = false;
$opts[CURLOPT_SSL_VERIFYPEER] = true;
Expand All @@ -83,11 +103,18 @@ private function createCurl()
$opts[CURLOPT_TIMEOUT] = ceil($timeout);
}

curl_setopt_array($curl, $opts);
curl_setopt_array($this->ch, $opts);

return $curl;
return $this->ch;
}

/**
* @param resource $curl
*
* @throws HttpException
*
* @return array
*/
private function execute($curl)
{
$body = curl_exec($curl);
Expand All @@ -103,7 +130,6 @@ private function execute($curl)

$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
curl_close($curl);

return [$statusCode, $contentType, $body];
}
Expand Down
6 changes: 3 additions & 3 deletions src/WebService/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
interface Request
{
/**
* @param $url
* @param $options
* @param string $url
* @param array $options
*/
public function __construct($url, $options);

/**
* @param $body
* @param string $body
*
* @return mixed
*/
Expand Down
23 changes: 20 additions & 3 deletions src/WebService/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,35 @@
*/
class RequestFactory
{
/**
* Keep the cURL resource here, so that if there are multiple API requests
* done the connection is kept alive, SSL resumption can be used
* etcetera.
*
* @var resource
*/
private $ch;

public function __construct()
{
$this->ch = curl_init();
}

public function __destruct()
{
curl_close($this->ch);
}

/**
* @param $url
* @param $options
* @param string $url
* @param array $options
*
* @return CurlRequest
* @return Request
*/
public function request($url, $options)
{
$options['curlHandle'] = $this->ch;

return new CurlRequest($url, $options);
}
}
26 changes: 16 additions & 10 deletions tests/MaxMind/Test/WebService/Http/CurlRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@
*/
class CurlRequestTest extends TestCase
{
private $options = [
'caBundle' => null,
'connectTimeout' => 0,
'headers' => [],
'proxy' => null,
'timeout' => 0,
'userAgent' => 'Test',
];
private $options;

public function setUp()
{
$this->options = [
'caBundle' => null,
'connectTimeout' => 0,
'curlHandle' => curl_init(),
'headers' => [],
'proxy' => null,
'timeout' => 0,
'userAgent' => 'Test',
];
}

/**
* @expectedException \MaxMind\Exception\HttpException
* @expectedExceptionMessage cURL error (6): Could not resolve host: invalid host
* @expectedExceptionMessageRegExp /^cURL error.*invalid host/
*/
public function testGet()
{
Expand All @@ -41,7 +47,7 @@ public function testGet()

/**
* @expectedException \MaxMind\Exception\HttpException
* @expectedExceptionMessage cURL error (6): Could not resolve host: invalid host
* @expectedExceptionMessageRegExp /^cURL error.*invalid host/
*/
public function testPost()
{
Expand Down

0 comments on commit ceee26d

Please sign in to comment.