package minio import ( "encoding/hex" "io" "io/ioutil" "net/http" "regexp" "strings" "unicode/utf8" ) // operation - rest operation type operation struct { HTTPServer string HTTPMethod string HTTPPath string } // request - a http request type request struct { req *http.Request config *Config body io.ReadSeeker expires int64 } // Do - start the request func (r *request) Do() (resp *http.Response, err error) { if r.config.AccessKeyID != "" && r.config.SecretAccessKey != "" { if r.config.Signature.isV2() { r.SignV2() } if r.config.Signature.isV4() || r.config.Signature.isLatest() { r.SignV4() } } transport := http.DefaultTransport if r.config.Transport != nil { transport = r.config.Transport } // do not use http.Client{}, while it may seem intuitive but the problem seems to be // that http.Client{} internally follows redirects and there is no easier way to disable // it from outside using a configuration parameter - // this auto redirect causes complications in verifying subsequent errors // // The best is to use RoundTrip() directly, so the request comes back to the caller where // we are going to handle such replies. And indeed that is the right thing to do here. // return transport.RoundTrip(r.req) } // Set - set additional headers if any func (r *request) Set(key, value string) { r.req.Header.Set(key, value) } // Get - get header values func (r *request) Get(key string) string { return r.req.Header.Get(key) } func path2BucketAndObject(path string) (bucketName, objectName string) { pathSplits := strings.SplitN(path, "?", 2) splits := strings.SplitN(pathSplits[0], separator, 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 } // path2Object gives objectName from URL path func path2Object(path string) (objectName string) { _, objectName = path2BucketAndObject(path) return } // path2Bucket gives bucketName from URL path func path2Bucket(path string) (bucketName string) { bucketName, _ = path2BucketAndObject(path) return } // path2Query gives query part from URL path func path2Query(path string) (query string) { pathSplits := strings.SplitN(path, "?", 2) if len(pathSplits) > 1 { query = pathSplits[1] } return } // getURLEncodedPath encode the strings from UTF-8 byte representations to HTML hex escape sequences // // This is necessary since regular url.Parse() and url.Encode() functions do not support UTF-8 // non english characters cannot be parsed due to the nature in which url.Encode() is written // // This function on the other hand is a direct replacement for url.Encode() technique to support // pretty much every UTF-8 character. func getURLEncodedPath(pathName string) string { // if object matches reserved string, no need to encode them reservedNames := regexp.MustCompile("^[a-zA-Z0-9-_.~/]+$") if reservedNames.MatchString(pathName) { return pathName } var encodedPathname string for _, s := range pathName { if 'A' <= s && s <= 'Z' || 'a' <= s && s <= 'z' || '0' <= s && s <= '9' { // §2.3 Unreserved characters (mark) encodedPathname = encodedPathname + string(s) continue } switch s { case '-', '_', '.', '~', '/': // §2.3 Unreserved characters (mark) encodedPathname = encodedPathname + string(s) continue default: len := utf8.RuneLen(s) if len < 0 { // if utf8 cannot convert return the same string as is return pathName } u := make([]byte, len) utf8.EncodeRune(u, s) for _, r := range u { hex := hex.EncodeToString([]byte{r}) encodedPathname = encodedPathname + "%" + strings.ToUpper(hex) } } } return encodedPathname } func (op *operation) getRequestURL(config Config) (url string) { // parse URL for the combination of HTTPServer + HTTPPath url = op.HTTPServer + separator if !config.isVirtualStyle { url += path2Bucket(op.HTTPPath) } objectName := getURLEncodedPath(path2Object(op.HTTPPath)) queryPath := path2Query(op.HTTPPath) if objectName == "" && queryPath != "" { url += "?" + queryPath return } if objectName != "" && queryPath == "" { if strings.HasSuffix(url, separator) { url += objectName } else { url += separator + objectName } return } if objectName != "" && queryPath != "" { if strings.HasSuffix(url, separator) { url += objectName + "?" + queryPath } else { url += separator + objectName + "?" + queryPath } } return } func newPresignedRequest(op *operation, config *Config, expires int64) (*request, error) { // if no method default to POST method := op.HTTPMethod if method == "" { method = "POST" } u := op.getRequestURL(*config) // get a new HTTP request, for the requested method req, err := http.NewRequest(method, u, nil) if err != nil { return nil, err } // set UserAgent req.Header.Set("User-Agent", config.userAgent) // set Accept header for response encoding style, if available if config.AcceptType != "" { req.Header.Set("Accept", config.AcceptType) } // save for subsequent use r := new(request) r.config = config r.expires = expires r.req = req r.body = nil return r, nil } // newUnauthenticatedRequest - instantiate a new unauthenticated request func newUnauthenticatedRequest(op *operation, config *Config, body io.Reader) (*request, error) { // if no method default to POST method := op.HTTPMethod if method == "" { method = "POST" } u := op.getRequestURL(*config) // get a new HTTP request, for the requested method req, err := http.NewRequest(method, u, nil) if err != nil { return nil, err } // set UserAgent req.Header.Set("User-Agent", config.userAgent) // set Accept header for response encoding style, if available if config.AcceptType != "" { req.Header.Set("Accept", config.AcceptType) } // add body switch { case body == nil: req.Body = nil default: req.Body = ioutil.NopCloser(body) } // save for subsequent use r := new(request) r.req = req r.config = config return r, nil } // newRequest - instantiate a new request func newRequest(op *operation, config *Config, body io.ReadSeeker) (*request, error) { // if no method default to POST method := op.HTTPMethod if method == "" { method = "POST" } u := op.getRequestURL(*config) // get a new HTTP request, for the requested method req, err := http.NewRequest(method, u, nil) if err != nil { return nil, err } // set UserAgent req.Header.Set("User-Agent", config.userAgent) // set Accept header for response encoding style, if available if config.AcceptType != "" { req.Header.Set("Accept", config.AcceptType) } // add body switch { case body == nil: req.Body = nil default: req.Body = ioutil.NopCloser(body) } // save for subsequent use r := new(request) r.config = config r.req = req r.body = body return r, nil }