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.

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