From 6a17a02900cebf93701f97e982794e1a67fd7b4e Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Mon, 21 Jul 2025 02:30:26 +0700 Subject: [PATCH] clear --- http/client.go | 32 +++++++++++++++++--------------- test_client/main.go | 2 +- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/http/client.go b/http/client.go index 2ba1f06..25bfdb9 100644 --- a/http/client.go +++ b/http/client.go @@ -1,9 +1,10 @@ package http import ( + "crypto/tls" "errors" "net" - "strings" + "net/url" ) type client struct{} @@ -19,21 +20,22 @@ func (c *client) getIP(domain string) string { 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 +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(arr[0]) - conn, err := net.Dial("tcp", connectPath) - // conn, err := tls.Dial("tcp", connectPath, nil) + 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) } @@ -43,7 +45,7 @@ func (c *client) Do(method string, url string, headers []Header) (*Response, err conn, &Request{ Method: method, - Path: path, + Path: u.Path, Protocol: "HTTP/1.0", Headers: headers, }, diff --git a/test_client/main.go b/test_client/main.go index 5215acb..ae2e3d4 100644 --- a/test_client/main.go +++ b/test_client/main.go @@ -8,7 +8,7 @@ func main() { client := http.NewClient() _, err := client.Do( "GET", - "http://test.ru", + "http://test.ru/", []http.Header{ {Name: "Host", Value: "3crabs.ru"}, {Name: "User-Agent", Value: "3crabs/0.0.1"},