-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsparql-proxy.php
211 lines (139 loc) · 5.04 KB
/
sparql-proxy.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
<?php
// Unless we succeed we fail
header('HTTP/1.1 500 Internal Server Error', true, 500);
error_reporting(E_ALL);
/*****************************************************************************/
/* Configuration */
/*****************************************************************************/
// Note: The defaultServiceUrl is not subject to proxy url validation.
$defaultServiceUrl = "http://localhost:8890/sparql";
// For security reasons, only SPARQL query string parameters are forwarded.
$allowedParams = array("format", "query", "timeout", "default-graph-uri");
$serviceUrl = null;
// Request headers that should not be forwarded to the target address
$ignoreRequestHeaders = array("host");
// Response headers that should not be returned to the client
// We ignore transfer encoding because curl already retrieves the full response
// Note: Unfortunately no streaming - feel free to contribute :)
$ignoreResponseHeaders=array("transfer-encoding");
/*****************************************************************************/
/* Utility */
/*****************************************************************************/
function isCurlAvailable() {
//$result = function_exists('curl_version') == 'Enabled';
$result = is_callable('curl_init');
return $result;
}
if(!isCurlAvailable()) {
//header('HTTP/1.1 500 Internal Server Error');
//header('HTTP/1.1 200 OK');
//throw new Exception('Curl is not available on this system.');
echo "Cannot serve request because PHP curl is not available.";
die;
//return 1;
}
/*****************************************************************************/
/* Business Logic */
/*****************************************************************************/
function validateProxyUrl($url) {
$u = parse_url($url);
$scheme = isset($u["scheme"]) ? $u["scheme"] : "";
if(strcmp($scheme, "http") != 0) {
echo "Only http scheme is allowed for proxying";
die;
}
// TODO Check for user, pass, query; rather than just discarding them silently
$host = $u["host"];
$port = isset($u["port"]) ? (":" . $u["port"]) : "";
$path = $u["path"];
$result = "$scheme://$host$port$path";
return $result;
}
if(isset($_REQUEST['service-uri'])) {
$rawServiceUrl = $_REQUEST['service-uri'];
$serviceUrl = validateProxyUrl($rawServiceUrl);
} else {
$serviceUrl = $defaultServiceUrl;
}
//echo "ServiceUrl: $serviceUrl\n\n";
$args = $_SERVER['QUERY_STRING'];
$validArgs = array();
$qs = explode("&", $args);
foreach($qs as $item) {
$kv = explode("=", $item, 2);
$key = $kv[0];
//echo "item: $item --- $key \n";
if(in_array($key, $allowedParams)) {
array_push($validArgs, $item);
}
}
$validArgsStr = implode("&", $validArgs);
// TODO Security issue: filter the query string for valid parameters
$finalUrl = "$serviceUrl?$validArgsStr";
//echo "Final URL: $finalUrl\n";
// NOTE getallheaders only works with apache - see http://php.net/manual/en/function.getallheaders.php
$requestHeaders = getallheaders();
//print_r($requestHeaders);
$headers = array();
foreach($requestHeaders as $k => $v) {
if(in_array(strtolower($k), $ignoreRequestHeaders)) {
continue;
}
array_push($headers, "$k: $v");
}
//print_r($headers);
// Prepare and perform the request
$ch = curl_init();
if($ch === FALSE) {
echo "Failed to initialize curl";
die;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "$finalUrl");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
if($response === FALSE) {
$errMsg = curl_error($ch);
$errCode = curl_errno($ch);
echo "Error communicating with service at: $serviceUrl\n";
echo "Curl error code $errCode: $errMsg";
die;
}
//echo "Response is $response\n";
$info = curl_getinfo($ch);
curl_close($ch);
//error_log($info['download_content_length']);
//error_log($response);
//$httpStatus = $info['CURLINFO_HTTP_CODE'];
//header('HTTP/1.1 200 OK', true, 200);
//echo "Status: $httpStatus";
$statusCode = $info['http_code'];
$headerSize = $info['header_size'];
$responseHeader = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
// The old way I used breaks with chunked transfer - lets of the current
// approach is more robust
##$body = substr($response, -$info['download_content_length']);
//$statusMsg = "HTTP/1.1 200 OK";
// Exclude ignored response headers
foreach (explode("\r\n",$responseHeader) as $hdr) {
$tmp = explode(":", $hdr, 2);
if(count($tmp) != 2) {
// This is the HTTP status code
$statusMsg = $tmp[0];
} else {
list($k, $v) = $tmp;
$k = strtolower(trim($k));
if($k == '' || in_array($k, $ignoreResponseHeaders)) {
continue;
}
}
header($hdr);
}
//header('HTTP/1.1 200 OK', true, 200);
//echo $statusMsg;
//die;
//header($statusMsg, true, $statusCode);
echo "$body";