2019-09-04 15:53:54 -04:00
|
|
|
// Copyright 2015 go-swagger maintainers
|
|
|
|
//
|
|
|
|
// 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 swag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LoadHTTPTimeout the default timeout for load requests
|
|
|
|
var LoadHTTPTimeout = 30 * time.Second
|
|
|
|
|
2020-09-01 10:01:23 -04:00
|
|
|
// LoadHTTPBasicAuthUsername the username to use when load requests require basic auth
|
|
|
|
var LoadHTTPBasicAuthUsername = ""
|
|
|
|
|
|
|
|
// LoadHTTPBasicAuthPassword the password to use when load requests require basic auth
|
|
|
|
var LoadHTTPBasicAuthPassword = ""
|
|
|
|
|
|
|
|
// LoadHTTPCustomHeaders an optional collection of custom HTTP headers for load requests
|
|
|
|
var LoadHTTPCustomHeaders = map[string]string{}
|
|
|
|
|
2019-09-04 15:53:54 -04:00
|
|
|
// LoadFromFileOrHTTP loads the bytes from a file or a remote http server based on the path passed in
|
|
|
|
func LoadFromFileOrHTTP(path string) ([]byte, error) {
|
|
|
|
return LoadStrategy(path, ioutil.ReadFile, loadHTTPBytes(LoadHTTPTimeout))(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFromFileOrHTTPWithTimeout loads the bytes from a file or a remote http server based on the path passed in
|
|
|
|
// timeout arg allows for per request overriding of the request timeout
|
|
|
|
func LoadFromFileOrHTTPWithTimeout(path string, timeout time.Duration) ([]byte, error) {
|
|
|
|
return LoadStrategy(path, ioutil.ReadFile, loadHTTPBytes(timeout))(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadStrategy returns a loader function for a given path or uri
|
|
|
|
func LoadStrategy(path string, local, remote func(string) ([]byte, error)) func(string) ([]byte, error) {
|
|
|
|
if strings.HasPrefix(path, "http") {
|
|
|
|
return remote
|
|
|
|
}
|
|
|
|
return func(pth string) ([]byte, error) {
|
2021-01-28 11:56:38 -05:00
|
|
|
upth, err := pathUnescape(strings.TrimPrefix(pth, `file://`))
|
2019-09-04 15:53:54 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return local(filepath.FromSlash(upth))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadHTTPBytes(timeout time.Duration) func(path string) ([]byte, error) {
|
|
|
|
return func(path string) ([]byte, error) {
|
|
|
|
client := &http.Client{Timeout: timeout}
|
2021-01-28 11:56:38 -05:00
|
|
|
req, err := http.NewRequest("GET", path, nil) // nolint: noctx
|
2019-09-04 15:53:54 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-01 10:01:23 -04:00
|
|
|
|
|
|
|
if LoadHTTPBasicAuthUsername != "" && LoadHTTPBasicAuthPassword != "" {
|
|
|
|
req.SetBasicAuth(LoadHTTPBasicAuthUsername, LoadHTTPBasicAuthPassword)
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range LoadHTTPCustomHeaders {
|
|
|
|
req.Header.Set(key, val)
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:53:54 -04:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
defer func() {
|
|
|
|
if resp != nil {
|
|
|
|
if e := resp.Body.Close(); e != nil {
|
|
|
|
log.Println(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("could not access document at %q [%s] ", path, resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.ReadAll(resp.Body)
|
|
|
|
}
|
|
|
|
}
|