mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-14 20:03:40 +00:00
net/http: add support for RoundTripper
This commit is contained in:
+33
-18
@@ -25,17 +25,43 @@ func (c *Client) Do(req *Request) (*Response, error) {
|
|||||||
req.AddCookie(cookie)
|
req.AddCookie(cookie)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
transport := c.Transport
|
||||||
|
if transport == nil {
|
||||||
|
transport = DefaultTransport
|
||||||
|
}
|
||||||
|
res, err := transport.RoundTrip(req)
|
||||||
|
|
||||||
|
if c.Jar != nil {
|
||||||
|
if rc := res.Cookies(); len(rc) > 0 {
|
||||||
|
c.Jar.SetCookies(req.URL, rc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
type Transport struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
var DefaultTransport RoundTripper
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
DefaultTransport = &Transport{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Transport) RoundTrip(req *Request) (*Response, error) {
|
||||||
switch req.URL.Scheme {
|
switch req.URL.Scheme {
|
||||||
case "http":
|
case "http":
|
||||||
return c.doHTTP(req)
|
return t.doHTTP(req)
|
||||||
case "https":
|
case "https":
|
||||||
return c.doHTTPS(req)
|
return t.doHTTPS(req)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("invalid schemer : %s", req.URL.Scheme)
|
return nil, fmt.Errorf("invalid schemer : %s", req.URL.Scheme)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) doHTTP(req *Request) (*Response, error) {
|
func (t *Transport) doHTTP(req *Request) (*Response, error) {
|
||||||
// make TCP connection
|
// make TCP connection
|
||||||
ip := net.ParseIP(req.URL.Hostname())
|
ip := net.ParseIP(req.URL.Hostname())
|
||||||
port := 80
|
port := 80
|
||||||
@@ -106,10 +132,10 @@ func (c *Client) doHTTP(req *Request) (*Response, error) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.doResp(conn, req)
|
return t.doResp(conn, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) doHTTPS(req *Request) (*Response, error) {
|
func (t *Transport) doHTTPS(req *Request) (*Response, error) {
|
||||||
conn, err := tls.Dial("tcp", req.URL.Host, nil)
|
conn, err := tls.Dial("tcp", req.URL.Host, nil)
|
||||||
retry := 0
|
retry := 0
|
||||||
for ; err != nil; conn, err = tls.Dial("tcp", req.URL.Host, nil) {
|
for ; err != nil; conn, err = tls.Dial("tcp", req.URL.Host, nil) {
|
||||||
@@ -167,10 +193,10 @@ func (c *Client) doHTTPS(req *Request) (*Response, error) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.doResp(conn, req)
|
return t.doResp(conn, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) doResp(conn net.Conn, req *Request) (*Response, error) {
|
func (t *Transport) doResp(conn net.Conn, req *Request) (*Response, error) {
|
||||||
resp := &Response{
|
resp := &Response{
|
||||||
Header: map[string][]string{},
|
Header: map[string][]string{},
|
||||||
}
|
}
|
||||||
@@ -256,11 +282,6 @@ func (c *Client) doResp(conn net.Conn, req *Request) (*Response, error) {
|
|||||||
remain -= int64(ofs)
|
remain -= int64(ofs)
|
||||||
if remain <= 0 {
|
if remain <= 0 {
|
||||||
resp.Body = io.NopCloser(bytes.NewReader(buf[:ofs]))
|
resp.Body = io.NopCloser(bytes.NewReader(buf[:ofs]))
|
||||||
if c.Jar != nil {
|
|
||||||
if rc := resp.Cookies(); len(rc) > 0 {
|
|
||||||
c.Jar.SetCookies(req.URL, rc)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return resp, conn.Close()
|
return resp, conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -298,11 +319,5 @@ func (c *Client) doResp(conn net.Conn, req *Request) (*Response, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.Jar != nil {
|
|
||||||
if rc := resp.Cookies(); len(rc) > 0 {
|
|
||||||
c.Jar.SetCookies(req.URL, rc)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return resp, conn.Close()
|
return resp, conn.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user