github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/tpl/data/resources.go (about)

     1  // Copyright 2016 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package data
    15  
    16  import (
    17  	"bytes"
    18  	"fmt"
    19  	"io"
    20  	"net/http"
    21  	"net/url"
    22  	"path/filepath"
    23  	"time"
    24  
    25  	"github.com/gohugoio/hugo/cache/filecache"
    26  
    27  	"github.com/gohugoio/hugo/helpers"
    28  	"github.com/spf13/afero"
    29  )
    30  
    31  var (
    32  	resSleep   = time.Second * 2 // if JSON decoding failed sleep for n seconds before retrying
    33  	resRetries = 1               // number of retries to load the JSON from URL
    34  )
    35  
    36  // getRemote loads the content of a remote file. This method is thread safe.
    37  func (ns *Namespace) getRemote(cache *filecache.Cache, unmarshal func([]byte) (bool, error), req *http.Request) error {
    38  	url := req.URL.String()
    39  	if err := ns.deps.ExecHelper.Sec().CheckAllowedHTTPURL(url); err != nil {
    40  		return err
    41  	}
    42  	if err := ns.deps.ExecHelper.Sec().CheckAllowedHTTPMethod("GET"); err != nil {
    43  		return err
    44  	}
    45  
    46  	var headers bytes.Buffer
    47  	req.Header.Write(&headers)
    48  	id := helpers.MD5String(url + headers.String())
    49  	var handled bool
    50  	var retry bool
    51  
    52  	_, b, err := cache.GetOrCreateBytes(id, func() ([]byte, error) {
    53  		var err error
    54  		handled = true
    55  		for i := 0; i <= resRetries; i++ {
    56  			ns.deps.Log.Infof("Downloading: %s ...", url)
    57  			var res *http.Response
    58  			res, err = ns.client.Do(req)
    59  			if err != nil {
    60  				return nil, err
    61  			}
    62  
    63  			var b []byte
    64  			b, err = io.ReadAll(res.Body)
    65  			if err != nil {
    66  				return nil, err
    67  			}
    68  			res.Body.Close()
    69  
    70  			if isHTTPError(res) {
    71  				return nil, fmt.Errorf("Failed to retrieve remote file: %s, body: %q", http.StatusText(res.StatusCode), b)
    72  			}
    73  
    74  			retry, err = unmarshal(b)
    75  
    76  			if err == nil {
    77  				// Return it so it can be cached.
    78  				return b, nil
    79  			}
    80  
    81  			if !retry {
    82  				return nil, err
    83  			}
    84  
    85  			ns.deps.Log.Infof("Cannot read remote resource %s: %s", url, err)
    86  			ns.deps.Log.Infof("Retry #%d for %s and sleeping for %s", i+1, url, resSleep)
    87  			time.Sleep(resSleep)
    88  		}
    89  
    90  		return nil, err
    91  	})
    92  
    93  	if !handled {
    94  		// This is cached content and should be correct.
    95  		_, err = unmarshal(b)
    96  	}
    97  
    98  	return err
    99  }
   100  
   101  // getLocal loads the content of a local file
   102  func getLocal(workingDir, url string, fs afero.Fs) ([]byte, error) {
   103  	filename := filepath.Join(workingDir, url)
   104  	return afero.ReadFile(fs, filename)
   105  }
   106  
   107  // getResource loads the content of a local or remote file and returns its content and the
   108  // cache ID used, if relevant.
   109  func (ns *Namespace) getResource(cache *filecache.Cache, unmarshal func(b []byte) (bool, error), req *http.Request) error {
   110  	switch req.URL.Scheme {
   111  	case "":
   112  		url, err := url.QueryUnescape(req.URL.String())
   113  		if err != nil {
   114  			return err
   115  		}
   116  		b, err := getLocal(ns.deps.Conf.BaseConfig().WorkingDir, url, ns.deps.Fs.Source)
   117  		if err != nil {
   118  			return err
   119  		}
   120  		_, err = unmarshal(b)
   121  		return err
   122  	default:
   123  		return ns.getRemote(cache, unmarshal, req)
   124  	}
   125  }
   126  
   127  func isHTTPError(res *http.Response) bool {
   128  	return res.StatusCode < 200 || res.StatusCode > 299
   129  }