2011-01-25 18:59:31 +01:00
|
|
|
<?php
|
|
|
|
|
2012-10-17 17:37:05 +02:00
|
|
|
abstract class CelerityResourceController extends PhabricatorController {
|
2011-01-31 20:55:26 +01:00
|
|
|
|
2012-10-17 17:37:05 +02:00
|
|
|
protected function buildResourceTransformer() {
|
|
|
|
return null;
|
|
|
|
}
|
2011-01-31 20:55:26 +01:00
|
|
|
|
Make CelerityController extend PhabricatorController
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
2012-08-28 22:46:35 +02:00
|
|
|
public function shouldRequireLogin() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function shouldRequireEnabledUser() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-01 16:46:18 +01:00
|
|
|
abstract public function getCelerityResourceMap();
|
2014-01-01 04:21:56 +01:00
|
|
|
|
2012-10-17 17:37:05 +02:00
|
|
|
protected function serveResource($path, $package_hash = null) {
|
Use Celerity to version all static resources
Summary:
We don't use versioned URIs for images, so when they change users may get old versions.
This was a particular issue with the recent logo change, which several users reported cache-related issues from.
Instead, use Celerity to manage image URI versions in addition to CSS/JS.
This is complicated, because we need to rewrite image URIs inside of CSS, which means the hash of a CSS file has to be derived from the current image data. Otherwise, when we updated an image the CSS wouldn't update, so we wouldn't be any better off.
So basically we:
- Find all the "raw" files, and put them into the map.
- Find all the CSS/JS, perform content-altering transformations on it (i.e., not minification) based on the partial map, and then put it into the map based on transformed hashes.
(If we wanted, we could now do CSS variables or whatever for "free", more or less.)
Test Plan:
- Regenerated celerity map, browsed site, verified images generated with versioned URIs.
- Moved "blue" flag image over "green" flag image, regenerated map, verified "green" flag image and the associated CSS changed hashes.
- Added transformation unit tests; ran unit tests.
Reviewers: btrahan, vrana, jungejason
Reviewed By: vrana
CC: aran
Maniphest Tasks: T1073
Differential Revision: https://secure.phabricator.com/D2146
2012-04-08 19:07:51 +02:00
|
|
|
// 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.");
|
2011-01-25 18:59:31 +01:00
|
|
|
}
|
2011-01-31 20:55:26 +01:00
|
|
|
|
2013-02-10 00:01:57 +01:00
|
|
|
if (AphrontRequest::getHTTPHeader('If-Modified-Since') &&
|
2013-02-01 18:34:06 +01:00
|
|
|
!PhabricatorEnv::getEnvConfig('phabricator.developer-mode')) {
|
2011-05-09 10:10:40 +02:00
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
2014-01-01 04:21:56 +01:00
|
|
|
$map = $this->getCelerityResourceMap();
|
2014-01-01 03:03:17 +01:00
|
|
|
|
2014-01-01 03:04:25 +01:00
|
|
|
if ($map->isPackageResource($path)) {
|
|
|
|
$resource_names = $map->getResourceNamesForPackageName($path);
|
2014-01-01 03:03:17 +01:00
|
|
|
if (!$resource_names) {
|
2011-01-30 01:10:05 +01:00
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
2011-01-31 20:55:26 +01:00
|
|
|
|
2011-01-30 01:10:05 +01:00
|
|
|
try {
|
|
|
|
$data = array();
|
2014-01-01 03:03:17 +01:00
|
|
|
foreach ($resource_names as $resource_name) {
|
|
|
|
$data[] = $map->getResourceDataForName($resource_name);
|
2011-01-30 01:10:05 +01:00
|
|
|
}
|
|
|
|
$data = implode("\n\n", $data);
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
2014-01-01 03:03:17 +01:00
|
|
|
$data = $map->getResourceDataForName($path);
|
2011-01-30 01:10:05 +01:00
|
|
|
} catch (Exception $ex) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
2011-01-25 18:59:31 +01:00
|
|
|
}
|
|
|
|
|
2012-10-17 17:37:05 +02:00
|
|
|
$xformer = $this->buildResourceTransformer();
|
|
|
|
if ($xformer) {
|
|
|
|
$data = $xformer->transformResource($path, $data);
|
|
|
|
}
|
Use Celerity to version all static resources
Summary:
We don't use versioned URIs for images, so when they change users may get old versions.
This was a particular issue with the recent logo change, which several users reported cache-related issues from.
Instead, use Celerity to manage image URI versions in addition to CSS/JS.
This is complicated, because we need to rewrite image URIs inside of CSS, which means the hash of a CSS file has to be derived from the current image data. Otherwise, when we updated an image the CSS wouldn't update, so we wouldn't be any better off.
So basically we:
- Find all the "raw" files, and put them into the map.
- Find all the CSS/JS, perform content-altering transformations on it (i.e., not minification) based on the partial map, and then put it into the map based on transformed hashes.
(If we wanted, we could now do CSS variables or whatever for "free", more or less.)
Test Plan:
- Regenerated celerity map, browsed site, verified images generated with versioned URIs.
- Moved "blue" flag image over "green" flag image, regenerated map, verified "green" flag image and the associated CSS changed hashes.
- Added transformation unit tests; ran unit tests.
Reviewers: btrahan, vrana, jungejason
Reviewed By: vrana
CC: aran
Maniphest Tasks: T1073
Differential Revision: https://secure.phabricator.com/D2146
2012-04-08 19:07:51 +02:00
|
|
|
|
2011-01-25 18:59:31 +01:00
|
|
|
$response = new AphrontFileResponse();
|
|
|
|
$response->setContent($data);
|
Use Celerity to version all static resources
Summary:
We don't use versioned URIs for images, so when they change users may get old versions.
This was a particular issue with the recent logo change, which several users reported cache-related issues from.
Instead, use Celerity to manage image URI versions in addition to CSS/JS.
This is complicated, because we need to rewrite image URIs inside of CSS, which means the hash of a CSS file has to be derived from the current image data. Otherwise, when we updated an image the CSS wouldn't update, so we wouldn't be any better off.
So basically we:
- Find all the "raw" files, and put them into the map.
- Find all the CSS/JS, perform content-altering transformations on it (i.e., not minification) based on the partial map, and then put it into the map based on transformed hashes.
(If we wanted, we could now do CSS variables or whatever for "free", more or less.)
Test Plan:
- Regenerated celerity map, browsed site, verified images generated with versioned URIs.
- Moved "blue" flag image over "green" flag image, regenerated map, verified "green" flag image and the associated CSS changed hashes.
- Added transformation unit tests; ran unit tests.
Reviewers: btrahan, vrana, jungejason
Reviewed By: vrana
CC: aran
Maniphest Tasks: T1073
Differential Revision: https://secure.phabricator.com/D2146
2012-04-08 19:07:51 +02:00
|
|
|
$response->setMimeType($type_map[$type]);
|
2011-05-09 10:10:40 +02:00
|
|
|
return $this->makeResponseCacheable($response);
|
|
|
|
}
|
|
|
|
|
2012-10-17 17:37:05 +02:00
|
|
|
protected function getSupportedResourceTypes() {
|
Use Celerity to version all static resources
Summary:
We don't use versioned URIs for images, so when they change users may get old versions.
This was a particular issue with the recent logo change, which several users reported cache-related issues from.
Instead, use Celerity to manage image URI versions in addition to CSS/JS.
This is complicated, because we need to rewrite image URIs inside of CSS, which means the hash of a CSS file has to be derived from the current image data. Otherwise, when we updated an image the CSS wouldn't update, so we wouldn't be any better off.
So basically we:
- Find all the "raw" files, and put them into the map.
- Find all the CSS/JS, perform content-altering transformations on it (i.e., not minification) based on the partial map, and then put it into the map based on transformed hashes.
(If we wanted, we could now do CSS variables or whatever for "free", more or less.)
Test Plan:
- Regenerated celerity map, browsed site, verified images generated with versioned URIs.
- Moved "blue" flag image over "green" flag image, regenerated map, verified "green" flag image and the associated CSS changed hashes.
- Added transformation unit tests; ran unit tests.
Reviewers: btrahan, vrana, jungejason
Reviewed By: vrana
CC: aran
Maniphest Tasks: T1073
Differential Revision: https://secure.phabricator.com/D2146
2012-04-08 19:07:51 +02:00
|
|
|
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',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-05-09 10:10:40 +02:00
|
|
|
private function makeResponseCacheable(AphrontResponse $response) {
|
2011-01-27 20:35:04 +01:00
|
|
|
$response->setCacheDurationInSeconds(60 * 60 * 24 * 30);
|
2011-05-09 10:10:40 +02:00
|
|
|
$response->setLastModified(time());
|
2011-01-25 18:59:31 +01:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|