Merge pull request #1918 from williamboman/fix/send-accept-language-in-link-previews
relay client's preferred language in link preview requests
This commit is contained in:
commit
5bc3ef3ed8
@ -38,7 +38,7 @@ 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(preview.link, function(res, err) {
|
||||
fetch(preview.link, {language: client.language}, function(res, err) {
|
||||
if (err) {
|
||||
preview.type = "error";
|
||||
preview.error = "message";
|
||||
@ -85,7 +85,7 @@ function parse(msg, preview, res, client) {
|
||||
|
||||
// Verify that thumbnail pic exists and is under allowed size
|
||||
if (preview.thumb.length) {
|
||||
fetch(escapeHeader(preview.thumb), (resThumb) => {
|
||||
fetch(escapeHeader(preview.thumb), {language: client.language}, (resThumb) => {
|
||||
if (resThumb === null
|
||||
|| !(/^image\/.+/.test(resThumb.type))
|
||||
|| 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;
|
||||
|
||||
try {
|
||||
@ -206,9 +218,7 @@ function fetch(uri, cb) {
|
||||
url: uri,
|
||||
maxRedirects: 5,
|
||||
timeout: 5000,
|
||||
headers: {
|
||||
"User-Agent": "Mozilla/5.0 (compatible; The Lounge IRC Client; +https://github.com/thelounge/lounge)",
|
||||
},
|
||||
headers: getRequestHeaders(language),
|
||||
});
|
||||
} catch (e) {
|
||||
return cb(null, e);
|
||||
|
@ -204,6 +204,17 @@ module.exports = function() {
|
||||
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) {
|
||||
let ip = socket.handshake.address;
|
||||
|
||||
@ -549,6 +560,7 @@ function performAuthentication(data) {
|
||||
socket.emit("configuration", getClientConfiguration());
|
||||
|
||||
client.ip = getClientIp(socket);
|
||||
client.language = getClientLanguage(socket);
|
||||
|
||||
// If webirc is enabled perform reverse dns lookup
|
||||
if (Helper.config.webirc === null) {
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user