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.

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