Skip to content

Commit

Permalink
Adds http support to node_stream logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mukulmishra18 committed Aug 4, 2017
1 parent 712de53 commit b148a29
Show file tree
Hide file tree
Showing 3 changed files with 309 additions and 115 deletions.
57 changes: 17 additions & 40 deletions src/display/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
*/

import {
assert, createPromiseCapability, globalScope, isInt, MissingPDFException,
assert, createPromiseCapability, globalScope, MissingPDFException,
UnexpectedResponseException
} from '../shared/util';
import { validateRangeRequestCapabilities } from './network_utils';

if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
throw new Error('Module "./network" shall not ' +
Expand Down Expand Up @@ -350,51 +351,27 @@ function PDFNetworkStreamFullRequestReader(manager, options) {
}

PDFNetworkStreamFullRequestReader.prototype = {
_validateRangeRequestCapabilities: function
PDFNetworkStreamFullRequestReader_validateRangeRequestCapabilities() {
getResponseHeader(name) {
let fullRequestXhrId = this._fullRequestId;
let fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId);

if (this._disableRange) {
return false;
}

var networkManager = this._manager;
if (!networkManager.isHttp) {
return false;
}
var fullRequestXhrId = this._fullRequestId;
var fullRequestXhr = networkManager.getRequestXhr(fullRequestXhrId);
if (fullRequestXhr.getResponseHeader('Accept-Ranges') !== 'bytes') {
return false;
}

var contentEncoding =
fullRequestXhr.getResponseHeader('Content-Encoding') || 'identity';
if (contentEncoding !== 'identity') {
return false;
}

var length = fullRequestXhr.getResponseHeader('Content-Length');
length = parseInt(length, 10);
if (!isInt(length)) {
return false;
}

this._contentLength = length; // setting right content length

if (length <= 2 * this._rangeChunkSize) {
// The file size is smaller than the size of two chunks, so it does
// not make any sense to abort the request and retry with a range
// request.
return false;
}

return true;
return fullRequestXhr.getResponseHeader(name);
},

_onHeadersReceived:
function PDFNetworkStreamFullRequestReader_onHeadersReceived() {
let { allowRangeRequests, suggestedLength, } =
validateRangeRequestCapabilities({
getResponseHeader: this.getResponseHeader.bind(this),
isHttp: this._manager.isHttp,
rangeChunkSize: this._rangeChunkSize,
disableRange: this._disableRange,
});

// Setting right content length.
this._contentLength = suggestedLength || this._contentLength;

if (this._validateRangeRequestCapabilities()) {
if (allowRangeRequests) {
this._isRangeSupported = true;
}

Expand Down
56 changes: 56 additions & 0 deletions src/display/network_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { isInt } from '../shared/util';

function validateRangeRequestCapabilities({ getResponseHeader, isHttp,
rangeChunkSize, disableRange, }) {
let returnValues = {
allowRangeRequests: false,
suggestedLength: undefined,
};
if (disableRange || !isHttp) {
return returnValues;
}
if (getResponseHeader('Accept-Ranges') !== 'bytes') {
return returnValues;
}

let contentEncoding = getResponseHeader('Content-Encoding') || 'identity';
if (contentEncoding !== 'identity') {
return returnValues;
}

let length = getResponseHeader('Content-Length');
length = parseInt(length, 10);
if (!isInt(length)) {
return returnValues;
}

returnValues.suggestedLength = length;
if (length <= 2 * rangeChunkSize) {
// The file size is smaller than the size of two chunks, so it does
// not make any sense to abort the request and retry with a range
// request.
return returnValues;
}

returnValues.allowRangeRequests = true;
return returnValues;
}

export {
validateRangeRequestCapabilities,
};
Loading

0 comments on commit b148a29

Please sign in to comment.