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.

169 lines
3.5 KiB

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