This repository was archived by the owner on Dec 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathhttpRequest.php
More file actions
431 lines (342 loc) · 10.6 KB
/
httpRequest.php
File metadata and controls
431 lines (342 loc) · 10.6 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
/*
* HTTPRequest class
* Any request can be made by this class
*
* First create HTTPRequest object
*
* Then call its open() method with 'method' and 'url' as parameters
*
* Call setRequestHeaders() method to set 'Range' or 'Referer' or 'User-Agent' header
*
* To get Response Headers call getResponseHeaders() method
*
* To save output to file call saveTo() method with a file resource opened to 'write'
* mode else default file is used
*
* To get the most recent error call anytime to getError() method
*
* To get file extension call getFileExtension() method after open() method had been called
*
* Call attachProgressFunction() with a function handler to monitor progress of download
*
* Call send() method to send the request
*
* To get Response Body call getResponseBody() method , it will be empty string if saveTo() method
* had been called already
*
* To get Response Code call getResponseCode() method
*
*/
class HTTPRequest
{
private $requestUrl_ ;
private $requestMethod_;
private $requestHeaders_;
private $channel_;
private $allowedRequestMethod = [ 'get' => 1, 'post' => 1, 'head' => 1 ];
private $responseBody_;
private $error_;
private $responseHeaders_;
private $fileExtension_;
private $outputToFile_;
private $defaultOutputFile_;
private $timeout_;
private $maxRedirect_;
private $fileHandle_;
private $info_;
function __construct()
{
$this->channel_ = curl_init();
$this->requestUrl_ =
$this->requestMethod_ =
$this->responseBody_ =
$this->error_ =
$this->responseHeaders_ =
$this->requestHeaders_ =
$this->defaultOutputFile_ =
$this->fileExtension_ = '';
$this->outputToFile_ = false;
$this->timeout_ = 18000;
$this->maxRedirect_ = 5;
$this->info_ = 0;
/* set default action */
// to send the Referer header on Redirection
curl_setopt( $this->channel_, CURLOPT_AUTOREFERER, true );
// not to display the output
curl_setopt( $this->channel_, CURLOPT_RETURNTRANSFER, true );
// does not verify the SSL certificate of server
curl_setopt( $this->channel_, CURLOPT_SSL_VERIFYPEER, false );
// to get the Last Modified time
curl_setopt( $this->channel_, CURLOPT_FILETIME, true );
// to follow any redirection
curl_setopt( $this->channel_, CURLOPT_FOLLOWLOCATION, true );
// max number of redirects
curl_setopt( $this->channel_, CURLOPT_MAXREDIRS, $this->maxRedirect_ );
// set connection timeout
curl_setopt( $this->channel_, CURLOPT_CONNECTTIMEOUT, $this->timeout_ );
// not to display progress in command line
curl_setopt( $this->channel_, CURLOPT_NOPROGRESS, false );
// to track the request headers
curl_setopt( $this->channel_, CURLINFO_HEADER_OUT, true );
// Accept - Encoding header ( accepting all encoding type )
curl_setopt( $this->channel_, CURLOPT_ENCODING, "" );
// User - Agent header ( as received by the client browser )
curl_setopt( $this->channel_, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
}
private function setError_()
{
// if a new error occured
if( !( curl_error( $this->channel_ ) === '' ) )
{
return $this->error_ = curl_error( $this->channel_ );
}
}
private function populateRequestHeaders_()
{
$this->info_ = curl_getinfo( $this->channel_ );
$headers = curl_getinfo( $this->channel_ )['request_header'];
$headersDelimiters = "\r\n";
$headers = explode( $headersDelimiters, $headers );
$headersCount = count( $headers );
$firstPropertyName = 'main';
for( $i = 0; $i < $headersCount - 2 /* last two are empty line*/; $i++ )
{
$parts = explode( ": ", $headers[$i] );
if( $i === 0 )
{
$parts[1] = $parts[0];
$parts[0] = $firstPropertyName;
}
$this->requestHeaders_[$parts[0]] = $parts[1];
}
}
private function setResponseHeader_()
{
if( $this->requestMethod_ != "head" )
{
$infoOfTransfer = curl_getinfo( $this->channel_ );
$this->responseHeaders_['Content-Type'] = $infoOfTransfer['content_type'];
$this->responseHeaders_['Content-Length'] = $infoOfTransfer['download_content_length'];
$this->responseHeaders_['Response-Code'] = $infoOfTransfer['http_code'];
$this->responseHeaders_['Last-Modified'] = $infoOfTransfer['filetime'];
}
else
{
$headers = explode("\r\n", $this->responseBody_ );
$totalHeaders = count( $headers );
$this->responseHeaders_['Response-Code'] = curl_getinfo( $this->channel_ )['http_code'];
for( $i = 0; $i < $totalHeaders - 2 /* last two empty lines*/; $i++ )
{
$parts = explode( ": ", $headers[$i] );
if( $i === 0 )
{
$parts[1] = $parts[0];
$parts[0] = "main";
}
$this->responseHeaders_[$parts[0]] = $parts[1];
}
}
}
public function open( $method, $url )
{
$method = strtolower( $method );
if( !isset( $this->allowedRequestMethod[$method] ) )
{
// invalid method
$this->error_ = "The requested method is not supported";
return $this->error_;
}
//if( preg_match( '#^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$#', $url ) === 0 )
//{
// invalid url ( does not support localhost url )
// $this->error_ = 'The requested url is not valid';
// return;
//}
$this->requestMethod_ = $method;
$this->requestUrl_ = $url;
// set the request url
curl_setopt( $this->channel_, CURLOPT_URL, $this->requestUrl_ );
// setting up the request method
switch ( $this->requestMethod_ )
{
case 'get' : curl_setopt( $this->channel_, CURLOPT_HTTPGET, true );
break;
case 'post': curl_setopt( $this->channel_, CURLOPT_POST, true );
break;
case 'head': curl_setopt( $this->channel_, CURLOPT_NOBODY, true );
// to get all response headers
curl_setopt( $this->channel_, CURLOPT_HEADER, true );
break;
}
// only when url contains the extension
//$this->setFileExtension_();
return $this->setError_();
}
public function getFileExtension()
{
return $this->fileExtension_;
}
public function getRequestHeaders()
{
return $this->requestHeaders_;
}
public function getInfo()
{
return $this->info_;
}
public function setRequestHeaders( $headersArray )
{
// for setting Range, Referer, Accept-Encoding headers
if( isset( $headersArray['Range'] ) )
{
curl_setopt( $this->channel_, CURLOPT_RANGE, $headersArray['Range'] );
}
if( isset( $headersArray['Referer'] ) )
{
curl_setopt($this->channel_, CURLOPT_REFERER, $headersArray['Referer'] );
}
if( isset( $headersArray['Accept-Encoding'] ) )
{
curl_setopt( $this->channel_, CURLOPT_ENCODING, $headersArray['Accept-Encoding'] );
}
// seeting error_ property
return $this->setError_();
}
public function setFileExtension( $filename = "" )
{
// query string is not present
if( $filename != "" )
{
$this->defaultOutputFile_ = $filename;
// taking string after the last . in name
$this->fileExtension_ = substr( $filename, strrpos($filename, '.') );
}
elseif( strpos( basename( $this->requestUrl_ ), '?') === false )
{
$lastPathPart = basename( $this->requestUrl_ );
// no query string
// this filename will be used if saveTo method is called with no resource handle
$this->defaultOutputFile_ = rawurldecode( $lastPathPart );
// taking string after the last . in name
$this->fileExtension_ = substr( $lastPathPart, strrpos($lastPathPart, '.') );
}// from Content-Disposition header
elseif( $this->requestMethod_ === "head" && isset( $this->getResponseHeaders()['Content-Disposition']) )
{
$header = $this->getResponseHeaders()['Content-Disposition'];
$regex = '#([\'"])([^\'"]*)\1#';
preg_match( $regex, $header , $matches);
$this->defaultOutputFile_ = $matches[2];
$this->fileExtension_ = substr( $matches[2], strrpos($matches[2], '.') );
}// from mime type list
else
{
// only setup extension no filename
$lines = file( "mimeTypeExtension.txt", FILE_IGNORE_NEW_LINES );
$totalLines = count( $lines );
$contentType = $this->getContentType();
for( $i = 0; $i < $totalLines; $i++ )
{
$parts = explode( "\t", $lines[$i] );
// parts[0] - Description
// parts[1] - Content-Type
// parts[2] - Extension
// parts[3] - Description
if( $parts[1] === $contentType )
{
$this->fileExtension_ = $parts[2];
break;
}
}
}
}
public function saveTo( $file = ''/* resource handle */ )
{
$this->outputToFile_ = true;
if( $file === '' )
{
// file name not given
// open handle for the new file
// close it when done
$file = fopen( $this->defaultOutputFile_, 'w' );
}
if( !is_resource( $file ) )
{
$this->error_ = 'Not a valid file handle';
return $this->error_;
}
// for closing the resource when done
$this->fileHandle_ = &$file;
curl_setopt( $this->channel_, CURLOPT_FILE, $file );
return $this->setError_();
}
public function send( $body = '' )
{
// message body of the request in POST method
if( $this->requestMethod_ === 'post' )
{
curl_setopt( $this->channel_, CURLOPT_POSTFIELDS, $body );
}
if( !$this->outputToFile_ )
{
$this->responseBody_ = curl_exec( $this->channel_ );
}
else
{
curl_exec( $this->channel_ );
}
// seeting error_ property
if( $this->setError_() )
{
return $this->setError_();
}
// updating requestHeaders_ property
$this->populateRequestHeaders_();
$this->setResponseHeader_();
if( $this->getFileName() === "" )
{
$this->setFileExtension();
}
/*
Freeing up the resources
*/
curl_close( $this->channel_ );
if( $this->outputToFile_ )
{
fclose( $this->fileHandle_ );
}
}
public function getFileName()
{
return $this->defaultOutputFile_;
}
public function getResponseBody()
{
return $this->responseBody_;
}
public function getResponseCode()
{
return $this->responseHeaders_['Response-Code'];
}
public function getResponseHeaders()
{
return $this->responseHeaders_;
}
public function getContentLength()
{
return $this->responseHeaders_['Content-Length'];
}
public function getContentType()
{
return $this->responseHeaders_['Content-Type'];
}
public function attachProgressFunction( $handler )
{
curl_setopt( $this->channel_, CURLOPT_PROGRESSFUNCTION, $handler );
}
public function getError()
{
return $this->error_;
}
}
?>