send Referer-s

Clsoes #7
This commit is contained in:
Nils Maier 2019-08-24 02:52:04 +02:00
parent 4236195ccf
commit d3c9f8bc89

View File

@ -2,7 +2,7 @@
// License: MIT // License: MIT
import { Prefs } from "../prefs"; import { Prefs } from "../prefs";
import { parsePath } from "../util"; import { parsePath, filterInSitu } from "../util";
import { import {
QUEUED, RUNNING, CANCELED, PAUSED, MISSING, DONE, QUEUED, RUNNING, CANCELED, PAUSED, MISSING, DONE,
FORCABLE, PAUSABLE, CANCELABLE, FORCABLE, PAUSABLE, CANCELABLE,
@ -18,6 +18,18 @@ const setShelfEnabled = downloads.setShelfEnabled || function() {
// ignored // 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 { export class Download extends BaseDownload {
public manager: Manager; public manager: Manager;
@ -82,41 +94,47 @@ export class Download extends BaseDownload {
if (this.state !== QUEUED) { if (this.state !== QUEUED) {
throw new Error("invalid state"); throw new Error("invalid state");
} }
console.trace("starting", this.toString(), this.dest, this.mask); console.trace("starting", this.toString(), this.toMsg());
this.changeState(RUNNING); this.changeState(RUNNING);
try { try {
const options: any = { const options: Options = {
conflictAction: await Prefs.get("conflict-action"), conflictAction: await Prefs.get("conflict-action"),
filename: this.dest.full, filename: this.dest.full,
saveAs: false, saveAs: false,
url: this.url, url: this.url,
headers: [], headers: [],
incognito: this.private
}; };
if (this.postData) { if (this.postData) {
options.body = this.postData; options.body = this.postData;
options.method = "POST"; 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) { if (this.referrer) {
options.headers.push({ options.headers.push({
name: "Referer", name: "Referer",
value: this.referrer value: this.referrer
}); });
} }
*/
if (this.manId) { if (this.manId) {
this.manager.removeManId(this.manId); this.manager.removeManId(this.manId);
} }
setShelfEnabled(false); setShelfEnabled(false);
try {
try { try {
this.manager.addManId( this.manager.addManId(
this.manId = await downloads.download(options), this); 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 { finally {
setShelfEnabled(true); setShelfEnabled(true);
} }