Skip to content

Commit 66730f9

Browse files
committed
Route isInternal flag. Closes hapijs#2750
1 parent 154ae23 commit 66730f9

File tree

6 files changed

+51
-6
lines changed

6 files changed

+51
-6
lines changed

API.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 9.0.x API Reference
1+
# 9.1.x API Reference
22

33
- [Server](#server)
44
- [`new Server([options])`](#new-serveroptions)
@@ -2043,7 +2043,11 @@ following options:
20432043
- `handler` - an alternative location for the route `handler` option.
20442044

20452045
- `id` - an optional unique identifier used to look up the route using
2046-
[`server.lookup()`](#serverlookupid).
2046+
[`server.lookup()`](#serverlookupid). Cannot be assigned to routes with an array of methods.
2047+
2048+
- `isInternal` - if `true`, the route cannot be accessed through the HTTP connection but only
2049+
through the `server.inject()` interface. Used for internal routes that should not be accessible
2050+
to the outside world. Defaults to `false`.
20472051

20482052
- `json` - optional arguments passed to `JSON.stringify()` when converting an object or
20492053
error response to a string payload. Supports the following:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Lead Maintainer: [Eran Hammer](https://github.com/hueniverse)
1313
authentication, and other essential facilities for building web and services applications. **hapi** enables
1414
developers to focus on writing reusable application logic in a highly modular and prescriptive approach.
1515

16-
Development version: **9.0.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed))
16+
Development version: **9.1.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed))
1717
[![Build Status](https://secure.travis-ci.org/hapijs/hapi.svg)](http://travis-ci.org/hapijs/hapi)
1818

1919
For the latest updates, [change log](http://hapijs.com/updates), and release information visit [hapijs.com](http://hapijs.com) and follow [@hapijs](https://twitter.com/hapijs) on twitter. If you have questions, please open an issue in the

lib/request.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Boom = require('boom');
77
var Hoek = require('hoek');
88
var Items = require('items');
99
var Peekaboo = require('peekaboo');
10+
var Shot = require('shot');
1011
var Qs = require('qs');
1112
var Handler = require('./handler');
1213
var Protect = require('./protect');
@@ -326,8 +327,13 @@ internals.Request.prototype._execute = function () {
326327
// Lookup route
327328

328329
var match = self.connection._router.route(self.method, self.path, self.info.hostname);
329-
self._route = match.route;
330-
self.route = self._route.public;
330+
if (!match.route.settings.isInternal ||
331+
Shot.isInjection(self.raw.req)) {
332+
333+
self._route = match.route;
334+
self.route = self._route.public;
335+
}
336+
331337
self.params = match.params;
332338
self.paramsArray = match.paramsArray;
333339

lib/schema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ internals.pre = [
238238

239239
internals.routeConfig = internals.routeBase.keys({
240240
id: Joi.string(),
241+
isInternal: Joi.boolean(),
241242
pre: Joi.array().items(internals.pre.concat(Joi.array().items(internals.pre).min(1))),
242243
handler: [
243244
Joi.func(),

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "hapi",
33
"description": "HTTP Server framework",
44
"homepage": "http://hapijs.com",
5-
"version": "9.0.4",
5+
"version": "9.1.0",
66
"repository": {
77
"type": "git",
88
"url": "git://github.com/hapijs/hapi"

test/request.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,40 @@ describe('Request', function () {
372372
clientRequest.end();
373373
});
374374
});
375+
376+
it('returns not found on internal only route', function (done) {
377+
378+
var server = new Hapi.Server();
379+
server.connection();
380+
server.route({
381+
method: 'GET',
382+
path: '/some/route',
383+
config: {
384+
isInternal: true,
385+
handler: function (request, reply) {
386+
387+
return reply('ok');
388+
}
389+
}
390+
});
391+
392+
server.start(function (err) {
393+
394+
expect(err).to.not.exist();
395+
Wreck.get('http://localhost:' + server.info.port, function (err, res1, body) {
396+
397+
expect(res1.statusCode).to.equal(404);
398+
expect(body.toString()).to.equal('{"statusCode":404,"error":"Not Found"}');
399+
400+
server.inject('/some/route', function (res2) {
401+
402+
expect(res2.statusCode).to.equal(200);
403+
expect(res2.result).to.equal('ok');
404+
server.stop(done);
405+
});
406+
});
407+
});
408+
});
375409
});
376410

377411
describe('_finalize()', function (done) {

0 commit comments

Comments
 (0)