diff --git a/src/aphront/response/AphrontFileResponse.php b/src/aphront/response/AphrontFileResponse.php index 6337c984b9..25750ddacd 100644 --- a/src/aphront/response/AphrontFileResponse.php +++ b/src/aphront/response/AphrontFileResponse.php @@ -1,137 +1,142 @@ allowOrigins[] = $origin; return $this; } public function setDownload($download) { if (!strlen($download)) { $download = 'untitled'; } $this->download = $download; return $this; } public function getDownload() { return $this->download; } public function setMimeType($mime_type) { $this->mimeType = $mime_type; return $this; } public function getMimeType() { return $this->mimeType; } public function setContent($content) { $this->setContentLength(strlen($content)); $this->content = $content; return $this; } public function setContentIterator($iterator) { $this->contentIterator = $iterator; return $this; } public function buildResponseString() { return $this->content; } public function getContentIterator() { if ($this->contentIterator) { return $this->contentIterator; } return parent::getContentIterator(); } public function setContentLength($length) { $this->contentLength = $length; return $this; } public function getContentLength() { return $this->contentLength; } public function setCompressResponse($compress_response) { $this->compressResponse = $compress_response; return $this; } public function getCompressResponse() { return $this->compressResponse; } public function setRange($min, $max) { $this->rangeMin = $min; $this->rangeMax = $max; return $this; } public function getHeaders() { $headers = array( array('Content-Type', $this->getMimeType()), // This tells clients that we can support requests with a "Range" header, // which allows downloads to be resumed, in some browsers, some of the // time, if the stars align. array('Accept-Ranges', 'bytes'), ); if ($this->rangeMin || $this->rangeMax) { $len = $this->getContentLength(); $min = $this->rangeMin; + $max = $this->rangeMax; + if ($max === null) { + $max = ($len - 1); + } + $headers[] = array('Content-Range', "bytes {$min}-{$max}/{$len}"); $content_len = ($max - $min) + 1; } else { $content_len = $this->getContentLength(); } if (!$this->shouldCompressResponse()) { $headers[] = array('Content-Length', $content_len); } if (strlen($this->getDownload())) { $headers[] = array('X-Download-Options', 'noopen'); $filename = $this->getDownload(); $filename = addcslashes($filename, '"\\'); $headers[] = array( 'Content-Disposition', 'attachment; filename="'.$filename.'"', ); } if ($this->allowOrigins) { $headers[] = array( 'Access-Control-Allow-Origin', implode(',', $this->allowOrigins), ); } $headers = array_merge(parent::getHeaders(), $headers); return $headers; } protected function shouldCompressResponse() { return $this->getCompressResponse(); } } diff --git a/src/applications/files/controller/PhabricatorFileDataController.php b/src/applications/files/controller/PhabricatorFileDataController.php index 43b5aa419f..31761d1244 100644 --- a/src/applications/files/controller/PhabricatorFileDataController.php +++ b/src/applications/files/controller/PhabricatorFileDataController.php @@ -1,199 +1,206 @@ getViewer(); $this->phid = $request->getURIData('phid'); $this->key = $request->getURIData('key'); $alt = PhabricatorEnv::getEnvConfig('security.alternate-file-domain'); $base_uri = PhabricatorEnv::getEnvConfig('phabricator.base-uri'); $alt_uri = new PhutilURI($alt); $alt_domain = $alt_uri->getDomain(); $req_domain = $request->getHost(); $main_domain = id(new PhutilURI($base_uri))->getDomain(); if (!strlen($alt) || $main_domain == $alt_domain) { // No alternate domain. $should_redirect = false; $is_alternate_domain = false; } else if ($req_domain != $alt_domain) { // Alternate domain, but this request is on the main domain. $should_redirect = true; $is_alternate_domain = false; } else { // Alternate domain, and on the alternate domain. $should_redirect = false; $is_alternate_domain = true; } $response = $this->loadFile(); if ($response) { return $response; } $file = $this->getFile(); if ($should_redirect) { return id(new AphrontRedirectResponse()) ->setIsExternal(true) ->setURI($file->getCDNURI()); } $response = new AphrontFileResponse(); $response->setCacheDurationInSeconds(60 * 60 * 24 * 30); $response->setCanCDN($file->getCanCDN()); $begin = null; $end = null; // NOTE: It's important to accept "Range" requests when playing audio. // If we don't, Safari has difficulty figuring out how long sounds are // and glitches when trying to loop them. In particular, Safari sends // an initial request for bytes 0-1 of the audio file, and things go south // if we can't respond with a 206 Partial Content. $range = $request->getHTTPHeader('range'); if ($range) { $matches = null; - if (preg_match('/^bytes=(\d+)-(\d+)$/', $range, $matches)) { + if (preg_match('/^bytes=(\d+)-(\d*)$/', $range, $matches)) { // Note that the "Range" header specifies bytes differently than // we do internally: the range 0-1 has 2 bytes (byte 0 and byte 1). $begin = (int)$matches[1]; - $end = (int)$matches[2] + 1; + + // The "Range" may be "200-299" or "200-", meaning "until end of file". + if (strlen($matches[2])) { + $range_end = (int)$matches[2]; + $end = $range_end + 1; + } else { + $range_end = null; + } $response->setHTTPResponseCode(206); - $response->setRange($begin, ($end - 1)); + $response->setRange($begin, $range_end); } } $is_viewable = $file->isViewableInBrowser(); $force_download = $request->getExists('download'); $request_type = $request->getHTTPHeader('X-Phabricator-Request-Type'); $is_lfs = ($request_type == 'git-lfs'); if ($is_viewable && !$force_download) { $response->setMimeType($file->getViewableMimeType()); } else { $is_public = !$viewer->isLoggedIn(); $is_post = $request->isHTTPPost(); // NOTE: Require POST to download files from the primary domain if the // request includes credentials. The "Download File" links we generate // in the web UI are forms which use POST to satisfy this requirement. // The intent is to make attacks based on tags like "