http_util.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. package util
  2. import (
  3. "compress/gzip"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/seaweedfs/seaweedfs/weed/util/mem"
  8. "io"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. "github.com/seaweedfs/seaweedfs/weed/glog"
  13. )
  14. var (
  15. client *http.Client
  16. Transport *http.Transport
  17. )
  18. func init() {
  19. Transport = &http.Transport{
  20. MaxIdleConns: 1024,
  21. MaxIdleConnsPerHost: 1024,
  22. }
  23. client = &http.Client{
  24. Transport: Transport,
  25. }
  26. }
  27. func Post(url string, values url.Values) ([]byte, error) {
  28. r, err := client.PostForm(url, values)
  29. if err != nil {
  30. return nil, err
  31. }
  32. defer r.Body.Close()
  33. b, err := io.ReadAll(r.Body)
  34. if r.StatusCode >= 400 {
  35. if err != nil {
  36. return nil, fmt.Errorf("%s: %d - %s", url, r.StatusCode, string(b))
  37. } else {
  38. return nil, fmt.Errorf("%s: %s", url, r.Status)
  39. }
  40. }
  41. if err != nil {
  42. return nil, err
  43. }
  44. return b, nil
  45. }
  46. // github.com/seaweedfs/seaweedfs/unmaintained/repeated_vacuum/repeated_vacuum.go
  47. // may need increasing http.Client.Timeout
  48. func Get(url string) ([]byte, bool, error) {
  49. request, err := http.NewRequest("GET", url, nil)
  50. request.Header.Add("Accept-Encoding", "gzip")
  51. response, err := client.Do(request)
  52. if err != nil {
  53. return nil, true, err
  54. }
  55. defer CloseResponse(response)
  56. var reader io.ReadCloser
  57. switch response.Header.Get("Content-Encoding") {
  58. case "gzip":
  59. reader, err = gzip.NewReader(response.Body)
  60. defer reader.Close()
  61. default:
  62. reader = response.Body
  63. }
  64. b, err := io.ReadAll(reader)
  65. if response.StatusCode >= 400 {
  66. retryable := response.StatusCode >= 500
  67. return nil, retryable, fmt.Errorf("%s: %s", url, response.Status)
  68. }
  69. if err != nil {
  70. return nil, false, err
  71. }
  72. return b, false, nil
  73. }
  74. func Head(url string) (http.Header, error) {
  75. r, err := client.Head(url)
  76. if err != nil {
  77. return nil, err
  78. }
  79. defer CloseResponse(r)
  80. if r.StatusCode >= 400 {
  81. return nil, fmt.Errorf("%s: %s", url, r.Status)
  82. }
  83. return r.Header, nil
  84. }
  85. func Delete(url string, jwt string) error {
  86. req, err := http.NewRequest("DELETE", url, nil)
  87. if jwt != "" {
  88. req.Header.Set("Authorization", "BEARER "+string(jwt))
  89. }
  90. if err != nil {
  91. return err
  92. }
  93. resp, e := client.Do(req)
  94. if e != nil {
  95. return e
  96. }
  97. defer resp.Body.Close()
  98. body, err := io.ReadAll(resp.Body)
  99. if err != nil {
  100. return err
  101. }
  102. switch resp.StatusCode {
  103. case http.StatusNotFound, http.StatusAccepted, http.StatusOK:
  104. return nil
  105. }
  106. m := make(map[string]interface{})
  107. if e := json.Unmarshal(body, &m); e == nil {
  108. if s, ok := m["error"].(string); ok {
  109. return errors.New(s)
  110. }
  111. }
  112. return errors.New(string(body))
  113. }
  114. func DeleteProxied(url string, jwt string) (body []byte, httpStatus int, err error) {
  115. req, err := http.NewRequest("DELETE", url, nil)
  116. if jwt != "" {
  117. req.Header.Set("Authorization", "BEARER "+string(jwt))
  118. }
  119. if err != nil {
  120. return
  121. }
  122. resp, err := client.Do(req)
  123. if err != nil {
  124. return
  125. }
  126. defer resp.Body.Close()
  127. body, err = io.ReadAll(resp.Body)
  128. if err != nil {
  129. return
  130. }
  131. httpStatus = resp.StatusCode
  132. return
  133. }
  134. func GetBufferStream(url string, values url.Values, allocatedBytes []byte, eachBuffer func([]byte)) error {
  135. r, err := client.PostForm(url, values)
  136. if err != nil {
  137. return err
  138. }
  139. defer CloseResponse(r)
  140. if r.StatusCode != 200 {
  141. return fmt.Errorf("%s: %s", url, r.Status)
  142. }
  143. for {
  144. n, err := r.Body.Read(allocatedBytes)
  145. if n > 0 {
  146. eachBuffer(allocatedBytes[:n])
  147. }
  148. if err != nil {
  149. if err == io.EOF {
  150. return nil
  151. }
  152. return err
  153. }
  154. }
  155. }
  156. func GetUrlStream(url string, values url.Values, readFn func(io.Reader) error) error {
  157. r, err := client.PostForm(url, values)
  158. if err != nil {
  159. return err
  160. }
  161. defer CloseResponse(r)
  162. if r.StatusCode != 200 {
  163. return fmt.Errorf("%s: %s", url, r.Status)
  164. }
  165. return readFn(r.Body)
  166. }
  167. func DownloadFile(fileUrl string, jwt string) (filename string, header http.Header, resp *http.Response, e error) {
  168. req, err := http.NewRequest("GET", fileUrl, nil)
  169. if err != nil {
  170. return "", nil, nil, err
  171. }
  172. if len(jwt) > 0 {
  173. req.Header.Set("Authorization", "BEARER "+jwt)
  174. }
  175. response, err := client.Do(req)
  176. if err != nil {
  177. return "", nil, nil, err
  178. }
  179. header = response.Header
  180. contentDisposition := response.Header["Content-Disposition"]
  181. if len(contentDisposition) > 0 {
  182. idx := strings.Index(contentDisposition[0], "filename=")
  183. if idx != -1 {
  184. filename = contentDisposition[0][idx+len("filename="):]
  185. filename = strings.Trim(filename, "\"")
  186. }
  187. }
  188. resp = response
  189. return
  190. }
  191. func Do(req *http.Request) (resp *http.Response, err error) {
  192. return client.Do(req)
  193. }
  194. func NormalizeUrl(url string) string {
  195. if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
  196. return url
  197. }
  198. return "http://" + url
  199. }
  200. func ReadUrl(fileUrl string, cipherKey []byte, isContentCompressed bool, isFullChunk bool, offset int64, size int, buf []byte) (int64, error) {
  201. if cipherKey != nil {
  202. var n int
  203. _, err := readEncryptedUrl(fileUrl, cipherKey, isContentCompressed, isFullChunk, offset, size, func(data []byte) {
  204. n = copy(buf, data)
  205. })
  206. return int64(n), err
  207. }
  208. req, err := http.NewRequest("GET", fileUrl, nil)
  209. if err != nil {
  210. return 0, err
  211. }
  212. if !isFullChunk {
  213. req.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+int64(size)-1))
  214. } else {
  215. req.Header.Set("Accept-Encoding", "gzip")
  216. }
  217. r, err := client.Do(req)
  218. if err != nil {
  219. return 0, err
  220. }
  221. defer CloseResponse(r)
  222. if r.StatusCode >= 400 {
  223. return 0, fmt.Errorf("%s: %s", fileUrl, r.Status)
  224. }
  225. var reader io.ReadCloser
  226. contentEncoding := r.Header.Get("Content-Encoding")
  227. switch contentEncoding {
  228. case "gzip":
  229. reader, err = gzip.NewReader(r.Body)
  230. defer reader.Close()
  231. default:
  232. reader = r.Body
  233. }
  234. var (
  235. i, m int
  236. n int64
  237. )
  238. // refers to https://github.com/golang/go/blob/master/src/bytes/buffer.go#L199
  239. // commit id c170b14c2c1cfb2fd853a37add92a82fd6eb4318
  240. for {
  241. m, err = reader.Read(buf[i:])
  242. i += m
  243. n += int64(m)
  244. if err == io.EOF {
  245. return n, nil
  246. }
  247. if err != nil {
  248. return n, err
  249. }
  250. if n == int64(len(buf)) {
  251. break
  252. }
  253. }
  254. // drains the response body to avoid memory leak
  255. data, _ := io.ReadAll(reader)
  256. if len(data) != 0 {
  257. glog.V(1).Infof("%s reader has remaining %d bytes", contentEncoding, len(data))
  258. }
  259. return n, err
  260. }
  261. func ReadUrlAsStream(fileUrl string, cipherKey []byte, isContentGzipped bool, isFullChunk bool, offset int64, size int, fn func(data []byte)) (retryable bool, err error) {
  262. if cipherKey != nil {
  263. return readEncryptedUrl(fileUrl, cipherKey, isContentGzipped, isFullChunk, offset, size, fn)
  264. }
  265. req, err := http.NewRequest("GET", fileUrl, nil)
  266. if err != nil {
  267. return false, err
  268. }
  269. if isFullChunk {
  270. req.Header.Add("Accept-Encoding", "gzip")
  271. } else {
  272. req.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+int64(size)-1))
  273. }
  274. r, err := client.Do(req)
  275. if err != nil {
  276. return true, err
  277. }
  278. defer CloseResponse(r)
  279. if r.StatusCode >= 400 {
  280. retryable = r.StatusCode == http.StatusNotFound || r.StatusCode >= 500
  281. return retryable, fmt.Errorf("%s: %s", fileUrl, r.Status)
  282. }
  283. var reader io.ReadCloser
  284. contentEncoding := r.Header.Get("Content-Encoding")
  285. switch contentEncoding {
  286. case "gzip":
  287. reader, err = gzip.NewReader(r.Body)
  288. defer reader.Close()
  289. default:
  290. reader = r.Body
  291. }
  292. var (
  293. m int
  294. )
  295. buf := mem.Allocate(64 * 1024)
  296. defer mem.Free(buf)
  297. for {
  298. m, err = reader.Read(buf)
  299. if m > 0 {
  300. fn(buf[:m])
  301. }
  302. if err == io.EOF {
  303. return false, nil
  304. }
  305. if err != nil {
  306. return true, err
  307. }
  308. }
  309. }
  310. func readEncryptedUrl(fileUrl string, cipherKey []byte, isContentCompressed bool, isFullChunk bool, offset int64, size int, fn func(data []byte)) (bool, error) {
  311. encryptedData, retryable, err := Get(fileUrl)
  312. if err != nil {
  313. return retryable, fmt.Errorf("fetch %s: %v", fileUrl, err)
  314. }
  315. decryptedData, err := Decrypt(encryptedData, CipherKey(cipherKey))
  316. if err != nil {
  317. return false, fmt.Errorf("decrypt %s: %v", fileUrl, err)
  318. }
  319. if isContentCompressed {
  320. decryptedData, err = DecompressData(decryptedData)
  321. if err != nil {
  322. glog.V(0).Infof("unzip decrypt %s: %v", fileUrl, err)
  323. }
  324. }
  325. if len(decryptedData) < int(offset)+size {
  326. return false, fmt.Errorf("read decrypted %s size %d [%d, %d)", fileUrl, len(decryptedData), offset, int(offset)+size)
  327. }
  328. if isFullChunk {
  329. fn(decryptedData)
  330. } else {
  331. fn(decryptedData[int(offset) : int(offset)+size])
  332. }
  333. return false, nil
  334. }
  335. func ReadUrlAsReaderCloser(fileUrl string, jwt string, rangeHeader string) (*http.Response, io.ReadCloser, error) {
  336. req, err := http.NewRequest("GET", fileUrl, nil)
  337. if err != nil {
  338. return nil, nil, err
  339. }
  340. if rangeHeader != "" {
  341. req.Header.Add("Range", rangeHeader)
  342. } else {
  343. req.Header.Add("Accept-Encoding", "gzip")
  344. }
  345. if len(jwt) > 0 {
  346. req.Header.Set("Authorization", "BEARER "+jwt)
  347. }
  348. r, err := client.Do(req)
  349. if err != nil {
  350. return nil, nil, err
  351. }
  352. if r.StatusCode >= 400 {
  353. CloseResponse(r)
  354. return nil, nil, fmt.Errorf("%s: %s", fileUrl, r.Status)
  355. }
  356. var reader io.ReadCloser
  357. contentEncoding := r.Header.Get("Content-Encoding")
  358. switch contentEncoding {
  359. case "gzip":
  360. reader, err = gzip.NewReader(r.Body)
  361. default:
  362. reader = r.Body
  363. }
  364. return r, reader, nil
  365. }
  366. func CloseResponse(resp *http.Response) {
  367. if resp == nil || resp.Body == nil {
  368. return
  369. }
  370. reader := &CountingReader{reader: resp.Body}
  371. io.Copy(io.Discard, reader)
  372. resp.Body.Close()
  373. if reader.BytesRead > 0 {
  374. glog.V(1).Infof("response leftover %d bytes", reader.BytesRead)
  375. }
  376. }
  377. func CloseRequest(req *http.Request) {
  378. reader := &CountingReader{reader: req.Body}
  379. io.Copy(io.Discard, reader)
  380. req.Body.Close()
  381. if reader.BytesRead > 0 {
  382. glog.V(1).Infof("request leftover %d bytes", reader.BytesRead)
  383. }
  384. }
  385. type CountingReader struct {
  386. reader io.Reader
  387. BytesRead int
  388. }
  389. func (r *CountingReader) Read(p []byte) (n int, err error) {
  390. n, err = r.reader.Read(p)
  391. r.BytesRead += n
  392. return n, err
  393. }