Skip to content

Commit

Permalink
Implement workaround for golang/go#21955 (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmt authored Jun 4, 2018
1 parent 423fe34 commit 54835a0
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 60 deletions.
10 changes: 5 additions & 5 deletions server/application/application.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/cluster/cluster.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 47 additions & 46 deletions server/repository/repository.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 22 additions & 4 deletions server/repository/repository.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/repository/repository.proto
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ service RepositoryService {

// ListKsonnetApps returns list of Ksonnet apps in the repo
rpc ListKsonnetApps(RepoKsonnetQuery) returns (RepoKsonnetResponse) {
option (google.api.http).get = "/api/v1/repositories/ksonnet";
option (google.api.http).get = "/api/v1/repositories/{repo}/ksonnet";
}

// Create creates a repo
Expand Down
66 changes: 65 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net"
"net/http"
"net/url"
"strings"
"time"

Expand Down Expand Up @@ -277,6 +278,7 @@ func (a *ArgoCDServer) newGRPCServer() *grpc.Server {
grpc_util.PanicLoggerStreamServerInterceptor(a.log),
)))
sOpts = append(sOpts, grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
bug21955WorkaroundInterceptor,
grpc_logrus.UnaryServerInterceptor(a.log),
grpc_auth.UnaryServerInterceptor(a.authenticate),
grpc_util.ErrorCodeUnaryServerInterceptor(),
Expand Down Expand Up @@ -322,7 +324,7 @@ func (a *ArgoCDServer) newHTTPServer(ctx context.Context, port int) *http.Server
mux := http.NewServeMux()
httpS := http.Server{
Addr: endpoint,
Handler: mux,
Handler: &bug21955Workaround{handler: mux},
}
var dOpts []grpc.DialOption
if a.useTLS() {
Expand Down Expand Up @@ -469,3 +471,65 @@ func getToken(md metadata.MD) string {
}
return ""
}

// Workaround for https://github.com/golang/go/issues/21955 to support escaped URLs in URL path.
type bug21955Workaround struct {
handler http.Handler
}

func (bf *bug21955Workaround) ServeHTTP(w http.ResponseWriter, r *http.Request) {
paths := map[string][]string{
"/api/v1/repositories/": {"ksonnet"},
"/api/v1/clusters/": {},
}
for path, subPaths := range paths {
if strings.Index(r.URL.Path, path) > -1 {
postfix := ""
for _, subPath := range subPaths {
if strings.LastIndex(r.URL.Path, subPath) == len(r.URL.Path)-len(subPath) {
postfix = "/" + subPath
r.URL.Path = r.URL.Path[0 : len(r.URL.Path)-len(subPath)-1]
break
}
}
r.URL.Path = path + url.QueryEscape(r.URL.Path[len(path):]) + postfix
break
}
}
bf.handler.ServeHTTP(w, r)
}

func bug21955WorkaroundInterceptor(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
if rq, ok := req.(*repository.RepoQuery); ok {
repo, err := url.QueryUnescape(rq.Repo)
if err != nil {
return nil, err
}
rq.Repo = repo
} else if rk, ok := req.(*repository.RepoKsonnetQuery); ok {
repo, err := url.QueryUnescape(rk.Repo)
if err != nil {
return nil, err
}
rk.Repo = repo
} else if ru, ok := req.(*repository.RepoUpdateRequest); ok {
repo, err := url.QueryUnescape(ru.Repo.Repo)
if err != nil {
return nil, err
}
ru.Repo.Repo = repo
} else if cq, ok := req.(*cluster.ClusterQuery); ok {
server, err := url.QueryUnescape(cq.Server)
if err != nil {
return nil, err
}
cq.Server = server
} else if cu, ok := req.(*cluster.ClusterUpdateRequest); ok {
server, err := url.QueryUnescape(cu.Cluster.Server)
if err != nil {
return nil, err
}
cu.Cluster.Server = server
}
return handler(ctx, req)
}
2 changes: 1 addition & 1 deletion server/session/session.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 54835a0

Please sign in to comment.