comparison vendor/symfony/http-foundation/BinaryFileResponse.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
34 protected $offset; 34 protected $offset;
35 protected $maxlen; 35 protected $maxlen;
36 protected $deleteFileAfterSend = false; 36 protected $deleteFileAfterSend = false;
37 37
38 /** 38 /**
39 * Constructor.
40 *
41 * @param \SplFileInfo|string $file The file to stream 39 * @param \SplFileInfo|string $file The file to stream
42 * @param int $status The response status code 40 * @param int $status The response status code
43 * @param array $headers An array of response headers 41 * @param array $headers An array of response headers
44 * @param bool $public Files are public by default 42 * @param bool $public Files are public by default
45 * @param null|string $contentDisposition The type of Content-Disposition to set automatically with the filename 43 * @param null|string $contentDisposition The type of Content-Disposition to set automatically with the filename
139 /** 137 /**
140 * Automatically sets the ETag header according to the checksum of the file. 138 * Automatically sets the ETag header according to the checksum of the file.
141 */ 139 */
142 public function setAutoEtag() 140 public function setAutoEtag()
143 { 141 {
144 $this->setEtag(sha1_file($this->file->getPathname())); 142 $this->setEtag(base64_encode(hash_file('sha256', $this->file->getPathname(), true)));
145 143
146 return $this; 144 return $this;
147 } 145 }
148 146
149 /** 147 /**
150 * Sets the Content-Disposition header with the given filename. 148 * Sets the Content-Disposition header with the given filename.
151 * 149 *
152 * @param string $disposition ResponseHeaderBag::DISPOSITION_INLINE or ResponseHeaderBag::DISPOSITION_ATTACHMENT 150 * @param string $disposition ResponseHeaderBag::DISPOSITION_INLINE or ResponseHeaderBag::DISPOSITION_ATTACHMENT
153 * @param string $filename Optionally use this filename instead of the real name of the file 151 * @param string $filename Optionally use this UTF-8 encoded filename instead of the real name of the file
154 * @param string $filenameFallback A fallback filename, containing only ASCII characters. Defaults to an automatically encoded filename 152 * @param string $filenameFallback A fallback filename, containing only ASCII characters. Defaults to an automatically encoded filename
155 * 153 *
156 * @return $this 154 * @return $this
157 */ 155 */
158 public function setContentDisposition($disposition, $filename = '', $filenameFallback = '') 156 public function setContentDisposition($disposition, $filename = '', $filenameFallback = '')
159 { 157 {
160 if ($filename === '') { 158 if ('' === $filename) {
161 $filename = $this->file->getFilename(); 159 $filename = $this->file->getFilename();
162 } 160 }
163 161
164 if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) { 162 if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) {
165 $encoding = mb_detect_encoding($filename, null, true); 163 $encoding = mb_detect_encoding($filename, null, true) ?: '8bit';
166 164
167 for ($i = 0, $filenameLength = mb_strlen($filename, $encoding); $i < $filenameLength; ++$i) { 165 for ($i = 0, $filenameLength = mb_strlen($filename, $encoding); $i < $filenameLength; ++$i) {
168 $char = mb_substr($filename, $i, 1, $encoding); 166 $char = mb_substr($filename, $i, 1, $encoding);
169 167
170 if ('%' === $char || ord($char) < 32 || ord($char) > 126) { 168 if ('%' === $char || ord($char) < 32 || ord($char) > 126) {
184 /** 182 /**
185 * {@inheritdoc} 183 * {@inheritdoc}
186 */ 184 */
187 public function prepare(Request $request) 185 public function prepare(Request $request)
188 { 186 {
189 $this->headers->set('Content-Length', $this->file->getSize()); 187 if (!$this->headers->has('Content-Type')) {
188 $this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
189 }
190
191 if ('HTTP/1.0' !== $request->server->get('SERVER_PROTOCOL')) {
192 $this->setProtocolVersion('1.1');
193 }
194
195 $this->ensureIEOverSSLCompatibility($request);
196
197 $this->offset = 0;
198 $this->maxlen = -1;
199
200 if (false === $fileSize = $this->file->getSize()) {
201 return $this;
202 }
203 $this->headers->set('Content-Length', $fileSize);
190 204
191 if (!$this->headers->has('Accept-Ranges')) { 205 if (!$this->headers->has('Accept-Ranges')) {
192 // Only accept ranges on safe HTTP methods 206 // Only accept ranges on safe HTTP methods
193 $this->headers->set('Accept-Ranges', $request->isMethodSafe(false) ? 'bytes' : 'none'); 207 $this->headers->set('Accept-Ranges', $request->isMethodSafe(false) ? 'bytes' : 'none');
194 } 208 }
195
196 if (!$this->headers->has('Content-Type')) {
197 $this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
198 }
199
200 if ('HTTP/1.0' !== $request->server->get('SERVER_PROTOCOL')) {
201 $this->setProtocolVersion('1.1');
202 }
203
204 $this->ensureIEOverSSLCompatibility($request);
205
206 $this->offset = 0;
207 $this->maxlen = -1;
208 209
209 if (self::$trustXSendfileTypeHeader && $request->headers->has('X-Sendfile-Type')) { 210 if (self::$trustXSendfileTypeHeader && $request->headers->has('X-Sendfile-Type')) {
210 // Use X-Sendfile, do not send any content. 211 // Use X-Sendfile, do not send any content.
211 $type = $request->headers->get('X-Sendfile-Type'); 212 $type = $request->headers->get('X-Sendfile-Type');
212 $path = $this->file->getRealPath(); 213 $path = $this->file->getRealPath();
213 // Fall back to scheme://path for stream wrapped locations. 214 // Fall back to scheme://path for stream wrapped locations.
214 if (false === $path) { 215 if (false === $path) {
215 $path = $this->file->getPathname(); 216 $path = $this->file->getPathname();
216 } 217 }
217 if (strtolower($type) === 'x-accel-redirect') { 218 if ('x-accel-redirect' === strtolower($type)) {
218 // Do X-Accel-Mapping substitutions. 219 // Do X-Accel-Mapping substitutions.
219 // @link http://wiki.nginx.org/X-accel#X-Accel-Redirect 220 // @link http://wiki.nginx.org/X-accel#X-Accel-Redirect
220 foreach (explode(',', $request->headers->get('X-Accel-Mapping', '')) as $mapping) { 221 foreach (explode(',', $request->headers->get('X-Accel-Mapping', '')) as $mapping) {
221 $mapping = explode('=', $mapping, 2); 222 $mapping = explode('=', $mapping, 2);
222 223
235 $this->maxlen = 0; 236 $this->maxlen = 0;
236 } elseif ($request->headers->has('Range')) { 237 } elseif ($request->headers->has('Range')) {
237 // Process the range headers. 238 // Process the range headers.
238 if (!$request->headers->has('If-Range') || $this->hasValidIfRangeHeader($request->headers->get('If-Range'))) { 239 if (!$request->headers->has('If-Range') || $this->hasValidIfRangeHeader($request->headers->get('If-Range'))) {
239 $range = $request->headers->get('Range'); 240 $range = $request->headers->get('Range');
240 $fileSize = $this->file->getSize();
241 241
242 list($start, $end) = explode('-', substr($range, 6), 2) + array(0); 242 list($start, $end) = explode('-', substr($range, 6), 2) + array(0);
243 243
244 $end = ('' === $end) ? $fileSize - 1 : (int) $end; 244 $end = ('' === $end) ? $fileSize - 1 : (int) $end;
245 245
252 252
253 if ($start <= $end) { 253 if ($start <= $end) {
254 if ($start < 0 || $end > $fileSize - 1) { 254 if ($start < 0 || $end > $fileSize - 1) {
255 $this->setStatusCode(416); 255 $this->setStatusCode(416);
256 $this->headers->set('Content-Range', sprintf('bytes */%s', $fileSize)); 256 $this->headers->set('Content-Range', sprintf('bytes */%s', $fileSize));
257 } elseif ($start !== 0 || $end !== $fileSize - 1) { 257 } elseif (0 !== $start || $end !== $fileSize - 1) {
258 $this->maxlen = $end < $fileSize ? $end - $start + 1 : -1; 258 $this->maxlen = $end < $fileSize ? $end - $start + 1 : -1;
259 $this->offset = $start; 259 $this->offset = $start;
260 260
261 $this->setStatusCode(206); 261 $this->setStatusCode(206);
262 $this->headers->set('Content-Range', sprintf('bytes %s-%s/%s', $start, $end, $fileSize)); 262 $this->headers->set('Content-Range', sprintf('bytes %s-%s/%s', $start, $end, $fileSize));