This commit is contained in:
Владимир Фёдоров 2025-07-21 02:30:26 +07:00
parent e72fa38b96
commit 6a17a02900
2 changed files with 18 additions and 16 deletions

View File

@ -1,9 +1,10 @@
package http package http
import ( import (
"crypto/tls"
"errors" "errors"
"net" "net"
"strings" "net/url"
) )
type client struct{} type client struct{}
@ -19,21 +20,22 @@ func (c *client) getIP(domain string) string {
return "" return ""
} }
func (c *client) Do(method string, url string, headers []Header) (*Response, error) { func (c *client) Do(method string, rawURl string, headers []Header) (*Response, error) {
url = strings.TrimPrefix(url, "http://") u, err := url.Parse(rawURl)
url = strings.TrimPrefix(url, "https://") if err != nil {
path := "/" return nil, err
arr := strings.Split(url, "/")
if len(arr) == 2 {
path = arr[1]
}
if !strings.HasPrefix(path, "/") {
path = "/" + path
} }
connectPath := c.getIP(arr[0]) connectPath := c.getIP(u.Host)
conn, err := net.Dial("tcp", connectPath) var conn net.Conn
// conn, err := tls.Dial("tcp", connectPath, nil) 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 { if err != nil {
return nil, errors.New("connect " + connectPath) return nil, errors.New("connect " + connectPath)
} }
@ -43,7 +45,7 @@ func (c *client) Do(method string, url string, headers []Header) (*Response, err
conn, conn,
&Request{ &Request{
Method: method, Method: method,
Path: path, Path: u.Path,
Protocol: "HTTP/1.0", Protocol: "HTTP/1.0",
Headers: headers, Headers: headers,
}, },

View File

@ -8,7 +8,7 @@ func main() {
client := http.NewClient() client := http.NewClient()
_, err := client.Do( _, err := client.Do(
"GET", "GET",
"http://test.ru", "http://test.ru/",
[]http.Header{ []http.Header{
{Name: "Host", Value: "3crabs.ru"}, {Name: "Host", Value: "3crabs.ru"},
{Name: "User-Agent", Value: "3crabs/0.0.1"}, {Name: "User-Agent", Value: "3crabs/0.0.1"},