Skip to content

Commit

Permalink
fix: Http errors that were crashing the server
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasdao committed Aug 10, 2017
1 parent 9d97880 commit 0d0015a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 50 deletions.
35 changes: 20 additions & 15 deletions src/webfunc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
const path = require('path')
const fs = require('fs')
const httpError = require('http-errors')
const functions = require('firebase-functions')

let _appconfig = null
Expand Down Expand Up @@ -84,26 +83,30 @@ const handleHttpRequest = (req, res, appconfig) => Promise.resolve(appconfig ||

if (noConfig) {
if (!sameOrigin) {
return setResponseHeaders(res, appConfig).then(() => {
throw httpError(403, `Forbidden - CORS issue. Origin '${origin}' is not allowed.`)
return setResponseHeaders(res, appConfig).then(res => {
res.status(403).send(`Forbidden - CORS issue. Origin '${origin}' is not allowed.`)
return res
})
}
if (method != 'head' && method != 'get' && method != 'options' && method != 'post') {
return setResponseHeaders(res, appConfig).then(() => {
throw httpError(403, `Forbidden - CORS issue. Method '${method.toUpperCase()}' is not allowed.`)
return setResponseHeaders(res, appConfig).then(res => {
res.status(403).send(`Forbidden - CORS issue. Method '${method.toUpperCase()}' is not allowed.`)
return res
})
}
}
// Check CORS

if (!origins['*'] && Object.keys(origins).length != 0 && !(origin in origins)) {
return setResponseHeaders(res, appConfig).then(() => {
throw httpError(403, `Forbidden - CORS issue. Origin '${origin}' is not allowed.`)
return setResponseHeaders(res, appConfig).then(res => {
res.status(403).send(`Forbidden - CORS issue. Origin '${origin}' is not allowed.`)
return res
})
}
if (Object.keys(methods).length != 0 && method != 'get' && method != 'head' && !(method in methods)) {
return setResponseHeaders(res, appConfig).then(() => {
throw httpError(403, `Forbidden - CORS issue. Method '${method.toUpperCase()}' is not allowed.`)
return setResponseHeaders(res, appConfig).then(res => {
res.status(403).send(`Forbidden - CORS issue. Method '${method.toUpperCase()}' is not allowed.`)
return res
})
}

Expand Down Expand Up @@ -157,10 +160,12 @@ const serveHttp = (arg1, arg2, appconfig) => {
if (route) {
const httpEndpoint = ((req._parsedUrl || {}).pathname || '/').toLowerCase()
const r = matchRoute(httpEndpoint, route)
if (!r)
return setResponseHeaders(res, _appconfig).then(() => {
throw httpError(404, `Endpoint '${httpEndpoint}' not found.`)
if (!r) {
return setResponseHeaders(res, _appconfig).then(res => {
res.status(404).send(`Endpoint '${httpEndpoint}' not found.`)
return res
})
}
else
parameters = r.parameters
}
Expand Down Expand Up @@ -249,13 +254,13 @@ const getRequestParameters = req => {
* @return {function} (req, res) => ...
*/
const serveHttpEndpoints = (endpoints, appconfig) => {
if (!endpoints || !endpoints.length)
throw new Error('No endpoints have been defined.')

const _appconfig = Object.assign(getAppConfig() || {}, appconfig || {})
const cloudFunction = (req, res) => handleHttpRequest(req, res, _appconfig)
.then(() => !res.headersSent
? setResponseHeaders(res, _appconfig).then(res => {
if (!endpoints || !endpoints.length)
throw httpError(500, 'No endpoints have been defined.')

const httpEndpoint = ((req._parsedUrl || {}).pathname || '/').toLowerCase()
const httpMethod = req.method
const endpoint = httpEndpoint == '/'
Expand Down
63 changes: 28 additions & 35 deletions test/webfunc.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ describe('webfunc', () =>
})
const res = httpMocks.createResponse()
const appconfig = {}
return handleHttpRequest(req, res, appconfig).then(() => {
assert.equal(1,2)
}).catch(err => {
assert.equal(err.message,'Forbidden - CORS issue. Origin \'http://localhost:8080\' is not allowed.')
return handleHttpRequest(req, res, appconfig).then(res => {
assert.equal(res.statusCode, 403)
assert.equal(res._getData(), 'Forbidden - CORS issue. Origin \'http://localhost:8080\' is not allowed.')
})
})))

Expand Down Expand Up @@ -147,10 +146,9 @@ describe('webfunc', () =>
'Access-Control-Max-Age': '1296000'
}
}
return handleHttpRequest(req, res, appconfig).then(() => {
assert.equal(1,2)
}).catch(err => {
assert.equal(err.message,'Forbidden - CORS issue. Origin \'http://localhost:8080\' is not allowed.')
return handleHttpRequest(req, res, appconfig).then(res => {
assert.equal(res.statusCode, 403)
assert.equal(res._getData(), 'Forbidden - CORS issue. Origin \'http://localhost:8080\' is not allowed.')
})
})))

Expand Down Expand Up @@ -201,10 +199,9 @@ describe('webfunc', () =>
'Access-Control-Max-Age': '1296000'
}
}
return handleHttpRequest(req, res, appconfig).then(() => {
assert.equal(1,2)
}).catch((err) => {
assert.equal(err.message, 'Forbidden - CORS issue. Method \'POST\' is not allowed.')
return handleHttpRequest(req, res, appconfig).then(res => {
assert.equal(res.statusCode, 403)
assert.equal(res._getData(), 'Forbidden - CORS issue. Method \'POST\' is not allowed.')
})
})))

Expand Down Expand Up @@ -248,10 +245,10 @@ describe('webfunc', () =>
})
const res = httpMocks.createResponse()
const appconfig = {}
return handleHttpRequest(req, res, appconfig).then(() => {
assert.equal(1,2)
return handleHttpRequest(req, res, appconfig).then(res => {
assert.equal(res.statusCode, 403)
assert.equal(res._getData(), 'Forbidden - CORS issue. Method \'PUT\' is not allowed.')
})
.catch(err => assert.equal(err.message, 'Forbidden - CORS issue. Method \'PUT\' is not allowed.'))
})))

/*eslint-disable */
Expand Down Expand Up @@ -295,12 +292,10 @@ describe('webfunc', () =>
res.status(200).send('Hello World')
return res
}, appconfig)
return fn(req, res)
.then(() => {
assert.equal(1,2)
}).catch(err => {
assert.equal(err.message,'Forbidden - CORS issue. Origin \'http://localhost:8080\' is not allowed.')
})
return fn(req, res).then(res => {
assert.equal(res.statusCode, 403)
assert.equal(res._getData(), 'Forbidden - CORS issue. Origin \'http://localhost:8080\' is not allowed.')
})
})))

