Merge pull request #15938 from civicrm/5.20
[civicrm-core.git] / CRM / Utils / Http.php
CommitLineData
5063e355 1<?php
50bfb460
SB
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
50bfb460 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
50bfb460
SB
9 +--------------------------------------------------------------------+
10 */
5063e355 11
50bfb460
SB
12/**
13 *
14 * @package CiviCRM_Hook
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
50bfb460 16 */
5063e355
TO
17class CRM_Utils_Http {
18
19 /**
20 * Parse the expiration time from a series of HTTP headers.
21 *
22 * @param array $headers
e97c66ff 23 * @return int|null
5063e355
TO
24 * Expiration tme as seconds since epoch, or NULL if not cacheable.
25 */
26 public static function parseExpiration($headers) {
27 $headers = CRM_Utils_Array::rekey($headers, function ($k, $v) {
28 return strtolower($k);
29 });
30
31 if (!empty($headers['cache-control'])) {
32 $cc = self::parseCacheControl($headers['cache-control']);
33 if ($cc['max-age'] && is_numeric($cc['max-age'])) {
34 return CRM_Utils_Time::getTimeRaw() + $cc['max-age'];
35 }
36 }
37
38 return NULL;
39 }
40
41 /**
42 * @param string $value
43 * Ex: "max-age=86400, public".
44 * @return array
45 * Ex: Array("max-age"=>86400, "public"=>1).
46 */
47 public static function parseCacheControl($value) {
be2fb01f 48 $result = [];
5063e355
TO
49
50 $parts = preg_split('/, */', $value);
51 foreach ($parts as $part) {
52 if (strpos($part, '=') !== FALSE) {
53 list ($key, $value) = explode('=', $part, 2);
54 $result[$key] = $value;
55 }
56 else {
57 $result[$part] = TRUE;
58 }
59 }
60
61 return $result;
62 }
63
64}