Commit | Line | Data |
---|---|---|
5063e355 TO |
1 | <?php |
2 | ||
3 | class CRM_Utils_Http { | |
4 | ||
5 | /** | |
6 | * Parse the expiration time from a series of HTTP headers. | |
7 | * | |
8 | * @param array $headers | |
9 | * @return int|NULL | |
10 | * Expiration tme as seconds since epoch, or NULL if not cacheable. | |
11 | */ | |
12 | public static function parseExpiration($headers) { | |
13 | $headers = CRM_Utils_Array::rekey($headers, function ($k, $v) { | |
14 | return strtolower($k); | |
15 | }); | |
16 | ||
17 | if (!empty($headers['cache-control'])) { | |
18 | $cc = self::parseCacheControl($headers['cache-control']); | |
19 | if ($cc['max-age'] && is_numeric($cc['max-age'])) { | |
20 | return CRM_Utils_Time::getTimeRaw() + $cc['max-age']; | |
21 | } | |
22 | } | |
23 | ||
24 | return NULL; | |
25 | } | |
26 | ||
27 | /** | |
28 | * @param string $value | |
29 | * Ex: "max-age=86400, public". | |
30 | * @return array | |
31 | * Ex: Array("max-age"=>86400, "public"=>1). | |
32 | */ | |
33 | public static function parseCacheControl($value) { | |
34 | $result = array(); | |
35 | ||
36 | $parts = preg_split('/, */', $value); | |
37 | foreach ($parts as $part) { | |
38 | if (strpos($part, '=') !== FALSE) { | |
39 | list ($key, $value) = explode('=', $part, 2); | |
40 | $result[$key] = $value; | |
41 | } | |
42 | else { | |
43 | $result[$part] = TRUE; | |
44 | } | |
45 | } | |
46 | ||
47 | return $result; | |
48 | } | |
49 | ||
50 | } |