newRequest - instantiate a new HTTP request for a given method.
(ctx context.Context, method string, metadata requestMetadata)
| 835 | |
| 836 | // newRequest - instantiate a new HTTP request for a given method. |
| 837 | func (c *Client) newRequest(ctx context.Context, method string, metadata requestMetadata) (req *http.Request, err error) { |
| 838 | // If no method is supplied default to 'POST'. |
| 839 | if method == "" { |
| 840 | method = http.MethodPost |
| 841 | } |
| 842 | |
| 843 | location := metadata.bucketLocation |
| 844 | if location == "" { |
| 845 | if metadata.bucketName != "" { |
| 846 | // Gather location only if bucketName is present. |
| 847 | location, err = c.getBucketLocation(ctx, metadata.bucketName) |
| 848 | if err != nil { |
| 849 | return nil, err |
| 850 | } |
| 851 | } |
| 852 | if location == "" { |
| 853 | location = getDefaultLocation(*c.endpointURL, c.region) |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | // Look if target url supports virtual host. |
| 858 | // We explicitly disallow MakeBucket calls to not use virtual DNS style, |
| 859 | // since the resolution may fail. |
| 860 | isMakeBucket := (metadata.objectName == "" && method == http.MethodPut && len(metadata.queryValues) == 0) |
| 861 | isVirtualHost := c.isVirtualHostStyleRequest(*c.endpointURL, metadata.bucketName) && !isMakeBucket |
| 862 | |
| 863 | // Construct a new target URL. |
| 864 | targetURL, err := c.makeTargetURL(metadata.bucketName, metadata.objectName, location, |
| 865 | isVirtualHost, metadata.queryValues) |
| 866 | if err != nil { |
| 867 | return nil, err |
| 868 | } |
| 869 | |
| 870 | if c.httpTrace != nil { |
| 871 | ctx = httptrace.WithClientTrace(ctx, c.httpTrace) |
| 872 | } |
| 873 | |
| 874 | // make sure to de-dup calls to credential services, this reduces |
| 875 | // the overall load to the endpoint generating credential service. |
| 876 | value, err, _ := c.credsGroup.Do(metadata.bucketName, func() (credentials.Value, error) { |
| 877 | if s3utils.IsS3ExpressBucket(metadata.bucketName) && s3utils.IsAmazonEndpoint(*c.endpointURL) { |
| 878 | return c.CreateSession(ctx, metadata.bucketName, SessionReadWrite) |
| 879 | } |
| 880 | // Get credentials from the configured credentials provider. |
| 881 | return c.credsProvider.GetWithContext(c.CredContext()) |
| 882 | }) |
| 883 | if err != nil { |
| 884 | return nil, err |
| 885 | } |
| 886 | |
| 887 | // Initialize a new HTTP request for the method. |
| 888 | req, err = http.NewRequestWithContext(ctx, method, targetURL.String(), nil) |
| 889 | if err != nil { |
| 890 | return nil, err |
| 891 | } |
| 892 | |
| 893 | var ( |
| 894 | signerType = value.SignerType |
no test coverage detected