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
|
|
|
use MultiHttp\Handler\Json; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Response |
21
|
|
|
* @package MultiHttp |
22
|
|
|
*/ |
23
|
|
|
class Response |
24
|
|
|
{ |
25
|
|
|
public |
26
|
|
|
$code, |
27
|
|
|
$errorCode, |
28
|
|
|
$error, |
29
|
|
|
$header, |
30
|
|
|
$body, |
31
|
|
|
/** |
32
|
|
|
* @var Request |
33
|
|
|
*/ |
34
|
|
|
$request, |
35
|
|
|
$contentType, |
36
|
|
|
$charset, |
37
|
|
|
$duration, |
38
|
|
|
$info; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Response constructor. |
42
|
|
|
*/ |
43
|
7 |
|
protected function __construct() |
44
|
|
|
{ |
45
|
7 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Request $request |
49
|
|
|
* @param $body |
50
|
|
|
* @param $info |
51
|
|
|
* @param $errorCode |
52
|
|
|
* @param $error |
53
|
|
|
* @return Response |
54
|
|
|
*/ |
55
|
7 |
|
public static function create(Request $request, $body, $info, $errorCode, $error) |
56
|
|
|
{ |
57
|
7 |
|
$self = new self; |
58
|
7 |
|
$self->request = $request; |
59
|
7 |
|
$self->body = $body; |
60
|
7 |
|
$self->info = $info; |
61
|
7 |
|
$self->errorCode = $errorCode; |
62
|
7 |
|
$self->error = $error; |
63
|
7 |
|
return $self; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
|
68
|
7 |
|
public function parse() |
69
|
|
|
{ |
70
|
|
|
//has header |
71
|
7 |
|
$headers = rtrim(substr($this->body, 0, $this->info['header_size'])); |
72
|
7 |
|
$this->body = substr($this->body, $this->info['header_size']); |
73
|
7 |
|
$headers = explode(PHP_EOL, $headers); |
74
|
7 |
|
array_shift($headers); // HTTP HEADER |
75
|
7 |
|
foreach ($headers as $h) { |
76
|
7 |
|
if (false !== strpos($h, ':')) |
77
|
7 |
|
list($k, $v) = explode(':', $h, 2); |
78
|
|
|
else |
79
|
3 |
|
list($k, $v) = array($h, ''); |
80
|
|
|
|
81
|
7 |
|
$this->header[trim($k)] = trim($v); |
82
|
7 |
|
} |
83
|
|
|
|
84
|
7 |
|
$this->code = $this->info['http_code']; |
85
|
7 |
|
$this->duration = $this->info['total_time']; |
86
|
7 |
|
$this->contentType = $this->info['content_type']; |
87
|
7 |
|
$content_type = isset($this->info['content_type']) ? $this->info['content_type'] : ''; |
88
|
7 |
|
$content_type = explode(';', $content_type); |
89
|
7 |
|
$this->contentType = $content_type[0]; |
90
|
7 |
|
if (count($content_type) == 2 && strpos($content_type[1], '=') !== false) { |
91
|
7 |
|
list(, $this->charset) = explode('=', $content_type[1]); |
92
|
7 |
|
} |
93
|
|
|
|
94
|
7 |
|
$this->unserializeBody(); |
95
|
|
|
|
96
|
7 |
|
} |
97
|
7 |
|
public function unserializeBody() |
98
|
|
|
{ |
99
|
7 |
|
if (isset($this->request->expectsMime)) { |
100
|
2 |
|
if (Mime::getFullMime($this->request->expectsMime) !== $this->contentType) throw new UnexpectedResponseException('expected mime can not be matched, real mime:'. $this->contentType. ', expected mime:'. Mime::getFullMime($this->request->expectsMime)); |
101
|
2 |
|
$clz = "\MultiHttp\\Handler\\".ucfirst($this->request->expectsMime); |
102
|
2 |
|
$inst = new $clz; |
103
|
2 |
|
if (!($inst instanceof Handler\IHandler)) throw new InvalidOperationException($clz . ' is not implement of IHandler'); |
104
|
2 |
|
$this->body = $inst->decode($this->body); |
105
|
2 |
|
} |
106
|
7 |
|
} |
107
|
|
|
/** |
108
|
|
|
* Status Code Definitions |
109
|
|
|
* |
110
|
|
|
* Informational 1xx |
111
|
|
|
* Successful 2xx |
112
|
|
|
* Redirection 3xx |
113
|
|
|
* Client Error 4xx |
114
|
|
|
* Server Error 5xx |
115
|
|
|
* |
116
|
|
|
* http://pretty-rfc.herokuapp.com/RFC2616#status.codes |
117
|
|
|
* |
118
|
|
|
* @return bool Did we receive a 4xx or 5xx? |
119
|
|
|
*/ |
120
|
2 |
|
public function hasErrors() |
121
|
|
|
{ |
122
|
2 |
|
return $this->code == 0 || $this->code >= 400; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |