Skip to content

Commit

Permalink
Forward any registry cache-control header for files (#83680) (#84172)
Browse files Browse the repository at this point in the history
closes #83631 

### Problem
Assets are served with a `cache-control` header that prevents any caching
<img src="https://user-images.githubusercontent.com/640/99534379-517d2300-2975-11eb-8c05-4fb3f127c52b.png"/>

### Root cause
Likely from this code https://github.com/elastic/kibana/blob/2a365ff6329544465227e61141ded6fba8bb2c80/src/core/server/http/http_tools.ts#L40-L43

Also based on these tests, it seems this is default/expected behavior

https://github.com/elastic/kibana/blob/b3eefb97da8e712789b5c5d2eeae65c886ed8f64/src/core/server/http/integration_tests/router.test.ts#L510-L520

### Proposed solution
Set the header via the response handler as shown in this test:
https://github.com/elastic/kibana/blob/b3eefb97da8e712789b5c5d2eeae65c886ed8f64/src/core/server/http/integration_tests/router.test.ts#L522-L536

### This PR
If this registry response contains a `cache-control` header, that value is included in the EPM response as well

In `master`, which points to `epr-snapshot`
<img width="742" alt="Screen Shot 2020-11-18 at 12 33 47 PM" src="https://user-images.githubusercontent.com/57655/99568352-4fc75580-299d-11eb-962f-6ff28fa9510d.png">
which matches https://epr-snapshot.elastic.co/package/apache/0.2.6/img/logo_apache.svg

or using `epr.elastic.co`, 
<img width="781" alt="Screen Shot 2020-11-18 at 12 31 56 PM" src="https://user-images.githubusercontent.com/57655/99568350-4fc75580-299d-11eb-966e-f3489c13edb5.png">
which matches https://epr.elastic.co/package/apache/0.2.6/img/logo_apache.svg
  • Loading branch information
John Schulz authored Nov 24, 2020
1 parent 518c24d commit e7d56a7
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions x-pack/plugins/fleet/server/routes/epm/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { TypeOf } from '@kbn/config-schema';
import { RequestHandler, CustomHttpResponseOptions } from 'src/core/server';
import { RequestHandler, ResponseHeaders, KnownHeaders } from 'src/core/server';
import {
GetInfoResponse,
InstallPackageResponse,
Expand Down Expand Up @@ -103,15 +103,21 @@ export const getFileHandler: RequestHandler<TypeOf<typeof GetFileRequestSchema.p
try {
const { pkgName, pkgVersion, filePath } = request.params;
const registryResponse = await getFile(`/package/${pkgName}/${pkgVersion}/${filePath}`);
const contentType = registryResponse.headers.get('Content-Type');
const customResponseObj: CustomHttpResponseOptions<typeof registryResponse.body> = {

const headersToProxy: KnownHeaders[] = ['content-type', 'cache-control'];
const proxiedHeaders = headersToProxy.reduce((headers, knownHeader) => {
const value = registryResponse.headers.get(knownHeader);
if (value !== null) {
headers[knownHeader] = value;
}
return headers;
}, {} as ResponseHeaders);

return response.custom({
body: registryResponse.body,
statusCode: registryResponse.status,
};
if (contentType !== null) {
customResponseObj.headers = { 'Content-Type': contentType };
}
return response.custom(customResponseObj);
headers: proxiedHeaders,
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
Expand Down

0 comments on commit e7d56a7

Please sign in to comment.