You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
3.1 KiB

10 years ago
10 years ago
10 years ago
  1. package util
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "github.com/chrislusf/seaweedfs/go/security"
  11. )
  12. var (
  13. client *http.Client
  14. Transport *http.Transport
  15. )
  16. func init() {
  17. Transport = &http.Transport{
  18. MaxIdleConnsPerHost: 1024,
  19. }
  20. client = &http.Client{Transport: Transport}
  21. }
  22. func PostBytes(url string, body []byte) ([]byte, error) {
  23. r, err := client.Post(url, "application/octet-stream", bytes.NewReader(body))
  24. if err != nil {
  25. return nil, fmt.Errorf("Post to %s: %v", url, err)
  26. }
  27. defer r.Body.Close()
  28. b, err := ioutil.ReadAll(r.Body)
  29. if err != nil {
  30. return nil, fmt.Errorf("Read response body: %v", err)
  31. }
  32. return b, nil
  33. }
  34. func Post(url string, values url.Values) ([]byte, error) {
  35. r, err := client.PostForm(url, values)
  36. if err != nil {
  37. return nil, err
  38. }
  39. defer r.Body.Close()
  40. b, err := ioutil.ReadAll(r.Body)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return b, nil
  45. }
  46. func Get(url string) ([]byte, error) {
  47. r, err := client.Get(url)
  48. if err != nil {
  49. return nil, err
  50. }
  51. defer r.Body.Close()
  52. b, err := ioutil.ReadAll(r.Body)
  53. if r.StatusCode != 200 {
  54. return nil, fmt.Errorf("%s: %s", url, r.Status)
  55. }
  56. if err != nil {
  57. return nil, err
  58. }
  59. return b, nil
  60. }
  61. func Delete(url string, jwt security.EncodedJwt) error {
  62. req, err := http.NewRequest("DELETE", url, nil)
  63. if jwt != "" {
  64. req.Header.Set("Authorization", "BEARER "+string(jwt))
  65. }
  66. if err != nil {
  67. return err
  68. }
  69. resp, e := client.Do(req)
  70. if e != nil {
  71. return e
  72. }
  73. defer resp.Body.Close()
  74. if _, err := ioutil.ReadAll(resp.Body); err != nil {
  75. return err
  76. }
  77. return nil
  78. }
  79. func GetBufferStream(url string, values url.Values, allocatedBytes []byte, eachBuffer func([]byte)) error {
  80. r, err := client.PostForm(url, values)
  81. if err != nil {
  82. return err
  83. }
  84. defer r.Body.Close()
  85. if r.StatusCode != 200 {
  86. return fmt.Errorf("%s: %s", url, r.Status)
  87. }
  88. bufferSize := len(allocatedBytes)
  89. for {
  90. n, err := r.Body.Read(allocatedBytes)
  91. if n == bufferSize {
  92. eachBuffer(allocatedBytes)
  93. }
  94. if err != nil {
  95. if err == io.EOF {
  96. return nil
  97. }
  98. return err
  99. }
  100. }
  101. return nil
  102. }
  103. func GetUrlStream(url string, values url.Values, readFn func(io.Reader) error) error {
  104. r, err := client.PostForm(url, values)
  105. if err != nil {
  106. return err
  107. }
  108. defer r.Body.Close()
  109. if r.StatusCode != 200 {
  110. return fmt.Errorf("%s: %s", url, r.Status)
  111. }
  112. return readFn(r.Body)
  113. }
  114. func DownloadUrl(fileUrl string) (filename string, content []byte, e error) {
  115. response, err := client.Get(fileUrl)
  116. if err != nil {
  117. return "", nil, err
  118. }
  119. defer response.Body.Close()
  120. contentDisposition := response.Header["Content-Disposition"]
  121. if len(contentDisposition) > 0 {
  122. if strings.HasPrefix(contentDisposition[0], "filename=") {
  123. filename = contentDisposition[0][len("filename="):]
  124. filename = strings.Trim(filename, "\"")
  125. }
  126. }
  127. content, e = ioutil.ReadAll(response.Body)
  128. return
  129. }
  130. func Do(req *http.Request) (resp *http.Response, err error) {
  131. return client.Do(req)
  132. }
  133. func NormalizeUrl(url string) string {
  134. if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
  135. return url
  136. }
  137. return "http://" + url
  138. }