From ca96d09a2337a5e6466b85800ace798d39530413 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Fri, 23 Mar 2018 16:50:52 +0200 Subject: [PATCH] Send Accept header Closes #2277 --- src/plugins/irc-events/link.js | 27 ++++++++++++++++++--------- test/plugins/link.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/plugins/irc-events/link.js b/src/plugins/irc-events/link.js index d5687e5e..370d9ab4 100644 --- a/src/plugins/irc-events/link.js +++ b/src/plugins/irc-events/link.js @@ -52,7 +52,10 @@ module.exports = function(client, chan, msg) { })).slice(0, 5); // Only preview the first 5 URLs in message to avoid abuse 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) { preview.type = "error"; preview.error = "message"; @@ -141,7 +144,12 @@ function parseHtmlMedia($, preview, res, client) { 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)) { return reject(); } @@ -273,19 +281,20 @@ function emitPreview(client, msg, preview) { client.emit("msg:preview", {id, preview}); } -function getRequestHeaders(language) { - const headers = { +function getRequestHeaders(headers) { + const formattedHeaders = { "User-Agent": "Mozilla/5.0 (compatible; The Lounge IRC Client; +https://github.com/thelounge/thelounge)", + Accept: headers.accept || "*/*", }; - if (language !== null) { - headers["Accept-Language"] = language; + if (headers.language) { + formattedHeaders["Accept-Language"] = headers.language; } - return headers; + return formattedHeaders; } -function fetch(uri, {language}, cb) { +function fetch(uri, headers, cb) { let req; try { @@ -293,7 +302,7 @@ function fetch(uri, {language}, cb) { url: uri, maxRedirects: 5, timeout: 5000, - headers: getRequestHeaders(language), + headers: getRequestHeaders(headers), }); } catch (e) { return cb(null, e); diff --git a/test/plugins/link.js b/test/plugins/link.js index 5cb180dc..a2499489 100644 --- a/test/plugins/link.js +++ b/test/plugins/link.js @@ -284,6 +284,38 @@ describe("Link plugin", function() { 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("404 image"); + }); + + 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) { const message = this.irc.createMessage({ text: "http://localhost:9002",