Skip to content

Commit aacd3bb

Browse files
committed
Fixed flickr
1 parent bb40572 commit aacd3bb

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ inherits(RandomAccessHTTP, events.EventEmitter)
3232

3333
RandomAccessHTTP.prototype.open = function (cb) {
3434
var self = this
35+
36+
this.keepAliveAgent = new this.client.Agent({ keepAlive: true })
3537
var reqOpts = xtend(this.urlObj, {
3638
method: 'HEAD',
37-
headers: {
38-
Connection: 'keep-alive'
39-
}
39+
agent: this.keepAliveAgent
4040
})
4141
var req = this.client.request(reqOpts, onres)
4242

@@ -76,14 +76,13 @@ RandomAccessHTTP.prototype.read = function (offset, length, cb) {
7676

7777
var self = this
7878

79-
var buf = Buffer(length)
80-
81-
if (!length) return cb(null, buf)
79+
var range = `${offset}-${offset + length - 1}`
8280
var reqOpts = xtend(this.urlObj, {
83-
method: 'HEAD',
81+
method: 'GET',
82+
agent: this.keepAliveAgent,
8483
headers: {
85-
Connection: 'keep-alive',
86-
Range: `bytes=${offset}-${offset + length}`
84+
Accept: '*/*',
85+
Range: `bytes=${range}`
8786
}
8887
})
8988

@@ -96,14 +95,14 @@ RandomAccessHTTP.prototype.read = function (offset, length, cb) {
9695
req.end()
9796

9897
function onres (res) {
99-
if (res.statusCode !== 206) return cb(new Error('Bad response: ' + res.statusCode))
10098
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}`
102101
if (res.headers['content-range'] !== expectedRange) return cb(new Error('Server returned unexpected range: ' + res.headers['content-range']))
103-
var limiter = limitStream(length)
104102
var concatStream = concat(onBuf)
103+
var limitter = limitStream(length + 1)
105104

106-
pump(res, limiter, concatStream, function (err) {
105+
pump(res, limitter, concatStream, function (err) {
107106
if (err) return cb(new Error(`problem while reading stream: ${err}`))
108107
})
109108
}
@@ -128,6 +127,7 @@ RandomAccessHTTP.prototype.read = function (offset, length, cb) {
128127

129128
RandomAccessHTTP.prototype.close = function (cb) {
130129
this.opened = false
130+
this.keepAliveAgent.destroy()
131131
this.emit('close')
132132
cb(null)
133133
}

test.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,51 @@ tape('open and close', function (t) {
2121
})
2222

2323
tape('read 10 bytes', function (t) {
24-
t.plan(2)
24+
t.plan(3)
2525
var popeye = rahttp(testUrl)
26-
popeye.read(0, 10, function (err) {
26+
var length = 10
27+
popeye.read(0, 10, function (err, buf) {
2728
t.error(err, 'url read without error')
29+
t.equal(buf.length, length)
30+
popeye.close(function (err) {
31+
t.error(err, 'url closed without error')
32+
})
33+
})
34+
})
35+
36+
tape('read 100 bytes at an offset of 2000', function (t) {
37+
t.plan(3)
38+
var popeye = rahttp(testUrl)
39+
var length = 100
40+
popeye.read(2000, length, function (err, buf) {
41+
t.error(err, 'url read without error')
42+
t.equal(buf.length, length)
43+
popeye.close(function (err) {
44+
t.error(err, 'url closed without error')
45+
})
46+
})
47+
})
48+
49+
tape('read from https flickr', function (t) {
50+
t.plan(3)
51+
var popeye = rahttp('https://c1.staticflickr.com/3/2892/12196828014_eb4ffac150_o.jpg')
52+
var length = 10
53+
popeye.read(0, 10, function (err, buf) {
54+
t.error(err, 'url read without error')
55+
t.equal(buf.length, length)
56+
popeye.close(function (err) {
57+
t.error(err, 'url closed without error')
58+
})
59+
})
60+
})
61+
62+
tape('read from http flickr', function (t) {
63+
t.plan(3)
64+
var popeye = rahttp('http://c1.staticflickr.com/3/2892/12196828014_eb4ffac150_o.jpg')
65+
var length = 10
66+
popeye.read(30, 10, function (err, buf) {
67+
t.error(err, 'url read without error')
68+
t.equal(buf.length, length)
2869
popeye.close(function (err) {
2970
t.error(err, 'url closed without error')
3071
})

0 commit comments

Comments
 (0)