forked from niutech/node.php
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
78 lines (52 loc) · 1.53 KB
/
Copy pathindex.php
File metadata and controls
78 lines (52 loc) · 1.53 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
<?php
/**
* Node.php v0.4
* (c) 2016 Jerzy Głowacki
* MIT License
*/
error_reporting(0);
set_time_limit(120);
define("NODE_DIR", "node");
define("NODE_PORT", 49999);
function node_serve($path = "") {
if(!file_exists(NODE_DIR)) {
header('HTTP/1.0 404 Not Found');
exit;
}
$node_pid = intval(file_get_contents("nodepid"));
if($node_pid === 0) {
header('HTTP/1.0 503 Service Unavailable');
echo "Server down, help me!\n";
exit;
}
$curl = curl_init("http://127.0.0.1:" . NODE_PORT . "/$path");
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$headers = array();
foreach(getallheaders() as $key => $value) {
$headers[] = $key . ": " . $value;
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $_SERVER["REQUEST_METHOD"]);
if($_SERVER["REQUEST_METHOD"] === "POST") {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($_POST));
}
$resp = curl_exec($curl);
if($resp === false) {
header('HTTP/1.0 404 Not Found');
exit;
} else {
list($head, $body) = explode("\r\n\r\n", $resp, 2);
$headarr = explode("\n", $head);
foreach($headarr as $headval) {
header($headval);
}
echo $body;
}
curl_close($curl);
}
function node_dispatch() {
isset($_GET['path']) ? node_serve($_GET['path']) : node_serve();
}
node_dispatch();