4 * Class CRM_Cxn_CiviCxnHttp
6 * This extends the PhpHttp client used by CiviConnect and adds:
7 * - Force-cache support for GET requests
8 * - Compliance with SSL policy
10 class CRM_Cxn_CiviCxnHttp
extends \Civi\Cxn\Rpc\Http\PhpHttp
{
12 protected static $singleton = NULL;
15 * @var CRM_Utils_Cache_Interface|null
24 * @return CRM_Cxn_CiviCxnHttp
25 * @throws \CRM_Core_Exception
27 public static function singleton($fresh = FALSE) {
28 if (self
::$singleton === NULL ||
$fresh) {
29 $cache = CRM_Utils_Cache
::create([
30 'name' => 'CiviCxnHttp',
31 'type' => Civi
::settings()->get('debug_enabled') ?
'ArrayCache' : ['SqlGroup', 'ArrayCache'],
35 self
::$singleton = new CRM_Cxn_CiviCxnHttp($cache);
37 return self
::$singleton;
41 * The cache data store.
43 * @param CRM_Utils_Cache_Interface|null $cache
45 public function __construct($cache) {
46 $this->cache
= $cache;
55 * @param array $headers
56 * Array of headers (e.g. "Content-type" => "text/plain").
58 * array($headers, $blob, $code)
60 public function send($verb, $url, $blob, $headers = []) {
61 $lowVerb = strtolower($verb);
63 if ($lowVerb === 'get' && $this->cache
) {
64 $cachePath = 'get_' . md5($url);
65 $cacheLine = $this->cache
->get($cachePath);
66 if ($cacheLine && $cacheLine['expires'] > CRM_Utils_Time
::getTimeRaw()) {
67 return $cacheLine['data'];
71 $result = parent
::send($verb, $url, $blob, $headers);
73 if ($lowVerb === 'get' && $this->cache
) {
74 $expires = CRM_Utils_Http
::parseExpiration($result[0]);
75 if ($expires !== NULL) {
76 $cachePath = 'get_' . md5($url);
79 'expires' => $expires,
82 $this->cache
->set($cachePath, $cacheLine);
90 * Create stream options.
95 * @param array $headers
100 protected function createStreamOpts($verb, $url, $blob, $headers) {
101 $result = parent
::createStreamOpts($verb, $url, $blob, $headers);
103 $caConfig = CA_Config_Stream
::probe([
104 'verify_peer' => (bool) Civi
::settings()->get('verifySSL'),
106 if ($caConfig->isEnableSSL()) {
107 $result['ssl'] = $caConfig->toStreamOptions();
109 if (!$caConfig->isEnableSSL() && preg_match('/^https:/', $url)) {
110 throw new CRM_Core_Exception('Cannot fetch document - system does not support SSL');
119 * @return \CRM_Utils_Cache_Interface|null
121 public function getCache() {