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