mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-02 19:52:44 +01:00
7fbcdfc52c
Summary: Currently, CelerityController extends AphrontController, not PhabricatorController. (I think I imagined Celerity being somewhat stand-alone and didn't want to create a dependency.) This creates a concrete problem if a static resource is missing, since we throw an exception, but the higher-level exception handlers depend on the User existing in order to show an appropriate response page. This is the only controller which doesn't extend PhabricatorController, and it doesn't seem worthwhile to make a weird edge case out of it. Specific repro case is: - Remove `externals/javelin/` (or forget to run `git submodule update --init`). - Load a static resource. - Get "[Rendering Exception] Argument 1 passed to PhabricatorMainMenuView::setUser() must be an instance of PhabricatorUser, null given, called in /services/apache/phabricator/phabricator/src/view/page/PhabricatorStandardPageView.php on line 435 and defined" Test Plan: - Followed above steps, no more fataling. - Verified this is the only weird controller. Reviewers: voldern, vrana, btrahan Reviewed By: voldern CC: aran Differential Revision: https://secure.phabricator.com/D3389
125 lines
3.7 KiB
PHP
125 lines
3.7 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright 2012 Facebook, Inc.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
/**
|
|
* Delivers CSS and JS resources to the browser. This controller handles all
|
|
* ##/res/## requests, and manages caching, package construction, and resource
|
|
* preprocessing.
|
|
*
|
|
* @group celerity
|
|
*/
|
|
final class CelerityResourceController extends PhabricatorController {
|
|
|
|
private $path;
|
|
private $hash;
|
|
private $package;
|
|
|
|
public function shouldRequireLogin() {
|
|
return false;
|
|
}
|
|
|
|
public function shouldRequireEnabledUser() {
|
|
return false;
|
|
}
|
|
|
|
public function willProcessRequest(array $data) {
|
|
$this->path = $data['path'];
|
|
$this->hash = $data['hash'];
|
|
$this->package = !empty($data['package']);
|
|
}
|
|
|
|
public function processRequest() {
|
|
$path = $this->path;
|
|
|
|
// Sanity checking to keep this from exposing anything sensitive, since it
|
|
// ultimately boils down to disk reads.
|
|
if (preg_match('@(//|\.\.)@', $path)) {
|
|
return new Aphront400Response();
|
|
}
|
|
|
|
$type = CelerityResourceTransformer::getResourceType($path);
|
|
$type_map = $this->getSupportedResourceTypes();
|
|
|
|
if (empty($type_map[$type])) {
|
|
throw new Exception("Only static resources may be served.");
|
|
}
|
|
|
|
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
|
|
!PhabricatorEnv::getEnvConfig('celerity.force-disk-reads')) {
|
|
// Return a "304 Not Modified". We don't care about the value of this
|
|
// field since we never change what resource is served by a given URI.
|
|
return $this->makeResponseCacheable(new Aphront304Response());
|
|
}
|
|
|
|
$root = dirname(phutil_get_library_root('phabricator'));
|
|
|
|
if ($this->package) {
|
|
$map = CelerityResourceMap::getInstance();
|
|
$paths = $map->resolvePackage($this->hash);
|
|
if (!$paths) {
|
|
return new Aphront404Response();
|
|
}
|
|
|
|
try {
|
|
$data = array();
|
|
foreach ($paths as $package_path) {
|
|
$data[] = Filesystem::readFile($root.'/webroot/'.$package_path);
|
|
}
|
|
$data = implode("\n\n", $data);
|
|
} catch (Exception $ex) {
|
|
return new Aphront404Response();
|
|
}
|
|
} else {
|
|
try {
|
|
$data = Filesystem::readFile($root.'/webroot/'.$path);
|
|
} catch (Exception $ex) {
|
|
return new Aphront404Response();
|
|
}
|
|
}
|
|
|
|
$xformer = new CelerityResourceTransformer();
|
|
$xformer->setMinify(PhabricatorEnv::getEnvConfig('celerity.minify'));
|
|
$xformer->setCelerityMap(CelerityResourceMap::getInstance());
|
|
|
|
$data = $xformer->transformResource($path, $data);
|
|
|
|
$response = new AphrontFileResponse();
|
|
$response->setContent($data);
|
|
$response->setMimeType($type_map[$type]);
|
|
return $this->makeResponseCacheable($response);
|
|
}
|
|
|
|
private function getSupportedResourceTypes() {
|
|
return array(
|
|
'css' => 'text/css; charset=utf-8',
|
|
'js' => 'text/javascript; charset=utf-8',
|
|
'png' => 'image/png',
|
|
'gif' => 'image/gif',
|
|
'jpg' => 'image/jpg',
|
|
'swf' => 'application/x-shockwave-flash',
|
|
);
|
|
}
|
|
|
|
private function makeResponseCacheable(AphrontResponse $response) {
|
|
$response->setCacheDurationInSeconds(60 * 60 * 24 * 30);
|
|
$response->setLastModified(time());
|
|
|
|
return $response;
|
|
}
|
|
|
|
}
|