Merge pull request #2287 from thelounge/xpaw/accept
Send `Accept` header when fetching links
This commit is contained in:
commit
80c6e48b98
@ -52,7 +52,10 @@ module.exports = function(client, chan, msg) {
|
|||||||
})).slice(0, 5); // Only preview the first 5 URLs in message to avoid abuse
|
})).slice(0, 5); // Only preview the first 5 URLs in message to avoid abuse
|
||||||
|
|
||||||
msg.previews.forEach((preview) => {
|
msg.previews.forEach((preview) => {
|
||||||
fetch(normalizeURL(preview.link), {language: client.language}, function(res, err) {
|
fetch(normalizeURL(preview.link), {
|
||||||
|
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||||
|
language: client.language,
|
||||||
|
}, function(res, err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
preview.type = "error";
|
preview.type = "error";
|
||||||
preview.error = "message";
|
preview.error = "message";
|
||||||
@ -141,7 +144,12 @@ function parseHtmlMedia($, preview, res, client) {
|
|||||||
|
|
||||||
foundMedia = true;
|
foundMedia = true;
|
||||||
|
|
||||||
fetch(normalizeURL(mediaUrl), {language: client.language}, (resMedia) => {
|
fetch(normalizeURL(mediaUrl), {
|
||||||
|
accept: type === "video" ?
|
||||||
|
"video/webm,video/ogg,video/*;q=0.9,application/ogg;q=0.7,audio/*;q=0.6,*/*;q=0.5" :
|
||||||
|
"audio/webm, audio/ogg, audio/wav, audio/*;q=0.9, application/ogg;q=0.7, video/*;q=0.6; */*;q=0.5",
|
||||||
|
language: client.language,
|
||||||
|
}, (resMedia) => {
|
||||||
if (resMedia === null || !mediaTypeRegex.test(resMedia.type)) {
|
if (resMedia === null || !mediaTypeRegex.test(resMedia.type)) {
|
||||||
return reject();
|
return reject();
|
||||||
}
|
}
|
||||||
@ -273,19 +281,20 @@ function emitPreview(client, msg, preview) {
|
|||||||
client.emit("msg:preview", {id, preview});
|
client.emit("msg:preview", {id, preview});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRequestHeaders(language) {
|
function getRequestHeaders(headers) {
|
||||||
const headers = {
|
const formattedHeaders = {
|
||||||
"User-Agent": "Mozilla/5.0 (compatible; The Lounge IRC Client; +https://github.com/thelounge/thelounge)",
|
"User-Agent": "Mozilla/5.0 (compatible; The Lounge IRC Client; +https://github.com/thelounge/thelounge)",
|
||||||
|
Accept: headers.accept || "*/*",
|
||||||
};
|
};
|
||||||
|
|
||||||
if (language !== null) {
|
if (headers.language) {
|
||||||
headers["Accept-Language"] = language;
|
formattedHeaders["Accept-Language"] = headers.language;
|
||||||
}
|
}
|
||||||
|
|
||||||
return headers;
|
return formattedHeaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
function fetch(uri, {language}, cb) {
|
function fetch(uri, headers, cb) {
|
||||||
let req;
|
let req;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -293,7 +302,7 @@ function fetch(uri, {language}, cb) {
|
|||||||
url: uri,
|
url: uri,
|
||||||
maxRedirects: 5,
|
maxRedirects: 5,
|
||||||
timeout: 5000,
|
timeout: 5000,
|
||||||
headers: getRequestHeaders(language),
|
headers: getRequestHeaders(headers),
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return cb(null, e);
|
return cb(null, e);
|
||||||
|
@ -284,6 +284,38 @@ describe("Link plugin", function() {
|
|||||||
link(this.irc, this.network.channels[0], message);
|
link(this.irc, this.network.channels[0], message);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should send accept text/html for initial request", function(done) {
|
||||||
|
app.get("/accept-header-html", function(req, res) {
|
||||||
|
expect(req.headers.accept).to.equal("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
|
||||||
|
res.send();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
const message = this.irc.createMessage({
|
||||||
|
text: "http://localhost:9002/accept-header-html",
|
||||||
|
});
|
||||||
|
|
||||||
|
link(this.irc, this.network.channels[0], message);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should send accept */* for meta image", function(done) {
|
||||||
|
app.get("/accept-header-thumb", function(req, res) {
|
||||||
|
res.send("<title>404 image</title><meta property='og:image' content='http://localhost:9002/accept-header-thumb.png'>");
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/accept-header-thumb.png", function(req, res) {
|
||||||
|
expect(req.headers.accept).to.equal("*/*");
|
||||||
|
res.send();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
const message = this.irc.createMessage({
|
||||||
|
text: "http://localhost:9002/accept-header-thumb",
|
||||||
|
});
|
||||||
|
|
||||||
|
link(this.irc, this.network.channels[0], message);
|
||||||
|
});
|
||||||
|
|
||||||
it("should not add slash to url", function(done) {
|
it("should not add slash to url", function(done) {
|
||||||
const message = this.irc.createMessage({
|
const message = this.irc.createMessage({
|
||||||
text: "http://localhost:9002",
|
text: "http://localhost:9002",
|
||||||
|
Loading…
Reference in New Issue
Block a user