Skip to content

Commit

Permalink
Forward client Accept-Language header during web translation
Browse files Browse the repository at this point in the history
Addresses #16
  • Loading branch information
dstillman committed Dec 4, 2018
1 parent 6464497 commit e763651
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion modules/zotero
Submodule zotero updated 71 files
+1 −1 CONTRIBUTING.md
+1 −1 chrome/content/zotero/bibliography.js
+3 −3 chrome/content/zotero/integration/integrationDocPrefs.xul
+10 −2 chrome/content/zotero/tools/testTranslators/translatorTester.js
+30 −10 chrome/content/zotero/xpcom/attachments.js
+14 −13 chrome/content/zotero/xpcom/data/search.js
+66 −34 chrome/content/zotero/xpcom/http.js
+1 −1 chrome/content/zotero/xpcom/integration.js
+29 −1 chrome/content/zotero/xpcom/style.js
+7 −0 chrome/content/zotero/xpcom/translation/translate.js
+9 −0 chrome/content/zotero/xpcom/utilities_internal.js
+5 −3 chrome/content/zotero/xpcom/utilities_translate.js
+1 −0 chrome/locale/af-ZA/zotero/zotero.properties
+17 −16 chrome/locale/ar/zotero/zotero.properties
+1 −0 chrome/locale/bg-BG/zotero/zotero.properties
+1 −0 chrome/locale/ca-AD/zotero/zotero.properties
+1 −0 chrome/locale/cs-CZ/zotero/zotero.properties
+1 −0 chrome/locale/da-DK/zotero/zotero.properties
+1 −0 chrome/locale/de/zotero/zotero.properties
+1 −0 chrome/locale/el-GR/zotero/zotero.properties
+1 −0 chrome/locale/en-US/zotero/zotero.properties
+6 −6 chrome/locale/es-ES/zotero/preferences.dtd
+8 −8 chrome/locale/es-ES/zotero/zotero.dtd
+14 −13 chrome/locale/es-ES/zotero/zotero.properties
+1 −0 chrome/locale/et-EE/zotero/zotero.properties
+1 −0 chrome/locale/eu-ES/zotero/zotero.properties
+1 −0 chrome/locale/fa/zotero/zotero.properties
+1 −1 chrome/locale/fi-FI/zotero/standalone.dtd
+1 −1 chrome/locale/fi-FI/zotero/zotero.dtd
+16 −15 chrome/locale/fi-FI/zotero/zotero.properties
+1 −0 chrome/locale/fr-FR/zotero/zotero.properties
+1 −0 chrome/locale/gl-ES/zotero/zotero.properties
+1 −0 chrome/locale/he-IL/zotero/zotero.properties
+1 −0 chrome/locale/hr-HR/zotero/zotero.properties
+1 −0 chrome/locale/hu-HU/zotero/zotero.properties
+1 −0 chrome/locale/id-ID/zotero/zotero.properties
+1 −0 chrome/locale/is-IS/zotero/zotero.properties
+1 −0 chrome/locale/it-IT/zotero/zotero.properties
+9 −9 chrome/locale/ja-JP/zotero/preferences.dtd
+18 −18 chrome/locale/ja-JP/zotero/zotero.dtd
+25 −24 chrome/locale/ja-JP/zotero/zotero.properties
+1 −0 chrome/locale/km/zotero/zotero.properties
+1 −0 chrome/locale/ko-KR/zotero/zotero.properties
+1 −0 chrome/locale/lt-LT/zotero/zotero.properties
+1 −0 chrome/locale/mn-MN/zotero/zotero.properties
+1 −0 chrome/locale/nb-NO/zotero/zotero.properties
+1 −0 chrome/locale/nl-NL/zotero/zotero.properties
+1 −0 chrome/locale/nn-NO/zotero/zotero.properties
+3 −2 chrome/locale/pl-PL/zotero/zotero.properties
+1 −0 chrome/locale/pt-BR/zotero/zotero.properties
+1 −0 chrome/locale/pt-PT/zotero/zotero.properties
+1 −0 chrome/locale/ro-RO/zotero/zotero.properties
+1 −0 chrome/locale/ru-RU/zotero/zotero.properties
+9 −8 chrome/locale/sk-SK/zotero/zotero.properties
+1 −0 chrome/locale/sl-SI/zotero/zotero.properties
+1 −0 chrome/locale/sr-RS/zotero/zotero.properties
+30 −29 chrome/locale/sv-SE/zotero/zotero.properties
+1 −0 chrome/locale/th-TH/zotero/zotero.properties
+3 −2 chrome/locale/tr-TR/zotero/zotero.properties
+1 −0 chrome/locale/uk-UA/zotero/zotero.properties
+1 −0 chrome/locale/vi-VN/zotero/zotero.properties
+1 −0 chrome/locale/zh-CN/zotero/zotero.properties
+1 −0 chrome/locale/zh-TW/zotero/zotero.properties
+1 −0 chrome/skin/default/zotero/bibliography.css
+2 −2 components/zotero-autocomplete.js
+1 −1 install.rdf
+1 −1 resource/schema/repotime.txt
+1 −1 styles
+25 −0 test/tests/attachmentsTest.js
+23 −0 test/tests/searchTest.js
+1 −1 translators
8 changes: 6 additions & 2 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,22 +236,26 @@ Zotero.HTTP = new function() {
* @param {CookieJar} [cookieSandbox] Cookie sandbox object
* @return {Promise<Array>} - A promise for an array of results from the processor runs
*/
this.processDocuments = async function (urls, processor, cookieSandbox) {
this.processDocuments = async function (urls, processor, options = {}) {
// Handle old signature: urls, processor, onDone, onError
if (arguments.length > 3) {
Zotero.debug("Zotero.HTTP.processDocuments() now takes only 2 arguments -- update your code");
var onDone = arguments[3];
var onError = arguments[4];
}

var cookieSandbox = options.cookieSandbox;
var headers = options.headers;

if (typeof urls == "string") urls = [urls];
var funcs = urls.map(url => () => {
return Zotero.HTTP.request(
"GET",
url,
{
responseType: 'document',
cookieSandbox
cookieSandbox,
headers
}
)
.then((req) => {
Expand Down
5 changes: 3 additions & 2 deletions src/testEndpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ var TestEndpoint = module.exports = {
},

handleSingle: async function (ctx, next) {
var title = ctx.headers['accept-language'] == 'fr' ? 'Titre' : 'Title';
ctx.response.body = `<html>
<head>
<title>Page Title</title>
<title>${title}</title>
<link rel="schema.DC" href="http://purl.org/dc/elements/1.1/" />
<meta name="citation_title" content="Title"/>
<meta name="citation_title" content="${title}"/>
</head>
<body>Body</body>
</html>`;
Expand Down
16 changes: 15 additions & 1 deletion src/webSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const SearchEndpoint = require('./searchEndpoint');
const { jar: cookieJar } = require('request');

const SERVER_TRANSLATION_TIMEOUT = 30;
const FORWARDED_HEADERS = ['Accept-Language'];

var WebSession = module.exports = function (ctx, next, data, options) {
this.ctx = ctx;
Expand All @@ -52,6 +53,15 @@ WebSession.prototype.handleURL = async function () {

var url = this.data;

// Forward supported headers
var headers = {};
for (let header of FORWARDED_HEADERS) {
let lc = header.toLowerCase();
if (this.ctx.headers[lc]) {
headers[header] = this.ctx.headers[lc];
}
}

try {
var parsedURL = urlLib.parse(url);
}
Expand Down Expand Up @@ -140,6 +150,7 @@ WebSession.prototype.handleURL = async function () {
resolve();
});
translate.setCookieSandbox(this._cookieSandbox);
translate.setRequestHeaders(headers);

try {
await HTTP.processDocuments(
Expand All @@ -150,7 +161,10 @@ WebSession.prototype.handleURL = async function () {
// if the first fails, but for now just run detect on all
return translate.getTranslators(true);
},
this._cookieSandbox
{
cookieSandbox: this._cookieSandbox,
headers
}
);
return promise;
}
Expand Down
17 changes: 17 additions & 0 deletions test/web_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,21 @@ describe("/web", function () {
assert.equal(response.statusCode, 400);
assert.equal(response.text, 'Remote page not found');
});


// Note: This doesn't test subsequent requests during translation
it("should forward the Accept-Language header in the initial request", async function () {
var url = testURL + 'single';
var response = await request()
.post('/web')
.set('Content-Type', 'text/plain')
.set('Accept-Language', 'fr')
.send(url);
assert.equal(response.statusCode, 200);
var json = response.body;

assert.lengthOf(json, 1);
assert.equal(json[0].itemType, 'journalArticle');
assert.equal(json[0].title, 'Titre');
});
});

0 comments on commit e763651

Please sign in to comment.