happy-dom icon indicating copy to clipboard operation
happy-dom copied to clipboard

add Referer when load web assets

Open Mas0nShi opened this issue 2 years ago • 0 comments

some websites have enabled anti-theft links, we need to add headers to solve the problem.

https://github.com/capricorn86/happy-dom/blob/f4320c383fefce587effbf856ef7ba9954533cba/packages/happy-dom/src/fetch/ResourceFetchHandler.ts#L33-L46

	/**
	 * Returns resource data synchonously.
	 *
	 * @param document Document.
	 * @param url URL.
	 * @returns Response.
	 */
	public static fetchSync(document: IDocument, url: string): string {
		// We want to only load SyncRequest when it is needed to improve performance and not have direct dependencies to server side packages.
		const absoluteURL = RelativeURL.getAbsoluteURL(document.defaultView.location, url);
		const syncRequest = require('sync-request');
		const response = syncRequest('GET', absoluteURL, {
			headers: {
				'user-agent': document.defaultView.navigator.userAgent,
				cookie: document.defaultView.document.cookie,
				referer: document.defaultView.location.href,
				pragma: 'no-cache'
			}
		});

		if (response.isError()) {
			throw new DOMException(
				`Failed to perform request to "${absoluteURL}". Status code: ${response.statusCode}`
			);
		}

		return response.getBody().toString();
	}
}

Mas0nShi avatar Jun 27 '22 03:06 Mas0nShi