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.

172 lines
3.6 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. if resp.StatusCode >= 400 {
  83. return fmt.Errorf("%s: %s", url, resp.Status)
  84. }
  85. body, err := ioutil.ReadAll(resp.Body)
  86. if err != nil {
  87. return err
  88. }
  89. switch resp.StatusCode {
  90. case http.StatusNotFound, http.StatusAccepted, http.StatusOK:
  91. return nil
  92. }
  93. m := make(map[string]interface{})
  94. if e := json.Unmarshal(body, m); e == nil {
  95. if s, ok := m["error"].(string); ok {
  96. return errors.New(s)
  97. }
  98. }
  99. return errors.New(string(body))
  100. }
  101. func GetBufferStream(url string, values url.Values, allocatedBytes []byte, eachBuffer func([]byte)) error {
  102. r, err := client.PostForm(url, values)
  103. if err != nil {
  104. return err
  105. }
  106. defer r.Body.Close()
  107. if r.StatusCode != 200 {
  108. return fmt.Errorf("%s: %s", url, r.Status)
  109. }
  110. bufferSize := len(allocatedBytes)
  111. for {
  112. n, err := r.Body.Read(allocatedBytes)
  113. if n == bufferSize {
  114. eachBuffer(allocatedBytes)
  115. }
  116. if err != nil {
  117. if err == io.EOF {
  118. return nil
  119. }
  120. return err
  121. }
  122. }
  123. return nil
  124. }
  125. func GetUrlStream(url string, values url.Values, readFn func(io.Reader) error) error {
  126. r, err := client.PostForm(url, values)
  127. if err != nil {
  128. return err
  129. }
  130. defer r.Body.Close()
  131. if r.StatusCode != 200 {
  132. return fmt.Errorf("%s: %s", url, r.Status)
  133. }
  134. return readFn(r.Body)
  135. }
  136. func DownloadUrl(fileUrl string) (filename string, rc io.ReadCloser, e error) {
  137. response, err := client.Get(fileUrl)
  138. if err != nil {
  139. return "", nil, err
  140. }
  141. contentDisposition := response.Header["Content-Disposition"]
  142. if len(contentDisposition) > 0 {
  143. if strings.HasPrefix(contentDisposition[0], "filename=") {
  144. filename = contentDisposition[0][len("filename="):]
  145. filename = strings.Trim(filename, "\"")
  146. }
  147. }
  148. rc = response.Body
  149. return
  150. }
  151. func Do(req *http.Request) (resp *http.Response, err error) {
  152. return client.Do(req)
  153. }
  154. func NormalizeUrl(url string) string {
  155. if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
  156. return url
  157. }
  158. return "http://" + url
  159. }