client.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package xhttp
  2. import (
  3. "compress/gzip"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "io"
  7. "net"
  8. "net/http"
  9. "time"
  10. )
  11. // readBody read response
  12. func readBody(res *http.Response) (msg Message, err error) {
  13. var (
  14. body []byte
  15. reader io.Reader
  16. )
  17. encoding := res.Header.Get("Content-Encoding")
  18. switch encoding {
  19. case "gzip":
  20. reader, err = gzip.NewReader(res.Body)
  21. if err == nil {
  22. body, err = io.ReadAll(reader)
  23. }
  24. default:
  25. body, err = io.ReadAll(res.Body)
  26. }
  27. if err != nil {
  28. return
  29. }
  30. msg.StatusCode = res.StatusCode
  31. msg.Header = res.Header
  32. msg.Body = body
  33. return
  34. }
  35. // newRequest new request
  36. func newRequest(method, uri string, body io.Reader, opt RequestOption) (res *http.Response, err error) {
  37. t := &http.Transport{
  38. Dial: func(netw, addr string) (net.Conn, error) {
  39. conn, err := net.DialTimeout(netw, addr, opt.RequestTimeOut)
  40. if err != nil {
  41. return nil, err
  42. }
  43. conn.SetDeadline(time.Now().Add(opt.RequestTimeOut))
  44. return conn, nil
  45. },
  46. ResponseHeaderTimeout: time.Second * opt.ResponseHeaderTimeout,
  47. }
  48. if len(opt.CertFile) > 0 {
  49. cert, e := tls.LoadX509KeyPair(opt.CertFile, opt.KeyFile)
  50. if e != nil {
  51. t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
  52. } else {
  53. pool := x509.NewCertPool()
  54. t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true, Certificates: []tls.Certificate{cert}, RootCAs: pool}
  55. }
  56. } else {
  57. t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
  58. }
  59. c := &http.Client{Transport: t}
  60. var req *http.Request
  61. req, err = http.NewRequest(method, uri, body)
  62. if err != nil {
  63. return
  64. }
  65. for k, v := range opt.Headers {
  66. req.Header.Add(k, v)
  67. }
  68. res, err = c.Do(req)
  69. return
  70. }
  71. // Get HTTP request GET
  72. func Get(uri string, opt RequestOption) (msg Message, err error) {
  73. var res *http.Response
  74. if res, err = newRequest("GET", uri, nil, opt); err != nil {
  75. return
  76. }
  77. defer res.Body.Close()
  78. msg, err = readBody(res)
  79. return
  80. }
  81. // Head HTTP request HEAD
  82. func Head(uri string, opt RequestOption) (msg Message, err error) {
  83. var res *http.Response
  84. if res, err = newRequest("HEAD", uri, nil, opt); err != nil {
  85. return
  86. }
  87. defer res.Body.Close()
  88. //msg, err = readBody(res)
  89. return
  90. }
  91. // GetJSON HTTP request GET, response JSON
  92. func GetJSON(v interface{}, uri string, opt RequestOption) (msg Message, err error) {
  93. var res *http.Response
  94. if res, err = newRequest("GET", uri, nil, opt); err != nil {
  95. return
  96. }
  97. defer res.Body.Close()
  98. msg, err = readBody(res)
  99. if err != nil {
  100. return
  101. }
  102. err = msg.JSON(v)
  103. return
  104. }
  105. // GetXML HTTP request GET, response XML
  106. func GetXML(v interface{}, uri string, opt RequestOption) (msg Message, err error) {
  107. var res *http.Response
  108. if res, err = newRequest("GET", uri, nil, opt); err != nil {
  109. return
  110. }
  111. defer res.Body.Close()
  112. msg, err = readBody(res)
  113. if err != nil {
  114. return
  115. }
  116. err = msg.XML(v)
  117. return
  118. }
  119. // Post HTTP request POST
  120. func Post(uri string, body io.Reader, opt RequestOption) (msg Message, err error) {
  121. var res *http.Response
  122. if res, err = newRequest("POST", uri, body, opt); err != nil {
  123. return
  124. }
  125. defer res.Body.Close()
  126. msg, err = readBody(res)
  127. return
  128. }
  129. // Put HTTP request PUT
  130. func Put(uri string, body io.Reader, opt RequestOption) (msg Message, err error) {
  131. var res *http.Response
  132. if res, err = newRequest("PUT", uri, body, opt); err != nil {
  133. return
  134. }
  135. defer res.Body.Close()
  136. msg, err = readBody(res)
  137. return
  138. }
  139. // Delete HTTP request DELETE
  140. func Delete(uri string, body io.Reader, opt RequestOption) (msg Message, err error) {
  141. var res *http.Response
  142. if res, err = newRequest("DELETE", uri, body, opt); err != nil {
  143. return
  144. }
  145. defer res.Body.Close()
  146. msg, err = readBody(res)
  147. return
  148. }
  149. // Connect HTTP request CONNECT
  150. func Connect(uri string, body io.Reader, opt RequestOption) (msg Message, err error) {
  151. var res *http.Response
  152. if res, err = newRequest("CONNECT", uri, body, opt); err != nil {
  153. return
  154. }
  155. defer res.Body.Close()
  156. msg, err = readBody(res)
  157. return
  158. }
  159. // Options HTTP request OPTIONS
  160. func Options(uri string, body io.Reader, opt RequestOption) (msg Message, err error) {
  161. var res *http.Response
  162. if res, err = newRequest("OPTIONS", uri, body, opt); err != nil {
  163. return
  164. }
  165. defer res.Body.Close()
  166. msg, err = readBody(res)
  167. return
  168. }
  169. // Trace HTTP request TRACE
  170. func Trace(uri string, body io.Reader, opt RequestOption) (msg Message, err error) {
  171. var res *http.Response
  172. if res, err = newRequest("TRACE", uri, body, opt); err != nil {
  173. return
  174. }
  175. defer res.Body.Close()
  176. msg, err = readBody(res)
  177. return
  178. }
  179. // Patch HTTP request PATCH
  180. func Patch(uri string, body io.Reader, opt RequestOption) (msg Message, err error) {
  181. var res *http.Response
  182. if res, err = newRequest("PATCH", uri, body, opt); err != nil {
  183. return
  184. }
  185. defer res.Body.Close()
  186. msg, err = readBody(res)
  187. return
  188. }