Skip to content
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

Strip repeated slashes from URL path #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"net/http"
"os"
"strings"

"github.com/go-openapi/swag"
"github.com/pottava/aws-s3-proxy/internal/config"
Expand All @@ -23,9 +24,11 @@ var (
func main() {
validateAwsConfigurations()

http.Handle("/", common.WrapHandler(controllers.AwsS3))
httpMux := http.NewServeMux()

http.HandleFunc("/--version", func(w http.ResponseWriter, r *http.Request) {
httpMux.Handle("/", common.WrapHandler(controllers.AwsS3))

httpMux.HandleFunc("/--version", func(w http.ResponseWriter, r *http.Request) {
if len(commit) > 0 && len(date) > 0 {
fmt.Fprintf(w, "%s-%s (built at %s)\n", ver, commit, date)
return
Expand All @@ -39,10 +42,10 @@ func main() {

if (len(config.Config.SslCert) > 0) && (len(config.Config.SslKey) > 0) {
log.Fatal(http.ListenAndServeTLS(
addr, config.Config.SslCert, config.Config.SslKey, nil,
addr, config.Config.SslCert, config.Config.SslKey, &slashFix{httpMux},
))
} else {
log.Fatal(http.ListenAndServe(addr, nil))
log.Fatal(http.ListenAndServe(addr, &slashFix{httpMux}))
}
}

Expand All @@ -63,3 +66,25 @@ func validateAwsConfigurations() {
}
}
}

type slashFix struct {
mux http.Handler
}

func (h *slashFix) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var pathBuilder strings.Builder
slash := false
for _, c := range r.URL.Path {
if c == '/' {
if !slash {
pathBuilder.WriteRune(c)
}
slash = true
} else {
pathBuilder.WriteRune(c)
slash = false
}
}
r.URL.Path = pathBuilder.String()
h.mux.ServeHTTP(w, r)
}