123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- package xhttp
- import (
- "compress/gzip"
- "crypto/tls"
- "crypto/x509"
- "io"
- "net"
- "net/http"
- "time"
- )
- // readBody read response
- func readBody(res *http.Response) (msg Message, err error) {
- var (
- body []byte
- reader io.Reader
- )
- encoding := res.Header.Get("Content-Encoding")
- switch encoding {
- case "gzip":
- reader, err = gzip.NewReader(res.Body)
- if err == nil {
- body, err = io.ReadAll(reader)
- }
- default:
- body, err = io.ReadAll(res.Body)
- }
- if err != nil {
- return
- }
- msg.StatusCode = res.StatusCode
- msg.Header = res.Header
- msg.Body = body
- return
- }
- // newRequest new request
- func newRequest(method, uri string, body io.Reader, opt RequestOption) (res *http.Response, err error) {
- t := &http.Transport{
- Dial: func(netw, addr string) (net.Conn, error) {
- conn, err := net.DialTimeout(netw, addr, opt.RequestTimeOut)
- if err != nil {
- return nil, err
- }
- conn.SetDeadline(time.Now().Add(opt.RequestTimeOut))
- return conn, nil
- },
- ResponseHeaderTimeout: time.Second * opt.ResponseHeaderTimeout,
- }
- if len(opt.CertFile) > 0 {
- cert, e := tls.LoadX509KeyPair(opt.CertFile, opt.KeyFile)
- if e != nil {
- t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
- } else {
- pool := x509.NewCertPool()
- t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true, Certificates: []tls.Certificate{cert}, RootCAs: pool}
- }
- } else {
- t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
- }
- c := &http.Client{Transport: t}
- var req *http.Request
- req, err = http.NewRequest(method, uri, body)
- if err != nil {
- return
- }
- for k, v := range opt.Headers {
- req.Header.Add(k, v)
- }
- res, err = c.Do(req)
- return
- }
- // Get HTTP request GET
- func Get(uri string, opt RequestOption) (msg Message, err error) {
- var res *http.Response
- if res, err = newRequest("GET", uri, nil, opt); err != nil {
- return
- }
- defer res.Body.Close()
- msg, err = readBody(res)
- return
- }
- // Head HTTP request HEAD
- func Head(uri string, opt RequestOption) (msg Message, err error) {
- var res *http.Response
- if res, err = newRequest("HEAD", uri, nil, opt); err != nil {
- return
- }
- defer res.Body.Close()
- //msg, err = readBody(res)
- return
- }
- // GetJSON HTTP request GET, response JSON
- func GetJSON(v interface{}, uri string, opt RequestOption) (msg Message, err error) {
- var res *http.Response
- if res, err = newRequest("GET", uri, nil, opt); err != nil {
- return
- }
- defer res.Body.Close()
- msg, err = readBody(res)
- if err != nil {
- return
- }
- err = msg.JSON(v)
- return
- }
- // GetXML HTTP request GET, response XML
- func GetXML(v interface{}, uri string, opt RequestOption) (msg Message, err error) {
- var res *http.Response
- if res, err = newRequest("GET", uri, nil, opt); err != nil {
- return
- }
- defer res.Body.Close()
- msg, err = readBody(res)
- if err != nil {
- return
- }
- err = msg.XML(v)
- return
- }
- // Post HTTP request POST
- func Post(uri string, body io.Reader, opt RequestOption) (msg Message, err error) {
- var res *http.Response
- if res, err = newRequest("POST", uri, body, opt); err != nil {
- return
- }
- defer res.Body.Close()
- msg, err = readBody(res)
- return
- }
- // Put HTTP request PUT
- func Put(uri string, body io.Reader, opt RequestOption) (msg Message, err error) {
- var res *http.Response
- if res, err = newRequest("PUT", uri, body, opt); err != nil {
- return
- }
- defer res.Body.Close()
- msg, err = readBody(res)
- return
- }
- // Delete HTTP request DELETE
- func Delete(uri string, body io.Reader, opt RequestOption) (msg Message, err error) {
- var res *http.Response
- if res, err = newRequest("DELETE", uri, body, opt); err != nil {
- return
- }
- defer res.Body.Close()
- msg, err = readBody(res)
- return
- }
- // Connect HTTP request CONNECT
- func Connect(uri string, body io.Reader, opt RequestOption) (msg Message, err error) {
- var res *http.Response
- if res, err = newRequest("CONNECT", uri, body, opt); err != nil {
- return
- }
- defer res.Body.Close()
- msg, err = readBody(res)
- return
- }
- // Options HTTP request OPTIONS
- func Options(uri string, body io.Reader, opt RequestOption) (msg Message, err error) {
- var res *http.Response
- if res, err = newRequest("OPTIONS", uri, body, opt); err != nil {
- return
- }
- defer res.Body.Close()
- msg, err = readBody(res)
- return
- }
- // Trace HTTP request TRACE
- func Trace(uri string, body io.Reader, opt RequestOption) (msg Message, err error) {
- var res *http.Response
- if res, err = newRequest("TRACE", uri, body, opt); err != nil {
- return
- }
- defer res.Body.Close()
- msg, err = readBody(res)
- return
- }
- // Patch HTTP request PATCH
- func Patch(uri string, body io.Reader, opt RequestOption) (msg Message, err error) {
- var res *http.Response
- if res, err = newRequest("PATCH", uri, body, opt); err != nil {
- return
- }
- defer res.Body.Close()
- msg, err = readBody(res)
- return
- }
|