@@ -32,11 +32,11 @@ inherits(RandomAccessHTTP, events.EventEmitter)
32
32
33
33
RandomAccessHTTP . prototype . open = function ( cb ) {
34
34
var self = this
35
+
36
+ this . keepAliveAgent = new this . client . Agent ( { keepAlive : true } )
35
37
var reqOpts = xtend ( this . urlObj , {
36
38
method : 'HEAD' ,
37
- headers : {
38
- Connection : 'keep-alive'
39
- }
39
+ agent : this . keepAliveAgent
40
40
} )
41
41
var req = this . client . request ( reqOpts , onres )
42
42
@@ -76,14 +76,13 @@ RandomAccessHTTP.prototype.read = function (offset, length, cb) {
76
76
77
77
var self = this
78
78
79
- var buf = Buffer ( length )
80
-
81
- if ( ! length ) return cb ( null , buf )
79
+ var range = `${ offset } -${ offset + length - 1 } `
82
80
var reqOpts = xtend ( this . urlObj , {
83
- method : 'HEAD' ,
81
+ method : 'GET' ,
82
+ agent : this . keepAliveAgent ,
84
83
headers : {
85
- Connection : 'keep-alive ' ,
86
- Range : `bytes=${ offset } - ${ offset + length } `
84
+ Accept : '*/* ' ,
85
+ Range : `bytes=${ range } `
87
86
}
88
87
} )
89
88
@@ -96,14 +95,14 @@ RandomAccessHTTP.prototype.read = function (offset, length, cb) {
96
95
req . end ( )
97
96
98
97
function onres ( res ) {
99
- if ( res . statusCode !== 206 ) return cb ( new Error ( 'Bad response: ' + res . statusCode ) )
100
98
if ( ! res . headers [ 'content-range' ] ) return cb ( new Error ( 'Server did not return a byte range' ) )
101
- var expectedRange = `bytes ${ offset } -${ offset + length } /${ self . length } `
99
+ if ( res . statusCode !== 206 ) return cb ( new Error ( 'Bad response: ' + res . statusCode ) )
100
+ var expectedRange = `bytes ${ range } /${ self . length } `
102
101
if ( res . headers [ 'content-range' ] !== expectedRange ) return cb ( new Error ( 'Server returned unexpected range: ' + res . headers [ 'content-range' ] ) )
103
- var limiter = limitStream ( length )
104
102
var concatStream = concat ( onBuf )
103
+ var limitter = limitStream ( length + 1 )
105
104
106
- pump ( res , limiter , concatStream , function ( err ) {
105
+ pump ( res , limitter , concatStream , function ( err ) {
107
106
if ( err ) return cb ( new Error ( `problem while reading stream: ${ err } ` ) )
108
107
} )
109
108
}
@@ -128,6 +127,7 @@ RandomAccessHTTP.prototype.read = function (offset, length, cb) {
128
127
129
128
RandomAccessHTTP . prototype . close = function ( cb ) {
130
129
this . opened = false
130
+ this . keepAliveAgent . destroy ( )
131
131
this . emit ( 'close' )
132
132
cb ( null )
133
133
}
0 commit comments