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.

285 lines
5.7 KiB

9 years ago
7 years ago
10 years ago
  1. package util
  2. import (
  3. "bytes"
  4. "compress/gzip"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "net/http"
  11. "net/url"
  12. "strings"
  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{
  23. Transport: Transport,
  24. }
  25. }
  26. func PostBytes(url string, body []byte) ([]byte, error) {
  27. r, err := client.Post(url, "", bytes.NewReader(body))
  28. if err != nil {
  29. return nil, fmt.Errorf("Post to %s: %v", url, err)
  30. }
  31. defer r.Body.Close()
  32. if r.StatusCode >= 400 {
  33. return nil, fmt.Errorf("%s: %s", url, r.Status)
  34. }
  35. b, err := ioutil.ReadAll(r.Body)
  36. if err != nil {
  37. return nil, fmt.Errorf("Read response body: %v", err)
  38. }
  39. return b, nil
  40. }
  41. func Post(url string, values url.Values) ([]byte, error) {
  42. r, err := client.PostForm(url, values)
  43. if err != nil {
  44. return nil, err
  45. }
  46. defer r.Body.Close()
  47. b, err := ioutil.ReadAll(r.Body)
  48. if r.StatusCode >= 400 {
  49. if err != nil {
  50. return nil, fmt.Errorf("%s: %d - %s", url, r.StatusCode, string(b))
  51. } else {
  52. return nil, fmt.Errorf("%s: %s", url, r.Status)
  53. }
  54. }
  55. if err != nil {
  56. return nil, err
  57. }
  58. return b, nil
  59. }
  60. // github.com/chrislusf/seaweedfs/unmaintained/repeated_vacuum/repeated_vacuum.go
  61. // may need increasing http.Client.Timeout
  62. func Get(url string) ([]byte, error) {
  63. r, err := client.Get(url)
  64. if err != nil {
  65. return nil, err
  66. }
  67. defer r.Body.Close()
  68. b, err := ioutil.ReadAll(r.Body)
  69. if r.StatusCode >= 400 {
  70. return nil, fmt.Errorf("%s: %s", url, r.Status)
  71. }
  72. if err != nil {
  73. return nil, err
  74. }
  75. return b, nil
  76. }
  77. func Head(url string) (http.Header, error) {
  78. r, err := client.Head(url)
  79. if err != nil {
  80. return nil, err
  81. }
  82. defer r.Body.Close()
  83. if r.StatusCode >= 400 {
  84. return nil, fmt.Errorf("%s: %s", url, r.Status)
  85. }
  86. return r.Header, nil
  87. }
  88. func Delete(url string, jwt string) error {
  89. req, err := http.NewRequest("DELETE", url, nil)
  90. if jwt != "" {
  91. req.Header.Set("Authorization", "BEARER "+string(jwt))
  92. }
  93. if err != nil {
  94. return err
  95. }
  96. resp, e := client.Do(req)
  97. if e != nil {
  98. return e
  99. }
  100. defer resp.Body.Close()
  101. body, err := ioutil.ReadAll(resp.Body)
  102. if err != nil {
  103. return err
  104. }
  105. switch resp.StatusCode {
  106. case http.StatusNotFound, http.StatusAccepted, http.StatusOK:
  107. return nil
  108. }
  109. m := make(map[string]interface{})
  110. if e := json.Unmarshal(body, m); e == nil {
  111. if s, ok := m["error"].(string); ok {
  112. return errors.New(s)
  113. }
  114. }
  115. return errors.New(string(body))
  116. }
  117. func GetBufferStream(url string, values url.Values, allocatedBytes []byte, eachBuffer func([]byte)) error {
  118. r, err := client.PostForm(url, values)
  119. if err != nil {
  120. return err
  121. }
  122. defer r.Body.Close()
  123. if r.StatusCode != 200 {
  124. return fmt.Errorf("%s: %s", url, r.Status)
  125. }
  126. for {
  127. n, err := r.Body.Read(allocatedBytes)
  128. if n > 0 {
  129. eachBuffer(allocatedBytes[:n])
  130. }
  131. if err != nil {
  132. if err == io.EOF {
  133. return nil
  134. }
  135. return err
  136. }
  137. }
  138. }
  139. func GetUrlStream(url string, values url.Values, readFn func(io.Reader) error) error {
  140. r, err := client.PostForm(url, values)
  141. if err != nil {
  142. return err
  143. }
  144. defer r.Body.Close()
  145. if r.StatusCode != 200 {
  146. return fmt.Errorf("%s: %s", url, r.Status)
  147. }
  148. return readFn(r.Body)
  149. }
  150. func DownloadFile(fileUrl string) (filename string, header http.Header, rc io.ReadCloser, e error) {
  151. response, err := client.Get(fileUrl)
  152. if err != nil {
  153. return "", nil, nil, err
  154. }
  155. header = response.Header
  156. contentDisposition := response.Header["Content-Disposition"]
  157. if len(contentDisposition) > 0 {
  158. idx := strings.Index(contentDisposition[0], "filename=")
  159. if idx != -1 {
  160. filename = contentDisposition[0][idx+len("filename="):]
  161. filename = strings.Trim(filename, "\"")
  162. }
  163. }
  164. rc = response.Body
  165. return
  166. }
  167. func Do(req *http.Request) (resp *http.Response, err error) {
  168. return client.Do(req)
  169. }
  170. func NormalizeUrl(url string) string {
  171. if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
  172. return url
  173. }
  174. return "http://" + url
  175. }
  176. func ReadUrl(fileUrl string, offset int64, size int, buf []byte, isReadRange bool) (int64, error) {
  177. req, err := http.NewRequest("GET", fileUrl, nil)
  178. if err != nil {
  179. return 0, err
  180. }
  181. if isReadRange {
  182. req.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+int64(size)))
  183. } else {
  184. req.Header.Set("Accept-Encoding", "gzip")
  185. }
  186. r, err := client.Do(req)
  187. if err != nil {
  188. return 0, err
  189. }
  190. defer r.Body.Close()
  191. if r.StatusCode >= 400 {
  192. return 0, fmt.Errorf("%s: %s", fileUrl, r.Status)
  193. }
  194. var reader io.ReadCloser
  195. switch r.Header.Get("Content-Encoding") {
  196. case "gzip":
  197. reader, err = gzip.NewReader(r.Body)
  198. defer reader.Close()
  199. default:
  200. reader = r.Body
  201. }
  202. var (
  203. i, m int
  204. n int64
  205. )
  206. // refers to https://github.com/golang/go/blob/master/src/bytes/buffer.go#L199
  207. // commit id c170b14c2c1cfb2fd853a37add92a82fd6eb4318
  208. for {
  209. m, err = reader.Read(buf[i:])
  210. i += m
  211. n += int64(m)
  212. if err == io.EOF {
  213. return n, nil
  214. }
  215. if err != nil {
  216. return n, err
  217. }
  218. if n == int64(len(buf)) {
  219. break
  220. }
  221. }
  222. // drains the response body to avoid memory leak
  223. data, _ := ioutil.ReadAll(reader)
  224. if len(data) != 0 {
  225. err = fmt.Errorf("buffer size is too small. remains %d", len(data))
  226. }
  227. return n, err
  228. }
  229. func ReadUrlAsStream(fileUrl string, offset int64, size int, fn func(data []byte)) (int64, error) {
  230. req, err := http.NewRequest("GET", fileUrl, nil)
  231. if err != nil {
  232. return 0, err
  233. }
  234. req.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+int64(size)))
  235. r, err := client.Do(req)
  236. if err != nil {
  237. return 0, err
  238. }
  239. defer r.Body.Close()
  240. if r.StatusCode >= 400 {
  241. return 0, fmt.Errorf("%s: %s", fileUrl, r.Status)
  242. }
  243. var (
  244. m int
  245. n int64
  246. )
  247. buf := make([]byte, 64*1024)
  248. for {
  249. m, err = r.Body.Read(buf)
  250. fn(buf[:m])
  251. n += int64(m)
  252. if err == io.EOF {
  253. return n, nil
  254. }
  255. if err != nil {
  256. return n, err
  257. }
  258. }
  259. }