53 lines
901 B
Go
53 lines
901 B
Go
package http
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
CRLF = []byte("\r\n")
|
|
SP = []byte(" ")
|
|
)
|
|
|
|
func getIP(domain string) string {
|
|
if domain == "test.ru" {
|
|
return "127.0.0.1:8081"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func 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 := 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()
|
|
|
|
DebugWrap(
|
|
conn,
|
|
&Request{
|
|
Method: method,
|
|
Path: path,
|
|
Protocol: "HTTP/1.0",
|
|
Headers: headers,
|
|
},
|
|
WriteRequest,
|
|
)
|
|
return ReadResponse(conn)
|
|
}
|