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.

163 lines
3.3 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. b, err := ioutil.ReadAll(r.Body)
  31. if err != nil {
  32. return nil, fmt.Errorf("Read response body: %v", err)
  33. }
  34. return b, nil
  35. }
  36. func Post(url string, values url.Values) ([]byte, error) {
  37. r, err := client.PostForm(url, values)
  38. if err != nil {
  39. return nil, err
  40. }
  41. defer r.Body.Close()
  42. b, err := ioutil.ReadAll(r.Body)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return b, nil
  47. }
  48. func Get(url string) ([]byte, error) {
  49. r, err := client.Get(url)
  50. if err != nil {
  51. return nil, err
  52. }
  53. defer r.Body.Close()
  54. b, err := ioutil.ReadAll(r.Body)
  55. if r.StatusCode != 200 {
  56. return nil, fmt.Errorf("%s: %s", url, r.Status)
  57. }
  58. if err != nil {
  59. return nil, err
  60. }
  61. return b, nil
  62. }
  63. func Delete(url string, jwt security.EncodedJwt) error {
  64. req, err := http.NewRequest("DELETE", url, nil)
  65. if jwt != "" {
  66. req.Header.Set("Authorization", "BEARER "+string(jwt))
  67. }
  68. if err != nil {
  69. return err
  70. }
  71. resp, e := client.Do(req)
  72. if e != nil {
  73. return e
  74. }
  75. defer resp.Body.Close()
  76. body, err := ioutil.ReadAll(resp.Body)
  77. if err != nil {
  78. return err
  79. }
  80. switch resp.StatusCode {
  81. case http.StatusNotFound, http.StatusAccepted, http.StatusOK:
  82. return nil
  83. }
  84. m := make(map[string]interface{})
  85. if e := json.Unmarshal(body, m); e == nil {
  86. if s, ok := m["error"].(string); ok {
  87. return errors.New(s)
  88. }
  89. }
  90. return errors.New(string(body))
  91. }
  92. func GetBufferStream(url string, values url.Values, allocatedBytes []byte, eachBuffer func([]byte)) error {
  93. r, err := client.PostForm(url, values)
  94. if err != nil {
  95. return err
  96. }
  97. defer r.Body.Close()
  98. if r.StatusCode != 200 {
  99. return fmt.Errorf("%s: %s", url, r.Status)
  100. }
  101. bufferSize := len(allocatedBytes)
  102. for {
  103. n, err := r.Body.Read(allocatedBytes)
  104. if n == bufferSize {
  105. eachBuffer(allocatedBytes)
  106. }
  107. if err != nil {
  108. if err == io.EOF {
  109. return nil
  110. }
  111. return err
  112. }
  113. }
  114. return nil
  115. }
  116. func GetUrlStream(url string, values url.Values, readFn func(io.Reader) error) error {
  117. r, err := client.PostForm(url, values)
  118. if err != nil {
  119. return err
  120. }
  121. defer r.Body.Close()
  122. if r.StatusCode != 200 {
  123. return fmt.Errorf("%s: %s", url, r.Status)
  124. }
  125. return readFn(r.Body)
  126. }
  127. func DownloadUrl(fileUrl string) (filename string, rc io.ReadCloser, e error) {
  128. response, err := client.Get(fileUrl)
  129. if err != nil {
  130. return "", nil, err
  131. }
  132. contentDisposition := response.Header["Content-Disposition"]
  133. if len(contentDisposition) > 0 {
  134. if strings.HasPrefix(contentDisposition[0], "filename=") {
  135. filename = contentDisposition[0][len("filename="):]
  136. filename = strings.Trim(filename, "\"")
  137. }
  138. }
  139. rc = response.Body
  140. return
  141. }
  142. func Do(req *http.Request) (resp *http.Response, err error) {
  143. return client.Do(req)
  144. }
  145. func NormalizeUrl(url string) string {
  146. if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
  147. return url
  148. }
  149. return "http://" + url
  150. }