handlers: Handle crash if r.URL.Path is empty. (#3554)

URL paths can be empty and not have preceding separator,
we do not yet know the conditions this can happen inside
Go http server.

This patch is to ensure that we do not crash ourselves
under conditions where r.URL.Path may be empty.

Fixes #3553
This commit is contained in:
Harshavardhana
2017-01-10 11:01:23 -08:00
committed by GitHub
parent eb6d53d2f5
commit 0563a9235a
3 changed files with 121 additions and 12 deletions

View File

@@ -70,6 +70,38 @@ func checkDuplicateStrings(list []string) error {
return nil
}
// splitStr splits a string into n parts, empty strings are added
// if we are not able to reach n elements
func splitStr(path, sep string, n int) []string {
splits := strings.SplitN(path, sep, n)
// Add empty strings if we found elements less than nr
for i := n - len(splits); i > 0; i-- {
splits = append(splits, "")
}
return splits
}
// Convert url path into bucket and object name.
func urlPath2BucketObjectName(u *url.URL) (bucketName, objectName string) {
if u == nil {
// Empty url, return bucket and object names.
return
}
// Trim any preceding slash separator.
urlPath := strings.TrimPrefix(u.Path, slashSeparator)
// Split urlpath using slash separator into a given number of
// expected tokens.
tokens := splitStr(urlPath, slashSeparator, 2)
// Extract bucket and objects.
bucketName, objectName = tokens[0], tokens[1]
// Success.
return bucketName, objectName
}
// checkDuplicates - function to validate if there are duplicates in a slice of endPoints.
func checkDuplicateEndpoints(endpoints []*url.URL) error {
var strs []string