ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ILIAS\StaticURL\Response\Response\Sender\DefaultResponseSenderStrategy Class Reference

Class DefaultResponseSenderStrategy. More...

+ Inheritance diagram for ILIAS\StaticURL\Response\Response\Sender\DefaultResponseSenderStrategy:
+ Collaboration diagram for ILIAS\StaticURL\Response\Response\Sender\DefaultResponseSenderStrategy:

Public Member Functions

 __construct ()
 
 sendResponse (ResponseInterface $response)
 Sends the rendered response to the client. More...
 

Private Member Functions

 initMemoryLimit ()
 
 initChunkSize ()
 

Private Attributes

const METHOD_FPASSTHRU = 'fpassthru'
 
const METHOD_READFILE = 'readfile'
 
string $method = self::METHOD_FPASSTHRU
 
int $chunk_size
 
int $memory_limit
 

Detailed Description

Class DefaultResponseSenderStrategy.

The default response sender strategy rewinds the current body stream and sends the entire stream out to the client.

Author
Nicolas Schäfli ns@st.nosp@m.uder.nosp@m.-raim.nosp@m.ann..nosp@m.ch

Definition at line 31 of file DefaultResponseSenderStrategy.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\StaticURL\Response\Response\Sender\DefaultResponseSenderStrategy::__construct ( )

Member Function Documentation

◆ initChunkSize()

ILIAS\StaticURL\Response\Response\Sender\DefaultResponseSenderStrategy::initChunkSize ( )
private

Definition at line 67 of file DefaultResponseSenderStrategy.php.

Referenced by ILIAS\StaticURL\Response\Response\Sender\DefaultResponseSenderStrategy\__construct().

67  : int
68  {
69  return (int) round(max($this->memory_limit / 4, 8 * 1024));
70  }
+ Here is the caller graph for this function:

◆ initMemoryLimit()

ILIAS\StaticURL\Response\Response\Sender\DefaultResponseSenderStrategy::initMemoryLimit ( )
private

Definition at line 51 of file DefaultResponseSenderStrategy.php.

References ILIAS\Repository\int(), and null.

Referenced by ILIAS\StaticURL\Response\Response\Sender\DefaultResponseSenderStrategy\__construct().

51  : int
52  {
53  $ini_memory_limit = ini_get('memory_limit');
55  if (preg_match('/^(\d+)(.)$/', $ini_memory_limit, $matches)) {
56  $memory_limit = match ($matches[2] ?? null) {
57  'G' => (int) $matches[1] * 1024 * 1024 * 1024,
58  'M' => (int) $matches[1] * 1024 * 1024,
59  'K' => (int) $matches[1] * 1024,
60  default => (int) $matches[1],
61  };
62  }
63 
64  return $memory_limit ?? 128 * 1024 * 1024;
65  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendResponse()

ILIAS\StaticURL\Response\Response\Sender\DefaultResponseSenderStrategy::sendResponse ( ResponseInterface  $response)

Sends the rendered response to the client.

Parameters
ResponseInterface$responseThe response which should be send to the client.
Exceptions
ResponseSendingExceptionThrown if the response was already sent to the client.

Implements ILIAS\StaticURL\Response\Response\Sender\ResponseSenderStrategy.

Definition at line 79 of file DefaultResponseSenderStrategy.php.

References ILIAS\UI\examples\Symbol\Glyph\Header\header().

79  : void
80  {
81  //check if the request is already send
82  if (headers_sent()) {
83  throw new ResponseSendingException("Response was already sent.");
84  }
85 
86  //set status code
87  http_response_code($response->getStatusCode());
88 
89  //render all headers
90  foreach (array_keys($response->getHeaders()) as $key) {
91  // See Mantis #37385.
92  if (strtolower($key) === 'set-cookie') {
93  foreach ($response->getHeader($key) as $header) {
94  header("$key: " . $header, false);
95  }
96  } else {
97  header("$key: " . $response->getHeaderLine($key));
98  }
99  }
100 
101  //rewind body stream
102  $stream = $response->getBody();
103  $stream->rewind();
104 
105  // check body size
106  $body_size = $stream->getSize();
107  if ($body_size > $this->memory_limit) {
108  $this->method = self::METHOD_READFILE;
109  }
110 
111  //detach psr-7 stream from resource
112  $resource = $stream->detach();
113 
114  $sendStatus = false;
115 
116  if (is_resource($resource)) {
117  set_time_limit(0);
118  try {
119  ob_end_clean(); // see https://mantis.ilias.de/view.php?id=32046
120  } catch (\Throwable) {
121  }
122  switch ($this->method) {
123  case self::METHOD_FPASSTHRU:
124  $sendStatus = fpassthru($resource);
125  break;
126  case self::METHOD_READFILE:
127  // more memory friendly than fpassthru
128  $sendStatus = true;
129  while (!feof($resource)) {
130  echo $return = fread($resource, $this->chunk_size);
131  $sendStatus = $sendStatus && $return !== false;
132 
133  }
134  break;
135  }
136 
137  //free up resources
138  fclose($resource);
139  }
140 
141  //check if the body was successfully send to the client
142  if ($sendStatus === false) {
143  throw new ResponseSendingException("Could not send body content to client.");
144  }
145  }
$response
Definition: xapitoken.php:93
header()
expected output: > ILIAS shows the rendered Component.
Definition: header.php:29
+ Here is the call graph for this function:

Field Documentation

◆ $chunk_size

int ILIAS\StaticURL\Response\Response::Sender\DefaultResponseSenderStrategy::$chunk_size
private

Definition at line 42 of file DefaultResponseSenderStrategy.php.

◆ $memory_limit

int ILIAS\StaticURL\Response\Response::Sender\DefaultResponseSenderStrategy::$memory_limit
private

Definition at line 43 of file DefaultResponseSenderStrategy.php.

◆ $method

string ILIAS\StaticURL\Response\Response::Sender\DefaultResponseSenderStrategy::$method = self::METHOD_FPASSTHRU
private

Definition at line 41 of file DefaultResponseSenderStrategy.php.

◆ METHOD_FPASSTHRU

const ILIAS\StaticURL\Response\Response::Sender\DefaultResponseSenderStrategy::METHOD_FPASSTHRU = 'fpassthru'
private

Definition at line 36 of file DefaultResponseSenderStrategy.php.

◆ METHOD_READFILE

const ILIAS\StaticURL\Response\Response::Sender\DefaultResponseSenderStrategy::METHOD_READFILE = 'readfile'
private

Definition at line 40 of file DefaultResponseSenderStrategy.php.


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