Skip to content

Commit

Permalink
Merge pull request #388 from dadi/patch/receive-full-response
Browse files Browse the repository at this point in the history
Receive full response from HTTP GET request
  • Loading branch information
jimlambie committed Jun 22, 2018
2 parents 76e6a85 + 6c55c25 commit 99c4cba
Showing 1 changed file with 58 additions and 54 deletions.
112 changes: 58 additions & 54 deletions dadi/lib/storage/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
const config = require('./../../../config')
const http = require('http')
const https = require('https')
const PassThrough = require('stream').PassThrough
const path = require('path')
const streamifier = require('streamifier')
const url = require('url')
const urljoin = require('url-join')
const Missing = require(path.join(__dirname, '/missing'))
Expand Down Expand Up @@ -39,8 +39,6 @@ HTTPStorage.prototype.get = function ({
redirects = 0,
requestUrl = this.getFullUrl()
} = {}) {
let outputStream = PassThrough()

return new Promise((resolve, reject) => {
let parsedUrl = url.parse(requestUrl)
let requestFn = parsedUrl.protocol === 'https:'
Expand All @@ -56,72 +54,78 @@ HTTPStorage.prototype.get = function ({
'User-Agent': 'DADI CDN'
}
}, res => {
if (res.statusCode === 200) {
res.pipe(outputStream)
let statusCode = res.statusCode

return resolve(outputStream)
}
if (statusCode === 200) {
let buffers = []

let statusCode = res.statusCode
res.on('data', chunk => {
buffers.push(chunk)
})

if (
[301, 302, 307].includes(res.statusCode) &&
typeof res.headers.location === 'string'
) {
let parsedRedirectUrl = url.parse(res.headers.location)

parsedRedirectUrl.host = parsedRedirectUrl.host || parsedUrl.host
parsedRedirectUrl.port = parsedRedirectUrl.port || parsedUrl.port
parsedRedirectUrl.protocol = parsedRedirectUrl.protocol || parsedUrl.protocol

if (redirects < config.get('http.followRedirects', this.domain)) {
return resolve(
this.get({
redirects: redirects + 1,
requestUrl: url.format(parsedRedirectUrl)
})
)
res.on('end', () => {
return resolve(streamifier.createReadStream(Buffer.concat(buffers)))
})
} else {
if (
[301, 302, 307].includes(statusCode) &&
typeof res.headers.location === 'string'
) {
let parsedRedirectUrl = url.parse(res.headers.location)

parsedRedirectUrl.host = parsedRedirectUrl.host || parsedUrl.host
parsedRedirectUrl.port = parsedRedirectUrl.port || parsedUrl.port
parsedRedirectUrl.protocol = parsedRedirectUrl.protocol || parsedUrl.protocol

if (redirects < config.get('http.followRedirects', this.domain)) {
return resolve(
this.get({
redirects: redirects + 1,
requestUrl: url.format(parsedRedirectUrl)
})
)
}

// We've hit the maximum number of redirects allowed, so we'll
// treat this as a 404.
statusCode = 404
}

// We've hit the maximum number of redirects allowed, so we'll
// treat this as a 404.
statusCode = 404
}

let httpError
let httpError

switch (statusCode) {
case 404:
httpError = new Error(`Not Found: ${this.getFullUrl()}`)
switch (statusCode) {
case 404:
httpError = new Error(`Not Found: ${this.getFullUrl()}`)

break
break

case 403:
httpError = new Error(`Forbidden: ${this.getFullUrl()}`)
case 403:
httpError = new Error(`Forbidden: ${this.getFullUrl()}`)

break
break

default:
httpError = new Error(`Remote server responded with error code ${statusCode} for URL: ${this.getFullUrl()}`)
default:
httpError = new Error(`Remote server responded with error code ${statusCode} for URL: ${this.getFullUrl()}`)

break
}
break
}

httpError.statusCode = statusCode
httpError.statusCode = statusCode

if (statusCode === 404) {
new Missing().get({
domain: this.domain
}).then(stream => {
this.notFound = true
this.lastModified = new Date()
if (statusCode === 404) {
new Missing().get({
domain: this.domain
}).then(stream => {
this.notFound = true
this.lastModified = new Date()

resolve(stream)
}).catch(() => {
resolve(stream)
}).catch(() => {
reject(httpError)
})
} else {
reject(httpError)
})
} else {
reject(httpError)
}
}
})
})
Expand Down

0 comments on commit 99c4cba

Please sign in to comment.