HeaderParser::getRetryAfter()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
/**
3
 * vipnytt/RobotsTxtParser
4
 *
5
 * @link https://github.com/VIPnytt/RobotsTxtParser
6
 * @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
7
 */
8
9
namespace vipnytt\RobotsTxtParser\Parser;
10
11
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
12
13
/**
14
 * Class HeaderParser
15
 *
16
 * @link https://tools.ietf.org/html/rfc7231
17
 * @link https://tools.ietf.org/html/rfc7230
18
 * @link https://tools.ietf.org/html/rfc2616
19
 *
20
 * @package vipnytt\RobotsTxtParser\Parser
21
 */
22
class HeaderParser implements RobotsTxtInterface
23
{
24
    /**
25
     * ANSI C's asctime() format
26
     */
27
    const DATE_ASCTIME = 'D M j h:i:s Y';
28
29
    /**
30
     * HTTP date formats
31
     * @link https://tools.ietf.org/html/rfc7231#section-7.1.1
32
     * @link https://tools.ietf.org/html/rfc2616#section-3.3
33
     */
34
    const DATE_HTTP = [
35
        DATE_RFC1123,
36
        DATE_RFC850,
37
        self::DATE_ASCTIME,
38
    ];
39
40
    /**
41
     * cURL resource
42
     * @var resource
43
     */
44
    protected $curlHandler;
45
46
    /**
47
     * Headers
48
     * @var string[]
49
     */
50
    private $headers;
51
52
    /**
53
     * HeaderParser constructor.
54
     *
55
     * @param resource $handler
56
     */
57
    public function __construct($handler)
58
    {
59
        $this->curlHandler = $handler;
60
    }
61
62
    /**
63
     * cURL CURLOPT_HEADERFUNCTION callback
64
     * @link https://tools.ietf.org/html/rfc7230#section-3.2.4
65
     *
66
     * This callback function must return the number of bytes actually taken care of.
67
     * If that amount differs from the amount passed in to your function, it'll signal an error to the library.
68
     * This will cause the transfer to get aborted and the libcurl function in progress will return CURLE_WRITE_ERROR.
69
     * @link https://curl.haxx.se/libcurl/c/CURLOPT_HEADERFUNCTION.html
70
     *
71
     * @param resource $handler - cURL resource
72
     * @param string $line - cURL header line string
73
     * @return int - the number of bytes written
74
     */
75
    public function curlCallback($handler, $line)
76
    {
77
        $this->curlHandler = $handler;
78
        $split = explode(':', $line, 2);
79
        $this->headers[strtolower($split[0])] = trim(end($split));
80
        return strlen($line);
81
    }
82
83
    /**
84
     * Content-Type encoding HTTP header
85
     * @link https://tools.ietf.org/html/rfc2616#section-14.17
86
     *
87
     * @return string
88
     */
89
    public function getCharset()
90
    {
91
        if (isset($this->headers['content-type']) &&
92
            ($value = $this->getInlineValue($this->headers['content-type'], 'charset', ';')) !== false
93
        ) {
94
            return $value;
95
        }
96
        return self::ENCODING;
97
    }
98
99
    /**
100
     * Get inline header variable value
101
     *
102
     * @param string $header
103
     * @param string $part
104
     * @param string $delimiter
105
     * @return string|false
106
     */
107
    private function getInlineValue($header, $part, $delimiter = ";")
108
    {
109
        foreach (array_map('trim', explode($delimiter, $header)) as $string) {
110
            if (stripos($string, $part . '=') === 0) {
111
                return trim(explode('=', $string, 2)[1]);
112
            }
113
        }
114
        return false;
115
    }
116
117
    /**
118
     * Cache-Control max-age HTTP header
119
     * @link https://tools.ietf.org/html/rfc7234#section-5.2.1.1
120
     * @link https://tools.ietf.org/html/rfc7234#section-5.2.2.8
121
     * @link https://tools.ietf.org/html/rfc2616#section-14.9.3
122
     *
123
     * @return int
124
     */
125
    public function getMaxAge()
126
    {
127
        if (isset($this->headers['cache-control']) &&
128
            ($value = $this->getInlineValue($this->headers['cache-control'], 'max-age', ',')) !== false
129
        ) {
130
            return intval($value);
131
        }
132
        return 0;
133
    }
134
135
    /**
136
     * Cache-Control Retry-After HTTP header
137
     * @link https://tools.ietf.org/html/rfc2616#section-14.37
138
     *
139
     * @param int $requestTime
140
     * @return int
141
     */
142
    public function getRetryAfter($requestTime)
143
    {
144
        if (isset($this->headers['retry-after'])) {
145
            if (is_numeric($this->headers['retry-after'])) {
146
                return intval($this->headers['retry-after']);
147
            } elseif (($time = $this->parseHttpDate($this->headers['retry-after'])) !== false) {
148
                return max(0, $time - $requestTime);
149
            }
150
        }
151
        // If no valid Retry-after header is found, retry after 15 minutes
152
        return 900;
153
    }
154
155
    /**
156
     * Parse HTTP-date
157
     * @link https://tools.ietf.org/html/rfc7231#section-7.1.1
158
     * @link https://tools.ietf.org/html/rfc2616#section-3.3
159
     *
160
     * @param string $string
161
     * @return int|false
162
     */
163
    private function parseHttpDate($string)
164
    {
165
        foreach (self::DATE_HTTP as $format) {
166
            if (($dateTime = date_create_from_format($format, $string, new \DateTimeZone('UTC'))) !== false) {
167
                return (int)date_format($dateTime, 'U');
168
            }
169
        }
170
        return false;
171
    }
172
}
173