1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: mathias |
5
|
|
|
* Date: 07/09/18 |
6
|
|
|
* Time: 10:23 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Spatie\Varnish; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class VarnishResponse |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Varnish return codes (from vcli.h) |
16
|
|
|
* https://github.com/varnishcache/varnish-cache/blob/master/include/vcli.h#L42. |
17
|
|
|
*/ |
18
|
|
|
const VARN_SYNTAX = 100; |
19
|
|
|
const VARN_UNKNOWN = 101; |
20
|
|
|
const VARN_UNIMPL = 102; |
21
|
|
|
const VARN_TOOFEW = 104; |
22
|
|
|
const VARN_TOOMANY = 105; |
23
|
|
|
const VARN_PARAM = 106; |
24
|
|
|
const VARN_AUTH = 107; |
25
|
|
|
const VARN_OK = 200; |
26
|
|
|
const VARN_TRUNCATED = 201; |
27
|
|
|
const VARN_CANT = 300; |
28
|
|
|
const VARN_COMMS = 400; |
29
|
|
|
const VARN_CLOSE = 500; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Varnish control command contains the status code and content length |
33
|
|
|
* e.g. 107 59 |
34
|
|
|
*/ |
35
|
|
|
const CONTROL_COMMAND_REGEX = '/^(\d{3}) (\d+)/'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Authentication challenge length |
39
|
|
|
*/ |
40
|
|
|
const CHALLENGE_LENGTH = 32; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var $code int: The varnish return code |
44
|
|
|
*/ |
45
|
|
|
private $code = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var $length int: The length of the following content |
49
|
|
|
*/ |
50
|
|
|
private $length = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var $content string: The actual content of the response |
54
|
|
|
*/ |
55
|
|
|
private $content = ''; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return int |
59
|
|
|
*/ |
60
|
|
|
public function getCode() |
61
|
|
|
{ |
62
|
|
|
return $this->code; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param int $code |
67
|
|
|
*/ |
68
|
|
|
public function setCode($code) |
69
|
|
|
{ |
70
|
|
|
$this->code = $code; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
|
|
public function getLength() |
77
|
|
|
{ |
78
|
|
|
return $this->length; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param int $length |
83
|
|
|
*/ |
84
|
|
|
public function setLength($length) |
85
|
|
|
{ |
86
|
|
|
$this->length = $length; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getContent(): string |
93
|
|
|
{ |
94
|
|
|
return $this->content; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $content |
99
|
|
|
*/ |
100
|
|
|
public function setContent(string $content) |
101
|
|
|
{ |
102
|
|
|
$this->content = $content; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function appendContent(string $content) |
106
|
|
|
{ |
107
|
|
|
$this->content .= $content; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function isAuthRequest() { |
114
|
|
|
return $this->getCode() === VarnishResponse::VARN_AUTH; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return bool|string |
119
|
|
|
*/ |
120
|
|
|
public function getAuthChallenge() { |
121
|
|
|
return substr($this->content, 0, self::CHALLENGE_LENGTH); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $chunk |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function parseControlCommand($chunk) { |
129
|
|
|
// Varnish will output a code and a content length, followed by the actual content |
130
|
|
|
if (preg_match(self::CONTROL_COMMAND_REGEX, $chunk, $match)) { |
131
|
|
|
$this->setCode((int) $match[1]); |
132
|
|
|
$this->setLength((int) $match[2]); |
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function hasLength() { |
142
|
|
|
return $this->getLength() !== null; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public function contentLengthReached() { |
149
|
|
|
return strlen($this->getContent()) >= $this->getLength(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/* |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public function finishedReading() { |
156
|
|
|
return $this->hasLength() && $this->contentLengthReached(); |
157
|
|
|
} |
158
|
|
|
} |