/
http.php
166 lines (125 loc) · 3.67 KB
/
http.php
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
<?php
class Http
{
const
CONNECTIONS_LIMIT = 3,
DELAY_MILLISECONDS = 1000;
protected $default_headers = array(
"Accept" => "text/xml,application/xml,application/xhtml+xml,text/html,q=0.9,text/plain,q=0.8,image/png,*/*,q=0.5",
"Cache-Control" => "no-cache",
"Connection" => "Keep-Alive",
"Keep-Alive" => "300",
"Accept-Charset" => "UTF-8,Windows-1251,ISO-8859-1,q=0.7,*,q=0.7",
"Accept-Language" => "ru,en-us,en,q=0.5",
);
protected $default_options = array(
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12",
CURLOPT_FOLLOWLOCATION => false
);
protected $range = false;
protected $norange_domains = array(
'4shared.com'
);
protected $response_header = array();
protected $response_data = array();
protected $worker;
public function __construct ($options = array(), $headers = array()) {
$worker = new MultiRequest_Handler();
$worker->setConnectionsLimit(self::CONNECTIONS_LIMIT);
$worker->setRequestingDelay(self::DELAY_MILLISECONDS);
$worker->onRequestComplete(array($this, "save_headers"));
$worker->onRequestComplete(array($this, "save_data"));
$headers = array_replace($this->default_headers, $headers);
foreach ($headers as $key => $header) {
$headers[$key] = "$key: $header";
}
$headers = array_values($headers);
$worker->requestsDefaults()->addHeaders($headers);
$options = array_replace($this->default_options, $options);
$worker->requestsDefaults()->addCurlOptions($options);
$this->worker = $worker;
}
public function add ($urls) {
$urls = (array) $urls;
foreach($urls as $url) {
$request = new MultiRequest_Request($url);
if ($this->range) {
$domain = parse_url($url, PHP_URL_HOST);
if (!empty($domain)) {
$domain = preg_replace('/^www\./i', '', $domain);
if (!in_array($domain, $this->norange_domains)) {
$request->setCurlOption(CURLOPT_RANGE, $this->range);
}
}
}
$this->worker->pushRequestToQueue($request);
}
return $this;
}
public function enable_limit($limit) {
$this->range = '1-' . $limit;
return $this;
}
public function disable_limit() {
$this->range = false;
return $this;
}
public function exec () {
$this->worker->start();
return $this;
}
public function set_debug() {
$this->worker->onRequestComplete(array($this, "notify_error"));
}
public function notify_error($request) {
$error = $request->getFailException();
if ($error && $request->getCode() != 206) {
ob_end_flush();
var_dump($error);
}
}
public function flush () {
$this->response_header = array();
$this->response_data = array();
return $this;
}
public function save_headers ($request) {
$this->response_header[$request->getRawUrl()] = $request->getResponseHeaders(true);
}
public function save_data ($request) {
$this->response_data[$request->getRawUrl()] = $request->getContent();
}
public function get ($url) {
return $this->response_data[$url];
}
public function get_headers ($url) {
return $this->response_header[$url];
}
public function get_full ($url) {
$headers = $this->get_headers($url);
$html = $this->get($url);
$string = '';
foreach ($headers as $type => $header) {
$string .= "$type: $header\n";
}
$string .= "\n$html";
return $string;
}
public static function download ($url) {
$worker = new Http();
$worker->add($url)->exec();
return $worker->get($url);
}
public static function redirect($url, $permanent = false) {
if ((bool) $permanent) {
header("HTTP/1.x 301 Moved Permanently");
} else {
header("HTTP/1.x 302 Moved Temporarily");
}
if (empty($url)) {
$url = $_SERVER["REQUEST_URI"];
}
header("Location: $url");
exit();
}
}