49 lines
812 B
Go
49 lines
812 B
Go
package http
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
CRLF = []byte("\r\n")
|
|
SP = []byte(" ")
|
|
)
|
|
|
|
type Client struct {
|
|
DNS DNS
|
|
Transport RoundTripper
|
|
}
|
|
|
|
func (c *Client) Do(method string, url string, headers []Header) (*Response, error) {
|
|
url = strings.TrimPrefix(url, "http://")
|
|
url = strings.TrimPrefix(url, "https://")
|
|
path := "/"
|
|
arr := strings.Split(url, "/")
|
|
if len(arr) == 2 {
|
|
path = arr[1]
|
|
}
|
|
if !strings.HasPrefix(path, "/") {
|
|
path = "/" + path
|
|
}
|
|
|
|
connectPath := ""
|
|
if c.DNS != nil {
|
|
var err error
|
|
connectPath, err = c.DNS.GetIP(arr[0])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
resp, err := c.Transport.RoundTrip(
|
|
&Request{
|
|
ConnectPath: connectPath,
|
|
Method: method,
|
|
Path: path,
|
|
Protocol: "HTTP/1.0",
|
|
Headers: headers,
|
|
},
|
|
)
|
|
return resp, err
|
|
}
|