client.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package web
  2. import (
  3. "compress/gzip"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "io"
  7. "io/ioutil"
  8. "net"
  9. "net/http"
  10. "time"
  11. )
  12. const (
  13. // RequestTimeOut http request timeout (second)
  14. RequestTimeOut = 30
  15. )
  16. // Message HTTP response
  17. type Message struct {
  18. StatusCode int
  19. Body []byte
  20. Header http.Header
  21. }
  22. func newRequest(method, uri, certPath, keyPath string, header map[string]string, body io.Reader) (res *http.Response, err error) {
  23. t := &http.Transport{
  24. Dial: func(netw, addr string) (net.Conn, error) {
  25. conn, err := net.DialTimeout(netw, addr, time.Second*RequestTimeOut)
  26. if err != nil {
  27. return nil, err
  28. }
  29. conn.SetDeadline(time.Now().Add(time.Second * RequestTimeOut))
  30. return conn, nil
  31. },
  32. ResponseHeaderTimeout: time.Second * RequestTimeOut,
  33. }
  34. if certPath != "" {
  35. cert, e := tls.LoadX509KeyPair(certPath, keyPath)
  36. if e != nil {
  37. t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
  38. } else {
  39. pool := x509.NewCertPool()
  40. t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true, Certificates: []tls.Certificate{cert}, RootCAs: pool}
  41. }
  42. }
  43. client := &http.Client{Transport: t}
  44. var (
  45. req *http.Request
  46. )
  47. if body != nil {
  48. req, err = http.NewRequest(method, uri, body)
  49. } else {
  50. req, err = http.NewRequest(method, uri, nil)
  51. }
  52. if err != nil {
  53. return
  54. }
  55. for k := range header {
  56. req.Header.Add(k, header[k])
  57. }
  58. res, err = client.Do(req)
  59. return
  60. }
  61. func readBody(res *http.Response) (msg Message, err error) {
  62. var (
  63. body []byte
  64. reader io.Reader
  65. )
  66. encoding := res.Header.Get("Content-Encoding")
  67. switch encoding {
  68. case "gzip":
  69. reader, err = gzip.NewReader(res.Body)
  70. if err == nil {
  71. body, err = ioutil.ReadAll(reader)
  72. }
  73. default:
  74. body, err = ioutil.ReadAll(res.Body)
  75. }
  76. if err != nil {
  77. return
  78. }
  79. msg.StatusCode = res.StatusCode
  80. msg.Header = res.Header
  81. msg.Body = body
  82. return
  83. }
  84. // Post HTTP request POST
  85. func Post(uri, certPath, keyPath string, header map[string]string, data io.Reader) (msg Message, err error) {
  86. var res *http.Response
  87. if res, err = newRequest("POST", uri, certPath, keyPath, header, data); err != nil {
  88. return
  89. }
  90. defer res.Body.Close()
  91. msg, err = readBody(res)
  92. return
  93. }
  94. // Get HTTP request GET
  95. func Get(uri, certPath, keyPath string, header map[string]string) (msg Message, err error) {
  96. var res *http.Response
  97. if res, err = newRequest("GET", uri, certPath, keyPath, header, nil); err != nil {
  98. return
  99. }
  100. defer res.Body.Close()
  101. msg, err = readBody(res)
  102. return
  103. }