-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
148 lines (105 loc) · 2.58 KB
/
index.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
<?php
class Proxy
{
protected $curl;
public function __construct($publicUrl, $privateUrl)
{
$this->curl = curl_init();
$privateUrl = rtrim($privateUrl,'/');
if (isset($_SERVER['PATH_INFO']))
{
$privateUrl .= $_SERVER['PATH_INFO'];
}
else
{
$privateUrl .= '/';
}
$privateUrl .= $_SERVER['QUERY_STRING'] !== '' ? "?{$_SERVER['QUERY_STRING']}" : "";
// set URL and other appropriate options
curl_setopt($this->curl, CURLOPT_URL, $privateUrl);
curl_setopt($this->curl, CURLOPT_HEADER, 0);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_BINARYTRANSFER, 0); // For images, etc.
curl_setopt($this->curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($this->curl, CURLOPT_WRITEFUNCTION, [$this, 'readResponse']);
curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, [$this, 'readHeaders']);
$this->createRequestHeaders();
}
public function excute()
{
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->requestHeaders);
curl_exec($this->curl);
curl_close($this->curl);
}
protected function readHeaders($cu, $string)
{
$length = strlen($string);
if (preg_match('#^HTTP#', $string))
{
//return $length;
}
if (preg_match('#^Location:#', $string))
{
//$string = str_replace($this->proxy_url, $this->url, $string);
}
elseif (preg_match('#^Cache-Control:#', $string))
{
//$this->cache_control = true;
//return;
}
elseif (preg_match('#^Pragma:#', $string))
{
//$this->pragma = true;
}
if ($string !== "\r\n")
{
if (!preg_match('#^Transfer#', $string))
{
header(rtrim($string));
}
}
return $length;
}
protected function readResponse(&$cu, $string)
{
$headersParsed = true;
// Clear the Cache-Control and Pragma headers
// if they aren't passed from the proxy application.
if ($headersParsed === false)
{
if (!$this->cache_control)
{
header('Cache-Control: ');
}
if (!$this->pragma)
{
header('Pragma: ');
}
$headersParsed = true;
}
$length = strlen($string);
echo $string;
return $length;
}
protected function createRequestHeaders()
{
$headers = apache_request_headers();
foreach ($headers as $header => $value)
{
switch($header)
{
case 'Host':
break;
default:
$this->requestHeaders[] = sprintf('%s: %s', $header, $value);
break;
}
}
}
}
//$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
//header('Content-Type:'.$contentType);
//echo '<pre/>';
//print_r(apache_request_headers());exit;
$p = new Proxy('', 'http://tutsplus.com');
$p->excute();