-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
47 lines (41 loc) · 1.48 KB
/
api.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
<?php
/**
* This is an extremely simple proxy so that all ClusterRunner API requests can go
* thru the same host as the dashboard.
*/
$masterUrl = substr($_SERVER['REQUEST_URI'], strlen('/api.php/'));
if (0 != strpos($masterUrl, 'http')) {
$masterUrl = 'http://' . $masterUrl;
}
$origin = 'http://' . $_SERVER['HTTP_HOST'];
$ch = curl_init($masterUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Origin: ' . $origin,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
curl_close($ch);
$headers = explode("\r\n", $headers);
$headersByName = [];
foreach ($headers as $header) {
$colonIndex = strpos($header, ':');
if ($colonIndex === false) continue;
$headerKey = substr($header, 0, $colonIndex);
$headerValue = substr($header, $colonIndex + 2);
$headersByName[$headerKey] = $headerValue;
}
if ($httpCode !== 200) {
$body = json_encode(['error' => 'Expected 200 http status but got ' . $httpCode]);
}
// Do a dumb version of web browser CORS check.
else if ($headersByName['Access-Control-Allow-Origin'] != $origin) {
$body = json_encode(['error' => 'Request denied due to cross-origin restrictions.']);
}
header('Content-Type: ' . $headersByName['Content-Type']);
echo($body);