Skip to content

Commit 3fabeb2

Browse files
John Schulzchrisronline
authored andcommitted
Forward any registry cache-control header for files (elastic#83680)
closes elastic#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
1 parent fb33340 commit 3fabeb2

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

x-pack/plugins/fleet/server/routes/epm/handlers.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66
import { TypeOf } from '@kbn/config-schema';
7-
import { RequestHandler, CustomHttpResponseOptions } from 'src/core/server';
7+
import { RequestHandler, ResponseHeaders, KnownHeaders } from 'src/core/server';
88
import {
99
GetInfoResponse,
1010
InstallPackageResponse,
@@ -103,15 +103,21 @@ export const getFileHandler: RequestHandler<TypeOf<typeof GetFileRequestSchema.p
103103
try {
104104
const { pkgName, pkgVersion, filePath } = request.params;
105105
const registryResponse = await getFile(`/package/${pkgName}/${pkgVersion}/${filePath}`);
106-
const contentType = registryResponse.headers.get('Content-Type');
107-
const customResponseObj: CustomHttpResponseOptions<typeof registryResponse.body> = {
106+
107+
const headersToProxy: KnownHeaders[] = ['content-type', 'cache-control'];
108+
const proxiedHeaders = headersToProxy.reduce((headers, knownHeader) => {
109+
const value = registryResponse.headers.get(knownHeader);
110+
if (value !== null) {
111+
headers[knownHeader] = value;
112+
}
113+
return headers;
114+
}, {} as ResponseHeaders);
115+
116+
return response.custom({
108117
body: registryResponse.body,
109118
statusCode: registryResponse.status,
110-
};
111-
if (contentType !== null) {
112-
customResponseObj.headers = { 'Content-Type': contentType };
113-
}
114-
return response.custom(customResponseObj);
119+
headers: proxiedHeaders,
120+
});
115121
} catch (error) {
116122
return defaultIngestErrorHandler({ error, response });
117123
}

0 commit comments

Comments
 (0)