-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
213 lines (184 loc) · 6.75 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
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
<?php
ini_set('display_errors', '1');
error_reporting(E_ERROR);
require_once "config.php";
$method = $_SERVER['REQUEST_METHOD'];
$req_url = $_SERVER['SCRIPT_URI'] ?? ($_SERVER['REQUEST_SCHEME'] ?? "http" ) . '://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$get = $_GET;
$post = $_POST;
$body = file_get_contents('php://input');
$headers = getRequestHeaders();
// respond to preflights
if ($CONFIG['cors_origin'] && $method == 'OPTIONS') {
// return only the headers and not the content
header('Access-Control-Allow-Origin: '. $CONFIG['cors_origin']);
header('Access-Control-Allow-Headers: '. $CONFIG['cors_headers']);
header('Access-Control-Allow-Methods: '. $CONFIG['cors_methods']);
exit;
}
//file_put_contents("server_dump_request.log", print_r($headers,true), FILE_APPEND );
$targetUrl = strtok(preg_replace($CONFIG['redirect_from'], $CONFIG['redirect_to'], $req_url), "?");
//echo $url;
//validate target URL
if (!preg_match("/^(http|https):\/\//", $targetUrl)) {
response(400, "url is required");
}
$targetMethod = $method;
$targetGet = $get;
$targetPost = $post;
$targetHeaders = $headers;
$targetBody = $body;
//unset($targetHeaders['Host']);
//Modify request if request config value is callable
if (is_callable($CONFIG['request'])) {
$processedRequest = call_user_func($CONFIG['request'], $targetMethod, $targetGet, $targetPost, $targetBody, $targetHeaders);
$targetMethod = $processedRequest[0];
$targetGet = $processedRequest[1];
$targetPost = $processedRequest[2];
$targetBody = $processedRequest[3];
$targetHeaders = $processedRequest[4];
}
$targetResponseBody = httpRequest(
$targetUrl,
$targetMethod,
$targetGet,
$targetPost,
$targetBody,
$targetHeaders,
$targetResponseStatus, //out
$targetResponseHeaders //out
);
//file_put_contents("server_dump_request.log", "return headers" . print_r($retHeaders,true), FILE_APPEND );
$responseBody = $targetResponseBody;
$responseStatus= $targetResponseStatus;
$responseHeaders = $targetResponseHeaders;
//Modify response if response config value is callable
if (is_callable($CONFIG['response'])) {
$processedResponse = call_user_func($CONFIG['response'], $targetResponseHeaders, $targetResponseBody, $targetResponseStatus);
$responseHeaders = $processedResponse[0];
$responseBody = $processedResponse[1];
$responseStatus = $processedResponse[2];
}
if ($responseStatus == 0) {
$responseStatus = 500;
}
//remove transfer-encoding header that is set up automatically
unset($responseHeaders["transfer-encoding"]);
//Set headers for response to the client
foreach ($responseHeaders as $key=>$header){
if (is_array($header)){
foreach ($header as $header_1){
header( $key . ': ' . $header_1, false);
}
} else {
header( $key . ': ' . $header. false);
}
}
//respond client
response($responseStatus, $responseBody);
exit;
function response($status,$data)
{
http_response_code($status);
echo $data;
exit();
}
/**
* @returns array [key=>value]
*/
function getRequestHeaders() {
$headers = array();
foreach($_SERVER as $key => $value) {
if (substr($key, 0, 5) <> 'HTTP_') {
continue;
}
$header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
$headers[$header] = $value;
}
return $headers;
}
/**
* @param string $url
* @param string $method - POST, GET
* @param array $queryArray [key=>value]
* @param array $headers [key=>value]
* @param int &$status - response status code
* @return string|null
*/
function httpRequest($url, $method, $queryArray, $post=[], $body = null, $headers = null, &$status = null, &$returnHeaders = null) {
$request = curl_init();
// Set request options
try {
switch ($method) {
case "PUT":
case 'DELETE':
case 'OPTIONS':
case 'POST':
curl_setopt($request, CURLOPT_URL, $url . ($queryArray ? "?" . http_build_query($queryArray):""));
if (count($post) > 0){
curl_setopt($request, CURLOPT_POST, 1);
curl_setopt($request, CURLOPT_POSTFIELDS, $post);
//these will be auto-calulated
unset($headers['Content-Type']);
unset($headers['Content-Length']);
}else{
curl_setopt($request, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($request, CURLOPT_POSTFIELDS, $body);
}
break;
case 'GET':
default:
curl_setopt_array($request, array(
CURLOPT_URL => $url . ($queryArray ? "?" . http_build_query($queryArray):""),
));
}
$k_headers = [];
if (!empty($headers)){
foreach ($headers as $key=>$value){
$k_headers[] = $key . ": " . $value;
}
}
curl_setopt($request, CURLOPT_HTTPHEADER, $k_headers);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_HEADER, true);
$returnHeaders = [];
curl_setopt($request, CURLOPT_HEADERFUNCTION,
function($curl, $header) use (&$returnHeaders)
{
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
$key = strtolower(trim($header[0]));
$value = trim($header[1]);
if (array_key_exists($key,$returnHeaders)) {
if (!is_array($returnHeaders[$key])) {
$returnHeaders[$key] = [$returnHeaders[$key]];
}
$returnHeaders[$key][] = $value;
} else {
$returnHeaders[$key] = $value;
}
return $len;
}
);
/*if (!empty($username) && !empty($password)) {
curl_setopt($request, CURLOPT_USERPWD, "$username:$password");
curl_setopt($request, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}*/
// Execute request and get response and status code
$response = curl_exec($request);
$status = curl_getinfo($request, CURLINFO_HTTP_CODE);
$error = curl_error($request);
//list($returnHeaders, $body) = explode("\r\n\r\n", $response, 2);
$header_size = curl_getinfo($request, CURLINFO_HEADER_SIZE);
//$returnHeaders = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($request);
return $body;
} catch (Exception $ex) {
return null;
}
return null;
}
?>