ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ILIAS\HTTP\Response\Sender\DefaultResponseSenderStrategy Class Reference

Class DefaultResponseSenderStrategy. More...

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

Public Member Functions

 __construct ()
 
 sendResponse (ResponseInterface $response)
 Sends the rendered response to the client. More...
 
 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\HTTP\Response\Sender\DefaultResponseSenderStrategy::__construct ( )

Member Function Documentation

◆ initChunkSize()

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

Definition at line 67 of file DefaultResponseSenderStrategy.php.

67 : int
68 {
69 return (int) round(max($this->memory_limit / 4, 8 * 1024));
70 }

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

+ Here is the caller graph for this function:

◆ initMemoryLimit()

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

Definition at line 51 of file DefaultResponseSenderStrategy.php.

51 : int
52 {
53 $ini_memory_limit = ini_get('memory_limit');
54 $memory_limit = null;
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 }

References ILIAS\HTTP\Response\Sender\DefaultResponseSenderStrategy\$memory_limit, and ILIAS\Repository\int().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendResponse()

ILIAS\HTTP\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\HTTP\Response\Sender\ResponseSenderStrategy.

Definition at line 79 of file DefaultResponseSenderStrategy.php.

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) {
124 $sendStatus = fpassthru($resource);
125 break;
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

References $response, ILIAS\UI\examples\Symbol\Glyph\Header\header(), ILIAS\HTTP\Response\Sender\DefaultResponseSenderStrategy\METHOD_FPASSTHRU, and ILIAS\HTTP\Response\Sender\DefaultResponseSenderStrategy\METHOD_READFILE.

+ Here is the call graph for this function:

Field Documentation

◆ $chunk_size

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

Definition at line 42 of file DefaultResponseSenderStrategy.php.

◆ $memory_limit

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

◆ $method

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

Definition at line 41 of file DefaultResponseSenderStrategy.php.

◆ METHOD_FPASSTHRU

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

◆ METHOD_READFILE

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

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