Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reporting] Fix map tiles not loading by using Chrome's Remote Protocol #55137

Merged
merged 7 commits into from
Jan 17, 2020
Merged
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 { trunc } from 'lodash';
import { trunc, map } from 'lodash';
import open from 'opn';
import { parse as parseUrl } from 'url';
import { Page, SerializableOrJSHandle, EvaluateFn } from 'puppeteer';
Expand All @@ -15,6 +15,7 @@ import {
ConditionalHeaders,
ConditionalHeadersConditions,
ElementPosition,
InterceptedRequest,
NetworkPolicy,
} from '../../../../types';

Expand Down Expand Up @@ -59,35 +60,52 @@ export class HeadlessChromiumDriver {
}: { conditionalHeaders: ConditionalHeaders; waitForSelector: string },
logger: LevelLogger
) {
await this.page.setRequestInterception(true);
logger.info(`opening url ${url}`);
// @ts-ignore
const client = this.page._client;
let interceptedCount = 0;

this.page.on('request', interceptedRequest => {
const interceptedUrl = interceptedRequest.url();
await this.page.setRequestInterception(true);

// We have to reach into the Chrome Remote Protocol to apply headers as using
// puppeteer's API will render map tiles as blanks :/
client.on('Fetch.requestPaused', (interceptedRequest: InterceptedRequest) => {
const requestId = interceptedRequest.requestId;
const interceptedUrl = interceptedRequest.request.url;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about:
const { requestId, request: { url: interceptedUrl } } = interceptedRequest;

const allowed = !interceptedUrl.startsWith('file://');
const isData = interceptedUrl.startsWith('data:');

// We should never ever let file protocol requests go through
if (!allowed || !this.allowRequest(interceptedUrl)) {
logger.error(`Got bad URL: "${interceptedUrl}", closing browser.`);
interceptedRequest.abort('blockedbyclient');
client.send('Fetch.failRequest', {
errorReason: 'Aborted',
requestId,
});
this.page.browser().close();
throw new Error(`Received disallowed outgoing URL: "${interceptedUrl}", exiting`);
}

if (this._shouldUseCustomHeaders(conditionalHeaders.conditions, interceptedUrl)) {
logger.debug(`Using custom headers for ${interceptedUrl}`);
interceptedRequest.continue({
headers: {
...interceptedRequest.headers(),
const headers = map(
{
...interceptedRequest.request.headers,
...conditionalHeaders.headers,
},
(value, name) => ({
name,
value,
})
);
client.send('Fetch.continueRequest', {
requestId,
headers,
});
} else {
const loggedUrl = isData ? this.truncateUrl(interceptedUrl) : interceptedUrl;
logger.debug(`No custom headers for ${loggedUrl}`);
interceptedRequest.continue();
client.send('Fetch.continueRequest', { requestId });
}
interceptedCount = interceptedCount + (isData ? 0 : 1);
});
Expand Down
15 changes: 15 additions & 0 deletions x-pack/legacy/plugins/reporting/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,19 @@ export interface AbsoluteURLFactoryOptions {
port: string | number;
}

export interface InterceptedRequest {
requestId: string;
request: {
url: string;
method: string;
headers: {
[key: string]: string;
};
initialPriority: string;
referrerPolicy: string;
};
frameId: string;
resourceType: string;
}

export { ServerFacade };