request.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package myth
  2. import (
  3. "compress/gzip"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "encoding/json"
  7. "encoding/xml"
  8. "errors"
  9. "io"
  10. "net"
  11. "net/http"
  12. "time"
  13. )
  14. const (
  15. // ReqContentTypeURL application/x-www-form-urlencoded
  16. ReqContentTypeURL = `application/x-www-form-urlencoded; charset=utf-8`
  17. // ReqContentTypeJSON application/json
  18. ReqContentTypeJSON = `application/json; charset=utf-8`
  19. // ReqContentTypeXML application/xml
  20. ReqContentTypeXML = `application/xml; charset=utf-8`
  21. // ReqContentTypeMultipart multipart/form-data
  22. ReqContentTypeMultipart = `multipart/form-data`
  23. // RequestTimeOut http request timeout (second)
  24. RequestTimeOut = 30
  25. )
  26. const (
  27. // response header Content-Encoding
  28. ResContentEncoding = `Content-Encoding`
  29. // encoding gzip
  30. EncodingGZIP = `gzip`
  31. )
  32. var (
  33. reqTimeOut = time.Duration(RequestTimeOut)
  34. ErrStausCodeNotOk = errors.New(`StatusCode not 200`)
  35. )
  36. // GetRealIP get real IP from Request
  37. func GetRealIP(req *http.Request) (ip string) {
  38. if ips := req.Header["X-Real-Ip"]; ips != nil {
  39. ip = ips[0]
  40. }
  41. return
  42. }
  43. // SetHTTPRequestTimeout set request timeout
  44. func SetHTTPRequestTimeout(seconds int) {
  45. reqTimeOut = time.Duration(seconds)
  46. }
  47. // HTTPReqOption request option
  48. type HTTPReqOption struct {
  49. }
  50. // HTTPMessage HTTP response
  51. type HTTPMessage struct {
  52. StatusCode int
  53. Body []byte
  54. Header http.Header
  55. Req *http.Request
  56. Res *http.Response
  57. }
  58. // JSON Body to JSON
  59. func (m HTTPMessage) JSON(dest interface{}) (err error) {
  60. if m.StatusCode != http.StatusOK {
  61. err = ErrStausCodeNotOk
  62. return
  63. }
  64. err = json.Unmarshal(m.Body, &dest)
  65. return
  66. }
  67. // XML Body to XML
  68. func (m HTTPMessage) XML(dest interface{}) (err error) {
  69. if m.StatusCode != http.StatusOK {
  70. err = ErrStausCodeNotOk
  71. return
  72. }
  73. err = xml.Unmarshal(m.Body, &dest)
  74. return
  75. }
  76. // JSONQuery Body to JSONQuery
  77. func (m HTTPMessage) JSONQuery() (jq *JSONQuery, err error) {
  78. if m.StatusCode != http.StatusOK {
  79. err = ErrStausCodeNotOk
  80. return
  81. }
  82. jq, err = NewJSONQuery(m.Body)
  83. return
  84. }
  85. func newRequest(method, uri, certPath, keyPath string, header map[string]string, body io.Reader) (req *http.Request, res *http.Response, err error) {
  86. t := &http.Transport{
  87. Dial: func(netw, addr string) (net.Conn, error) {
  88. var c net.Conn
  89. c, err = net.DialTimeout(netw, addr, time.Second*RequestTimeOut)
  90. if err != nil {
  91. return nil, err
  92. }
  93. c.SetDeadline(time.Now().Add(time.Second * reqTimeOut))
  94. return c, nil
  95. },
  96. MaxIdleConnsPerHost: 50,
  97. ResponseHeaderTimeout: time.Second * reqTimeOut,
  98. }
  99. if certPath != "" {
  100. cert, e := tls.LoadX509KeyPair(certPath, keyPath)
  101. if e != nil {
  102. t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
  103. } else {
  104. pool := x509.NewCertPool()
  105. t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true, Certificates: []tls.Certificate{cert}, RootCAs: pool}
  106. }
  107. }
  108. var (
  109. client = &http.Client{Transport: t}
  110. )
  111. req, err = http.NewRequest(method, uri, body)
  112. if err != nil {
  113. return
  114. }
  115. for k := range header {
  116. req.Header.Add(k, header[k])
  117. }
  118. res, err = client.Do(req)
  119. return
  120. }
  121. func readBody(msg *HTTPMessage, res *http.Response) (err error) {
  122. var (
  123. body []byte
  124. reader io.Reader
  125. )
  126. encoding := res.Header.Get(ResContentEncoding)
  127. switch encoding {
  128. case EncodingGZIP:
  129. reader, err = gzip.NewReader(res.Body)
  130. if err == nil {
  131. body, err = io.ReadAll(reader)
  132. }
  133. default:
  134. body, err = io.ReadAll(res.Body)
  135. }
  136. if err != nil {
  137. return
  138. }
  139. msg.StatusCode = res.StatusCode
  140. msg.Header = res.Header
  141. msg.Body = body
  142. return
  143. }
  144. // Post HTTP request POST
  145. func Post(uri, certPath, keyPath string, header map[string]string, data io.Reader) (msg HTTPMessage, err error) {
  146. if msg.Req, msg.Res, err = newRequest(http.MethodPost, uri, certPath, keyPath, header, data); err != nil {
  147. return
  148. }
  149. defer msg.Res.Body.Close()
  150. err = readBody(&msg, msg.Res)
  151. return
  152. }
  153. // Get HTTP request GET
  154. func Get(uri, certPath, keyPath string, header map[string]string) (msg HTTPMessage, err error) {
  155. //var res *http.Response
  156. if msg.Req, msg.Res, err = newRequest(http.MethodGet, uri, certPath, keyPath, header, nil); err != nil {
  157. return
  158. }
  159. defer msg.Res.Body.Close()
  160. err = readBody(&msg, msg.Res)
  161. return
  162. }