FAQ
I am having a problem when I have a url that is redirected. This only
happens when
a "mux" is used. In my program:

       mux := mux.NewRouter()
          ...
         mux.HandleFunc("/api/status", respHandlerStatusGet).Methods("GET")

When I access the handler with http://localhost/api/status?abc=def it
works just fine. That is good.
When I have a bad URL sent in like "http://localhost//api/status?abc=def"
(note the // after the localhost)
this results in a 301 permanent redirect. That is good and bad. The
redirect is just "http://localhost/api/status".
The redirect code drops the query string completely. The browser (chrome
for example) will then act
appropriately and make a 2nd call to the server. The 2nd call to the
server is missing "?abc=def" query
string. Ouch!

It appears to me that the problem (version 1.1.2 of go) is near lines 1377
to 1287 of net/http/server.go

     func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
if r.Method != "CONNECT" {
if p := cleanPath(r.URL.Path); p != r.URL.Path {
_, pattern = mux.handler(r.Host, p)
return RedirectHandler(p, StatusMovedPermanently), pattern
}
}

return mux.handler(r.Host, r.URL.Path)
    }

The "return RedirectHandler(p, StatusMovedPermanently), pattern" should
realy be
"return RedirectHandler(p+"?"+queryString, StatusMovedPermanently), pattern"
where queryString := r.RequestURI.

From what I can tell it is not possible to catch the problem with a custom
RedirectHandler
because you will not have the query string by the time it is called.

Also it appears to me that the redirect will loose all of the headers that
came in with the
original query.

Is there some other way to catch the 301 and put in a handler that will
preserve the
Query String?

