Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

When route is not '/', req.url that is not trailing '/' causes incorrect response. #445

Closed
wants to merge 1 commit into from

5 participants

@luuvish

server = connect.createServer();
server.use('/any-path', connect.static(__dirname + '/public'));

When req.url is 'http://any-domain/any-path',
static middleware will response with 'http://any-domain/any-path/index.html'.

But the request of web browser is '/any-path', not '/any-path/'. the browser mis-guesses the path of request is '/', not '/any-path/'.

@luuvish luuvish In case of routes is not '/', the request URL that is not trailing '/…
…' causes mis-response by static middleware.

the URL that is directory without trailing '/' must not be added to '/index.html', must be redirect to correct path.
d2fbbc2
@valette

+1 for this old issue

One strange thing is that http://any-domain/any-path/subdir is redirected to http://any-domain/any-path/subdir/

but http://any-domain/any-path is not

I tried the proposed patch, and it seems to work. Are there any undesirable side-effects?

Thanks

@valette

bumping this issue with a self-contained example:

var fs      = require('fs'),
    connect = require('connect');

var app=connect();
var dataDir='data';
var subFolder=dataDir+'/subfolder';

if (!fs.existsSync(dataDir)) {
    fs.mkdirSync(dataDir);
    if (!fs.existsSync(subFolder)) {
        fs.mkdirSync(subFolder);
    }
}

var htmlFile='<html><head><script src="subfolder/canvas.js"></script></head><body><canvas name="canvas" width="500" height="400"></canvas></body></html>';
fs.writeFileSync(dataDir+'/index.html', htmlFile);

var jsFile='function draw() {var canvas = document.getElementsByName("canvas")[0];var ctx = canvas.getContext("2d");ctx.fillStyle = "rgb(0,500,0)";ctx.fillRect (50, 50, 200, 200);}window.onload=draw';
fs.writeFileSync(subFolder+'/canvas.js', jsFile);

// serve data files. this one does not work correctly
app.use('/test', connect.static(dataDir));
app.listen(8080);

accessing to
http://localhost:8080/test/
shows a green rectangle, but accessing
http://localhost:8080/test
does not work the same way.

@tj tj was assigned
@dougwilson
Owner

@jonathanong this was fixed long ago in the static middleware. This can be closed.

@valette

Sorry, but I just tried again the minimal test case provided above, and I still have the same behavior. Am I missing something here?

@dougwilson
Owner

@valette I didn't understand what the OP was really talking about, but I re-read your comment below that and I believe I know what you are saying now. I'll check it out and whip up a patch (this patch fixes it in the wrong place, because having a trailing / is actually meant to be optional for routing, though I can see whanting it for the static middleware). @jonathanong I suppose reopen this?

@jonathanong jonathanong reopened this
@jonathanong jonathanong closed this in #937
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 22, 2011
  1. @luuvish

    In case of routes is not '/', the request URL that is not trailing '/…

    luuvish authored
    …' causes mis-response by static middleware.
    
    the URL that is directory without trailing '/' must not be added to '/index.html', must be redirect to correct path.
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 1 deletion.
  1. +9 −1 lib/proto.js
View
10 lib/proto.js
@@ -1,4 +1,3 @@
-
/*!
* Connect - HTTPServer
* Copyright(c) 2010 Sencha Inc.
@@ -156,6 +155,15 @@ app.handle = function(req, res, out) {
c = path[layer.route.length];
if (c && '/' != c && '.' != c) return next(err);
+ // In case of req.url is not trailing '/' but is directory,
+ // Must be redirect to directory followed by '/', instead of adding '/index.html'.
+ if (path == layer.route) {
+ res.statusCode = 301;
+ res.setHeader('Location', layer.route + '/');
+ res.end('Redirecting to ' + layer.route + '/');
+ return;
+ }
+
// Call the layer handler
// Trim off the part of the url that matches the route
removed = layer.route;
Something went wrong with that request. Please try again.