|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mpyw\Cowitter; |
|
4
|
|
|
|
|
5
|
|
|
class Response implements ResponseInterface |
|
6
|
|
|
{ |
|
7
|
|
|
protected $version; |
|
8
|
|
|
protected $statusCode; |
|
9
|
|
|
protected $reasonPhrase; |
|
10
|
|
|
protected $headers = []; |
|
11
|
|
|
protected $handle; |
|
12
|
|
|
protected $rawContent; |
|
13
|
|
|
protected $content; |
|
14
|
|
|
|
|
15
|
67 |
|
public function __construct($buffer, $ch) |
|
16
|
67 |
|
{ |
|
17
|
67 |
|
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
|
18
|
67 |
|
$header_buffer = substr($buffer, 0, $header_size - 4); |
|
19
|
67 |
|
$body_buffer = substr($buffer, $header_size); |
|
20
|
67 |
|
$header_buffer = current(array_slice(explode("\r\n\r\n", $header_buffer), -1)); |
|
21
|
67 |
|
$lines = explode("\r\n", $header_buffer); |
|
22
|
67 |
|
if (!preg_match('@\AHTTP/([\d.]+)\s+(\d{3})\s+(.+)\z@i', array_shift($lines), $matches)) { |
|
23
|
1 |
|
throw new \UnexpectedValueException('Invalid response line.'); |
|
24
|
|
|
} |
|
25
|
66 |
|
$this->version = $matches[1]; |
|
26
|
66 |
|
$this->statusCode = (int)$matches[2]; |
|
27
|
66 |
|
$this->reasonPhrase = $matches[3]; |
|
28
|
66 |
|
foreach ($lines as $line) { |
|
29
|
66 |
|
list($key, $value) = explode(':', $line, 2) + [1 => '']; |
|
30
|
66 |
|
$this->headers[strtolower(trim($key))][] = trim($value); |
|
31
|
|
|
} |
|
32
|
66 |
|
$this->handle = $ch; |
|
33
|
66 |
|
$this->rawContent = $body_buffer; |
|
34
|
66 |
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function getVersion() |
|
37
|
1 |
|
{ |
|
38
|
1 |
|
return $this->version; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
49 |
|
public function getStatusCode() |
|
42
|
49 |
|
{ |
|
43
|
49 |
|
return $this->statusCode; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
public function getReasonPhrase() |
|
47
|
2 |
|
{ |
|
48
|
2 |
|
return $this->reasonPhrase; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public function getHeaders() |
|
52
|
1 |
|
{ |
|
53
|
1 |
|
return $this->headers; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
public function getHeader($name) |
|
57
|
1 |
|
{ |
|
58
|
1 |
|
$name = strtolower($name); |
|
59
|
1 |
|
return isset($this->headers[$name]) ? $this->headers[$name] : []; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public function getHeaderLine($name, $delimiter = ', ') |
|
63
|
1 |
|
{ |
|
64
|
1 |
|
$name = strtolower($name); |
|
65
|
1 |
|
return implode($delimiter, $this->getHeader($name)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
62 |
|
public function getHandle() |
|
69
|
62 |
|
{ |
|
70
|
62 |
|
return $this->handle; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
51 |
|
public function getRawContent() |
|
74
|
51 |
|
{ |
|
75
|
51 |
|
return $this->rawContent; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
51 |
|
public function getContent() |
|
79
|
51 |
|
{ |
|
80
|
51 |
|
if ($this->content === null) { |
|
81
|
1 |
|
throw new \UnderflowException('Decoded content has not created yet.'); |
|
82
|
|
|
} |
|
83
|
50 |
|
return $this->content; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
39 |
|
public function hasContent() |
|
87
|
39 |
|
{ |
|
88
|
39 |
|
return $this->content !== null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
51 |
|
public function withDecodedContent($content) |
|
92
|
51 |
|
{ |
|
93
|
51 |
|
$clone = clone $this; |
|
94
|
51 |
|
$clone->content = $content; |
|
95
|
51 |
|
return $clone; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|