-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPHPHttpResponse.class.php
More file actions
165 lines (149 loc) · 4.61 KB
/
Copy pathPHPHttpResponse.class.php
File metadata and controls
165 lines (149 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* HttpResponse
*
* Parse the response of HTTP request
*
* @package PHPHttpRequest
* @subpackage PHPHttpResponse
* @author qakcn <qakcnyn@gmail.com>
* @copyright 2015 qakcn
* @version 0.1
* @license http://mozilla.org/MPL/2.0/
* @link https://github.com/qakcn/PHPHttpRequest
*/
//namespace PHPHttpRequest;
class PHPHttpResponse {
private $status;
private $data;
private $headers = array();
private $cookies = array();
/**
* parse "Set-Cookie" header
*
* @param string #cookiestr "Set-Cookie" value
* @access private
*/
private function parseCookie($cookiestr) {
$c=array();
$options = explode(';', trim($cookiestr));
foreach($options as $o) {
if(trim($o) == '') {
continue;
}else if(strtolower(trim($o)) == 'secure') {
$c['secure'] = true;
}else if(strtolower(trim($o)) == 'httponly') {
$c['httponly'] = true;
}else {
list($on, $ov) = explode('=', trim($o));
if(strtolower($on) == 'expires') {
$time=DateTime::createFromFormat('D, d-M-Y H:i:s e', trim($ov));
$c['expires']=$time->getTimestamp();
}else if(in_array(strtolower($on), array('path', 'domain'))) {
$c[strtolower($on)] = trim($ov);
}else {
$c['name'] = trim($on);
$c['value'] = trim($ov);
}
}
}
array_push($this->cookies, $c);
}
/**
* constructor, parse HTTP headers and body
*
* @param string $res response data
* @access public
*/
public function __construct($res) {
$pos = strpos($res, "\r\n\r\n");
$headers = substr($res, 0, $pos);
$headers = explode("\r\n", $headers);
foreach($headers as $h) {
if(preg_match('/^HTTP\/.+ ([1-5][0-9]{2}) .*$/', $h, $match)) {
$this->status = $match[1];
}else {
list($hn, $hv) = explode(':', $h, 2);
if(strtolower(trim($hn)) == 'set-cookie') {
$this->parseCookie($hv);
}else {
array_push($this->headers, array('name'=>trim($hn), 'value' => trim($hv)));
}
}
}
$transencode = $this->getHeader('Transfer-Encoding');
if(strtolower($transencode[0]['value']) == 'chunked') {
$data = substr($res, $pos+4);
list($shiftdata, $data) = explode("\r\n", $data, 2);
$this->data = '';
$chunk_size = (integer)hexdec($shiftdata);
while($chunk_size > 0) {
$this->data .= substr($data, 0, $chunk_size);
$data = substr($data, $chunk_size+2);
list($shiftdata, $data) = explode("\r\n", $data, 2);
$chunk_size = (integer)hexdec($shiftdata);
}
}else {
$this->data = substr($res, $pos+4);
}
}
/**
* get headers of response
*
* @param string $name header name, empty for all headers
* @return array/boolean false for no match
* @access public
*/
public function getHeader($name='') {
if(empty($name)) {
return $this->headers;
}
$res = array();
foreach($this->headers as $h) {
if(strtolower($name) == strtolower($h['name'])) {
array_push($res, $h);
}
}
if(count($res)==0){
return false;
}else {
return $res;
}
}
/**
* get cookies of response
*
* @param string $name cookie name, empty for all cookies
* @return array/boolean false for no match
* @access public
*/
public function getCookie($name='') {
if(empty($name)) {
return $this->cookies;
}
$res = array();
foreach($this->cookies as $c) {
if(strtolower($name) == strtolower($c['name'])) {
array_push($res, $c);
}
}
if(count($res)==0){
return false;
}else {
return $res;
}
}
/**
* getter, called when tring to get value of private properties
*
* @param string $name private property name, should be one of $valid
* @return mixed value of the property
* @access public
*/
public function __get($name) {
$valid = array('status', 'data');
if(in_array($name, $valid)) {
return $this->$name;
}
}
}