It appears to me that the regular (non-mux) server will do a 301 redirect
with the
query string intact. Which behavior is the correct one? My Nginx server
is configured
to do the 301 redirects with the query string preserved.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Alex Skinner at Nov 14, 2013 at 9:20 pm
    I believe this has been fixed in 1.2. Can you read over
    http://code.google.com/p/go/issues/detail?id=5252, and the codereview link,
    to see if that solves/describes your problem? That, or just test 1.2rc4.

    Thanks,
    Alex
    On Thursday, November 14, 2013 2:24:09 PM UTC-5, Philip Schlump wrote:

    I am having a problem when I have a url that is redirected. This only
    happens when
    a "mux" is used. In my program:

    mux := mux.NewRouter()
    ...
    mux.HandleFunc("/api/status", respHandlerStatusGet).Methods("GET")

    When I access the handler with http://localhost/api/status?abc=def it
    works just fine. That is good.
    When I have a bad URL sent in like "http://localhost//api/status?abc=def"
    (note the // after the localhost)
    this results in a 301 permanent redirect. That is good and bad. The
    redirect is just "http://localhost/api/status".
    The redirect code drops the query string completely. The browser (chrome
    for example) will then act
    appropriately and make a 2nd call to the server. The 2nd call to the
    server is missing "?abc=def" query
    string. Ouch!

    It appears to me that the problem (version 1.1.2 of go) is near lines 1377
    to 1287 of net/http/server.go

    func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
    if r.Method != "CONNECT" {
    if p := cleanPath(r.URL.Path); p != r.URL.Path {
    _, pattern = mux.handler(r.Host, p)
    return RedirectHandler(p, StatusMovedPermanently), pattern
    }
    }

    return mux.handler(r.Host, r.URL.Path)
    }

    The "return RedirectHandler(p, StatusMovedPermanently), pattern" should
    realy be
    "return RedirectHandler(p+"?"+queryString, StatusMovedPermanently),
    pattern"
    where queryString := r.RequestURI.

    From what I can tell it is not possible to catch the problem with a custom
    RedirectHandler
    because you will not have the query string by the time it is called.

    Also it appears to me that the redirect will loose all of the headers that
    came in with the
    original query.

    Is there some other way to catch the 301 and put in a handler that will
    preserve the
    Query String?

    It appears to me that the regular (non-mux) server will do a 301 redirect
    with the
    query string intact. Which behavior is the correct one? My Nginx server
    is configured
    to do the 301 redirects with the query string preserved.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Matt Silverlock at Nov 14, 2013 at 11:55 pm
    1.2rc4 works as "expected" for me: the extra / is dropped and the query string remains. Using gorilla/mux (of course).

    HTH.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Philip Schlump at Nov 15, 2013 at 2:33 am
    I have a "git" fork of the gorilla mux with this problem fixed. I am
    attempting to figure out how to make a "Pull Request" to include the change.
    The change to fix Goilla MUX is in mux.go. My fork is
    https://github.com/pschlump/mux.git - Look at lines 70 to 77 in mux.go.
    On Thursday, November 14, 2013 4:55:14 PM UTC-7, Matt Silverlock wrote:

    1.2rc4 works as "expected" for me: the extra / is dropped and the query
    string remains. Using gorilla/mux (of course).

    HTH.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Philip Schlump at Nov 14, 2013 at 11:57 pm
    I can confirm this is fixed in 1.2rc4 - this makes me very happy! I
    installed 1.2rc4 and tested it.
    On Thursday, November 14, 2013 2:20:36 PM UTC-7, Alex Skinner wrote:

    I believe this has been fixed in 1.2. Can you read over
    http://code.google.com/p/go/issues/detail?id=5252, and the codereview
    link, to see if that solves/describes your problem? That, or just test
    1.2rc4.

    Thanks,
    Alex
    On Thursday, November 14, 2013 2:24:09 PM UTC-5, Philip Schlump wrote:

    I am having a problem when I have a url that is redirected. This only
    happens when
    a "mux" is used. In my program:

    mux := mux.NewRouter()
    ...
    mux.HandleFunc("/api/status", respHandlerStatusGet).Methods("GET")

    When I access the handler with http://localhost/api/status?abc=def it
    works just fine. That is good.
    When I have a bad URL sent in like "http://localhost//api/status?abc=def"
    (note the // after the localhost)
    this results in a 301 permanent redirect. That is good and bad. The
    redirect is just "http://localhost/api/status".
    The redirect code drops the query string completely. The browser
    (chrome for example) will then act
    appropriately and make a 2nd call to the server. The 2nd call to the
    server is missing "?abc=def" query
    string. Ouch!

    It appears to me that the problem (version 1.1.2 of go) is near lines
    1377 to 1287 of net/http/server.go

    func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
    if r.Method != "CONNECT" {
    if p := cleanPath(r.URL.Path); p != r.URL.Path {
    _, pattern = mux.handler(r.Host, p)
    return RedirectHandler(p, StatusMovedPermanently), pattern
    }
    }

    return mux.handler(r.Host, r.URL.Path)
    }

    The "return RedirectHandler(p, StatusMovedPermanently), pattern" should
    realy be
    "return RedirectHandler(p+"?"+queryString, StatusMovedPermanently),
    pattern"
    where queryString := r.RequestURI.

    From what I can tell it is not possible to catch the problem with a
    custom RedirectHandler
    because you will not have the query string by the time it is called.

    Also it appears to me that the redirect will loose all of the headers
    that came in with the
    original query.

    Is there some other way to catch the 301 and put in a handler that will
    preserve the
    Query String?

    It appears to me that the regular (non-mux) server will do a 301 redirect
    with the
    query string intact. Which behavior is the correct one? My Nginx
    server is configured
    to do the 301 redirects with the query string preserved.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Philip Schlump at Nov 15, 2013 at 2:32 am
    I have a "git" fork of the gorilla mux with this problem fixed. I am
    attempting to figure out how to make a "Pull Request" to include the change.
    The change to fix Goilla MUX is in mux.go. My fork
    is https://github.com/pschlump/mux.git - Look at lines 70 to 77 in mux.go.
    On Thursday, November 14, 2013 4:57:29 PM UTC-7, Philip Schlump wrote:

    I can confirm this is fixed in 1.2rc4 - this makes me very happy! I
    installed 1.2rc4 and tested it.
    On Thursday, November 14, 2013 2:20:36 PM UTC-7, Alex Skinner wrote:

    I believe this has been fixed in 1.2. Can you read over
    http://code.google.com/p/go/issues/detail?id=5252, and the codereview
    link, to see if that solves/describes your problem? That, or just test
    1.2rc4.

    Thanks,
    Alex
    On Thursday, November 14, 2013 2:24:09 PM UTC-5, Philip Schlump wrote:

    I am having a problem when I have a url that is redirected. This only
    happens when
    a "mux" is used. In my program:

    mux := mux.NewRouter()
    ...
    mux.HandleFunc("/api/status",
    respHandlerStatusGet).Methods("GET")

    When I access the handler with http://localhost/api/status?abc=def it
    works just fine. That is good.
    When I have a bad URL sent in like "http://localhost//api/status?abc=def"
    (note the // after the localhost)
    this results in a 301 permanent redirect. That is good and bad. The
    redirect is just "http://localhost/api/status".
    The redirect code drops the query string completely. The browser
    (chrome for example) will then act
    appropriately and make a 2nd call to the server. The 2nd call to the
    server is missing "?abc=def" query
    string. Ouch!

    It appears to me that the problem (version 1.1.2 of go) is near lines
    1377 to 1287 of net/http/server.go

    func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string)
    {
    if r.Method != "CONNECT" {
    if p := cleanPath(r.URL.Path); p != r.URL.Path {
    _, pattern = mux.handler(r.Host, p)
    return RedirectHandler(p, StatusMovedPermanently), pattern
    }
    }

    return mux.handler(r.Host, r.URL.Path)
    }

    The "return RedirectHandler(p, StatusMovedPermanently), pattern" should
    realy be
    "return RedirectHandler(p+"?"+queryString, StatusMovedPermanently),
    pattern"
    where queryString := r.RequestURI.

    From what I can tell it is not possible to catch the problem with a
    custom RedirectHandler
    because you will not have the query string by the time it is called.

    Also it appears to me that the redirect will loose all of the headers
    that came in with the
    original query.

    Is there some other way to catch the 301 and put in a handler that will
    preserve the
    Query String?

    It appears to me that the regular (non-mux) server will do a 301
    redirect with the
    query string intact. Which behavior is the correct one? My Nginx
    server is configured
    to do the 301 redirects with the query string preserved.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Sig at Mar 31, 2015 at 3:32 pm
    Seeing this again in go 1.4.2, is this a regression?

    Test case
    http://play.golang.org/p/_ctBPD8wWA
    On Thursday, November 14, 2013 at 11:24:09 AM UTC-8, Philip Schlump wrote:

    I am having a problem when I have a url that is redirected. This only
    happens when
    a "mux" is used. In my program:

    mux := mux.NewRouter()
    ...
    mux.HandleFunc("/api/status", respHandlerStatusGet).Methods("GET")

    When I access the handler with http://localhost/api/status?abc=def it
    works just fine. That is good.
    When I have a bad URL sent in like "http://localhost//api/status?abc=def"
    (note the // after the localhost)
    this results in a 301 permanent redirect. That is good and bad. The
    redirect is just "http://localhost/api/status".
    The redirect code drops the query string completely. The browser (chrome
    for example) will then act
    appropriately and make a 2nd call to the server. The 2nd call to the
    server is missing "?abc=def" query
    string. Ouch!

    It appears to me that the problem (version 1.1.2 of go) is near lines 1377
    to 1287 of net/http/server.go

    func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
    if r.Method != "CONNECT" {
    if p := cleanPath(r.URL.Path); p != r.URL.Path {
    _, pattern = mux.handler(r.Host, p)
    return RedirectHandler(p, StatusMovedPermanently), pattern
    }
    }

    return mux.handler(r.Host, r.URL.Path)
    }

    The "return RedirectHandler(p, StatusMovedPermanently), pattern" should
    realy be
    "return RedirectHandler(p+"?"+queryString, StatusMovedPermanently),
    pattern"
    where queryString := r.RequestURI.

    From what I can tell it is not possible to catch the problem with a custom
    RedirectHandler
    because you will not have the query string by the time it is called.

    Also it appears to me that the redirect will loose all of the headers that
    came in with the
    original query.

    Is there some other way to catch the 301 and put in a handler that will
    preserve the
    Query String?

    It appears to me that the regular (non-mux) server will do a 301 redirect
    with the
    query string intact. Which behavior is the correct one? My Nginx server
    is configured
    to do the 301 redirects with the query string preserved.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 14, '13 at 7:24p
activeMar 31, '15 at 3:32p
posts7
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase