ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
XapiProxy\XapiProxyResponse Class Reference
+ Collaboration diagram for XapiProxy\XapiProxyResponse:

Public Member Functions

 __construct (XapiProxy $xapiproxy)
 
 checkResponse (array $response, string $endpoint)
 
 handleResponse (Request $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 27 of file XapiProxyResponse.php.

Constructor & Destructor Documentation

◆ __construct()

XapiProxy\XapiProxyResponse::__construct ( XapiProxy  $xapiproxy)

Definition at line 33 of file XapiProxyResponse.php.

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

References XapiProxy\$xapiproxy.

Member Function Documentation

◆ checkResponse()

XapiProxy\XapiProxyResponse::checkResponse ( array  $response,
string  $endpoint 
)

Definition at line 39 of file XapiProxyResponse.php.

39 : bool
40 {
41 if ($response['state'] == 'fulfilled') {
42 $status = $response['value']->getStatusCode();
43 if ($status === 200 || $status === 204 || $status === 404) {
44 return true;
45 } else {
46 $this->xapiproxy->log()->error("LRS error {$endpoint}: " . $response['value']->getBody());
47 return false;
48 }
49 } else {
50 try {
51 $this->xapiproxy->log()->error("Connection error {$endpoint}: " . $response['reason']->getMessage());
52 } catch (\Exception $e) {
53 $this->xapiproxy->log()->error("error {$endpoint}:" . $e->getMessage());
54 }
55 return false;
56 }
57 }
$response
Definition: xapitoken.php:93

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

◆ emit()

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

Definition at line 215 of file XapiProxyResponse.php.

215 : void
216 {
217 $this->xapiproxy->log()->debug($this->msg('emitting response'));
218 if (headers_sent()) {
219 $this->xapiproxy->log()->error($this->msg("Headers already sent!"));
220 $this->exitProxyError();
221 }
222 if (ob_get_level() > 0 && ob_get_length() > 0) {
223 $this->xapiproxy->log()->error($this->msg("Outputstream not empty!"));
224 $this->exitProxyError();
225 }
226
227 $reasonPhrase = $response->getReasonPhrase();
228 $statusCode = $response->getStatusCode();
229
230 // header
231 foreach ($response->getHeaders() as $header => $values) {
232 $name = ucwords($header, '-');
233 $first = $name === 'Set-Cookie' ? false : true;
234 foreach ($values as $value) {
235 header(sprintf(
236 '%s: %s',
237 $name,
238 $value
239 ), $first, $statusCode);
240 $first = false;
241 }
242 }
243
244 // statusline
245 header(sprintf(
246 'HTTP/%s %d%s',
247 $response->getProtocolVersion(),
248 $statusCode,
249 ($reasonPhrase ? ' ' . $reasonPhrase : '')
250 ), true, $statusCode);
251
252 // body
253 echo $response->getBody();
254 }

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 174 of file XapiProxyResponse.php.

174 : void
175 {
176 try {
177 $origin = (isset($_SERVER["HTTP_ORIGIN"])) ? $_SERVER["HTTP_ORIGIN"] : $_SERVER["HTTP_REFERRER"];
178 if (isset($origin) && $origin != "") {
179 header('Access-Control-Allow-Origin: ' . $origin);
180 } else {
181 $this->xapiproxy->log()->warning("could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
182 }
183 } catch (\Exception $e) {
184 $this->xapiproxy->log()->warning($e->getMessage());
185 }
186 header('Access-Control-Allow-Credentials: true');
187 header('X-Experience-API-Version: 1.0.3');
188 header("HTTP/1.1 400 XapiProxy Bad Request (Ask For Logs)");
189 echo "HTTP/1.1 400 XapiProxy Bad Request (Ask For Logs)";
190 exit;
191 }
$_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 155 of file XapiProxyResponse.php.

155 : void
156 {
157 try {
158 $origin = (isset($_SERVER["HTTP_ORIGIN"])) ? $_SERVER["HTTP_ORIGIN"] : $_SERVER["HTTP_REFERRER"];
159 if (isset($origin) && $origin != "") {
160 header('Access-Control-Allow-Origin: ' . $origin);
161 } else {
162 $this->xapiproxy->log()->warning("could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
163 }
164 } catch (\Exception $e) {
165 $this->xapiproxy->log()->warning($e->getMessage());
166 }
167 header('Access-Control-Allow-Credentials: true');
168 header('X-Experience-API-Version: 1.0.3');
169 header("HTTP/1.1 500 XapiProxy Error (Ask For Logs)");
170 echo "HTTP/1.1 500 XapiProxy Error (Ask For Logs)";
171 exit;
172 }

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 136 of file XapiProxyResponse.php.

136 : void
137 {
138 try {
139 $origin = (isset($_SERVER["HTTP_ORIGIN"])) ? $_SERVER["HTTP_ORIGIN"] : $_SERVER["HTTP_REFERRER"];
140 if (isset($origin) && $origin != "") {
141 header('Access-Control-Allow-Origin: ' . $origin);
142 } else {
143 $this->xapiproxy->log()->warning("could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
144 }
145 } catch (\Exception $e) {
146 $this->xapiproxy->log()->warning($e->getMessage());
147 }
148 header('Access-Control-Allow-Credentials: true');
149 header('X-Experience-API-Version: 1.0.3');
150 header("HTTP/1.1 412 Wrong Response");
151 echo "HTTP/1.1 412 Wrong Response";
152 exit;
153 }

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 94 of file XapiProxyResponse.php.

94 : void
95 {
96 $this->xapiproxy->log()->debug($this->msg("fakeResponseFromBlockedRequest"));
97 if ($post === null) {
98 $this->xapiproxy->log()->debug($this->msg("post === NULL"));
99 try {
100 $origin = (isset($_SERVER["HTTP_ORIGIN"])) ? $_SERVER["HTTP_ORIGIN"] : $_SERVER["HTTP_REFERRER"];
101 if (isset($origin) && $origin != "") {
102 header('Access-Control-Allow-Origin: ' . $origin);
103 } else {
104 $this->xapiproxy->log()->warning("could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
105 }
106 } catch (\Exception $e) {
107 $this->xapiproxy->log()->warning($e->getMessage());
108 }
109 header('Access-Control-Allow-Credentials: true');
110 header('X-Experience-API-Version: 1.0.3');
111 header('HTTP/1.1 204 No Content');
112 exit;
113 } else {
114 $ids = json_encode($post);
115 $this->xapiproxy->log()->debug($this->msg("post: " . $ids));
116 try {
117 $origin = (isset($_SERVER["HTTP_ORIGIN"])) ? $_SERVER["HTTP_ORIGIN"] : $_SERVER["HTTP_REFERRER"];
118 if (isset($origin) && $origin != "") {
119 header('Access-Control-Allow-Origin: ' . $origin);
120 } else {
121 $this->xapiproxy->log()->warning("could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
122 }
123 } catch (\Exception $e) {
124 $this->xapiproxy->log()->warning($e->getMessage());
125 }
126 header('Access-Control-Allow-Credentials: true');
127 header('X-Experience-API-Version: 1.0.3');
128 header('Content-Length: ' . strlen($ids));
129 header('Content-Type: application/json; charset=utf-8');
130 header('HTTP/1.1 200 Ok');
131 echo $ids;
132 exit;
133 }
134 }
$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 ( Request  $request,
Response  $response,
array|string|null  $fakePostBody = null 
)
Parameters
Request$request
Response$response
array | string | null$fakePostBody
Returns
void

Definition at line 65 of file XapiProxyResponse.php.

65 : void
66 {
67 // check transfer encoding bug
68 if ($fakePostBody !== null) {
69 $origBody = $response->getBody();
70 $this->xapiproxy->log()->debug($this->msg("orig body: " . $origBody));
71 $this->xapiproxy->log()->debug($this->msg("fake body: " . json_encode($fakePostBody)));
72 // because there is a real response object, it should also be possible to override the response stream...
73 // but this does the job as well:
74 $this->fakeResponseBlocked($fakePostBody);
75 }
76 $status = $response->getStatusCode();
77 $headers = $response->getHeaders();
78 if (array_key_exists('Transfer-Encoding', $headers) && $headers['Transfer-Encoding'][0] == "chunked") {
79 $this->xapiproxy->log()->debug($this->msg("sniff response transfer-encoding for unallowed Content-length"));
80 $body = (string) $response->getBody();
81 unset($headers['Transfer-Encoding']);
82 $headers['Content-Length'] = array(strlen($body));
83 $response2 = new \GuzzleHttp\Psr7\Response($status, $headers, $body);
84 $this->emit($response2);
85 } else {
86 $this->emit($response);
87 }
88 }
fakeResponseBlocked(array|string|null $post=null)
emit(\GuzzleHttp\Psr7\Response $response)

References $response.

◆ msg()

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

Definition at line 256 of file XapiProxyResponse.php.

256 : string
257 {
258 return $this->xapiproxy->msg($msg);
259 }

◆ sendData()

XapiProxy\XapiProxyResponse::sendData ( string  $obj)

Definition at line 193 of file XapiProxyResponse.php.

193 : void
194 {
195 $this->xapiproxy->log()->debug($this->msg("sendData: " . $obj));
196 try {
197 $origin = (isset($_SERVER["HTTP_ORIGIN"])) ? $_SERVER["HTTP_ORIGIN"] : $_SERVER["HTTP_REFERRER"];
198 if (isset($origin) && $origin != "") {
199 header('Access-Control-Allow-Origin: ' . $origin);
200 } else {
201 $this->xapiproxy->log()->warning("could not get \$_SERVER[\"HTTP_ORIGIN\"] or \$_SERVER[\"HTTP_REFERRER\"]");
202 }
203 } catch (\Exception $e) {
204 $this->xapiproxy->log()->warning($e->getMessage());
205 }
206 header('Access-Control-Allow-Credentials: true');
207 header('X-Experience-API-Version: 1.0.3');
208 header('Content-Length: ' . strlen($obj));
209 header('Content-Type: application/json; charset=utf-8');
210 header('HTTP/1.1 200 Ok');
211 echo $obj;
212 exit;
213 }

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 30 of file XapiProxyResponse.php.


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