/*eslint-disable */
Expand Down Expand Up @@ -388,10 +383,9 @@ describe('webfunc', () =>
res.status(200).send('Hello World')
return res
}, appconfig)
return fn(req, res).then(() => {
assert.equal(1,2)
}).catch(err => {
assert.equal(err.message,'Forbidden - CORS issue. Origin \'http://localhost:8080\' is not allowed.')
return fn(req, res).then(res => {
assert.equal(res.statusCode, 403)
assert.equal(res._getData(), 'Forbidden - CORS issue. Origin \'http://localhost:8080\' is not allowed.')
})
})))

Expand Down Expand Up @@ -450,10 +444,9 @@ describe('webfunc', () =>
res.status(200).send('Hello World')
return res
}, appconfig)
return fn(req, res).then(() => {
assert.equal(1,2)
}).catch((err) => {
assert.equal(err.message, 'Forbidden - CORS issue. Method \'POST\' is not allowed.')
return fn(req, res).then(res => {
assert.equal(res.statusCode, 403)
assert.equal(res._getData(), 'Forbidden - CORS issue. Method \'POST\' is not allowed.')
})
})))

Expand Down Expand Up @@ -505,10 +498,10 @@ describe('webfunc', () =>
res.status(200).send('Hello World')
return res
}, appconfig)
return fn(req, res).then(() => {
assert.equal(1,2)
return fn(req, res).then(res => {
assert.equal(res.statusCode, 403)
assert.equal(res._getData(), 'Forbidden - CORS issue. Method \'PUT\' is not allowed.')
})
.catch(err => assert.equal(err.message, 'Forbidden - CORS issue. Method \'PUT\' is not allowed.'))
})))

/*eslint-disable */
Expand Down Expand Up @@ -726,10 +719,10 @@ describe('webfunc', () =>
assert.equal(headers['Access-Control-Allow-Origin'], 'http://boris.com, http://localhost:8080')
assert.equal(headers['Access-Control-Max-Age'], '1296000')
})
const result_03 = fn(req_03, res_03).then(() => {
assert.equal(1,2, 'Requests with the wrong route should failed with a \'not found\' error.')
const result_03 = fn(req_03, res_03).then(res => {
assert.equal(res.statusCode, 404)
assert.equal(res._getData(), 'Endpoint \'/\' not found.')
})
.catch(err => assert.equal(err.message, 'Endpoint \'/\' not found.'))

return Promise.all([result_01, result_02, result_03])
})))
Expand Down

0 comments on commit 0d0015a

Please sign in to comment.