Compare commits
5 Commits
e72fa38b96
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| d877726797 | |||
| 6d1bf72d9d | |||
| 78cf1e5e3f | |||
| b897a4dfda | |||
| 6a17a02900 |
@@ -2,7 +2,7 @@ module git.3crabs.ru/VLADIMIR/net
|
||||
|
||||
go 1.20
|
||||
|
||||
require github.com/stretchr/testify v1.8.4
|
||||
require github.com/stretchr/testify v1.11.1
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
|
||||
@@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
+20
-39
@@ -1,56 +1,37 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"strings"
|
||||
"git.3crabs.ru/VLADIMIR/net/url"
|
||||
)
|
||||
|
||||
type client struct{}
|
||||
|
||||
func NewClient() *client {
|
||||
return &client{}
|
||||
type Client struct {
|
||||
DNS DNS
|
||||
Transport RoundTripper
|
||||
}
|
||||
|
||||
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)
|
||||
func (c *Client) Do(method string, rawURl string, headers []Header) (*Response, error) {
|
||||
u, err := url.Parse(rawURl)
|
||||
if err != nil {
|
||||
return nil, errors.New("connect " + connectPath)
|
||||
return nil, err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
WriteRequestDebugWrap(
|
||||
conn,
|
||||
connectPath := ""
|
||||
if c.DNS != nil {
|
||||
var err error
|
||||
connectPath, err = c.DNS.GetIP(u.Host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := c.Transport.RoundTrip(
|
||||
&Request{
|
||||
ConnectPath: connectPath,
|
||||
Method: method,
|
||||
Path: path,
|
||||
URL: u,
|
||||
Protocol: "HTTP/1.0",
|
||||
Headers: headers,
|
||||
},
|
||||
WriteRequest,
|
||||
)
|
||||
return ReadResponseDebugWrap(
|
||||
conn,
|
||||
ReadResponse,
|
||||
)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package http
|
||||
|
||||
type DNS interface {
|
||||
GetIP(domain string) (string, error)
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
package http
|
||||
|
||||
import "git.3crabs.ru/VLADIMIR/net/url"
|
||||
|
||||
type Request struct {
|
||||
ConnectPath string // ip
|
||||
|
||||
Method string
|
||||
Path string
|
||||
URL *url.URL
|
||||
Protocol string
|
||||
|
||||
Headers []Header
|
||||
@@ -0,0 +1,5 @@
|
||||
package http
|
||||
|
||||
type RoundTripper interface {
|
||||
RoundTrip(*Request) (*Response, error)
|
||||
}
|
||||
@@ -2,13 +2,64 @@ package http
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type HttpTransport struct{}
|
||||
|
||||
func (t *HttpTransport) RoundTrip(req *Request) (*Response, error) {
|
||||
var err error
|
||||
var conn net.Conn
|
||||
switch req.URL.Scheme {
|
||||
case "http":
|
||||
conn, err = net.Dial("tcp", req.ConnectPath)
|
||||
case "https":
|
||||
conn, err = tls.Dial("tcp", req.ConnectPath, nil)
|
||||
default:
|
||||
panic("scheme not support")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.New("connect " + req.ConnectPath)
|
||||
}
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
WriteRequestDebugWrap(conn, req, WriteRequest)
|
||||
return ReadResponseDebugWrap(conn, ReadResponse)
|
||||
}
|
||||
|
||||
func WriteRequest(w io.Writer, req *Request) {
|
||||
fmt.Fprintf(w, "%s %s %s\r\n", req.Method, req.URL.Path, req.Protocol)
|
||||
|
||||
for _, header := range req.Headers {
|
||||
fmt.Fprintf(w, "%s: %s\r\n", header.Name, header.Value)
|
||||
}
|
||||
fmt.Fprintf(w, "\r\n")
|
||||
|
||||
if len(req.Body) > 0 {
|
||||
fmt.Fprintf(w, "%s\r\n", req.Body)
|
||||
}
|
||||
}
|
||||
|
||||
func WriteRequestDebugWrap(w io.Writer, req *Request, f func(w io.Writer, req *Request)) {
|
||||
buffer := &bytes.Buffer{}
|
||||
|
||||
f(buffer, req)
|
||||
|
||||
fmt.Println("----- Debug Info -----")
|
||||
fmt.Println(buffer.String())
|
||||
fmt.Println("----- Debug End Info -----")
|
||||
|
||||
w.Write(buffer.Bytes())
|
||||
}
|
||||
|
||||
func ReadResponse(r io.Reader) (*Response, error) {
|
||||
b := bufio.NewReader(r)
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.3crabs.ru/VLADIMIR/net/url"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_WriteRequest(t *testing.T) {
|
||||
b := &strings.Builder{}
|
||||
|
||||
WriteRequest(
|
||||
b,
|
||||
&Request{
|
||||
Method: "GET",
|
||||
URL: &url.URL{
|
||||
Path: "/",
|
||||
},
|
||||
Protocol: "HTTP/1.0",
|
||||
},
|
||||
)
|
||||
|
||||
assert.Equal(t, "GET / HTTP/1.0\r\n\r\n", b.String())
|
||||
}
|
||||
|
||||
func Test_ReadResponse(t *testing.T) {
|
||||
b := bytes.NewBuffer(
|
||||
[]byte("HTTP/1.1 200 OK\r\nDate: Sat, 03 Feb 2024 16:40:29 GMT\r\nContent-Length: 12\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nHello World!"),
|
||||
)
|
||||
|
||||
r, err := ReadResponse(b)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, "200", r.StatusCode)
|
||||
|
||||
assert.Equal(t, "Date", r.Headers[0].Name)
|
||||
assert.Equal(t, "Sat, 03 Feb 2024 16:40:29 GMT", r.Headers[0].Value)
|
||||
|
||||
assert.Equal(t, "Content-Length", r.Headers[1].Name)
|
||||
assert.Equal(t, "12", r.Headers[1].Value)
|
||||
|
||||
assert.Equal(t, "Content-Type", r.Headers[2].Name)
|
||||
assert.Equal(t, "text/plain; charset=utf-8", r.Headers[2].Value)
|
||||
|
||||
assert.Equal(t, []byte("Hello World!"), r.Body)
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
func WriteRequest(w io.Writer, req *Request) {
|
||||
fmt.Fprintf(w, "%s %s %s\r\n", req.Method, req.Path, req.Protocol)
|
||||
|
||||
for _, header := range req.Headers {
|
||||
fmt.Fprintf(w, "%s: %s\r\n", header.Name, header.Value)
|
||||
}
|
||||
fmt.Fprintf(w, "\r\n")
|
||||
|
||||
if len(req.Body) > 0 {
|
||||
fmt.Fprintf(w, "%s\r\n", req.Body)
|
||||
}
|
||||
}
|
||||
|
||||
func WriteRequestDebugWrap(w io.Writer, req *Request, f func(w io.Writer, req *Request)) {
|
||||
buffer := &bytes.Buffer{}
|
||||
|
||||
f(buffer, req)
|
||||
|
||||
fmt.Println("----- Debug Info -----")
|
||||
fmt.Println(buffer.String())
|
||||
fmt.Println("----- Debug End Info -----")
|
||||
|
||||
w.Write(buffer.Bytes())
|
||||
}
|
||||
+16
-2
@@ -1,14 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.3crabs.ru/VLADIMIR/net/http"
|
||||
)
|
||||
|
||||
type CustomDNS struct{}
|
||||
|
||||
func (*CustomDNS) GetIP(domain string) (string, error) {
|
||||
if domain == "test.ru" {
|
||||
return "127.0.0.1:8081", nil
|
||||
}
|
||||
return "", errors.New("ip not found")
|
||||
}
|
||||
|
||||
func main() {
|
||||
client := http.NewClient()
|
||||
client := http.Client{
|
||||
DNS: &CustomDNS{},
|
||||
Transport: &http.HttpTransport{},
|
||||
}
|
||||
_, 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"},
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.3crabs.ru/VLADIMIR/net/url"
|
||||
)
|
||||
|
||||
func main() {
|
||||
u, err := url.Parse("http://test.ru/")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%+v", u)
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package url
|
||||
|
||||
type URL struct {
|
||||
Scheme string
|
||||
Host string
|
||||
Path string
|
||||
}
|
||||
|
||||
func Parse(rawURL string) (*URL, error) {
|
||||
schemeIndex := -1
|
||||
hostStartIndex := -1
|
||||
hostEndIndex := -1
|
||||
for i := 0; i < len(rawURL); i++ {
|
||||
if schemeIndex == -1 && rawURL[i] == ':' {
|
||||
schemeIndex = i
|
||||
hostStartIndex = i + 3
|
||||
i += 3
|
||||
continue
|
||||
}
|
||||
if rawURL[i] == '/' {
|
||||
hostEndIndex = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return &URL{
|
||||
Scheme: rawURL[:schemeIndex],
|
||||
Host: rawURL[hostStartIndex:hostEndIndex],
|
||||
Path: rawURL[hostEndIndex:],
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user