From 353a43cb46e34ee82b3458df1138952282973aec Mon Sep 17 00:00:00 2001 From: asraniel Date: Sun, 6 May 2012 21:24:42 +0200 Subject: [PATCH 1/5] Add first API change, not working yet --- src/api.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/api.js b/src/api.js index bbab680ce..b3415c08e 100644 --- a/src/api.js +++ b/src/api.js @@ -8,10 +8,10 @@ * e.g. No cross domain requests without CORS. * * @param {string|TypedAray} source Either a url to a PDF is located or a - * typed array already populated with data. + * typed array (Uint8Array) already populated with data. * @return {Promise} A promise that is resolved with {PDFDocumentProxy} object. */ -PDFJS.getDocument = function getDocument(source) { +PDFJS.getDocument = function getDocument(source, headers) { var promise = new PDFJS.Promise(); var transport = new WorkerTransport(promise); if (typeof source === 'string') { @@ -29,7 +29,8 @@ PDFJS.getDocument = function getDocument(source) { error: function getPDFError(e) { promise.reject('Unexpected server response of ' + e.target.status + '.'); - } + }, + headers: headers }, function getPDFLoad(data) { transport.sendData(data); From 315071ca28ab1bdba6f1a27558f507f41a48d962 Mon Sep 17 00:00:00 2001 From: asraniel Date: Sun, 6 May 2012 21:32:30 +0200 Subject: [PATCH 2/5] Add stub for the last piece of the puzzle --- src/core.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/core.js b/src/core.js index 6a5ec275c..908ba63af 100644 --- a/src/core.js +++ b/src/core.js @@ -31,6 +31,11 @@ function getPdf(arg, callback) { params = { url: arg }; var xhr = new XMLHttpRequest(); + + if(params.headers){ + //TODO: Code this, use xhr.setRequestHeader(key, value); + } + xhr.open('GET', params.url); xhr.mozResponseType = xhr.responseType = 'arraybuffer'; var protocol = params.url.indexOf(':') < 0 ? window.location.protocol : From 7786e4fefbc4bc2f5f8301b8aaa89c8180ff8999 Mon Sep 17 00:00:00 2001 From: beat Date: Mon, 7 May 2012 09:17:00 +0200 Subject: [PATCH 3/5] Make authentication work --- src/api.js | 2 ++ src/core.js | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/api.js b/src/api.js index b3415c08e..0c4865841 100644 --- a/src/api.js +++ b/src/api.js @@ -9,6 +9,8 @@ * * @param {string|TypedAray} source Either a url to a PDF is located or a * typed array (Uint8Array) already populated with data. + * @param {Object} headers An object containing the http headers like this: + * { Authorization: "BASIC XXX" } * @return {Promise} A promise that is resolved with {PDFDocumentProxy} object. */ PDFJS.getDocument = function getDocument(source, headers) { diff --git a/src/core.js b/src/core.js index 908ba63af..e999550ca 100644 --- a/src/core.js +++ b/src/core.js @@ -32,11 +32,14 @@ function getPdf(arg, callback) { var xhr = new XMLHttpRequest(); - if(params.headers){ - //TODO: Code this, use xhr.setRequestHeader(key, value); - } - xhr.open('GET', params.url); + if(params.headers){ + for(var property in params.headers){ + if(typeof(params.headers[property]) !== undefined){ + xhr.setRequestHeader(property, params.headers[property]); + } + } + } xhr.mozResponseType = xhr.responseType = 'arraybuffer'; var protocol = params.url.indexOf(':') < 0 ? window.location.protocol : params.url.substring(0, params.url.indexOf(':') + 1); From e11cad884c1c0af012e422e26c8b45905548361a Mon Sep 17 00:00:00 2001 From: beat Date: Tue, 8 May 2012 14:17:51 +0200 Subject: [PATCH 4/5] code style fixes --- src/api.js | 4 ++-- src/core.js | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/api.js b/src/api.js index 0c4865841..1efb22caa 100644 --- a/src/api.js +++ b/src/api.js @@ -10,7 +10,7 @@ * @param {string|TypedAray} source Either a url to a PDF is located or a * typed array (Uint8Array) already populated with data. * @param {Object} headers An object containing the http headers like this: - * { Authorization: "BASIC XXX" } + * { Authorization: "BASIC XXX" }. * @return {Promise} A promise that is resolved with {PDFDocumentProxy} object. */ PDFJS.getDocument = function getDocument(source, headers) { @@ -32,7 +32,7 @@ PDFJS.getDocument = function getDocument(source, headers) { promise.reject('Unexpected server response of ' + e.target.status + '.'); }, - headers: headers + headers: headers }, function getPDFLoad(data) { transport.sendData(data); diff --git a/src/core.js b/src/core.js index e999550ca..c6d8b743c 100644 --- a/src/core.js +++ b/src/core.js @@ -33,13 +33,17 @@ function getPdf(arg, callback) { var xhr = new XMLHttpRequest(); xhr.open('GET', params.url); - if(params.headers){ - for(var property in params.headers){ - if(typeof(params.headers[property]) !== undefined){ - xhr.setRequestHeader(property, params.headers[property]); - } + + var headers = params.headers; + if (headers) { + for (var property in headers) { + if (typeof headers[property] === 'undefined') + continue; + + xhr.setRequestHeader(property, params.headers[property]); } } + xhr.mozResponseType = xhr.responseType = 'arraybuffer'; var protocol = params.url.indexOf(':') < 0 ? window.location.protocol : params.url.substring(0, params.url.indexOf(':') + 1); From e1146b64adf3d9408e8679d06136aab6db6978f8 Mon Sep 17 00:00:00 2001 From: beat Date: Tue, 8 May 2012 16:34:46 +0200 Subject: [PATCH 5/5] fix 4 lint errors --- src/core.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core.js b/src/core.js index c6d8b743c..99a8dd161 100644 --- a/src/core.js +++ b/src/core.js @@ -31,19 +31,19 @@ function getPdf(arg, callback) { params = { url: arg }; var xhr = new XMLHttpRequest(); - + xhr.open('GET', params.url); - + var headers = params.headers; if (headers) { for (var property in headers) { if (typeof headers[property] === 'undefined') continue; - + xhr.setRequestHeader(property, params.headers[property]); } } - + xhr.mozResponseType = xhr.responseType = 'arraybuffer'; var protocol = params.url.indexOf(':') < 0 ? window.location.protocol : params.url.substring(0, params.url.indexOf(':') + 1);