1
0
mirror of https://github.com/minio/mc.git synced 2026-01-04 02:44:40 +03:00
Files
mc/pkg/client/s3/request.go
2015-05-11 16:24:26 -07:00

96 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package s3
import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/minio/minio/pkg/iodine"
)
func (c *s3Client) isValidQueryURL(queryURL string) bool {
u, err := url.Parse(queryURL)
if err != nil {
return false
}
if !strings.Contains(u.Scheme, "http") {
return false
}
return true
}
// url2BucketAndObject gives bucketName and objectName from URL path
func (c *s3Client) url2BucketAndObject() (bucketName, objectName string) {
splits := strings.SplitN(c.Path, "/", 3)
switch len(splits) {
case 0, 1:
bucketName = ""
objectName = ""
case 2:
bucketName = splits[1]
objectName = ""
case 3:
bucketName = splits[1]
objectName = splits[2]
}
return bucketName, objectName
}
func (c *s3Client) getBucketRequestURL(bucket string) string {
return fmt.Sprintf("%s://%s/%s", c.Scheme, c.Host, bucket)
}
// getObjectRequestURL constructs a URL using bucket and object
func (c *s3Client) getObjectRequestURL(bucket, object string) string {
return c.getBucketRequestURL(bucket) + "/" + object
}
func (c *s3Client) mustGetRequestURL() string {
requestURL, _ := c.getRequestURL()
return requestURL
}
// getRequestURL constructs a URL. URL is appropriately encoded based on the host's object storage implementation.
func (c *s3Client) getRequestURL() (string, error) {
bucket, object := c.url2BucketAndObject()
// Avoid bucket names with "." in them
// Citing - http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html
//
// =====================
// When using virtual hostedstyle buckets with SSL, the SSL wild card certificate
// only matches buckets that do not contain periods. To work around this, use HTTP
// or write your own certificate verification logic.
// =====================
//
switch {
case bucket == "" && object == "":
return fmt.Sprintf("%s://%s/", c.Scheme, c.Host), nil
case bucket != "" && object == "":
return c.getBucketRequestURL(bucket), nil
case bucket != "" && object != "":
return c.getObjectRequestURL(bucket, object), nil
}
return "", iodine.New(errors.New("Unexpected error, please report this.."), nil)
}
// Instantiate a new request
func (c *s3Client) newRequest(method, url string, body io.ReadCloser) (*http.Request, error) {
errParams := map[string]string{
"url": url,
"method": method,
"userAgent": c.UserAgent,
}
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, iodine.New(err, errParams)
}
req.Header.Set("User-Agent", c.UserAgent)
req.Header.Set("X-Amz-Date", time.Now().UTC().Format(http.TimeFormat))
return req, nil
}