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