Permalink
Browse files

static handler fixes

  • Loading branch information...
dimsmol committed Feb 14, 2013
1 parent 4ba5292 commit 55fadfa629e9ea22ac1c5cad2429bf67589923ab
Showing with 12 additions and 11 deletions.
  1. +12 −11 lib/handlers/static.js
View
@@ -25,24 +25,24 @@ inherits(Static, HttpOnly);
Static.prototype.name = 'Static';
Static.prototype.handleHttpRequest = function (ctx) {
+ var path = this.extractPath(ctx);
var options = this.options;
var self = this;
-
- send(ctx.req, this.extractPath(ctx))
+ var sendStream = send(ctx.req, path)
.root(this.rootPath)
.hidden(options.hidden)
.maxage(options.maxAge)
.index(options.allowIndex && options.index)
.on('error', function (err) {
- self.onError(ctx, err);
+ self.onError(ctx, sendStream, err);
})
.on('directory', function () {
- self.onDir(ctx);
+ self.onDir(ctx, sendStream);
})
.on('end', function () {
- self.onEnd(ctx);
- })
- .pipe(ctx.res);
+ self.onEnd(ctx, sendStream);
+ });
+ sendStream.pipe(ctx.res);
};
Static.prototype.extractPath = function (ctx) {
@@ -58,7 +58,7 @@ Static.prototype.extractPath = function (ctx) {
return result;
};
-Static.prototype.onError = function (ctx, err) {
+Static.prototype.onError = function (ctx, sendStream, err) {
var status = err.status;
if (status != null && status >= 400 && status < 500) {
if ([400, 403, 404].indexOf(status) != -1) {
@@ -73,9 +73,10 @@ Static.prototype.onError = function (ctx, err) {
this.error(ctx, err);
};
-Static.prototype.onDir = function (ctx) {
+Static.prototype.onDir = function (ctx, sendStream) {
var res = ctx.res;
- if (this.options.redirect) {
+ var path = ctx.res.path;
+ if (this.options.redirect && !(path && path[path.length - 1] == '/')) {
res.redirect(ctx.req.path + '/', 301);
ctx.responseSent();
ctx.next();
@@ -85,7 +86,7 @@ Static.prototype.onDir = function (ctx) {
}
};
-Static.prototype.onEnd = function (ctx) {
+Static.prototype.onEnd = function (ctx, sendStream) {
ctx.res.end();
ctx.responseSent();
ctx.next();

0 comments on commit 55fadfa

Please sign in to comment.