ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
XapiProxy\XapiProxyResponse Class Reference
+ Collaboration diagram for XapiProxy\XapiProxyResponse:

Public Member Functions

 __construct ()
 
 checkResponse ($response, $endpoint)
 
 handleResponse ($request, $response, $fakePostBody=null)
 
 fakeResponseBlocked ($post=null)
 
 exitResponseError ()
 
 exitProxyError ()
 
 exitBadRequest ()
 
 sendData ($obj)
 
 emit ($response)
 

Private Member Functions

 msg ($msg)
 

Private Attributes

 $dic
 
 $xapiproxy
 

Detailed Description

Definition at line 7 of file XapiProxyResponse.php.

Constructor & Destructor Documentation

◆ __construct()

XapiProxy\XapiProxyResponse::__construct ( )

Definition at line 13 of file XapiProxyResponse.php.

References $GLOBALS.

14  {
15  $this->dic = $GLOBALS['DIC'];
16  $this->xapiproxy = $this->dic['xapiproxy'];
17  }
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64

Member Function Documentation

◆ checkResponse()

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

Definition at line 19 of file XapiProxyResponse.php.

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

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  }
$response

◆ emit()

XapiProxy\XapiProxyResponse::emit (   $response)

Definition at line 185 of file XapiProxyResponse.php.

References $name, and $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  }
if($format !==null) $name
Definition: metadata.php:230
$response

◆ exitBadRequest()

XapiProxy\XapiProxyResponse::exitBadRequest ( )

Definition at line 144 of file XapiProxyResponse.php.

References $_SERVER, Vendor\Package\$e, and exit.

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

◆ exitProxyError()

XapiProxy\XapiProxyResponse::exitProxyError ( )

Definition at line 125 of file XapiProxyResponse.php.

References $_SERVER, Vendor\Package\$e, and exit.

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

◆ exitResponseError()

XapiProxy\XapiProxyResponse::exitResponseError ( )

Definition at line 106 of file XapiProxyResponse.php.

References $_SERVER, Vendor\Package\$e, and exit.

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

◆ fakeResponseBlocked()

XapiProxy\XapiProxyResponse::fakeResponseBlocked (   $post = null)

Definition at line 64 of file XapiProxyResponse.php.

References $_SERVER, Vendor\Package\$e, and exit.

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

◆ handleResponse()

XapiProxy\XapiProxyResponse::handleResponse (   $request,
  $response,
  $fakePostBody = null 
)

Definition at line 39 of file XapiProxyResponse.php.

References $response.

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  }
$response

◆ msg()

XapiProxy\XapiProxyResponse::msg (   $msg)
private

Definition at line 226 of file XapiProxyResponse.php.

227  {
228  return $this->xapiproxy->msg($msg);
229  }

◆ sendData()

XapiProxy\XapiProxyResponse::sendData (   $obj)

Definition at line 163 of file XapiProxyResponse.php.

References $_SERVER, Vendor\Package\$e, and exit.

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

Field Documentation

◆ $dic

XapiProxy\XapiProxyResponse::$dic
private

Definition at line 9 of file XapiProxyResponse.php.

◆ $xapiproxy

XapiProxy\XapiProxyResponse::$xapiproxy
private

Definition at line 10 of file XapiProxyResponse.php.


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