relay client's preferred language in link preview requests

Closes #1440.
This commit is contained in:
William Boman 2017-12-28 14:34:49 +01:00
parent f6c76ff9bd
commit acb6179b30
3 changed files with 45 additions and 6 deletions

View File

@ -38,7 +38,7 @@ 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(preview.link, function(res, err) { fetch(preview.link, {language: client.language}, function(res, err) {
if (err) { if (err) {
preview.type = "error"; preview.type = "error";
preview.error = "message"; preview.error = "message";
@ -85,7 +85,7 @@ function parse(msg, preview, res, client) {
// Verify that thumbnail pic exists and is under allowed size // Verify that thumbnail pic exists and is under allowed size
if (preview.thumb.length) { if (preview.thumb.length) {
fetch(escapeHeader(preview.thumb), (resThumb) => { fetch(escapeHeader(preview.thumb), {language: client.language}, (resThumb) => {
if (resThumb === null if (resThumb === null
|| !(/^image\/.+/.test(resThumb.type)) || !(/^image\/.+/.test(resThumb.type))
|| resThumb.size > (Helper.config.prefetchMaxImageSize * 1024)) { || resThumb.size > (Helper.config.prefetchMaxImageSize * 1024)) {
@ -198,7 +198,19 @@ function emitPreview(client, msg, preview) {
}); });
} }
function fetch(uri, cb) { function getRequestHeaders(language) {
const headers = {
"User-Agent": "Mozilla/5.0 (compatible; The Lounge IRC Client; +https://github.com/thelounge/lounge)",
};
if (language !== null) {
headers["Accept-Language"] = language;
}
return headers;
}
function fetch(uri, {language}, cb) {
let req; let req;
try { try {
@ -206,9 +218,7 @@ function fetch(uri, cb) {
url: uri, url: uri,
maxRedirects: 5, maxRedirects: 5,
timeout: 5000, timeout: 5000,
headers: { headers: getRequestHeaders(language),
"User-Agent": "Mozilla/5.0 (compatible; The Lounge IRC Client; +https://github.com/thelounge/lounge)",
},
}); });
} catch (e) { } catch (e) {
return cb(null, e); return cb(null, e);

View File

@ -204,6 +204,17 @@ module.exports = function() {
return server; return server;
}; };
function getClientLanguage(socket) {
const acceptLanguage = socket.handshake.headers["accept-language"];
if (typeof acceptLanguage === "string" && /^[\x00-\x7F]{1,50}$/.test(acceptLanguage)) {
// only allow ASCII strings between 1-50 characters in length
return acceptLanguage;
}
return null;
}
function getClientIp(socket) { function getClientIp(socket) {
let ip = socket.handshake.address; let ip = socket.handshake.address;
@ -549,6 +560,7 @@ function performAuthentication(data) {
socket.emit("configuration", getClientConfiguration()); socket.emit("configuration", getClientConfiguration());
client.ip = getClientIp(socket); client.ip = getClientIp(socket);
client.language = getClientLanguage(socket);
// If webirc is enabled perform reverse dns lookup // If webirc is enabled perform reverse dns lookup
if (Helper.config.webirc === null) { if (Helper.config.webirc === null) {

View File

@ -266,4 +266,21 @@ describe("Link plugin", function() {
} }
}); });
}); });
it("should use client's preferred language as Accept-Language header", function(done) {
const language = "sv,en-GB;q=0.9,en;q=0.8";
this.irc.language = language;
app.get("/language-check", function(req, res) {
expect(req.headers["accept-language"]).to.equal(language);
res.send();
done();
});
const message = this.irc.createMessage({
text: "http://localhost:9002/language-check",
});
link(this.irc, this.network.channels[0], message);
});
}); });