|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* @author [email protected] [email protected] |
|
5
|
|
|
* Date: 2017/6/19 |
|
6
|
|
|
* Time: 10:55 |
|
7
|
|
|
* @version $Id: $ |
|
8
|
|
|
* @since 1.0 |
|
9
|
|
|
* @copyright Sina Corp. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace MultiHttp; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
use MultiHttp\Exception\InvalidOperationException; |
|
16
|
|
|
use MultiHttp\Exception\UnexpectedResponseException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class Response |
|
20
|
|
|
* @package MultiHttp |
|
21
|
|
|
*/ |
|
22
|
|
|
class Response |
|
23
|
|
|
{ |
|
24
|
|
|
public |
|
25
|
|
|
$code, |
|
|
|
|
|
|
26
|
|
|
$errorCode, |
|
27
|
|
|
$error, |
|
28
|
|
|
$header, |
|
29
|
|
|
$body, |
|
30
|
|
|
/** |
|
31
|
|
|
* @var Request |
|
32
|
2 |
|
*/ |
|
33
|
|
|
$request, |
|
34
|
2 |
|
$contentType, |
|
35
|
|
|
$charset, |
|
36
|
2 |
|
$duration, |
|
37
|
2 |
|
$info; |
|
38
|
2 |
|
|
|
39
|
2 |
|
/** |
|
40
|
2 |
|
* Response constructor. |
|
41
|
2 |
|
*/ |
|
42
|
2 |
|
protected function __construct() |
|
43
|
2 |
|
{ |
|
44
|
2 |
|
} |
|
45
|
2 |
|
|
|
46
|
|
|
/** |
|
47
|
2 |
|
* @param Request $request |
|
48
|
|
|
* @param $body |
|
49
|
2 |
|
* @param $info |
|
50
|
2 |
|
* @param $errorCode |
|
51
|
2 |
|
* @param $error |
|
52
|
2 |
|
* @return Response |
|
53
|
2 |
|
*/ |
|
54
|
2 |
|
public static function create(Request $request, $body, $info, $errorCode, $error) |
|
55
|
2 |
|
{ |
|
56
|
2 |
|
$self = new self; |
|
57
|
2 |
|
$self->request = $request; |
|
58
|
2 |
|
$self->body = $body; |
|
59
|
|
|
$self->info = $info; |
|
60
|
2 |
|
$self->errorCode = $errorCode; |
|
61
|
|
|
$self->error = $error; |
|
62
|
2 |
|
return $self; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
|
|
66
|
2 |
|
|
|
67
|
2 |
|
public function parse() |
|
68
|
2 |
|
{ |
|
69
|
2 |
|
//has header |
|
70
|
2 |
|
$headers = rtrim(substr($this->body, 0, $this->info['header_size'])); |
|
71
|
2 |
|
$this->body = substr($this->body, $this->info['header_size']); |
|
72
|
2 |
|
$headers = explode(PHP_EOL, $headers); |
|
73
|
|
|
array_shift($headers); // HTTP HEADER |
|
74
|
2 |
|
foreach ($headers as $h) { |
|
75
|
|
|
if (false !== strpos($h, ':')) |
|
76
|
|
|
list($k, $v) = explode(':', $h, 2); |
|
77
|
|
|
else |
|
78
|
|
|
list($k, $v) = array($h, ''); |
|
79
|
|
|
|
|
80
|
|
|
$this->header[trim($k)] = trim($v); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->code = $this->info['http_code']; |
|
84
|
|
|
$this->duration = $this->info['total_time']; |
|
85
|
|
|
$this->contentType = $this->info['content_type']; |
|
86
|
|
|
$content_type = isset($this->info['content_type']) ? $this->info['content_type'] : ''; |
|
87
|
|
|
$content_type = explode(';', $content_type); |
|
88
|
|
|
$this->contentType = $content_type[0]; |
|
89
|
2 |
|
if (count($content_type) == 2 && strpos($content_type[1], '=') !== false) { |
|
90
|
|
|
list(, $this->charset) = explode('=', $content_type[1]); |
|
91
|
2 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
$this->unserializeBody(); |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
public function unserializeBody() |
|
97
|
|
|
{ |
|
98
|
|
|
if (isset($this->request->expectedMime)) { |
|
99
|
|
|
if (Mime::getFullMime($this->request->expectedMime) !== $this->contentType) throw new UnexpectedResponseException('expected mime can not be matched, real mime:'. $this->contentType. ', expected mime:'. Mime::getFullMime($this->request->expectedMime)); |
|
100
|
|
|
$method = 'un'.ucfirst($this->request->expectedMime); |
|
101
|
|
|
if (!method_exists($this->request, $method)) throw new InvalidOperationException($method . ' is not exists in ' . __CLASS__); |
|
102
|
|
|
$this->body = $this->request->{$method}($this->body); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
/** |
|
106
|
|
|
* Status Code Definitions |
|
107
|
|
|
* |
|
108
|
|
|
* Informational 1xx |
|
109
|
|
|
* Successful 2xx |
|
110
|
|
|
* Redirection 3xx |
|
111
|
|
|
* Client Error 4xx |
|
112
|
|
|
* Server Error 5xx |
|
113
|
|
|
* |
|
114
|
|
|
* http://pretty-rfc.herokuapp.com/RFC2616#status.codes |
|
115
|
|
|
* |
|
116
|
|
|
* @return bool Did we receive a 4xx or 5xx? |
|
117
|
|
|
*/ |
|
118
|
|
|
public function hasErrors() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->code == 0 || $this->code >= 400; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.