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.

114 lines
2.4 KiB

  1. package util
  2. import (
  3. "bytes"
  4. "code.google.com/p/weed-fs/go/glog"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. )
  11. var (
  12. client *http.Client
  13. Transport *http.Transport
  14. )
  15. func init() {
  16. Transport = &http.Transport{
  17. MaxIdleConnsPerHost: 1024,
  18. }
  19. client = &http.Client{Transport: Transport}
  20. }
  21. func PostBytes(url string, body []byte) ([]byte, error) {
  22. r, err := client.Post(url, "application/octet-stream", bytes.NewReader(body))
  23. if err != nil {
  24. glog.V(0).Infoln(err)
  25. return nil, err
  26. }
  27. defer r.Body.Close()
  28. b, err := ioutil.ReadAll(r.Body)
  29. if err != nil {
  30. glog.V(0).Infoln("read post result from", url, err)
  31. return nil, err
  32. }
  33. return b, nil
  34. }
  35. func Post(url string, values url.Values) ([]byte, error) {
  36. r, err := client.PostForm(url, values)
  37. if err != nil {
  38. glog.V(0).Infoln(err)
  39. return nil, err
  40. }
  41. defer r.Body.Close()
  42. b, err := ioutil.ReadAll(r.Body)
  43. if err != nil {
  44. glog.V(0).Infoln("read post result from", url, err)
  45. return nil, err
  46. }
  47. return b, nil
  48. }
  49. func Get(url string) ([]byte, error) {
  50. r, err := client.Get(url)
  51. if err != nil {
  52. glog.V(0).Infoln(err)
  53. return nil, err
  54. }
  55. defer r.Body.Close()
  56. b, err := ioutil.ReadAll(r.Body)
  57. if r.StatusCode != 200 {
  58. return nil, fmt.Errorf("%s: %s", url, r.Status)
  59. }
  60. if err != nil {
  61. glog.V(0).Infoln("read get result from", url, err)
  62. return nil, err
  63. }
  64. return b, nil
  65. }
  66. func Delete(url string) error {
  67. req, err := http.NewRequest("DELETE", url, nil)
  68. if err != nil {
  69. glog.V(0).Infoln("failing to delete", url)
  70. return err
  71. }
  72. resp, e := client.Do(req)
  73. if e != nil {
  74. glog.V(0).Infoln(e)
  75. return e
  76. }
  77. defer resp.Body.Close()
  78. if _, err := ioutil.ReadAll(resp.Body); err != nil {
  79. glog.V(0).Infoln("read get result from", url, err)
  80. return err
  81. }
  82. return nil
  83. }
  84. func DownloadUrl(fileUrl string) (filename string, content []byte, e error) {
  85. response, err := client.Get(fileUrl)
  86. if err != nil {
  87. return "", nil, err
  88. }
  89. defer response.Body.Close()
  90. contentDisposition := response.Header["Content-Disposition"]
  91. if len(contentDisposition) > 0 {
  92. glog.V(4).Info("Content-Disposition: ", contentDisposition[0])
  93. if strings.HasPrefix(contentDisposition[0], "filename=") {
  94. filename = contentDisposition[0][len("filename="):]
  95. filename = strings.Trim(filename, "\"")
  96. }
  97. } else {
  98. glog.V(4).Info("No Content-Disposition!")
  99. }
  100. content, e = ioutil.ReadAll(response.Body)
  101. return
  102. }
  103. func Do(req *http.Request) (resp *http.Response, err error) {
  104. return client.Do(req)
  105. }