From d3c9f8bc892621f4276012f2707615e369914896 Mon Sep 17 00:00:00 2001 From: Nils Maier Date: Sat, 24 Aug 2019 02:52:04 +0200 Subject: [PATCH] send Referer-s Clsoes #7 --- lib/manager/download.ts | 42 +++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/manager/download.ts b/lib/manager/download.ts index e869f9f..2fad584 100644 --- a/lib/manager/download.ts +++ b/lib/manager/download.ts @@ -2,7 +2,7 @@ // License: MIT import { Prefs } from "../prefs"; -import { parsePath } from "../util"; +import { parsePath, filterInSitu } from "../util"; import { QUEUED, RUNNING, CANCELED, PAUSED, MISSING, DONE, FORCABLE, PAUSABLE, CANCELABLE, @@ -18,6 +18,18 @@ const setShelfEnabled = downloads.setShelfEnabled || function() { // ignored }; +type Header = {name: string; value: string}; +interface Options { + conflictAction: string; + filename: string; + saveAs: boolean; + url: string; + method?: string; + body?: string; + incognito: boolean; + headers: Header[]; +} + export class Download extends BaseDownload { public manager: Manager; @@ -82,40 +94,46 @@ export class Download extends BaseDownload { if (this.state !== QUEUED) { throw new Error("invalid state"); } - console.trace("starting", this.toString(), this.dest, this.mask); + console.trace("starting", this.toString(), this.toMsg()); this.changeState(RUNNING); try { - const options: any = { + const options: Options = { conflictAction: await Prefs.get("conflict-action"), filename: this.dest.full, saveAs: false, url: this.url, headers: [], + incognito: this.private }; if (this.postData) { options.body = this.postData; options.method = "POST"; } - if (this.private) { - options.incognito = true; - } - /* XXX "forbidden" - Cannot be worked around with webRequest either - as those do not see downloads. if (this.referrer) { options.headers.push({ name: "Referer", value: this.referrer }); } - */ if (this.manId) { this.manager.removeManId(this.manId); } + setShelfEnabled(false); try { - this.manager.addManId( - this.manId = await downloads.download(options), this); + try { + this.manager.addManId( + this.manId = await downloads.download(options), this); + } + catch (ex) { + if (!this.referrer) { + throw ex; + } + // Re-attempt without referrer + filterInSitu(options.headers, h => h.name !== "Referer"); + this.manager.addManId( + this.manId = await downloads.download(options), this); + } } finally { setShelfEnabled(true);