github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/client/http_request.go (about) 1 package client 2 3 import ( 4 "fmt" 5 "io" 6 "net/http" 7 _url "net/url" 8 "strings" 9 10 "github.com/volts-dev/volts/internal/body" 11 "github.com/volts-dev/volts/internal/header" 12 ) 13 14 type httpRequest struct { 15 URL *_url.URL 16 header header.Header 17 url string 18 method string 19 //contentType string 20 ContentLength int64 21 body *body.TBody 22 opts RequestOptions 23 } 24 type HttpRequest = httpRequest 25 26 /* 27 @service: 目标URL地址 28 @method: GET/POST... 29 @data: 原始数据 30 */ 31 func newHttpRequest(method, url string, data interface{}, opts ...RequestOption) (*httpRequest, error) { 32 // 检测Method是否合法字符 33 if len(method) > 0 && strings.IndexFunc(method, isNotToken) != -1 { 34 return nil, fmt.Errorf("net/http: invalid method %q", method) 35 } 36 37 u, err := _url.Parse(url) 38 if err != nil { 39 return nil, err 40 } 41 42 reqOpts := RequestOptions{} 43 for _, o := range opts { 44 o(&reqOpts) 45 } 46 47 // 初始化Body数据编码器 48 /* cf, err := newHTTPCodec(reqOpts.ContentType) 49 if err != nil { 50 // Warn 51 logger.Warnf("%s,Will using defalut codec %s for this request!", err.Error(), codec.Bytes.String()) 52 //return nil, errors.InternalServerError("http.client", ) 53 }*/ 54 55 req := &httpRequest{ 56 URL: u, 57 header: make(header.Header), // TODO 不初始化 58 body: body.New(reqOpts.Codec), 59 url: url, 60 method: strings.ToUpper(method), 61 opts: reqOpts, 62 } 63 // 检测是否已经重复实现 64 req.header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36") // 65 req.header.Set("Accept", "*/*") // TODO 66 req.header.Set("Host", u.Host) 67 err = req.write(data) 68 if err != nil { 69 return nil, err 70 } 71 return req, nil 72 } 73 74 func (self *httpRequest) write(data interface{}) error { 75 if data == nil { 76 return nil 77 } 78 79 var err error 80 switch v := data.(type) { 81 case io.Reader: 82 d, err := io.ReadAll(v) 83 if err != nil { 84 return err 85 } 86 _, err = self.body.Encode(d) 87 default: 88 _, err = self.body.Encode(v) 89 } 90 91 self.ContentLength = int64(self.body.Data.Len()) 92 return err 93 } 94 95 func (r *httpRequest) AddCookie(c *http.Cookie) { 96 s := c.String() 97 if c := r.header.Get("Cookie"); c != "" { 98 r.header.Set("Cookie", c+"; "+s) 99 } else { 100 r.header.Set("Cookie", s) 101 } 102 } 103 104 func (r *httpRequest) Referer() string { 105 return r.header.Get("Referer") 106 } 107 108 func (self *httpRequest) ContentType() string { 109 return self.body.Codec.String() 110 } 111 112 func (h *httpRequest) Service() string { 113 return h.URL.Host 114 } 115 116 func (h *httpRequest) Method() string { 117 return h.method 118 } 119 120 func (h *httpRequest) Endpoint() string { 121 return h.method 122 } 123 124 func (self *httpRequest) Body() *body.TBody { 125 return self.body 126 } 127 128 func (h *httpRequest) Stream() bool { 129 return h.opts.Stream 130 } 131 132 func (self *httpRequest) Header() header.Header { 133 if self.header == nil { 134 self.header = make(header.Header) 135 } 136 return self.header 137 }