-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpWebRequest.php
More file actions
322 lines (297 loc) · 9.62 KB
/
HttpWebRequest.php
File metadata and controls
322 lines (297 loc) · 9.62 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
namespace HttpWebRequest;
class HttpWebRequest
{
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const HEAD = 'HEAD';
const DELETE = 'DELETE';
const TRACE = 'TRACE';
const OPTIONS = 'OPTIONS';
const CONNECT = 'CONNECT';
const HTTP_11 = 'HTTP/1.1';
const HTTP_10 = 'HTTP/1.0';
const URLENCODED = 'application/x-www-form-urlencoded';
const FORMDATA = 'multipart/form-data';
protected $method = self::GET;
protected $protocol = self::HTTP_11;
protected $url;
protected $scheme;
protected $host;
protected $port = null;
protected $page;
protected $query = array();
protected $auth = null;
protected $cookie = null;
protected $errno;
protected $errstr;
protected $sockettimeout = 30;
protected $head;
protected $body;
protected $status;
protected $header = [];
protected $content;
protected $addheader = [];
protected $fp;
protected $proxy = null;
public function __construct($url)
{
$this->url = $url;
$this->query['get'] = null;
$this->query['post'] = null;
$url_array = @parse_url($url);
$this->scheme = $url_array['scheme'];
$this->host = idn_to_ascii($url_array['host']);
$this->port = isset($url_array['port']) ? $url_array['port'] : null;
$this->page = isset($url_array['path']) ? $url_array['path'] : '/';
$this->query['get'] = isset($url_array['query']) ? $url_array['query'] : null;
if (isset($url_array['user'])) {
$this->setAuth($url_array['user'], $url_array['pass']);
}
$this->addHeader('Host', $this->host);
$this->addHeader('Connection', 'Close');
}
public function __destruct()
{
if ($this->fp) {
fclose($this->fp);
}
}
public function run()
{
$this->connect();
$this->write();
$this->read();
}
public function connect()
{
if ($this->port === null) {
$this->port = $this->scheme == 'https' ? 443 : 80;
}
$ip = @gethostbyname($this->proxy === null ? $this->host : $this->proxy['proxy']);
$target = ($this->scheme == 'https' && $this->proxy === null ? 'sslv2://' : '') . $ip;
$this->fp = @fsockopen(
$target,
$this->proxy === null ? $this->port : $this->proxy['port'],
$this->errno,
$this->errstr,
$this->sockettimeout
);
return $this->errno;
}
public function write()
{
$output = '';
$page = str_replace(
' ',
'+',
$this->page . ($this->query['get'] !== null ? '?' . $this->query['get'] : '')
);
$url = $this->proxy === null ? $page : $this->scheme . '://' . $this->host . ':' . $this->port . $page;
$output .= $this->method . ' ' . $url . ' ' . $this->protocol . "\r\n";
if ($this->method == self::POST) {
$output .= 'Content-Type: ' . self::URLENCODED . "\r\n";
$output .= 'Content-Length: ' . strlen($this->query['post']) . "\r\n";
}
if ($this->auth !== null) {
$output .= 'Authorization: Basic ' . $this->auth . "\r\n";
}
$output .= $this->buildHeader();
if ($this->cookie !== null) {
$output .= 'Cookie: ' . $this->cookie . "\r\n";
}
$output .= "\r\n";
if ($this->query['post'] !== null) {
$output .= $this->query['post'];
}
if ($this->fp) {
fwrite($this->fp, $output);
}
}
public function read()
{
$input = '';
if ($this->fp) {
while (!feof($this->fp)) {
$input .= fgets($this->fp, 1024);
}
}
$head_body_array = preg_split("/(\n\n|\r\r|\r\n\r\n)/", $input, 2);
if (isset($head_body_array[0])) {
$this->head = $head_body_array[0];
}
if (isset($head_body_array[1])) {
$this->body = $head_body_array[1];
}
$header = preg_split("/\r?\n/", $this->head);
$header_line = array_shift($header);
list($dummy, $this->status, $dummy) = explode(' ', $header_line);
while (($header_line = array_shift($header)) !== null) {
list($header_key, $header_value) = explode(':', $header_line, 2);
$header_key = str_replace('-', '_', strtoupper(trim($header_key)));
if (isset($this->header[$header_key]) === true) {
if (is_array($this->header[$header_key]) === true) {
$this->header[$header_key][] = trim($header_value);
} else {
$temp_header_value = $this->header[$header_key];
$this->header[$header_key] = [];
$this->header[$header_key][] = $temp_header_value;
$this->header[$header_key][] = trim($header_value);
}
} else {
$this->header[$header_key] = trim($header_value);
}
}
$body = $this->body;
if (strtolower($this->getHeader('Transfer-Encoding')) == 'chunked') {
$body = $this->decodeChunkedBody($body);
}
if ($this->getHeader('Content-Encoding') !== null) {
$body = $this->decodeContent($body);
}
$this->content = $body;
}
private function decodeChunkedBody($body)
{
$rest = $body;
$length = 0;
$body = '';
while (strlen($rest) > 0) {
list($len, $rest) = explode("\r\n", $rest, 2);
$len = hexdec(trim($len));
$length += $len;
$body .= substr($rest, 0, $len);
$rest = substr($rest, $len);
}
return $body;
}
private function decodeContent($body)
{
switch (strtolower($this->getHeader('Content-Encoding'))) {
case 'gzip' :
return gzinflate(substr($body, 10));
break;
case 'deflate' :
return gzuncompress($body);
break;
case 'identity' :
return $body;
break;
default :
return $body;
break;
}
}
public function setMethod($method)
{
$this->method = strtoupper($method);
}
public function setAuth($user, $password)
{
$this->auth = base64_encode($user . ':' . $password);
}
public function addGet($key, $value = null)
{
if ($this->query['get'] === null) {
$this->query['get'] = '';
}
if ($value === null) {
$array = explode('=', $key);
$key = $array[0];
$value = (strlen($array[1]) > 0) ? $array[1] : '';
}
$this->query['get'] .= ($this->query['get'] !== '' ? '&' : '') . $key . "=" . $value;
}
public function addPost($key, $value = false)
{
if ($this->query['post'] === null) {
$this->query['post'] = '';
}
$this->setMethod(self::POST);
if ($value === false) {
$array = explode('=', $key);
$key = $array[0];
$value = (strlen($array[1]) > 0) ? $array[1] : '';
}
$this->query['post'] .= ($this->query['post'] != '' ? '&' : '') . $key . "=" . $value;
}
public function useCookie($str)
{
$this->cookie = $str;
}
public function setTimeout($sec)
{
$this->sockettimeout = $sec;
}
public function addHeader($key, $value, $override = true)
{
if ($override === true) {
$this->addheader[$key] = $value;
}
}
protected function buildHeader()
{
$header = '';
foreach ($this->addheader as $key => $value) {
$header .= $key . ': ' . $value . "\r\n";
}
return $header;
}
public function getHeader($key = null)
{
if ($key === null) {
$out = '';
foreach ($this->header as $k => $v) {
if (is_array($v) === false) {
$out .= str_replace('_', '-', ucfirst(strtolower($k))) . ': ' . $v . "\n";
} else {
foreach ($v as $v2) {
$out .= str_replace('_', '-', ucfirst(strtolower($k))) . ': ' . $v2 . "\n";
}
}
}
return $out;
} else {
if (isset($this->header[str_replace('-', '_', strtoupper(trim($key)))])) {
return $this->header[str_replace('-', '_', strtoupper(trim($key)))];
} else {
return null;
}
}
}
public function getContent()
{
return $this->content;
}
public function getCookie()
{
$x = $this->getHeader('Set-Cookie');
if ($x === null) {
return null;
} elseif (is_array($x)) {
$parts = [];
foreach ($x as $value) {
list($part, $rest) = explode(';', $value, 2);
$parts[] = trim($part);
}
$cookie = implode('; ', $parts);
return $cookie;
} elseif (isset($x)) {
list($cookie, $rest) = explode(';', $x, 2);
return trim($cookie);
}
}
public function getStatus()
{
return $this->status;
}
public function useProxy($proxy, $port, $user = '', $password = '')
{
$this->proxy['proxy'] = $proxy;
$this->proxy['port'] = $port;
if ($user != '' && $password != '') {
$this->addHeader('Proxy-Authorization', 'Basic ' . base64_encode($user . ':' . $password));
}
}
}