2021-12-16 05:09:21 -05:00
|
|
|
// Copyright 2021 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package create
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-05-17 11:06:47 -04:00
|
|
|
"context"
|
2022-05-02 10:07:52 -04:00
|
|
|
"fmt"
|
2021-12-16 05:09:21 -05:00
|
|
|
"io"
|
2023-08-03 11:52:17 -04:00
|
|
|
"math/rand"
|
2021-12-16 05:09:21 -05:00
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2023-08-03 11:52:17 -04:00
|
|
|
"time"
|
2021-12-16 05:09:21 -05:00
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
gmaps "maps"
|
|
|
|
|
|
|
|
"github.com/gohugoio/httpcache"
|
2021-12-16 05:09:21 -05:00
|
|
|
"github.com/gohugoio/hugo/common/hugio"
|
2024-05-17 11:06:47 -04:00
|
|
|
"github.com/gohugoio/hugo/common/loggers"
|
2022-04-11 04:34:08 -04:00
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
2024-05-17 11:06:47 -04:00
|
|
|
"github.com/gohugoio/hugo/common/tasks"
|
2021-12-16 05:09:21 -05:00
|
|
|
"github.com/gohugoio/hugo/common/types"
|
2024-05-17 11:06:47 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
2023-01-24 14:57:15 -05:00
|
|
|
"github.com/gohugoio/hugo/identity"
|
2021-12-16 09:12:13 -05:00
|
|
|
"github.com/gohugoio/hugo/media"
|
2021-12-16 05:09:21 -05:00
|
|
|
"github.com/gohugoio/hugo/resources"
|
|
|
|
"github.com/gohugoio/hugo/resources/resource"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2022-03-24 03:12:51 -04:00
|
|
|
type HTTPError struct {
|
|
|
|
error
|
|
|
|
Data map[string]any
|
|
|
|
|
|
|
|
StatusCode int
|
|
|
|
Body string
|
|
|
|
}
|
|
|
|
|
2023-01-16 05:05:28 -05:00
|
|
|
func responseToData(res *http.Response, readBody bool) map[string]any {
|
|
|
|
var body []byte
|
|
|
|
if readBody {
|
2023-02-18 17:43:26 -05:00
|
|
|
body, _ = io.ReadAll(res.Body)
|
2023-01-16 05:05:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
m := map[string]any{
|
|
|
|
"StatusCode": res.StatusCode,
|
|
|
|
"Status": res.Status,
|
|
|
|
"TransferEncoding": res.TransferEncoding,
|
|
|
|
"ContentLength": res.ContentLength,
|
|
|
|
"ContentType": res.Header.Get("Content-Type"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if readBody {
|
|
|
|
m["Body"] = string(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func toHTTPError(err error, res *http.Response, readBody bool) *HTTPError {
|
2022-03-24 03:12:51 -04:00
|
|
|
if err == nil {
|
|
|
|
panic("err is nil")
|
|
|
|
}
|
|
|
|
if res == nil {
|
|
|
|
return &HTTPError{
|
|
|
|
error: err,
|
|
|
|
Data: map[string]any{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &HTTPError{
|
|
|
|
error: err,
|
2023-01-16 05:05:28 -05:00
|
|
|
Data: responseToData(res, readBody),
|
2022-03-24 03:12:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 11:52:17 -04:00
|
|
|
var temporaryHTTPStatusCodes = map[int]bool{
|
|
|
|
408: true,
|
|
|
|
429: true,
|
|
|
|
500: true,
|
|
|
|
502: true,
|
|
|
|
503: true,
|
|
|
|
504: true,
|
|
|
|
}
|
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
func (c *Client) configurePollingIfEnabled(uri, optionsKey string, getRes func() (*http.Response, error)) {
|
|
|
|
if c.remoteResourceChecker == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up polling for changes to this resource.
|
|
|
|
pollingConfig := c.httpCacheConfig.PollConfigFor(uri)
|
|
|
|
if pollingConfig.IsZero() || pollingConfig.Config.Disable {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.remoteResourceChecker.Has(optionsKey) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastChange time.Time
|
|
|
|
c.remoteResourceChecker.Add(optionsKey,
|
|
|
|
tasks.Func{
|
|
|
|
IntervalLow: pollingConfig.Config.Low,
|
|
|
|
IntervalHigh: pollingConfig.Config.High,
|
|
|
|
F: func(interval time.Duration) (time.Duration, error) {
|
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
|
|
|
duration := time.Since(start)
|
|
|
|
c.rs.Logger.Debugf("Polled remote resource for changes in %13s. Interval: %4s (low: %4s high: %4s) resource: %q ", duration, interval, pollingConfig.Config.Low, pollingConfig.Config.High, uri)
|
|
|
|
}()
|
|
|
|
// TODO(bep) figure out a ways to remove unused tasks.
|
|
|
|
res, err := getRes()
|
|
|
|
if err != nil {
|
|
|
|
return pollingConfig.Config.High, err
|
|
|
|
}
|
|
|
|
// The caching is delayed until the body is read.
|
|
|
|
io.Copy(io.Discard, res.Body)
|
|
|
|
res.Body.Close()
|
|
|
|
x1, x2 := res.Header.Get(httpcache.XETag1), res.Header.Get(httpcache.XETag2)
|
|
|
|
if x1 != x2 {
|
|
|
|
lastChange = time.Now()
|
|
|
|
c.remoteResourceLogger.Logf("detected change in remote resource %q", uri)
|
|
|
|
c.rs.Rebuilder.SignalRebuild(identity.StringIdentity(optionsKey))
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Since(lastChange) < 10*time.Second {
|
|
|
|
// The user is typing, check more often.
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increase the interval to avoid hammering the server.
|
|
|
|
interval += 1 * time.Second
|
|
|
|
|
|
|
|
return interval, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-16 05:09:21 -05:00
|
|
|
// FromRemote expects one or n-parts of a URL to a resource
|
|
|
|
// If you provide multiple parts they will be joined together to the final URL.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (c *Client) FromRemote(uri string, optionsm map[string]any) (resource.Resource, error) {
|
2021-12-16 05:09:21 -05:00
|
|
|
rURL, err := url.Parse(uri)
|
|
|
|
if err != nil {
|
2022-05-02 10:07:52 -04:00
|
|
|
return nil, fmt.Errorf("failed to parse URL for resource %s: %w", uri, err)
|
2021-12-16 05:09:21 -05:00
|
|
|
}
|
|
|
|
|
2023-01-16 05:05:28 -05:00
|
|
|
method := "GET"
|
2024-05-17 11:06:47 -04:00
|
|
|
if s, _, ok := maps.LookupEqualFold(optionsm, "method"); ok {
|
2023-01-16 05:05:28 -05:00
|
|
|
method = strings.ToUpper(s.(string))
|
|
|
|
}
|
|
|
|
isHeadMethod := method == "HEAD"
|
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
optionsm = gmaps.Clone(optionsm)
|
|
|
|
userKey, optionsKey := remoteResourceKeys(uri, optionsm)
|
|
|
|
|
|
|
|
// A common pattern is to use the key in the options map as
|
|
|
|
// a way to control cache eviction,
|
2024-06-04 12:48:39 -04:00
|
|
|
// so make sure we use any user provided key as the file cache key,
|
2024-05-17 11:06:47 -04:00
|
|
|
// but the auto generated and more stable key for everything else.
|
|
|
|
filecacheKey := userKey
|
2021-12-16 05:09:21 -05:00
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
return c.rs.ResourceCache.CacheResourceRemote.GetOrCreate(optionsKey, func(key string) (resource.Resource, error) {
|
2021-12-16 05:09:21 -05:00
|
|
|
options, err := decodeRemoteOptions(optionsm)
|
|
|
|
if err != nil {
|
2022-05-02 10:07:52 -04:00
|
|
|
return nil, fmt.Errorf("failed to decode options for resource %s: %w", uri, err)
|
2021-12-16 05:09:21 -05:00
|
|
|
}
|
2024-05-17 11:06:47 -04:00
|
|
|
|
2021-12-16 05:09:21 -05:00
|
|
|
if err := c.validateFromRemoteArgs(uri, options); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
getRes := func() (*http.Response, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx = c.resourceIDDispatcher.Set(ctx, filecacheKey)
|
2023-08-03 11:52:17 -04:00
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
req, err := options.NewRequest(uri)
|
2023-08-03 11:52:17 -04:00
|
|
|
if err != nil {
|
2024-05-17 11:06:47 -04:00
|
|
|
return nil, fmt.Errorf("failed to create request for resource %s: %w", uri, err)
|
2023-08-03 11:52:17 -04:00
|
|
|
}
|
2022-03-24 03:12:51 -04:00
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
req = req.WithContext(ctx)
|
2021-12-16 05:09:21 -05:00
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
return c.httpClient.Do(req)
|
2021-12-16 05:09:21 -05:00
|
|
|
}
|
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
res, err := getRes()
|
2023-01-16 05:05:28 -05:00
|
|
|
if err != nil {
|
2024-05-17 11:06:47 -04:00
|
|
|
return nil, err
|
2023-01-16 05:05:28 -05:00
|
|
|
}
|
2024-05-17 11:06:47 -04:00
|
|
|
defer res.Body.Close()
|
2021-12-16 05:09:21 -05:00
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
c.configurePollingIfEnabled(uri, optionsKey, getRes)
|
|
|
|
|
|
|
|
if res.StatusCode == http.StatusNotFound {
|
|
|
|
// Not found. This matches how lookups for local resources work.
|
|
|
|
return nil, nil
|
2021-12-16 05:09:21 -05:00
|
|
|
}
|
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
if res.StatusCode < 200 || res.StatusCode > 299 {
|
|
|
|
return nil, toHTTPError(fmt.Errorf("failed to fetch remote resource: %s", http.StatusText(res.StatusCode)), res, !isHeadMethod)
|
|
|
|
}
|
2021-12-21 04:35:33 -05:00
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
var (
|
|
|
|
body []byte
|
|
|
|
mediaType media.Type
|
|
|
|
)
|
|
|
|
// A response to a HEAD method should not have a body. If it has one anyway, that body must be ignored.
|
|
|
|
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD
|
|
|
|
if !isHeadMethod && res.Body != nil {
|
|
|
|
body, err = io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read remote resource %q: %w", uri, err)
|
|
|
|
}
|
2023-05-20 11:37:04 -04:00
|
|
|
}
|
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
filename := path.Base(rURL.Path)
|
|
|
|
if _, params, _ := mime.ParseMediaType(res.Header.Get("Content-Disposition")); params != nil {
|
|
|
|
if _, ok := params["filename"]; ok {
|
|
|
|
filename = params["filename"]
|
|
|
|
}
|
|
|
|
}
|
2023-01-16 05:05:28 -05:00
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
contentType := res.Header.Get("Content-Type")
|
2023-01-16 05:05:28 -05:00
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
// For HEAD requests we have no body to work with, so we need to use the Content-Type header.
|
|
|
|
if isHeadMethod || c.rs.ExecHelper.Sec().HTTP.MediaTypes.Accept(contentType) {
|
|
|
|
var found bool
|
|
|
|
mediaType, found = c.rs.MediaTypes().GetByType(contentType)
|
|
|
|
if !found {
|
|
|
|
// A media type not configured in Hugo, just create one from the content type string.
|
|
|
|
mediaType, _ = media.FromString(contentType)
|
2023-01-16 05:05:28 -05:00
|
|
|
}
|
2021-12-21 04:35:33 -05:00
|
|
|
}
|
2021-12-16 05:09:21 -05:00
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
if mediaType.IsZero() {
|
|
|
|
|
|
|
|
var extensionHints []string
|
|
|
|
|
|
|
|
// mime.ExtensionsByType gives a long list of extensions for text/plain,
|
|
|
|
// just use ".txt".
|
|
|
|
if strings.HasPrefix(contentType, "text/plain") {
|
|
|
|
extensionHints = []string{".txt"}
|
|
|
|
} else {
|
|
|
|
exts, _ := mime.ExtensionsByType(contentType)
|
|
|
|
if exts != nil {
|
|
|
|
extensionHints = exts
|
|
|
|
}
|
2023-01-16 05:05:28 -05:00
|
|
|
}
|
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
// Look for a file extension. If it's .txt, look for a more specific.
|
|
|
|
if extensionHints == nil || extensionHints[0] == ".txt" {
|
|
|
|
if ext := path.Ext(filename); ext != "" {
|
|
|
|
extensionHints = []string{ext}
|
|
|
|
}
|
|
|
|
}
|
2023-01-16 05:05:28 -05:00
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
// Now resolve the media type primarily using the content.
|
|
|
|
mediaType = media.FromContent(c.rs.MediaTypes(), extensionHints, body)
|
2021-12-16 05:09:21 -05:00
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
}
|
2021-12-16 05:09:21 -05:00
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
if mediaType.IsZero() {
|
|
|
|
return nil, fmt.Errorf("failed to resolve media type for remote resource %q", uri)
|
|
|
|
}
|
|
|
|
|
|
|
|
userKey = filename[:len(filename)-len(path.Ext(filename))] + "_" + userKey + mediaType.FirstSuffix.FullSuffix
|
|
|
|
data := responseToData(res, false)
|
|
|
|
|
|
|
|
return c.rs.NewResource(
|
|
|
|
resources.ResourceSourceDescriptor{
|
|
|
|
MediaType: mediaType,
|
|
|
|
Data: data,
|
|
|
|
GroupIdentity: identity.StringIdentity(optionsKey),
|
|
|
|
LazyPublish: true,
|
|
|
|
OpenReadSeekCloser: func() (hugio.ReadSeekCloser, error) {
|
|
|
|
return hugio.NewReadSeekerNoOpCloser(bytes.NewReader(body)), nil
|
|
|
|
},
|
|
|
|
TargetPath: userKey,
|
|
|
|
})
|
|
|
|
})
|
2021-12-16 05:09:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) validateFromRemoteArgs(uri string, options fromRemoteOptions) error {
|
|
|
|
if err := c.rs.ExecHelper.Sec().CheckAllowedHTTPURL(uri); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.rs.ExecHelper.Sec().CheckAllowedHTTPMethod(options.Method); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
func remoteResourceKeys(uri string, optionsm map[string]any) (string, string) {
|
|
|
|
var userKey string
|
|
|
|
if key, k, found := maps.LookupEqualFold(optionsm, "key"); found {
|
|
|
|
userKey = identity.HashString(key)
|
|
|
|
delete(optionsm, k)
|
|
|
|
}
|
|
|
|
optionsKey := identity.HashString(uri, optionsm)
|
|
|
|
if userKey == "" {
|
|
|
|
userKey = optionsKey
|
2022-04-11 04:34:08 -04:00
|
|
|
}
|
2024-05-17 11:06:47 -04:00
|
|
|
return userKey, optionsKey
|
2022-04-11 04:34:08 -04:00
|
|
|
}
|
|
|
|
|
2023-01-16 05:00:55 -05:00
|
|
|
func addDefaultHeaders(req *http.Request) {
|
2021-12-16 05:09:21 -05:00
|
|
|
if !hasHeaderKey(req.Header, "User-Agent") {
|
|
|
|
req.Header.Add("User-Agent", "Hugo Static Site Generator")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func addUserProvidedHeaders(headers map[string]any, req *http.Request) {
|
2021-12-16 05:09:21 -05:00
|
|
|
if headers == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for key, val := range headers {
|
|
|
|
vals := types.ToStringSlicePreserveString(val)
|
|
|
|
for _, s := range vals {
|
|
|
|
req.Header.Add(key, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasHeaderKey(m http.Header, key string) bool {
|
|
|
|
_, ok := m[key]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
type fromRemoteOptions struct {
|
|
|
|
Method string
|
2022-03-17 17:03:27 -04:00
|
|
|
Headers map[string]any
|
2021-12-16 05:09:21 -05:00
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o fromRemoteOptions) BodyReader() io.Reader {
|
|
|
|
if o.Body == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return bytes.NewBuffer(o.Body)
|
|
|
|
}
|
|
|
|
|
2023-01-16 05:00:55 -05:00
|
|
|
func (o fromRemoteOptions) NewRequest(url string) (*http.Request, error) {
|
|
|
|
req, err := http.NewRequest(o.Method, url, o.BodyReader())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// First add any user provided headers.
|
|
|
|
if o.Headers != nil {
|
|
|
|
addUserProvidedHeaders(o.Headers, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then add default headers not provided by the user.
|
|
|
|
addDefaultHeaders(req)
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func decodeRemoteOptions(optionsm map[string]any) (fromRemoteOptions, error) {
|
2021-12-21 04:35:33 -05:00
|
|
|
options := fromRemoteOptions{
|
2021-12-16 05:09:21 -05:00
|
|
|
Method: "GET",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := mapstructure.WeakDecode(optionsm, &options)
|
|
|
|
if err != nil {
|
|
|
|
return options, err
|
|
|
|
}
|
|
|
|
options.Method = strings.ToUpper(options.Method)
|
|
|
|
|
|
|
|
return options, nil
|
|
|
|
}
|
2024-05-17 11:06:47 -04:00
|
|
|
|
|
|
|
var _ http.RoundTripper = (*transport)(nil)
|
|
|
|
|
|
|
|
type transport struct {
|
|
|
|
Cfg config.AllProvider
|
|
|
|
Logger loggers.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
|
|
|
defer func() {
|
|
|
|
if resp != nil && resp.StatusCode != http.StatusNotFound && resp.StatusCode != http.StatusNotModified {
|
|
|
|
t.Logger.Debugf("Fetched remote resource: %s", req.URL.String())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var (
|
|
|
|
start time.Time
|
|
|
|
nextSleep = time.Duration((rand.Intn(1000) + 100)) * time.Millisecond
|
|
|
|
nextSleepLimit = time.Duration(5) * time.Second
|
|
|
|
retry bool
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
resp, retry, err = func() (*http.Response, bool, error) {
|
|
|
|
resp2, err := http.DefaultTransport.RoundTrip(req)
|
|
|
|
if err != nil {
|
|
|
|
return resp2, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp2.StatusCode != http.StatusNotFound && resp2.StatusCode != http.StatusNotModified {
|
|
|
|
if resp2.StatusCode < 200 || resp2.StatusCode > 299 {
|
|
|
|
return resp2, temporaryHTTPStatusCodes[resp2.StatusCode], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resp2, false, nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
if retry {
|
|
|
|
if start.IsZero() {
|
|
|
|
start = time.Now()
|
|
|
|
} else if d := time.Since(start) + nextSleep; d >= t.Cfg.Timeout() {
|
|
|
|
msg := "<nil>"
|
|
|
|
if resp != nil {
|
|
|
|
msg = resp.Status
|
|
|
|
}
|
|
|
|
err := toHTTPError(fmt.Errorf("retry timeout (configured to %s) fetching remote resource: %s", t.Cfg.Timeout(), msg), resp, req.Method != "HEAD")
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
time.Sleep(nextSleep)
|
|
|
|
if nextSleep < nextSleepLimit {
|
|
|
|
nextSleep *= 2
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to send the redirect responses back to the HTTP client from RoundTrip,
|
|
|
|
// but we don't want to cache them.
|
|
|
|
func shouldCache(statusCode int) bool {
|
|
|
|
switch statusCode {
|
|
|
|
case http.StatusMovedPermanently, http.StatusFound, http.StatusSeeOther, http.StatusTemporaryRedirect, http.StatusPermanentRedirect:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|