Skip to content

Commit

Permalink
refactor: remove unnecessary else statement
Browse files Browse the repository at this point in the history
  • Loading branch information
jimlambie committed Nov 21, 2018
1 parent 9b534f2 commit fc511f6
Showing 1 changed file with 54 additions and 49 deletions.
103 changes: 54 additions & 49 deletions dadi/lib/storage/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,64 +63,69 @@ HTTPStorage.prototype.get = function ({
res.on('end', () => {
let statusCode = res.statusCode

// Successful response, return the data
if (statusCode === 200) {
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
}

// Determine what to do with the response when not successful. If it's
// a redirect status code, we continue trying to get it until we reach the
// configured redirect limit.
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)
})
)
}

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

switch (statusCode) {
case 404:
httpError = new Error(`Not Found: ${this.getFullUrl()}`)
// It's not a redirect, determine what to return
let httpError

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

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

httpError.statusCode = statusCode

if (statusCode === 404) {
new Missing().get({
domain: this.domain,
isDirectory: path.parse(this.getFullUrl()).ext === ''
}).then(stream => {
this.notFound = true
this.lastModified = new Date()
resolve(stream)
}).catch(() => {
reject(httpError)
})
} else {
break
default:
httpError = new Error(`Remote server responded with error code ${statusCode} for URL: ${this.getFullUrl()}`)
}

httpError.statusCode = statusCode

if (statusCode === 404) {
new Missing().get({
domain: this.domain,
isDirectory: path.parse(this.getFullUrl()).ext === ''
}).then(stream => {
this.notFound = true
this.lastModified = new Date()
resolve(stream)
}).catch(() => {
reject(httpError)
}
})
} else {
reject(httpError)
}
})
}).on('error', (err) => {
Expand Down

0 comments on commit fc511f6

Please sign in to comment.