60 lines
977 B
Go
60 lines
977 B
Go
package http
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"errors"
|
|
"net"
|
|
|
|
"git.3crabs.ru/VLADIMIR/net/url"
|
|
)
|
|
|
|
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, rawURl string, headers []Header) (*Response, error) {
|
|
u, err := url.Parse(rawURl)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
connectPath := c.getIP(u.Host)
|
|
var conn net.Conn
|
|
switch u.Scheme {
|
|
case "http":
|
|
conn, err = net.Dial("tcp", connectPath)
|
|
case "https":
|
|
conn, err = tls.Dial("tcp", connectPath, nil)
|
|
default:
|
|
panic("scheme not support")
|
|
}
|
|
if err != nil {
|
|
return nil, errors.New("connect " + connectPath)
|
|
}
|
|
defer conn.Close()
|
|
|
|
WriteRequestDebugWrap(
|
|
conn,
|
|
&Request{
|
|
Method: method,
|
|
Path: u.Path,
|
|
Protocol: "HTTP/1.0",
|
|
Headers: headers,
|
|
},
|
|
WriteRequest,
|
|
)
|
|
return ReadResponseDebugWrap(
|
|
conn,
|
|
ReadResponse,
|
|
)
|
|
}
|