forked from chricke/php5rp_ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxyHandler.class.php
557 lines (501 loc) · 15.4 KB
/
ProxyHandler.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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?php
/**
* php5rp_ng - PHP5 Reverse Proxy Next Generation
*
* @link https://github.com/chricke/php5rp_ng
* @copyright Copyright (c) 2010, 2013 Christian "chricke" Beckmann < mail@christian-beckmann.net >.
* @license https://github.com/chricke/php5rp_ng/blob/master/README.md BSD license.
*/
class ProxyHandler
{
/**
* Статусы ответов.
*
* @link http://httpstatus.es/
* @link http://upload.wikimedia.org/wikipedia/commons/6/65/Http-headers-status.gif?uselang=ru
*
* @var array
*/
private $_status_codes = array (
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing', // (WebDAV) (RFC 2518)
103 => 'Checkpoint',
122 => 'Request-URI too long',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information', // (since HTTP/1.1)
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status', // (WebDAV) (RFC 4918)
208 => 'Already Reported', // (WebDAV) (RFC 5842)
226 => 'IM Used', // (RFC 3229)
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy', // (since HTTP/1.1)
306 => 'Switch Proxy',
307 => 'Temporary Redirect', // (since HTTP/1.1)
308 => 'Resume Incomplete',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
418 => 'I\'m a teapot', // (RFC 2324)
420 => 'Enhance Your Calm',
422 => 'Unprocessable Entity', // (WebDAV) (RFC 4918)
423 => 'Locked', // (WebDAV) (RFC 4918)
424 => 'Failed Dependency', // (WebDAV) (RFC 4918)
426 => 'Upgrade Required', // (RFC 2817)
428 => 'Precondition Required',
429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
444 => 'No Response',
449 => 'Retry With',
450 => 'Blocked by Windows Parental Controls',
451 => 'Wrong Exchange server',
499 => 'Client Closed Request',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates', // (RFC 2295)
507 => 'Insufficient Storage', // (WebDAV) (RFC 4918)
508 => 'Loop Detected', // (WebDAV) (RFC 5842)
509 => 'Bandwidth Limit Exceeded', // (Apache bw/limited extension)
510 => 'Not Extended', // (RFC 2774)
511 => 'Network Authentication Required',
598 => 'Network read timeout error',
599 => 'Network connect timeout error',
);
/**
* @type string
*/
const RN = "\r\n";
/**
* @type boolean
*/
private $_cacheControl = false;
/**
* @type boolean
*/
private $_chunked = false;
/**
* @type array
*/
private $_clientHeaders = array();
/**
* @type resource
*/
private $_curlHandle;
/**
* @type boolean
*/
private $_pragma = false;
/**
* Body when chunked response
* @var string
*/
private $_body = '';
/**
* @var string
*/
private $_encoding = '';
/**
* @var bool
*/
private $_passRealIP = true;
/**
* Uri
* @var string
*/
private $_translatedUri = '';
/**
* http status of response
* @var int
*/
private $_httpStatus = 0;
/**
* User specified callback to modify client headers
* Called for each header (two arguments: headerName, value)
* Should return array of two elements 0=>headerName, 1=>value
* Or false if header should be removed from request to upstream
*
* @var null|callback
*/
private $_processClientHeaderCallback = null;
/**
* User specified callback to filter response headers
* Called for each header
* Should return modified header or empty string to skip header
* @var null|callback
*/
private $_filterResponseHeaderCallback = null;
/**
* User specified callback to filter response body
* Called for body
* Should return modified body
* @var null|callback
*/
private $_filterResponseBodyCallback = null;
/**
* Create a new ProxyHandler
*
* @param array|string $options
*/
function __construct($options)
{
if (is_string($options)) {
$options = array('proxyUri' => $options);
}
// Trim slashes, we will append what is needed later
$translatedUri = rtrim($options['proxyUri'], '/');
// Get all parameters from options
$baseUri = '';
if (isset($options['baseUri'])) {
$baseUri = $options['baseUri'];
}
elseif (!empty($_SERVER['REDIRECT_URL'])) {
$baseUri = dirname($_SERVER['REDIRECT_URL']);
}
$requestUri = '';
if (isset($options['requestUri'])) {
$requestUri = $options['requestUri'];
}
else {
if (!empty($_SERVER['REQUEST_URI'])) {
$requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
}
if (!empty($_SERVER['QUERY_STRING'])) {
$requestUri .= '?' . $_SERVER['QUERY_STRING'];
}
}
if (!empty($requestUri)) {
if (!empty($baseUri)) {
$baseUriLength = strlen($baseUri);
if (substr($requestUri, 0, $baseUriLength) === $baseUri) {
$requestUri = substr($requestUri, $baseUriLength);
}
}
$translatedUri .= $requestUri;
}
else {
$translatedUri .= '/';
}
if ( isset($options['processClientHeaderCallback']) ) {
$this->_processClientHeaderCallback = $options['processClientHeaderCallback'];
}
if ( isset($options['filterResponseHeaderCallback']) ) {
$this->_filterResponseHeaderCallback = $options['filterResponseHeaderCallback'];
}
if ( isset($options['filterResponseBodyCallback']) ) {
$this->_filterResponseBodyCallback = $options['filterResponseBodyCallback'];
}
if ( isset($options['passRealIP']) ) {
$this->_passRealIP = (bool) $options['passRealIP'];
}
$this->_translatedUri = $translatedUri;
$this->_curlHandle = curl_init($this->_translatedUri);
// Set various cURL options
$this->setCurlOption(CURLOPT_FOLLOWLOCATION, true);
$this->setCurlOption(CURLOPT_RETURNTRANSFER, true);
// For images, etc.
$this->setCurlOption(CURLOPT_BINARYTRANSFER, true);
$this->setCurlOption(CURLOPT_WRITEFUNCTION, array($this, 'readResponse'));
$this->setCurlOption(CURLOPT_HEADERFUNCTION, array($this, 'readHeaders'));
//$this->setCurlOption(CURLOPT_VERBOSE, true);
$requestMethod = '';
if (isset($options['requestMethod'])) {
$requestMethod = $options['requestMethod'];
}
elseif (!empty($_SERVER['REQUEST_METHOD'])) {
$requestMethod = $_SERVER['REQUEST_METHOD'];
}
// Default cURL request method is 'GET'
if ($requestMethod !== 'GET') {
$this->setCurlOption(CURLOPT_CUSTOMREQUEST, $requestMethod);
$inputStream = isset($options['inputStream']) ? $options['inputStream'] : 'php://input';
switch($requestMethod) {
case 'POST':
if (isset($options['data'])) {
$data = $options['data'];
}
else {
if (!isset($HTTP_RAW_POST_DATA)) {
$HTTP_RAW_POST_DATA = file_get_contents($inputStream);
}
$data = $HTTP_RAW_POST_DATA;
}
$this->setCurlOption(CURLOPT_POSTFIELDS, $data);
break;
case 'PUT':
// Set the request method.
$this->setCurlOption(CURLOPT_UPLOAD, 1);
// PUT data comes in on the stdin stream.
$putData = fopen($inputStream, 'r');
$this->setCurlOption(CURLOPT_READDATA, $putData);
// TODO: set CURLOPT_INFILESIZE to the value of Content-Length.
break;
}
}
// Handle the client headers.
$this->handleClientHeaders();
}
/**
* @return array
*/
private function _getRequestHeaders()
{
if (function_exists('apache_request_headers')) {
if ($headers = apache_request_headers()) {
return $headers;
}
}
$headers = array();
foreach ($_SERVER as $key => $value) {
if (substr($key, 0, 5) == 'HTTP_' && !empty($value)) {
$headerName = strtolower(substr($key, 5, strlen($key)));
$headerName = str_replace(' ', '-', ucwords(str_replace('_', ' ', $headerName)));
$headers[$headerName] = $value;
}
}
return $headers;
}
/**
* @param string $headerName
* @return void
*/
private function _removeHeader($headerName)
{
if (function_exists('header_remove')) {
header_remove($headerName);
} else {
header($headerName . ': ');
}
}
/**
* Adds the remote servers address to the 'X-Forwarded-For' headers,
* sets the 'X-Real-IP' header to the first address forwarded to and
* removes some headers we shouldn't pass through.
*
* @return void
*/
protected function handleClientHeaders()
{
$headers = $this->_getRequestHeaders();
$xForwardedFor = array();
foreach ($headers as $headerName => $value) {
$l = $this->processClientHeader($headerName, $value);
if ( $l !== false ) {
$this->setClientHeader($l[0], $l[1]);
continue;
}
switch($headerName) {
case 'Host':
case 'X-Real-IP':
break;
case 'X-Forwarded-For':
$xForwardedFor[] = $value;
break;
default:
$this->setClientHeader($headerName, $value);
break;
}
}
if ( $this->_passRealIP === true ) {
$xForwardedFor[] = $_SERVER['REMOTE_ADDR'];
$this->setClientHeader('X-Forwarded-For', implode(',', $xForwardedFor));
$this->setClientHeader('X-Real-IP', $xForwardedFor[0]);
}
}
/**
* Process client header (call user callback)
* Header can be modified by user func or deny to proxy pass
*
* @param $headerName
* @param $value
* @return array|bool - array if header should be set, false otherwise
*/
protected function processClientHeader($headerName, $value)
{
if ( $this->_processClientHeaderCallback !== null ) {
$l = call_user_func( $this->_processClientHeaderCallback, $headerName, $value );
if ( $l !== false && is_array($l) && count($l) === 2)
return $l;
else
return false;
}
else
return array ( $headerName, $value );
}
/**
* Our handler for cURL option CURLOPT_HEADERFUNCTION
*
* @param resource $cu
* @param string $header
* @return int
*/
protected function readHeaders(&$cu, $header)
{
$length = strlen($header);
if (preg_match(',^HTTP/[^\s]+\s+(\d+)\s+,', $header, $rg)) {
$this->_httpStatus = intval($rg[1]);
if ( $this->_httpStatus === 0 || !in_array($this->_httpStatus, $this->_status_codes) )
$this->_httpStatus = 500;
// skip this header
return $length;
}
elseif (preg_match(',^Cache-Control:,', $header)) {
$this->_cacheControl = true;
}
elseif (preg_match(',^Pragma:,', $header)) {
$this->_pragma = true;
}
elseif (preg_match(',^Transfer-Encoding:,', $header)) {
$this->_chunked = strpos($header, 'chunked') !== false;
// skip this header
return $length;
}
elseif (preg_match(',^Content-Encoding:(.+)$,', trim($header), $rg)) {
$this->_encoding = trim($rg[1]);
// skip this header
return $length;
}
if ($header !== self::RN) {
if ( $this->_filterResponseHeaderCallback !== null ) {
$header = call_user_func( $this->_filterResponseHeaderCallback, $header );
if ( $header !== "" ) {
header($header, false);
}
} else {
header(rtrim($header), false);
}
}
return $length;
}
/**
* Our handler for cURL option CURLOPT_WRITEFUNCTION
*
* @param resource $cu
* @param string $body
* @return int
*/
protected function readResponse(&$cu, $body)
{
static $headersParsed = false;
// Clear the Cache-Control and Pragma headers
// if they aren't passed from the proxy application.
if ($headersParsed === false) {
if (!$this->_cacheControl) {
$this->_removeHeader('Cache-Control');
}
if (!$this->_pragma) {
$this->_removeHeader('Pragma');
}
$headersParsed = true;
}
$length = strlen($body);
$this->_body .= $body;
return $length;
}
/**
* Close the cURL handle and a possible chunked response
*
* @return void
*/
public function close()
{
header(isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0', ' ' . $this->_httpStatus . ' ' . $this->_status_codes[$this->_httpStatus], true);
if ( $this->_encoding == 'gzip' ) {
$this->_body = gzdecode($this->_body);
}
if ($this->_chunked) {
if ( $this->_filterResponseBodyCallback !== null ) {
echo call_user_func( $this->_filterResponseBodyCallback, $this->_translatedUri, $this->_body );
} else {
echo $this->_body;
}
}else{
echo $this->_body;
}
curl_close($this->_curlHandle);
}
/**
* Executes the cURL handler, making the proxy request.
* Returns true if request is successful, false if there was an error.
* By checking this return, you may output the return from getCurlError
* Or output your own bad gateway page.
*
* @return boolean
*/
public function execute()
{
$this->setCurlOption(CURLOPT_HTTPHEADER, $this->_clientHeaders);
return curl_exec($this->_curlHandle) !== false;
}
/**
* Get possible cURL error.
* Should NOT be called before exec.
*
* @return string
*/
public function getCurlError()
{
return curl_error($this->_curlHandle);
}
/**
* Get information about the request.
* Should NOT be called before exec.
*
* @return array
*/
public function getCurlInfo()
{
return curl_getinfo($this->_curlHandle);
}
/**
* Sets a new header that will be sent with the proxy request
*
* @param string $headerName
* @param string $value
* @return void
*/
public function setClientHeader($headerName, $value)
{
$this->_clientHeaders[] = $headerName . ': ' . $value;
}
/**
* Sets a cURL option.
*
* @param string $option
* @param string $value
* @return void
*/
public function setCurlOption($option, $value)
{
curl_setopt($this->_curlHandle, $option, $value);
}
}
if ( !function_exists('gzdecode') ) {
include 'func.gzdecode.php';
}