19declare(strict_types=1);
23use Psr\Http\Message\ServerRequestInterface;
24use Psr\Http\Message\ResponseInterface;
25use GuzzleHttp\Psr7\Response;
26use GuzzleHttp\Psr7\Request;
43 $status =
$response[
'value']->getStatusCode();
44 if ($status === 200 || $status === 204 || $status === 404) {
47 $this->xapiproxy->log()->error(
"LRS error {$endpoint}: " .
$response[
'value']->getBody());
52 $this->xapiproxy->log()->error(
"Connection error {$endpoint}: " .
$response[
'reason']->getMessage());
53 }
catch (\Exception
$e) {
54 $this->xapiproxy->log()->error(
"error {$endpoint}:" .
$e->getMessage());
60 public function handleResponse(\Psr\Http\Message\RequestInterface $request, Response
$response, array|
string|
null $fakePostBody =
null): void
63 if ($fakePostBody !==
null) {
65 $this->xapiproxy->log()->debug($this->msg(
"orig body: " . $origBody));
66 $this->xapiproxy->log()->debug($this->msg(
"fake body: " . json_encode($fakePostBody)));
69 $this->fakeResponseBlocked($fakePostBody);
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"));
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);
91 $this->xapiproxy->log()->debug($this->msg(
"fakeResponseFromBlockedRequest"));
93 $this->xapiproxy->log()->debug($this->msg(
"post === NULL"));
96 if (isset($origin) && $origin !=
"") {
97 header(
'Access-Control-Allow-Origin: ' . $origin);
99 $this->xapiproxy->log()->warning(
"could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
101 }
catch (\Exception
$e) {
102 $this->xapiproxy->log()->warning(
$e->getMessage());
104 header(
'Access-Control-Allow-Credentials: true');
105 header(
'X-Experience-API-Version: 1.0.3');
106 header(
'HTTP/1.1 204 No Content');
109 $ids = json_encode(
$post);
110 $this->xapiproxy->log()->debug($this->msg(
"post: " . $ids));
113 if (isset($origin) && $origin !=
"") {
114 header(
'Access-Control-Allow-Origin: ' . $origin);
116 $this->xapiproxy->log()->warning(
"could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
118 }
catch (\Exception
$e) {
119 $this->xapiproxy->log()->warning(
$e->getMessage());
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');
135 if (isset($origin) && $origin !=
"") {
136 header(
'Access-Control-Allow-Origin: ' . $origin);
138 $this->xapiproxy->log()->warning(
"could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
140 }
catch (\Exception
$e) {
141 $this->xapiproxy->log()->warning(
$e->getMessage());
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";
154 if (isset($origin) && $origin !=
"") {
155 header(
'Access-Control-Allow-Origin: ' . $origin);
157 $this->xapiproxy->log()->warning(
"could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
159 }
catch (\Exception
$e) {
160 $this->xapiproxy->log()->warning(
$e->getMessage());
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)";
173 if (isset($origin) && $origin !=
"") {
174 header(
'Access-Control-Allow-Origin: ' . $origin);
176 $this->xapiproxy->log()->warning(
"could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
178 }
catch (\Exception
$e) {
179 $this->xapiproxy->log()->warning(
$e->getMessage());
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)";
190 $this->xapiproxy->log()->debug($this->msg(
"sendData: " . $obj));
193 if (isset($origin) && $origin !=
"") {
194 header(
'Access-Control-Allow-Origin: ' . $origin);
196 $this->xapiproxy->log()->warning(
"could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
198 }
catch (\Exception
$e) {
199 $this->xapiproxy->log()->warning(
$e->getMessage());
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');
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();
217 if (ob_get_level() > 0 && ob_get_length() > 0) {
218 $this->xapiproxy->log()->error($this->msg(
"Outputstream not empty!"));
219 $this->exitProxyError();
222 $reasonPhrase =
$response->getReasonPhrase();
223 $statusCode =
$response->getStatusCode();
226 foreach (
$response->getHeaders() as $header => $values) {
227 $name = ucwords($header,
'-');
228 $first = $name ===
'Set-Cookie' ? false :
true;
229 foreach ($values as $value) {
234 ), $first, $statusCode);
244 ($reasonPhrase ?
' ' . $reasonPhrase :
'')
245 ),
true, $statusCode);
251 private function msg(
string $msg): string
253 return $this->xapiproxy->msg($msg);
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)