Preserve trailing slash in apiserver proxy.

Some servers are sensitive to the presence of a trailing slash.
For example, http://etcd/v2/keys returns 404 while
http://etcd/v2/keys/ returns the key named "/".
This commit is contained in:
Anthony Yeh 2015-01-14 21:42:34 -08:00
parent 84ce5c441a
commit 6f641744ff
2 changed files with 7 additions and 0 deletions

View File

@ -96,6 +96,12 @@ func (r *ProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if len(parts) > 2 { if len(parts) > 2 {
proxyParts := parts[2:] proxyParts := parts[2:]
rest = strings.Join(proxyParts, "/") rest = strings.Join(proxyParts, "/")
if strings.HasSuffix(req.URL.Path, "/") {
// The original path had a trailing slash, which has been stripped
// by KindAndNamespace(). We should add it back because some
// servers (like etcd) require it.
rest = rest + "/"
}
} }
storage, ok := r.storage[kind] storage, ok := r.storage[kind]
if !ok { if !ok {

View File

@ -147,6 +147,7 @@ func TestProxy(t *testing.T) {
{"PUT", "/some/dir/id", "different question", "answer", "text/css", "default"}, {"PUT", "/some/dir/id", "different question", "answer", "text/css", "default"},
{"DELETE", "/some/dir/id", "", "ok", "text/css", "default"}, {"DELETE", "/some/dir/id", "", "ok", "text/css", "default"},
{"GET", "/some/dir/id", "", "answer", "text/css", "other"}, {"GET", "/some/dir/id", "", "answer", "text/css", "other"},
{"GET", "/trailing/slash/", "", "answer", "text/css", "default"},
} }
for _, item := range table { for _, item := range table {