ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
XapiProxy\XapiProxyResponse Class Reference
+ Collaboration diagram for XapiProxy\XapiProxyResponse:

Public Member Functions

 __construct (XapiProxy $xapiproxy)
 
 checkResponse (\GuzzleHttp\Psr7\Response $response, string $endpoint)
 
 handleResponse (\Psr\Http\Message\RequestInterface $request, Response $response, array|string|null $fakePostBody=null)
 
 fakeResponseBlocked (array|string|null $post=null)
 
 exitResponseError ()
 
 exitProxyError ()
 
 exitBadRequest ()
 
 sendData (string $obj)
 
 emit (\GuzzleHttp\Psr7\Response $response)
 

Private Member Functions

 msg (string $msg)
 

Private Attributes

XapiProxy $xapiproxy
 

Detailed Description

Definition at line 28 of file XapiProxyResponse.php.

Constructor & Destructor Documentation

◆ __construct()

XapiProxy\XapiProxyResponse::__construct ( XapiProxy  $xapiproxy)

Definition at line 34 of file XapiProxyResponse.php.

35 {
36 // $this->dic = $GLOBALS['DIC'];
37 $this->xapiproxy = $xapiproxy;
38 }

References XapiProxy\$xapiproxy.

Member Function Documentation

◆ checkResponse()

XapiProxy\XapiProxyResponse::checkResponse ( \GuzzleHttp\Psr7\Response  $response,
string  $endpoint 
)

Definition at line 40 of file XapiProxyResponse.php.

40 : 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 }
$response
Definition: xapitoken.php:90

References Vendor\Package\$e, and $response.

◆ emit()

XapiProxy\XapiProxyResponse::emit ( \GuzzleHttp\Psr7\Response  $response)

Definition at line 210 of file XapiProxyResponse.php.

210 : 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 }

References $response, and ILIAS\UI\examples\Symbol\Glyph\Header\header().

+ Here is the call graph for this function:

◆ exitBadRequest()

XapiProxy\XapiProxyResponse::exitBadRequest ( )

Definition at line 169 of file XapiProxyResponse.php.

169 : 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 }
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26

References $_SERVER, Vendor\Package\$e, exit, and ILIAS\UI\examples\Symbol\Glyph\Header\header().

+ Here is the call graph for this function:

◆ exitProxyError()

XapiProxy\XapiProxyResponse::exitProxyError ( )

Definition at line 150 of file XapiProxyResponse.php.

150 : 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 }

References $_SERVER, Vendor\Package\$e, exit, and ILIAS\UI\examples\Symbol\Glyph\Header\header().

+ Here is the call graph for this function:

◆ exitResponseError()

XapiProxy\XapiProxyResponse::exitResponseError ( )

Definition at line 131 of file XapiProxyResponse.php.

131 : 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 }

References $_SERVER, Vendor\Package\$e, exit, and ILIAS\UI\examples\Symbol\Glyph\Header\header().

+ Here is the call graph for this function:

◆ fakeResponseBlocked()

XapiProxy\XapiProxyResponse::fakeResponseBlocked ( array|string|null  $post = null)
Parameters
array | string | null$post
Returns
void

Definition at line 89 of file XapiProxyResponse.php.

89 : 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 }
$post
Definition: ltitoken.php:46

References $_SERVER, Vendor\Package\$e, $post, exit, and ILIAS\UI\examples\Symbol\Glyph\Header\header().

+ Here is the call graph for this function:

◆ handleResponse()

XapiProxy\XapiProxyResponse::handleResponse ( \Psr\Http\Message\RequestInterface  $request,
Response  $response,
array|string|null  $fakePostBody = null 
)

Definition at line 60 of file XapiProxyResponse.php.

60 : 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 }
fakeResponseBlocked(array|string|null $post=null)
emit(\GuzzleHttp\Psr7\Response $response)

References $response.

◆ msg()

XapiProxy\XapiProxyResponse::msg ( string  $msg)
private

Definition at line 251 of file XapiProxyResponse.php.

251 : string
252 {
253 return $this->xapiproxy->msg($msg);
254 }

◆ sendData()

XapiProxy\XapiProxyResponse::sendData ( string  $obj)

Definition at line 188 of file XapiProxyResponse.php.

188 : 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 }

References $_SERVER, Vendor\Package\$e, exit, and ILIAS\UI\examples\Symbol\Glyph\Header\header().

+ Here is the call graph for this function:

Field Documentation

◆ $xapiproxy

XapiProxy XapiProxy\XapiProxyResponse::$xapiproxy
private

Definition at line 31 of file XapiProxyResponse.php.


The documentation for this class was generated from the following file: