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