ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
XapiProxyResponse.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20namespace XapiProxy;
21
22use Psr\Http\Message\ServerRequestInterface;
23use Psr\Http\Message\ResponseInterface;
24use GuzzleHttp\Psr7\Response;
25use GuzzleHttp\Psr7\Request;
26
28{
29 // private $dic;
31 //private $xapiProxyRequest;
32
34 {
35 // $this->dic = $GLOBALS['DIC'];
36 $this->xapiproxy = $xapiproxy;
37 }
38
39 public function checkResponse(array $response, string $endpoint): bool
40 {
41 if ($response['state'] == 'fulfilled') {
42 $status = $response['value']->getStatusCode();
43 if ($status === 200 || $status === 204 || $status === 404) {
44 return true;
45 } else {
46 $this->xapiproxy->log()->error("LRS error {$endpoint}: " . $response['value']->getBody());
47 return false;
48 }
49 } else {
50 try {
51 $this->xapiproxy->log()->error("Connection error {$endpoint}: " . $response['reason']->getMessage());
52 } catch (\Exception $e) {
53 $this->xapiproxy->log()->error("error {$endpoint}:" . $e->getMessage());
54 }
55 return false;
56 }
57 }
58
65 public function handleResponse(Request $request, Response $response, array|string|null $fakePostBody = null): void
66 {
67 // check transfer encoding bug
68 if ($fakePostBody !== null) {
69 $origBody = $response->getBody();
70 $this->xapiproxy->log()->debug($this->msg("orig body: " . $origBody));
71 $this->xapiproxy->log()->debug($this->msg("fake body: " . json_encode($fakePostBody)));
72 // because there is a real response object, it should also be possible to override the response stream...
73 // but this does the job as well:
74 $this->fakeResponseBlocked($fakePostBody);
75 }
76 $status = $response->getStatusCode();
77 $headers = $response->getHeaders();
78 if (array_key_exists('Transfer-Encoding', $headers) && $headers['Transfer-Encoding'][0] == "chunked") {
79 $this->xapiproxy->log()->debug($this->msg("sniff response transfer-encoding for unallowed Content-length"));
80 $body = (string) $response->getBody();
81 unset($headers['Transfer-Encoding']);
82 $headers['Content-Length'] = array(strlen($body));
83 $response2 = new \GuzzleHttp\Psr7\Response($status, $headers, $body);
84 $this->emit($response2);
85 } else {
86 $this->emit($response);
87 }
88 }
89
94 public function fakeResponseBlocked(array|string|null $post = null): void
95 {
96 $this->xapiproxy->log()->debug($this->msg("fakeResponseFromBlockedRequest"));
97 if ($post === null) {
98 $this->xapiproxy->log()->debug($this->msg("post === NULL"));
99 try {
100 $origin = (isset($_SERVER["HTTP_ORIGIN"])) ? $_SERVER["HTTP_ORIGIN"] : $_SERVER["HTTP_REFERRER"];
101 if (isset($origin) && $origin != "") {
102 header('Access-Control-Allow-Origin: ' . $origin);
103 } else {
104 $this->xapiproxy->log()->warning("could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
105 }
106 } catch (\Exception $e) {
107 $this->xapiproxy->log()->warning($e->getMessage());
108 }
109 header('Access-Control-Allow-Credentials: true');
110 header('X-Experience-API-Version: 1.0.3');
111 header('HTTP/1.1 204 No Content');
112 exit;
113 } else {
114 $ids = json_encode($post);
115 $this->xapiproxy->log()->debug($this->msg("post: " . $ids));
116 try {
117 $origin = (isset($_SERVER["HTTP_ORIGIN"])) ? $_SERVER["HTTP_ORIGIN"] : $_SERVER["HTTP_REFERRER"];
118 if (isset($origin) && $origin != "") {
119 header('Access-Control-Allow-Origin: ' . $origin);
120 } else {
121 $this->xapiproxy->log()->warning("could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
122 }
123 } catch (\Exception $e) {
124 $this->xapiproxy->log()->warning($e->getMessage());
125 }
126 header('Access-Control-Allow-Credentials: true');
127 header('X-Experience-API-Version: 1.0.3');
128 header('Content-Length: ' . strlen($ids));
129 header('Content-Type: application/json; charset=utf-8');
130 header('HTTP/1.1 200 Ok');
131 echo $ids;
132 exit;
133 }
134 }
135
136 public function exitResponseError(): void
137 {
138 try {
139 $origin = (isset($_SERVER["HTTP_ORIGIN"])) ? $_SERVER["HTTP_ORIGIN"] : $_SERVER["HTTP_REFERRER"];
140 if (isset($origin) && $origin != "") {
141 header('Access-Control-Allow-Origin: ' . $origin);
142 } else {
143 $this->xapiproxy->log()->warning("could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
144 }
145 } catch (\Exception $e) {
146 $this->xapiproxy->log()->warning($e->getMessage());
147 }
148 header('Access-Control-Allow-Credentials: true');
149 header('X-Experience-API-Version: 1.0.3');
150 header("HTTP/1.1 412 Wrong Response");
151 echo "HTTP/1.1 412 Wrong Response";
152 exit;
153 }
154
155 public function exitProxyError(): void
156 {
157 try {
158 $origin = (isset($_SERVER["HTTP_ORIGIN"])) ? $_SERVER["HTTP_ORIGIN"] : $_SERVER["HTTP_REFERRER"];
159 if (isset($origin) && $origin != "") {
160 header('Access-Control-Allow-Origin: ' . $origin);
161 } else {
162 $this->xapiproxy->log()->warning("could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
163 }
164 } catch (\Exception $e) {
165 $this->xapiproxy->log()->warning($e->getMessage());
166 }
167 header('Access-Control-Allow-Credentials: true');
168 header('X-Experience-API-Version: 1.0.3');
169 header("HTTP/1.1 500 XapiProxy Error (Ask For Logs)");
170 echo "HTTP/1.1 500 XapiProxy Error (Ask For Logs)";
171 exit;
172 }
173
174 public function exitBadRequest(): void
175 {
176 try {
177 $origin = (isset($_SERVER["HTTP_ORIGIN"])) ? $_SERVER["HTTP_ORIGIN"] : $_SERVER["HTTP_REFERRER"];
178 if (isset($origin) && $origin != "") {
179 header('Access-Control-Allow-Origin: ' . $origin);
180 } else {
181 $this->xapiproxy->log()->warning("could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
182 }
183 } catch (\Exception $e) {
184 $this->xapiproxy->log()->warning($e->getMessage());
185 }
186 header('Access-Control-Allow-Credentials: true');
187 header('X-Experience-API-Version: 1.0.3');
188 header("HTTP/1.1 400 XapiProxy Bad Request (Ask For Logs)");
189 echo "HTTP/1.1 400 XapiProxy Bad Request (Ask For Logs)";
190 exit;
191 }
192
193 public function sendData(string $obj): void
194 {
195 $this->xapiproxy->log()->debug($this->msg("sendData: " . $obj));
196 try {
197 $origin = (isset($_SERVER["HTTP_ORIGIN"])) ? $_SERVER["HTTP_ORIGIN"] : $_SERVER["HTTP_REFERRER"];
198 if (isset($origin) && $origin != "") {
199 header('Access-Control-Allow-Origin: ' . $origin);
200 } else {
201 $this->xapiproxy->log()->warning("could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
202 }
203 } catch (\Exception $e) {
204 $this->xapiproxy->log()->warning($e->getMessage());
205 }
206 header('Access-Control-Allow-Credentials: true');
207 header('X-Experience-API-Version: 1.0.3');
208 header('Content-Length: ' . strlen($obj));
209 header('Content-Type: application/json; charset=utf-8');
210 header('HTTP/1.1 200 Ok');
211 echo $obj;
212 exit;
213 }
214
215 public function emit(\GuzzleHttp\Psr7\Response $response): void
216 {
217 $this->xapiproxy->log()->debug($this->msg('emitting response'));
218 if (headers_sent()) {
219 $this->xapiproxy->log()->error($this->msg("Headers already sent!"));
220 $this->exitProxyError();
221 }
222 if (ob_get_level() > 0 && ob_get_length() > 0) {
223 $this->xapiproxy->log()->error($this->msg("Outputstream not empty!"));
224 $this->exitProxyError();
225 }
226
227 $reasonPhrase = $response->getReasonPhrase();
228 $statusCode = $response->getStatusCode();
229
230 // header
231 foreach ($response->getHeaders() as $header => $values) {
232 $name = ucwords($header, '-');
233 $first = $name === 'Set-Cookie' ? false : true;
234 foreach ($values as $value) {
235 header(sprintf(
236 '%s: %s',
237 $name,
238 $value
239 ), $first, $statusCode);
240 $first = false;
241 }
242 }
243
244 // statusline
245 header(sprintf(
246 'HTTP/%s %d%s',
247 $response->getProtocolVersion(),
248 $statusCode,
249 ($reasonPhrase ? ' ' . $reasonPhrase : '')
250 ), true, $statusCode);
251
252 // body
253 echo $response->getBody();
254 }
255
256 private function msg(string $msg): string
257 {
258 return $this->xapiproxy->msg($msg);
259 }
260}
handleResponse(Request $request, Response $response, array|string|null $fakePostBody=null)
__construct(XapiProxy $xapiproxy)
fakeResponseBlocked(array|string|null $post=null)
emit(\GuzzleHttp\Psr7\Response $response)
checkResponse(array $response, string $endpoint)
exit
$post
Definition: ltitoken.php:46
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26
$response
Definition: xapitoken.php:93