57 lines
988 B
Go
57 lines
988 B
Go
package http
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"strings"
|
|
)
|
|
|
|
type client struct{}
|
|
|
|
func NewClient() *client {
|
|
return &client{}
|
|
}
|
|
|
|
func (c *client) getIP(domain string) string {
|
|
if domain == "test.ru" {
|
|
return "127.0.0.1:8081"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
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 := c.getIP(arr[0])
|
|
conn, err := net.Dial("tcp", connectPath)
|
|
// conn, err := tls.Dial("tcp", connectPath, nil)
|
|
if err != nil {
|
|
return nil, errors.New("connect " + connectPath)
|
|
}
|
|
defer conn.Close()
|
|
|
|
WriteRequestDebugWrap(
|
|
conn,
|
|
&Request{
|
|
Method: method,
|
|
Path: path,
|
|
Protocol: "HTTP/1.0",
|
|
Headers: headers,
|
|
},
|
|
WriteRequest,
|
|
)
|
|
return ReadResponseDebugWrap(
|
|
conn,
|
|
ReadResponse,
|
|
)
|
|
}
|