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