Skip to content
Permalink
Browse files

Require and use the Requests library for HTTP client engine

Resolve #76
  • Loading branch information...
mkosturkov committed Jul 2, 2018
1 parent 30f669a commit 246ecf5183e67764bbad882db71328338293aed0
Showing with 3,410 additions and 5 deletions.
  1. +1 −0 CHANGELOG.md
  2. +3,314 −0 certificates/mozilla-cacert.pem
  3. +2 −1 composer.json
  4. +51 −2 composer.lock
  5. +9 −2 src/Environment/config-default.php
  6. +33 −0 src/HTTP/RequestsHTTPClient.php
@@ -6,6 +6,7 @@
### Added
* Support for `<PICTURE>` elements.
* `retrieverMap` path prefixes are now regexes.
* Bundle the `Requests` library and Mozilla CA certificates and use them as default HTTP client engine.


## [1.6.0] - 2018-06-27

Large diffs are not rendered by default.

@@ -9,7 +9,8 @@
"license": "AGPL-3.0-or-later",
"require": {
"php": ">=5.6",
"mrclay/jsmin-php": "2.3.2"
"mrclay/jsmin-php": "2.3.2",
"rmccue/requests": "^1.7"
},
"autoload": {
"psr-4": {

Some generated files are not rendered by default. Learn more.

@@ -1,8 +1,8 @@
<?php
use Kibo\Phast\Common\System;
use Kibo\Phast\HTTP\CURLClient;
use Kibo\Phast\HTTP\Request;
use Kibo\Phast\HTTP\RequestsHTTPClient;
return [
@@ -12,7 +12,14 @@
$_SERVER['HTTP_HOST'] => Request::fromGlobals()->getDocumentRoot()
],
'httpClient' => CURLClient::class,
'httpClient' => function () {
if (!class_exists(Requests::class)) {
require_once __DIR__ . '/../../vendor/rmccue/requests/library/Requests.php';
Requests::register_autoloader();
Requests::set_certificate_path(__DIR__ . '/../../certificates/mozilla-cacert.pem');
}
return new RequestsHTTPClient();
},
'cache' => [
'cacheRoot' => sys_get_temp_dir() . '/phast-cache-' . (new System())->getUserId(),
@@ -0,0 +1,33 @@
<?php
namespace Kibo\Phast\HTTP;
use Kibo\Phast\ValueObjects\URL;
class RequestsHTTPClient implements Client {
public function get(URL $url, array $headers = []) {
$response = \Requests::get((string)$url, $headers);
$response->throw_for_status();
return $this->makePhastResponse($response);
}
public function post(URL $url, $data, array $headers = []) {
$options = ['connect_timeout' => 2, 'timeout' => 10];
$response = \Requests::post((string)$url, $headers, $data, $options);
$response->throw_for_status();
return $this->makePhastResponse($response);
}
private function makePhastResponse(\Requests_Response $requestsResponse) {
$phastResponse = new Response();
$phastResponse->setContent($requestsResponse->body);
foreach ($requestsResponse->headers as $name => $value) {
$phastResponse->setHeader($name, $value);
}
return $phastResponse;
}
}

0 comments on commit 246ecf5

Please sign in to comment.
You can’t perform that action at this time.