Skip to content

http.Handle("/", ...) effectively hijacks pages from being not found (due to trailing "/" in the "pattern") #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gopherbot opened this issue Nov 16, 2009 · 9 comments

Comments

@gopherbot
Copy link
Contributor

by montsamu:

Before filing a bug, please check whether it has been fixed since
the latest release: run "hg pull -u" and retry what you did to
reproduce the problem.  Thanks.

What steps will reproduce the problem?
1. http.Handle("/", SomeFunc);
2. visit your web server "/notfound"

What is the expected output? What do you see instead?

Expected output is a 404 page; instead the SomeFunc result behaviour occurs 
because "/" is treated as any other pattern instead of allowing an exact 
match only.

What is your $GOOS?  $GOARCH?

sam@sol:~/go$ set | grep GO
GOARCH=amd64
GOOS=linux
GOROOT=/home/sam/go

Which revision are you sync'ed to?  (hg log -l 1)

sam@sol:~/go$ hg log -l 1
changeset:   4068:57c2810ca63f
tag:         tip
user:        Robert Griesemer <[email protected]>
date:        Mon Nov 16 08:58:55 2009 -0800
summary:     - Clarify that struct composite literal keys are field names 
not selectors.

Please provide any additional information below.

I am requesting a means of specifying an exact pattern only in a ServeMux, 
specifically to handle the the case of "/" instead of the default pattern 
behavior for patterns ending in a slash. There may be such a behavior but 
it is not documented at: http://golang.org/pkg/http/#tmp_272

(Some examples might be: allow "/$" to mean an exact path match only 
instead of the default sub-pattern handling.)
@gopherbot
Copy link
Contributor Author

Comment 1 by montsamu:

Alternatively or better, perhaps another interface into the Mux system using lower-
level regex calls (if that is what is underneath the default Mux "pattern language").
Ex: http.HandleRegex("/$", SomeFunc)
Or have it be type specific and just:
http.Handle(SomeRegexObject, SomeFunc)

@griesemer
Copy link
Contributor

Comment 2:

Owner changed to [email protected].

@rsc
Copy link
Contributor

rsc commented Nov 17, 2009

Comment 3:

I have to think more about this.  I like the current
not-pattern-based approach.  Regexps are overkill
for this kind of thing and too easy to get wrong.
A workaround is for the handler to check for just "/":
    if r.URL.Path != "/" {
        http.NotFound(c, r);
        return;
    }

Status changed to Thinking.

@gopherbot
Copy link
Contributor Author

Comment 4 by montsamu:

Agree on the workaround being simple; and that regexps can be overkill. However now 
you've also introduced another "URL matching language" instead of perhaps using one 
already familiar.
I would suggest:
http.Handle(string, SomeHandler) to be the exact string only (or if you must the "URL 
pattern language" which is currently used, though I do think the common case is for 
exact string) and:
http.HandleRegexp(SomeRegexp, SomeHandler) when something else is required.
(Apologies for silly suggestion of method overloading, I had not fully read the Go 
specification to understand that this is not done in Go at the time of the issue 
being opened.)

@rsc
Copy link
Contributor

rsc commented Nov 17, 2009

Comment 5:

The URL matching language is not new.  It is file+directory names,
which are about as old as regular expressions.
Like I said, I'm thinking about it.

@gopherbot
Copy link
Contributor Author

Comment 6 by montsamu:

It's file and directory names, with the important addition that directory names (ending 
with "/") become catch-all for any longer paths.
Thanks very much for the consideration.

@rsc
Copy link
Contributor

rsc commented Dec 2, 2009

Comment 7:

Labels changed: added packagechange.

@gopherbot
Copy link
Contributor Author

Comment 8 by tdhutt:

"directory names (ending with "/") become catch-all for any longer paths."
I thought it was designed to be like that, so you can easily implement something like:
http://traintimes.org.uk/london/cambridge

@rsc
Copy link
Contributor

rsc commented Jan 18, 2010

Comment 9:

I've thought more about this and don't think it warrants the
complexity of adding another function to the interface.
The workaround in comment #8 is straightforward and simple.

Status changed to WorkingAsIntended.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants