2016-03-21 19:28:42 -04:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2014-12-26 22:40:10 -05:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-12-26 22:40:10 -05:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-12-26 22:40:10 -05:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
package data
|
2014-12-26 22:40:10 -05:00
|
|
|
|
|
|
|
import (
|
2021-06-05 06:44:45 -04:00
|
|
|
"bytes"
|
2014-12-26 22:40:10 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2020-03-07 13:56:02 -05:00
|
|
|
"net/url"
|
2015-06-26 06:23:37 -04:00
|
|
|
"path/filepath"
|
2015-05-28 01:36:06 -04:00
|
|
|
"time"
|
2014-12-26 22:40:10 -05:00
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/cache/filecache"
|
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/afero"
|
2014-12-26 22:40:10 -05:00
|
|
|
)
|
|
|
|
|
2015-05-28 01:36:06 -04:00
|
|
|
var (
|
2018-11-08 04:24:13 -05:00
|
|
|
resSleep = time.Second * 2 // if JSON decoding failed sleep for n seconds before retrying
|
|
|
|
resRetries = 1 // number of retries to load the JSON from URL
|
2015-05-28 01:36:06 -04:00
|
|
|
)
|
2014-12-26 22:40:10 -05:00
|
|
|
|
2017-05-01 15:04:21 -04:00
|
|
|
// getRemote loads the content of a remote file. This method is thread safe.
|
2019-03-24 05:11:16 -04:00
|
|
|
func (ns *Namespace) getRemote(cache *filecache.Cache, unmarshal func([]byte) (bool, error), req *http.Request) error {
|
2017-05-01 23:41:08 -04:00
|
|
|
url := req.URL.String()
|
2021-06-05 06:44:45 -04:00
|
|
|
var headers bytes.Buffer
|
|
|
|
req.Header.Write(&headers)
|
|
|
|
id := helpers.MD5String(url + headers.String())
|
2018-11-08 04:24:13 -05:00
|
|
|
var handled bool
|
|
|
|
var retry bool
|
|
|
|
|
|
|
|
_, b, err := cache.GetOrCreateBytes(id, func() ([]byte, error) {
|
|
|
|
var err error
|
|
|
|
handled = true
|
|
|
|
for i := 0; i <= resRetries; i++ {
|
2020-10-21 05:17:48 -04:00
|
|
|
ns.deps.Log.Infof("Downloading: %s ...", url)
|
2018-11-08 04:24:13 -05:00
|
|
|
var res *http.Response
|
|
|
|
res, err = ns.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var b []byte
|
|
|
|
b, err = ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.Body.Close()
|
|
|
|
|
2021-06-07 06:06:27 -04:00
|
|
|
if isHTTPError(res) {
|
|
|
|
return nil, errors.Errorf("Failed to retrieve remote file: %s, body: %q", http.StatusText(res.StatusCode), b)
|
|
|
|
}
|
|
|
|
|
2019-03-24 05:11:16 -04:00
|
|
|
retry, err = unmarshal(b)
|
2018-11-08 04:24:13 -05:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
// Return it so it can be cached.
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !retry {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-21 05:17:48 -04:00
|
|
|
ns.deps.Log.Infof("Cannot read remote resource %s: %s", url, err)
|
|
|
|
ns.deps.Log.Infof("Retry #%d for %s and sleeping for %s", i+1, url, resSleep)
|
2018-11-08 04:24:13 -05:00
|
|
|
time.Sleep(resSleep)
|
|
|
|
}
|
2017-05-01 23:41:08 -04:00
|
|
|
|
2014-12-26 22:40:10 -05:00
|
|
|
return nil, err
|
2018-11-08 04:24:13 -05:00
|
|
|
})
|
2014-12-26 22:40:10 -05:00
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
if !handled {
|
|
|
|
// This is cached content and should be correct.
|
2019-03-24 05:11:16 -04:00
|
|
|
_, err = unmarshal(b)
|
2017-05-01 23:41:08 -04:00
|
|
|
}
|
2014-12-26 22:40:10 -05:00
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
return err
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
|
|
|
|
2017-05-01 15:04:21 -04:00
|
|
|
// getLocal loads the content of a local file
|
|
|
|
func getLocal(url string, fs afero.Fs, cfg config.Provider) ([]byte, error) {
|
2017-02-04 22:20:06 -05:00
|
|
|
filename := filepath.Join(cfg.GetString("workingDir"), url)
|
2016-07-30 09:37:03 -04:00
|
|
|
return afero.ReadFile(fs, filename)
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
// getResource loads the content of a local or remote file and returns its content and the
|
|
|
|
// cache ID used, if relevant.
|
2019-03-24 05:11:16 -04:00
|
|
|
func (ns *Namespace) getResource(cache *filecache.Cache, unmarshal func(b []byte) (bool, error), req *http.Request) error {
|
2017-05-01 23:41:08 -04:00
|
|
|
switch req.URL.Scheme {
|
|
|
|
case "":
|
2020-03-07 13:56:02 -05:00
|
|
|
url, err := url.QueryUnescape(req.URL.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b, err := getLocal(url, ns.deps.Fs.Source, ns.deps.Cfg)
|
2018-11-08 04:24:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-24 05:11:16 -04:00
|
|
|
_, err = unmarshal(b)
|
2018-11-08 04:24:13 -05:00
|
|
|
return err
|
2017-05-01 23:41:08 -04:00
|
|
|
default:
|
2018-11-08 04:24:13 -05:00
|
|
|
return ns.getRemote(cache, unmarshal, req)
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
|
|
|
}
|
2018-11-08 04:24:13 -05:00
|
|
|
|
|
|
|
func isHTTPError(res *http.Response) bool {
|
|
|
|
return res.StatusCode < 200 || res.StatusCode > 299
|
|
|
|
}
|