From 7953de7a19dca71ce53639473e57c76a3d80908e Mon Sep 17 00:00:00 2001 From: gggeek Date: Thu, 26 May 2022 09:00:30 +0000 Subject: [PATCH] Improve testing around http2 usage --- src/Client.php | 4 ++++ src/Helper/Http.php | 5 +++-- tests/5ServerTest.php | 6 ++++++ tests/6HTTPTest.php | 29 +++++++++++++++++++++++++++++ tests/ci/config/apache_vhost | 9 ++++++--- 5 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Client.php b/src/Client.php index 08a9a192..adecb1fa 100644 --- a/src/Client.php +++ b/src/Client.php @@ -4,6 +4,7 @@ namespace PhpXmlRpc; use PhpXmlRpc\Helper\Logger; use PhpXmlRpc\Helper\XMLParser; + /** * Used to represent a client of an XML-RPC server. */ @@ -137,11 +138,13 @@ class Client * e.g. /xmlrpc/server.php * e.g. http://phpxmlrpc.sourceforge.net/server.php * e.g. https://james:bond@secret.service.com:443/xmlrpcserver?agent=007 + * e.g. http2tls://fast-and-secure-services/endpoint * @param string $server the server name / ip address * @param integer $port the port the server is listening on, when omitted defaults to 80 or 443 depending on * protocol used * @param string $method the http protocol variant: defaults to 'http'; 'https', 'http11', 'http2' and 'http2tls' can * be used if CURL is installed. The value set here can be overridden in any call to $this->send(). + * Use 'http2' to make the lib attempt to use http/2 without tls and 'http2tls' for secure http/2 */ public function __construct($path, $server = '', $port = '', $method = '') { @@ -477,6 +480,7 @@ class Client * for $timeout seconds, the connection will be closed). * @param string $method valid values are 'http', 'http11', 'https', 'http2' and 'http2tls'. If left unspecified, * the http protocol chosen during creation of the object will be used. + * Use 'http2' to make the lib attempt to use http/2 without tls and 'http2tls' for secure http/2 * * @return Response|Response[] Note that the client will always return a Response object, even if the call fails * @todo allow throwing exceptions instead of returning responses in case of failed calls and/or Fault responses diff --git a/src/Helper/Http.php b/src/Helper/Http.php index 77d4a9d2..7d6cb48e 100644 --- a/src/Helper/Http.php +++ b/src/Helper/Http.php @@ -121,8 +121,9 @@ class Http } } - if (preg_match('/^HTTP\/[0-9](?:\.[0-9])? ([0-9]{3}) /', $data, $matches)) { - $httpResponse['status_code'] = $matches[1]; + if (preg_match('/^HTTP\/([0-9](?:\.[0-9])?) ([0-9]{3}) /', $data, $matches)) { + $httpResponse['protocol_version'] = $matches[1]; + $httpResponse['status_code'] = $matches[2]; } if ($httpResponse['status_code'] !== '200') { diff --git a/tests/5ServerTest.php b/tests/5ServerTest.php index 0345b797..3fa4fe84 100644 --- a/tests/5ServerTest.php +++ b/tests/5ServerTest.php @@ -141,6 +141,7 @@ class ServerTest extends PhpXmlRpc_PolyfillTestCase if (is_array($r)) { return $r; } + $this->validateResponse($r); if (is_array($errorCode)) { $this->assertContains($r->faultCode(), $errorCode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString()); } else { @@ -157,6 +158,11 @@ class ServerTest extends PhpXmlRpc_PolyfillTestCase } } + protected function validateResponse($r) + { + // to be implemented in subclasses + } + /** * Adds (and replaces) query params to the url currently used by the client * @param array $data diff --git a/tests/6HTTPTest.php b/tests/6HTTPTest.php index 83c361c7..948117d5 100644 --- a/tests/6HTTPTest.php +++ b/tests/6HTTPTest.php @@ -13,6 +13,8 @@ include_once __DIR__ . '/5ServerTest.php'; */ class HTTPTest extends ServerTest { + protected $expectHttp2 = false; + /** * Returns all test methods from the base class, except the ones which failed already * @@ -313,12 +315,19 @@ class HTTPTest extends ServerTest { $this->markTestSkipped('CURL missing: cannot test http/2'); return; + } else if (!defined('CURL_HTTP_VERSION_2')) + { + $this->markTestSkipped('CURL http/2 support missing: cannot test http/2'); + return; } $this->method = 'http2'; // not an error the double assignment! $this->client->method = 'http2'; //$this->client->keepalive = false; // q: is this a good idea? + + $this->expectHttp2 = true; $this->$method(); + $this->expectHttp2 = false; } /** @@ -335,6 +344,10 @@ class HTTPTest extends ServerTest { $this->markTestSkipped('HTTPS SERVER definition missing: cannot test http/2 tls'); return; + } else if (!defined('CURL_HTTP_VERSION_2')) + { + $this->markTestSkipped('CURL http/2 support missing: cannot test http/2 tls'); + return; } $this->client->server = $this->args['HTTPSSERVER']; @@ -345,7 +358,9 @@ class HTTPTest extends ServerTest $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']); $this->client->setSSLVersion($this->args['SSLVERSION']); + $this->expectHttp2 = true; $this->$method(); + $this->expectHttp2 = false; } /** @@ -423,4 +438,18 @@ class HTTPTest extends ServerTest $this->$method(); } + + /** + * @param \PhpXmlRpc\Response $r + * @return void + */ + protected function validateResponse($r) + { + /// @todo check $r->httpResponse()['protocol_version'] + if ($this->expectHttp2) { + + } else { + + } + } } diff --git a/tests/ci/config/apache_vhost b/tests/ci/config/apache_vhost index 6c7b6757..c040f8f7 100644 --- a/tests/ci/config/apache_vhost +++ b/tests/ci/config/apache_vhost @@ -2,11 +2,11 @@ # HTTPSERVER # TESTS_ROOT_DIR -# Enable http2 -Protocols h2 h2c http/1.1 - + # Enable http2 + Protocols h2c http/1.1 + DocumentRoot ${TESTS_ROOT_DIR} #ErrorLog "${TESTS_ROOT_DIR}/apache_error.log" @@ -33,6 +33,9 @@ Protocols h2 h2c http/1.1 + # Enable http2 + Protocols h2 http/1.1 + DocumentRoot ${TESTS_ROOT_DIR} #ErrorLog "${TESTS_ROOT_DIR}/apache_error.log" -- 2.47.0