Skip to content

Commit f36ff76

Browse files
committed
Prep for hapijs#2840
1 parent 0603104 commit f36ff76

File tree

5 files changed

+14
-11
lines changed

5 files changed

+14
-11
lines changed

API.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,8 +2123,8 @@ following options:
21232123
- `cors` - the [Cross-Origin Resource Sharing](http://www.w3.org/TR/cors/) protocol allows
21242124
browsers to make cross-origin API calls. CORS is required by web applications running
21252125
inside a browser which are loaded from a different domain than the API server. CORS
2126-
headers are disabled by default. To enable, set `cors` to `true`, or to an object with
2127-
the following options:
2126+
headers are disabled by default (`false`). To enable, set `cors` to `true`, or to an object
2127+
with the following options:
21282128
- `origin` - a strings array of allowed origin servers ('Access-Control-Allow-Origin').
21292129
The array can contain any combination of fully qualified origins along with origin
21302130
strings containing a wildcard '*' character, or a single `'*'` origin string. Defaults

lib/connection.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ exports = module.exports = internals.Connection = function (server, options) {
7878
this.states = new Statehood.Definitions(this.settings.state);
7979
this.auth = new Auth(this);
8080
this._router = new Call.Router(this.settings.router);
81-
this._defaultRoutes();
8281
this._corsPaths = {};
82+
this._defaultRoutes();
8383

8484
this.plugins = {}; // Registered plugin APIs by plugin name
8585
this.app = {}; // Place for application-specific state without conflicts with hapi, should not be used by plugins
@@ -219,7 +219,7 @@ internals.Connection.prototype._stop = function (options, callback) {
219219

220220
internals.Connection.prototype._cors = function (method, path, plugin, options) {
221221

222-
if (method === 'options' ||
222+
if (['options', 'notfound', 'badrequest'].indexOf(method) !== -1 ||
223223
Hoek.deepEqual(this.settings.routes.cors, options)) {
224224

225225
return;
@@ -230,6 +230,10 @@ internals.Connection.prototype._cors = function (method, path, plugin, options)
230230
return;
231231
}
232232

233+
if (!options) {
234+
return;
235+
}
236+
233237
this._corsPaths[path] = Hoek.clone(options);
234238

235239
this._route({

lib/route.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,11 @@ exports = module.exports = internals.Route = function (options, connection, plug
153153

154154
// CORS
155155

156-
if (this.settings.cors) {
157-
this.settings.cors = Hoek.applyToDefaults(Defaults.cors, this.settings.cors);
158-
159-
var cors = this.settings.cors;
160-
this.connection._cors(this.method, this.path, this.plugin, cors);
156+
this.settings.cors = Hoek.applyToDefaults(Defaults.cors, this.settings.cors);
157+
var cors = this.settings.cors;
158+
this.connection._cors(this.method, this.path, this.plugin, cors);
161159

160+
if (cors) {
162161
cors._headers = cors.headers.concat(cors.additionalHeaders).join(',');
163162
cors._methods = cors.methods.concat(cors.additionalMethods).join(',');
164163
cors._exposedHeaders = cors.exposedHeaders.concat(cors.additionalExposedHeaders).join(',');

lib/schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ internals.routeBase = Joi.object({
8989
credentials: Joi.boolean(),
9090
override: Joi.boolean().allow('merge')
9191
})
92-
.allow(null, false, true),
92+
.allow(false, true),
9393
ext: Joi.object({
9494
onPreAuth: Joi.array().items(internals.event).single(),
9595
onPostAuth: Joi.array().items(internals.event).single(),

lib/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ internals.Server.prototype.connection = function (options) {
111111
var root = this.root; // Explicitly use the root reference (for plugin invocation)
112112

113113
var settings = Hoek.applyToDefaultsWithShallow(root._settings.connections, options || {}, ['listener', 'routes.bind']);
114-
settings.routes.cors = Hoek.applyToDefaults(root._settings.connections.routes.cors || Defaults.cors, settings.routes.cors);
114+
settings.routes.cors = Hoek.applyToDefaults(root._settings.connections.routes.cors || Defaults.cors, settings.routes.cors) || false;
115115
settings.routes.security = Hoek.applyToDefaults(root._settings.connections.routes.security || Defaults.security, settings.routes.security);
116116

117117
settings = Schema.apply('connection', settings); // Applies validation changes (type cast)

0 commit comments

Comments
 (0)