Compare commits
2 Commits
78cf1e5e3f
...
d877726797
| Author | SHA1 | Date | |
|---|---|---|---|
| d877726797 | |||
| 6d1bf72d9d |
8
go.mod
8
go.mod
@ -1,3 +1,11 @@
|
||||
module git.3crabs.ru/VLADIMIR/net
|
||||
|
||||
go 1.20
|
||||
|
||||
require github.com/stretchr/testify v1.11.1
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
9
go.sum
9
go.sum
@ -0,0 +1,9 @@
|
||||
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.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=
|
||||
@ -1,59 +1,37 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"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, rawURl string, headers []Header) (*Response, error) {
|
||||
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")
|
||||
connectPath := ""
|
||||
if c.DNS != nil {
|
||||
var err error
|
||||
connectPath, err = c.DNS.GetIP(u.Host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.New("connect " + connectPath)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
WriteRequestDebugWrap(
|
||||
conn,
|
||||
resp, err := c.Transport.RoundTrip(
|
||||
&Request{
|
||||
Method: method,
|
||||
Path: u.Path,
|
||||
Protocol: "HTTP/1.0",
|
||||
Headers: headers,
|
||||
ConnectPath: connectPath,
|
||||
Method: method,
|
||||
URL: u,
|
||||
Protocol: "HTTP/1.0",
|
||||
Headers: headers,
|
||||
},
|
||||
WriteRequest,
|
||||
)
|
||||
return ReadResponseDebugWrap(
|
||||
conn,
|
||||
ReadResponse,
|
||||
)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
5
http/dns.go
Normal file
5
http/dns.go
Normal file
@ -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
|
||||
5
http/round_tripper.go
Normal file
5
http/round_tripper.go
Normal file
@ -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)
|
||||
|
||||
49
http/transport_test.go
Normal file
49
http/transport_test.go
Normal file
@ -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())
|
||||
}
|
||||
@ -1,11 +1,25 @@
|
||||
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/",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user