loading.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2015 go-swagger maintainers
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package swag
  15. import (
  16. "fmt"
  17. "io"
  18. "log"
  19. "net/http"
  20. "net/url"
  21. "os"
  22. "path"
  23. "path/filepath"
  24. "runtime"
  25. "strings"
  26. "time"
  27. )
  28. // LoadHTTPTimeout the default timeout for load requests
  29. var LoadHTTPTimeout = 30 * time.Second
  30. // LoadHTTPBasicAuthUsername the username to use when load requests require basic auth
  31. var LoadHTTPBasicAuthUsername = ""
  32. // LoadHTTPBasicAuthPassword the password to use when load requests require basic auth
  33. var LoadHTTPBasicAuthPassword = ""
  34. // LoadHTTPCustomHeaders an optional collection of custom HTTP headers for load requests
  35. var LoadHTTPCustomHeaders = map[string]string{}
  36. // LoadFromFileOrHTTP loads the bytes from a file or a remote http server based on the path passed in
  37. func LoadFromFileOrHTTP(pth string) ([]byte, error) {
  38. return LoadStrategy(pth, os.ReadFile, loadHTTPBytes(LoadHTTPTimeout))(pth)
  39. }
  40. // LoadFromFileOrHTTPWithTimeout loads the bytes from a file or a remote http server based on the path passed in
  41. // timeout arg allows for per request overriding of the request timeout
  42. func LoadFromFileOrHTTPWithTimeout(pth string, timeout time.Duration) ([]byte, error) {
  43. return LoadStrategy(pth, os.ReadFile, loadHTTPBytes(timeout))(pth)
  44. }
  45. // LoadStrategy returns a loader function for a given path or URI.
  46. //
  47. // The load strategy returns the remote load for any path starting with `http`.
  48. // So this works for any URI with a scheme `http` or `https`.
  49. //
  50. // The fallback strategy is to call the local loader.
  51. //
  52. // The local loader takes a local file system path (absolute or relative) as argument,
  53. // or alternatively a `file://...` URI, **without host** (see also below for windows).
  54. //
  55. // There are a few liberalities, initially intended to be tolerant regarding the URI syntax,
  56. // especially on windows.
  57. //
  58. // Before the local loader is called, the given path is transformed:
  59. // - percent-encoded characters are unescaped
  60. // - simple paths (e.g. `./folder/file`) are passed as-is
  61. // - on windows, occurrences of `/` are replaced by `\`, so providing a relative path such a `folder/file` works too.
  62. //
  63. // For paths provided as URIs with the "file" scheme, please note that:
  64. // - `file://` is simply stripped.
  65. // This means that the host part of the URI is not parsed at all.
  66. // For example, `file:///folder/file" becomes "/folder/file`,
  67. // but `file://localhost/folder/file` becomes `localhost/folder/file` on unix systems.
  68. // Similarly, `file://./folder/file` yields `./folder/file`.
  69. // - on windows, `file://...` can take a host so as to specify an UNC share location.
  70. //
  71. // Reminder about windows-specifics:
  72. // - `file://host/folder/file` becomes an UNC path like `\\host\folder\file` (no port specification is supported)
  73. // - `file:///c:/folder/file` becomes `C:\folder\file`
  74. // - `file://c:/folder/file` is tolerated (without leading `/`) and becomes `c:\folder\file`
  75. func LoadStrategy(pth string, local, remote func(string) ([]byte, error)) func(string) ([]byte, error) {
  76. if strings.HasPrefix(pth, "http") {
  77. return remote
  78. }
  79. return func(p string) ([]byte, error) {
  80. upth, err := url.PathUnescape(p)
  81. if err != nil {
  82. return nil, err
  83. }
  84. if !strings.HasPrefix(p, `file://`) {
  85. // regular file path provided: just normalize slashes
  86. return local(filepath.FromSlash(upth))
  87. }
  88. if runtime.GOOS != "windows" {
  89. // crude processing: this leaves full URIs with a host with a (mostly) unexpected result
  90. upth = strings.TrimPrefix(upth, `file://`)
  91. return local(filepath.FromSlash(upth))
  92. }
  93. // windows-only pre-processing of file://... URIs
  94. // support for canonical file URIs on windows.
  95. u, err := url.Parse(filepath.ToSlash(upth))
  96. if err != nil {
  97. return nil, err
  98. }
  99. if u.Host != "" {
  100. // assume UNC name (volume share)
  101. // NOTE: UNC port not yet supported
  102. // when the "host" segment is a drive letter:
  103. // file://C:/folder/... => C:\folder
  104. upth = path.Clean(strings.Join([]string{u.Host, u.Path}, `/`))
  105. if !strings.HasSuffix(u.Host, ":") && u.Host[0] != '.' {
  106. // tolerance: if we have a leading dot, this can't be a host
  107. // file://host/share/folder\... ==> \\host\share\path\folder
  108. upth = "//" + upth
  109. }
  110. } else {
  111. // no host, let's figure out if this is a drive letter
  112. upth = strings.TrimPrefix(upth, `file://`)
  113. first, _, _ := strings.Cut(strings.TrimPrefix(u.Path, "/"), "/")
  114. if strings.HasSuffix(first, ":") {
  115. // drive letter in the first segment:
  116. // file:///c:/folder/... ==> strip the leading slash
  117. upth = strings.TrimPrefix(upth, `/`)
  118. }
  119. }
  120. return local(filepath.FromSlash(upth))
  121. }
  122. }
  123. func loadHTTPBytes(timeout time.Duration) func(path string) ([]byte, error) {
  124. return func(path string) ([]byte, error) {
  125. client := &http.Client{Timeout: timeout}
  126. req, err := http.NewRequest(http.MethodGet, path, nil) //nolint:noctx
  127. if err != nil {
  128. return nil, err
  129. }
  130. if LoadHTTPBasicAuthUsername != "" && LoadHTTPBasicAuthPassword != "" {
  131. req.SetBasicAuth(LoadHTTPBasicAuthUsername, LoadHTTPBasicAuthPassword)
  132. }
  133. for key, val := range LoadHTTPCustomHeaders {
  134. req.Header.Set(key, val)
  135. }
  136. resp, err := client.Do(req)
  137. defer func() {
  138. if resp != nil {
  139. if e := resp.Body.Close(); e != nil {
  140. log.Println(e)
  141. }
  142. }
  143. }()
  144. if err != nil {
  145. return nil, err
  146. }
  147. if resp.StatusCode != http.StatusOK {
  148. return nil, fmt.Errorf("could not access document at %q [%s] ", path, resp.Status)
  149. }
  150. return io.ReadAll(resp.Body)
  151. }
  152. }