From 6f71a582d9cde3afb88f42c3f18256e711c51dc5 Mon Sep 17 00:00:00 2001 From: Alexander Leitner Date: Mon, 1 Dec 2014 13:32:00 -0500 Subject: [PATCH 1/4] Curl options can now be set in the adapter class --- examples/SetCurlOptions.php | 19 ++++++ src/Bitpay/Client/Adapter/CurlAdapter.php | 72 +++++++++++++++++------ src/Bitpay/Client/Request.php | 23 +++++++- tests/Bitpay/Client/RequestTest.php | 10 ++++ 4 files changed, 106 insertions(+), 18 deletions(-) create mode 100644 examples/SetCurlOptions.php diff --git a/examples/SetCurlOptions.php b/examples/SetCurlOptions.php new file mode 100644 index 00000000..cb839b41 --- /dev/null +++ b/examples/SetCurlOptions.php @@ -0,0 +1,19 @@ + 443, + CURLOPT_TIMEOUT => 10, + CURLOPT_SSL_VERIFYPEER => 1, + CURLOPT_SSL_VERIFYHOST => 2, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_FORBID_REUSE => 1, + CURLOPT_FRESH_CONNECT => 1, + CURLOPT_HEADER => true, + ); + +// If nothing is passed into the CurlAdapter +// then default values are used +$adapter = new Bitpay\Client\Adapter\CurlAdapter($curl_options); diff --git a/src/Bitpay/Client/Adapter/CurlAdapter.php b/src/Bitpay/Client/Adapter/CurlAdapter.php index 65ef09a7..ae75caa6 100644 --- a/src/Bitpay/Client/Adapter/CurlAdapter.php +++ b/src/Bitpay/Client/Adapter/CurlAdapter.php @@ -19,29 +19,43 @@ */ class CurlAdapter implements AdapterInterface { + /** + * @var array + */ + protected $curlOptions; + + /** + * @param array $options + */ + public function __construct(array $curlOptions = array()) + { + $this->curlOptions = $curlOptions; + } + + public function getCurlOptions() + { + return $this->curlOptions; + } + /** * @inheritdoc */ public function sendRequest(RequestInterface $request) { $curl = curl_init(); - curl_setopt_array( - $curl, - array( - CURLOPT_URL => $request->getUri(), - CURLOPT_PORT => 443, - CURLOPT_CUSTOMREQUEST => $request->getMethod(), - CURLOPT_HTTPHEADER => $request->getHeaderFields(), - CURLOPT_TIMEOUT => 10, - CURLOPT_SSL_VERIFYPEER => 1, - CURLOPT_SSL_VERIFYHOST => 2, - CURLOPT_CAINFO => __DIR__ . '/ca-bundle.crt', - CURLOPT_RETURNTRANSFER => true, - CURLOPT_FORBID_REUSE => 1, - CURLOPT_FRESH_CONNECT => 1, - CURLOPT_HEADER => true, - ) - ); + + $default_curl_options = $this->getCurlDefaultOptions($request); + + foreach ($this->getCurlOptions() as $curl_option_key => $curl_option_value) { + if (!is_null($curl_option_value)) { + if (CURLOPT_PORT === $curl_option_key) { + $request->setPort($curl_option_value); + } + $default_curl_options[$curl_option_key] = $curl_option_value; + } + } + + curl_setopt_array($curl, $default_curl_options); if (RequestInterface::METHOD_POST == $request->getMethod()) { curl_setopt_array( @@ -68,4 +82,28 @@ public function sendRequest(RequestInterface $request) return $response; } + + /** + * Returns an array of default curl settings to use + * + * @param RequestInterface $request + * @return array + */ + private function getCurlDefaultOptions(RequestInterface $request) + { + return array( + CURLOPT_URL => $request->getUri(), + CURLOPT_PORT => $request->getPort(), + CURLOPT_CUSTOMREQUEST => $request->getMethod(), + CURLOPT_HTTPHEADER => $request->getHeaderFields(), + CURLOPT_TIMEOUT => 10, + CURLOPT_SSL_VERIFYPEER => 1, + CURLOPT_SSL_VERIFYHOST => 2, + CURLOPT_CAINFO => __DIR__.'/ca-bundle.crt', + CURLOPT_RETURNTRANSFER => true, + CURLOPT_FORBID_REUSE => 1, + CURLOPT_FRESH_CONNECT => 1, + CURLOPT_HEADER => true, + ); + } } diff --git a/src/Bitpay/Client/Request.php b/src/Bitpay/Client/Request.php index 7b139dd0..f73e1bf4 100644 --- a/src/Bitpay/Client/Request.php +++ b/src/Bitpay/Client/Request.php @@ -51,6 +51,13 @@ class Request implements RequestInterface */ protected $path; + /** + * Default is 443 but should be changed by whatever is passed in through the Adapter. + * + * @var integer + */ + protected $port; + /** */ public function __construct() @@ -61,8 +68,12 @@ public function __construct() 'X-BitPay-Plugin-Info' => null, ); + $this->port = 443; + // Default method is POST $this->method = self::METHOD_POST; + + } /** @@ -93,7 +104,17 @@ public function isMethod($method) */ public function getPort() { - return 443; + return $this->port; + } + + /** + * This is called in the Adapter + * + * @inheritdoc + */ + public function setPort($port) + { + $this->port = $port; } /** diff --git a/tests/Bitpay/Client/RequestTest.php b/tests/Bitpay/Client/RequestTest.php index d90e00f3..9817fc81 100644 --- a/tests/Bitpay/Client/RequestTest.php +++ b/tests/Bitpay/Client/RequestTest.php @@ -77,11 +77,21 @@ public function testIsMethodUnknown() $this->assertFalse($this->request->isMethod('unknown method')); } + /** + * @depends testGetPort + */ public function testGetPort() { $this->assertSame(443, $this->request->getPort()); } + public function testSetPort() + { + $this->request->setPort(444); + $this->assertSame(444, $this->request->getPort()); + } + + public function testGetSchema() { $this->assertSame('https', $this->request->getSchema()); From 6949086e45e45a8c2b2e8f461df478018f95d6a1 Mon Sep 17 00:00:00 2001 From: Alexander Leitner Date: Tue, 2 Dec 2014 13:44:00 -0500 Subject: [PATCH 2/4] updated CurlAdapter and tests --- src/Bitpay/Client/Adapter/CurlAdapter.php | 7 +- src/Bitpay/Client/Request.php | 2 - .../Bitpay/Client/Adapter/CurlAdapterTest.php | 64 +++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 tests/Bitpay/Client/Adapter/CurlAdapterTest.php diff --git a/src/Bitpay/Client/Adapter/CurlAdapter.php b/src/Bitpay/Client/Adapter/CurlAdapter.php index ae75caa6..b360ec4a 100644 --- a/src/Bitpay/Client/Adapter/CurlAdapter.php +++ b/src/Bitpay/Client/Adapter/CurlAdapter.php @@ -25,13 +25,18 @@ class CurlAdapter implements AdapterInterface protected $curlOptions; /** - * @param array $options + * @param array $curlOptions */ public function __construct(array $curlOptions = array()) { $this->curlOptions = $curlOptions; } + /** + * Returns an array of curl settings to use + * + * @return array + */ public function getCurlOptions() { return $this->curlOptions; diff --git a/src/Bitpay/Client/Request.php b/src/Bitpay/Client/Request.php index f73e1bf4..bf8c986f 100644 --- a/src/Bitpay/Client/Request.php +++ b/src/Bitpay/Client/Request.php @@ -72,8 +72,6 @@ public function __construct() // Default method is POST $this->method = self::METHOD_POST; - - } /** diff --git a/tests/Bitpay/Client/Adapter/CurlAdapterTest.php b/tests/Bitpay/Client/Adapter/CurlAdapterTest.php new file mode 100644 index 00000000..561c97a0 --- /dev/null +++ b/tests/Bitpay/Client/Adapter/CurlAdapterTest.php @@ -0,0 +1,64 @@ +request = new Request(); + } + + public function testConstruct() + { + $adapter = new CurlAdapter(); + $this->assertNotNull($adapter->getCurlOptions()); + } + + public function testGetCurlOptions() + { + $adapter = new CurlAdapter(); + $this->assertEquals(array(), $adapter->getCurlOptions()); + } + + public function testSendRequestWithException() + { + $http = $this->getMock('HttpRequest'); + $this->setExpectedException('Exception'); + + $curl_options = array( + CURLOPT_URL => "example.com", + CURLOPT_PORT => 443, + CURLOPT_SSL_VERIFYPEER => 1, + CURLOPT_SSL_VERIFYHOST => 2, + ); + + $adapter = new CurlAdapter($curl_options); + $response = $adapter->sendRequest($this->request); + } + + public function testSendRequestWithoutException() + { + $http = $this->getMock('HttpRequest'); + + $curl_options = array( + CURLOPT_URL => "www.bitpay.com", + CURLOPT_PORT => 443, + CURLOPT_SSL_VERIFYPEER => 1, + CURLOPT_SSL_VERIFYHOST => 2, + ); + + $adapter = new CurlAdapter($curl_options); + $response = $adapter->sendRequest($this->request); + $this->assertNotNull($response); + } + +} \ No newline at end of file From fce2981a97ff741413d24b2cf213c19d103ff7d5 Mon Sep 17 00:00:00 2001 From: Alexander Leitner Date: Wed, 3 Dec 2014 18:10:50 -0500 Subject: [PATCH 3/4] Port and Curl settings can be changed, along with URI for working with local instance of bitpay --- examples/SetCurlOptions.php | 15 +++--- src/Bitpay/Client/Adapter/CurlAdapter.php | 3 -- src/Bitpay/Client/Client.php | 11 ++++- src/Bitpay/Network/Customnet.php | 47 +++++++++++++++++++ src/Bitpay/Network/Livenet.php | 5 ++ src/Bitpay/Network/NetworkInterface.php | 7 +++ src/Bitpay/Network/Testnet.php | 5 ++ .../Bitpay/Client/Adapter/CurlAdapterTest.php | 4 +- 8 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 src/Bitpay/Network/Customnet.php diff --git a/examples/SetCurlOptions.php b/examples/SetCurlOptions.php index cb839b41..f28962f1 100644 --- a/examples/SetCurlOptions.php +++ b/examples/SetCurlOptions.php @@ -2,18 +2,17 @@ require __DIR__ . '/../vendor/autoload.php'; +//When running bitpay on your local server +$network = new Bitpay\Network\Customnet("alex.bp", 8088, true); + //Customize the curl options $curl_options = array( - CURLOPT_PORT => 443, - CURLOPT_TIMEOUT => 10, - CURLOPT_SSL_VERIFYPEER => 1, - CURLOPT_SSL_VERIFYHOST => 2, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_FORBID_REUSE => 1, - CURLOPT_FRESH_CONNECT => 1, - CURLOPT_HEADER => true, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_SSL_VERIFYHOST => false, ); // If nothing is passed into the CurlAdapter // then default values are used $adapter = new Bitpay\Client\Adapter\CurlAdapter($curl_options); +var_dump($network); +var_dump($adapter->getCurlOptions()); \ No newline at end of file diff --git a/src/Bitpay/Client/Adapter/CurlAdapter.php b/src/Bitpay/Client/Adapter/CurlAdapter.php index b360ec4a..833a7254 100644 --- a/src/Bitpay/Client/Adapter/CurlAdapter.php +++ b/src/Bitpay/Client/Adapter/CurlAdapter.php @@ -53,9 +53,6 @@ public function sendRequest(RequestInterface $request) foreach ($this->getCurlOptions() as $curl_option_key => $curl_option_value) { if (!is_null($curl_option_value)) { - if (CURLOPT_PORT === $curl_option_key) { - $request->setPort($curl_option_value); - } $default_curl_options[$curl_option_key] = $curl_option_value; } } diff --git a/src/Bitpay/Client/Client.php b/src/Bitpay/Client/Client.php index e792d1ca..1507dc7f 100644 --- a/src/Bitpay/Client/Client.php +++ b/src/Bitpay/Client/Client.php @@ -590,9 +590,17 @@ protected function addSignatureHeader(RequestInterface $request) throw new \Exception('Please set your Private Key'); } + if (isset($this->network->isPortRequiredInUrl)) { + if ($this->network->isPortRequiredInUrl === true) { + $url = $request->getUriWithPort(); + } + } else { + $url = $request->getUri(); + } + $message = sprintf( '%s%s', - $request->getUri(), + $url, $request->getBody() ); @@ -608,6 +616,7 @@ protected function createNewRequest() { $request = new Request(); $request->setHost($this->network->getApiHost()); + $request->setPort($this->network->getApiPort()); $this->prepareRequestHeaders($request); return $request; diff --git a/src/Bitpay/Network/Customnet.php b/src/Bitpay/Network/Customnet.php new file mode 100644 index 00000000..c3747182 --- /dev/null +++ b/src/Bitpay/Network/Customnet.php @@ -0,0 +1,47 @@ +host_url = $url; + $this->host_port = $port; + $this->isPortRequiredInUrl = $isPortRequiredInUrl; + } + + public function getName() + { + return 'Custom Network'; + } + + public function getAddressVersion() + { + return 0x00; + } + + public function getApiHost() + { + return $this->host_url; + } + + public function getApiPort() + { + return $this->host_port; + } +} diff --git a/src/Bitpay/Network/Livenet.php b/src/Bitpay/Network/Livenet.php index c52ac079..ae91f715 100644 --- a/src/Bitpay/Network/Livenet.php +++ b/src/Bitpay/Network/Livenet.php @@ -26,4 +26,9 @@ public function getApiHost() { return 'bitpay.com'; } + + public function getApiPort() + { + return 443; + } } diff --git a/src/Bitpay/Network/NetworkInterface.php b/src/Bitpay/Network/NetworkInterface.php index c858750d..ccb32df0 100644 --- a/src/Bitpay/Network/NetworkInterface.php +++ b/src/Bitpay/Network/NetworkInterface.php @@ -33,4 +33,11 @@ public function getAddressVersion(); * @return string */ public function getApiHost(); + + /** + * The port of the host + * + * @return integer + */ + public function getApiPort(); } diff --git a/src/Bitpay/Network/Testnet.php b/src/Bitpay/Network/Testnet.php index 8d8a0bbd..43131337 100644 --- a/src/Bitpay/Network/Testnet.php +++ b/src/Bitpay/Network/Testnet.php @@ -26,4 +26,9 @@ public function getApiHost() { return 'test.bitpay.com'; } + + public function getApiPort() + { + return 443; + } } diff --git a/tests/Bitpay/Client/Adapter/CurlAdapterTest.php b/tests/Bitpay/Client/Adapter/CurlAdapterTest.php index 561c97a0..0e769403 100644 --- a/tests/Bitpay/Client/Adapter/CurlAdapterTest.php +++ b/tests/Bitpay/Client/Adapter/CurlAdapterTest.php @@ -35,8 +35,7 @@ public function testSendRequestWithException() $this->setExpectedException('Exception'); $curl_options = array( - CURLOPT_URL => "example.com", - CURLOPT_PORT => 443, + CURLOPT_URL => "www.example.com", CURLOPT_SSL_VERIFYPEER => 1, CURLOPT_SSL_VERIFYHOST => 2, ); @@ -51,7 +50,6 @@ public function testSendRequestWithoutException() $curl_options = array( CURLOPT_URL => "www.bitpay.com", - CURLOPT_PORT => 443, CURLOPT_SSL_VERIFYPEER => 1, CURLOPT_SSL_VERIFYHOST => 2, ); From 5c71d6a4df348d2e70305a691ee7535796ea0459 Mon Sep 17 00:00:00 2001 From: Alexander Leitner Date: Thu, 4 Dec 2014 10:48:40 -0500 Subject: [PATCH 4/4] Removed var dumps in example --- examples/SetCurlOptions.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/SetCurlOptions.php b/examples/SetCurlOptions.php index f28962f1..c129ec37 100644 --- a/examples/SetCurlOptions.php +++ b/examples/SetCurlOptions.php @@ -13,6 +13,4 @@ // If nothing is passed into the CurlAdapter // then default values are used -$adapter = new Bitpay\Client\Adapter\CurlAdapter($curl_options); -var_dump($network); -var_dump($adapter->getCurlOptions()); \ No newline at end of file +$adapter = new Bitpay\Client\Adapter\CurlAdapter($curl_options); \ No newline at end of file