-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.class.php
224 lines (192 loc) · 5.46 KB
/
http.class.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
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
<?php
date_default_timezone_set('America/Los_Angeles');
class http {
public $cookies = array();
public $last_headers = array();
public $options = array(
'http'=>array(
'max_redirects' => 1,
'ignore_errors'=>1,
'method'=>"GET",
),
'other' => array(
'follow_redirects' => true,
),
);
public $default_headers = array(
#'Host' => 'www.facebook.com',
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language' => 'en-us,en;q=0.5',
'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Content-Type' => 'application/json; charset=UTF-8',
'Pragma' => 'no-cache',
'Cache-Control' => 'no-cache'
);
public function __construct($options = null)
{
if(isset($options['proxy'])){
$this->options = Util::merge($this->options, array(
'http'=>array(
'proxy' => $options['proxy'],
'request_fulluri' => true,
)
));
}
if($options){
$this->options['other'] = Util::merge($this->options['other'],$options);
}
if(isset($options['headers'])){
$this->default_headers = Util::merge($this->default_headers,$options['headers']);
}
}
public function get($url){
return $this->do_request($url, $this->options);
}
public function post($url, $content = ''){
$options = Util::merge($this->options, array(
'http'=>array(
'method'=>"POST",
'content' => $content,
)
));
return $this->do_request($url, $options);
}
/* HTTP Helpers */
public function set_user_agent($ua){
$this->default_headers['User-Agent'] = $ua;
}
public function add_header($key, $value){
$this->default_headers[$key] = $value;
}
public function get_cookie($key){
return $this->cookies[$key]['value'];
}
public function get_cookies(){
return $this->cookies;
}
public function set_cookie($key, $value){
$this->cookies[$key] = $value;
}
/* internal stuff */
private function do_request($url, $options){
$options['http']['header'] = $this->build_headers();
stream_context_get_default($options);
$data = file_get_contents($url);
if($http_response_header){
$response_headers = $this->parse_headers($http_response_header);
if(isset($response_headers["Location"])){
#$response_headers["Location"];
if($this->options['other']['follow_redirects']){
$data = $this->get($response_headers["Location"]);
}
}
}
return $data;
}
private function parse_headers($headers){
foreach ($headers as $header) {
if (stripos($header, ": ") === false) {
$http_response = $header;
}
else{
$parts = explode(": ",$header);
#some headers may have another : in it. we only care about the first one.
$key = array_shift($parts);
$value = join(": ", $parts);
if(isset($response_headers[$key])){
// if we already have a key make the first item an array
if(is_string($response_headers[$key])){
$response_headers[$key] = Array($response_headers[$key]);
}
// push all other items to the array
$response_headers[$key][] = $value;
}
else{
// new item so just add key/value
$response_headers[$key] = $value;
}
}
}
if(isset($response_headers["Set-Cookie"])){
$cookies = $response_headers["Set-Cookie"];
if(is_array($cookies)){
foreach( $cookies as $cookie ) {
$this->parse_cookies($cookie);
}
}
else{
$this->parse_cookies($cookies);
}
}
$this->last_headers = $response_headers;
return $response_headers;
}
public function build_headers($content = null){
$headers = $this->default_headers;
if($this->cookies) $headers['Cookie'] = $this->build_cookies();
if($content) $headers['Content-Length'] = strlen($content);
foreach($headers as $name=>$value){
$head[] = "{$name}: {$value}";
}
return trim( implode( "\r\n", $head ) );
}
private function build_cookies(){
foreach($this->cookies as $name=>$value){
$value = $value['value'];
$cookie[] = "{$name}={$value}";
}
if( count( $cookie ) > 0 ) {
return trim( implode( '; ', $cookie ) );
}
}
private function parse_cookies($header) {
$csplit = explode( ';', $header );
$cdata = array();
foreach( $csplit as $data ) {
$cinfo = explode( '=', $data );
// todo clean up and use cookie class
$cinfo[0] = trim( $cinfo[0] );
if(in_array($cinfo[0], array('secure', 'httponly'))) $cinfo[1] = true;
if( $cinfo[0] == 'expires' ) $cinfo[1] = strtotime( $cinfo[1] );
if( in_array( $cinfo[0], array('domain', 'expires', 'path', 'secure', 'comment', 'httponly' ) ) ) {
$cdata[trim( $cinfo[0] )] = $cinfo[1];
}
else{
$cdata['name'] = trim( $cinfo[0] );
$cdata['value'] = trim( $cinfo[1] );
}
$cdata['data'] = $data;
}
$this->cookies[$cdata['name']] = $cdata;
return $this->cookies;
}
}
class cookie {
public $value = "";
public $expires = "";
public $domain = "";
public $path = "";
public $name = "";
public $secure = false;
public function set_value($key,$value) {
switch (strtolower($key)) {
case "expires":
$this->expires = $value;
return;
case "domain":
$this->domain = $value;
return;
case "path":
$this->path = $value;
return;
case "secure":
$this->secure = ($value == true);
return;
}
if ($this->name == "" && $this->value == "") {
$this->name = $key;
$this->value = $value;
}
}
}