Changeset View
Changeset View
Standalone View
Standalone View
src/aphront/response/AphrontFileResponse.php
Show All 12 Lines | final class AphrontFileResponse extends AphrontResponse { | ||||
private $rangeMax; | private $rangeMax; | ||||
private $allowOrigins = array(); | private $allowOrigins = array(); | ||||
public function addAllowOrigin($origin) { | public function addAllowOrigin($origin) { | ||||
$this->allowOrigins[] = $origin; | $this->allowOrigins[] = $origin; | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | |||||
* Set the download filename | |||||
* | |||||
* @param $download mixed | |||||
* @return self | |||||
*/ | |||||
public function setDownload($download) { | public function setDownload($download) { | ||||
if (!phutil_nonempty_string($download)) { | // Known usage: | ||||
valerio.bozzolan: That above check was introduced by this commit:
{rP96ae4ba13acbf0e2f8932e950a92af0495f034d7}… | |||||
// setDownload(null) -> no download - default | |||||
// setDownload(false) -> no download - expected | |||||
// setDownload(true) -> yes named 'untitled' - expected | |||||
// setDownload('foo.ical') -> yes named 'foo.ical' - expected | |||||
// setDownload('') -> no download - please avoid | |||||
// setDownload(0) -> no download - please avoid | |||||
// setDownload(1) -> yes named 'untitled' - please avoid | |||||
if ($download) { | |||||
// If you want a download, but you have no idea | |||||
// about the title, let's choose for you | |||||
if (!is_string($download)) { | |||||
$download = 'untitled'; | $download = 'untitled'; | ||||
} | } | ||||
} | |||||
$this->download = $download; | $this->download = $download; | ||||
return $this; | return $this; | ||||
} | } | ||||
valerio.bozzolanAuthorUnsubmitted Done Inline ActionsAlso this method ↓ deserved an inline comment to show that it can return mixed stuff. valerio.bozzolan: Also this method ↓ deserved an inline comment to show that it can return mixed stuff. | |||||
/** | |||||
* Get the download filename | |||||
* | |||||
* @return mixed | |||||
*/ | |||||
public function getDownload() { | public function getDownload() { | ||||
return $this->download; | return $this->download; | ||||
} | } | ||||
public function setMimeType($mime_type) { | public function setMimeType($mime_type) { | ||||
$this->mimeType = $mime_type; | $this->mimeType = $mime_type; | ||||
return $this; | return $this; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | public function getHeaders() { | ||||
} else { | } else { | ||||
$content_len = $this->getContentLength(); | $content_len = $this->getContentLength(); | ||||
} | } | ||||
if (!$this->shouldCompressResponse()) { | if (!$this->shouldCompressResponse()) { | ||||
$headers[] = array('Content-Length', $content_len); | $headers[] = array('Content-Length', $content_len); | ||||
} | } | ||||
if (strlen($this->getDownload())) { | if (phutil_nonempty_string($this->getDownload())) { | ||||
valerio.bozzolanAuthorUnsubmitted Not Done Inline ActionsIMPORTANT: this cannot be used as-is, since getDownload() returns mixed stuff by design it seems. valerio.bozzolan: IMPORTANT: this cannot be used as-is, since getDownload() returns mixed stuff by design it… | |||||
$headers[] = array('X-Download-Options', 'noopen'); | $headers[] = array('X-Download-Options', 'noopen'); | ||||
$filename = $this->getDownload(); | $filename = $this->getDownload(); | ||||
$filename = addcslashes($filename, '"\\'); | $filename = addcslashes($filename, '"\\'); | ||||
$headers[] = array( | $headers[] = array( | ||||
'Content-Disposition', | 'Content-Disposition', | ||||
'attachment; filename="'.$filename.'"', | 'attachment; filename="'.$filename.'"', | ||||
); | ); | ||||
▲ Show 20 Lines • Show All 43 Lines • Show Last 20 Lines |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0
That above check was introduced by this commit:
rP96ae4ba13acb: PHP 8.1: fixes for strlen() not accepting NULL anymore, part 2
But it's too much strict. I have double-checked and this deserves more inline documentation. See on the right.
I just added some comments, and a strong default.