Merge branch 'master' of https://git.3crabs.ru/VLADIMIR/net
This commit is contained in:
		
						commit
						d877726797
					
				
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@ -2,7 +2,7 @@ module git.3crabs.ru/VLADIMIR/net
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
go 1.20
 | 
					go 1.20
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require github.com/stretchr/testify v1.8.4
 | 
					require github.com/stretchr/testify v1.11.1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
	github.com/davecgh/go-spew v1.1.1 // indirect
 | 
						github.com/davecgh/go-spew v1.1.1 // indirect
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
									
									
									
									
								
							@ -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/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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 | 
				
			||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 | 
					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.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
 | 
				
			||||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
 | 
					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/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 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 | 
				
			||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
					gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
				
			||||||
 | 
				
			|||||||
@ -1,12 +1,7 @@
 | 
				
			|||||||
package http
 | 
					package http
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"strings"
 | 
						"git.3crabs.ru/VLADIMIR/net/url"
 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var (
 | 
					 | 
				
			||||||
	CRLF = []byte("\r\n")
 | 
					 | 
				
			||||||
	SP   = []byte(" ")
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Client struct {
 | 
					type Client struct {
 | 
				
			||||||
@ -14,22 +9,16 @@ type Client struct {
 | 
				
			|||||||
	Transport RoundTripper
 | 
						Transport RoundTripper
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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 := ""
 | 
						connectPath := ""
 | 
				
			||||||
	if c.DNS != nil {
 | 
						if c.DNS != nil {
 | 
				
			||||||
		var err error
 | 
							var err error
 | 
				
			||||||
		connectPath, err = c.DNS.GetIP(arr[0])
 | 
							connectPath, err = c.DNS.GetIP(u.Host)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@ -39,7 +28,7 @@ func (c *Client) Do(method string, url string, headers []Header) (*Response, err
 | 
				
			|||||||
		&Request{
 | 
							&Request{
 | 
				
			||||||
			ConnectPath: connectPath,
 | 
								ConnectPath: connectPath,
 | 
				
			||||||
			Method:      method,
 | 
								Method:      method,
 | 
				
			||||||
			Path:        path,
 | 
								URL:         u,
 | 
				
			||||||
			Protocol:    "HTTP/1.0",
 | 
								Protocol:    "HTTP/1.0",
 | 
				
			||||||
			Headers:     headers,
 | 
								Headers:     headers,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +0,0 @@
 | 
				
			|||||||
package http
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type Header struct {
 | 
					 | 
				
			||||||
	Name  string
 | 
					 | 
				
			||||||
	Value string
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										30
									
								
								http/models.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								http/models.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,30 @@
 | 
				
			|||||||
 | 
					package http
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "git.3crabs.ru/VLADIMIR/net/url"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Request struct {
 | 
				
			||||||
 | 
						ConnectPath string // ip
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Method   string
 | 
				
			||||||
 | 
						URL      *url.URL
 | 
				
			||||||
 | 
						Protocol string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Headers []Header
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Body []byte
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Response struct {
 | 
				
			||||||
 | 
						Protocol      string
 | 
				
			||||||
 | 
						StatusCode    string
 | 
				
			||||||
 | 
						StatusMessage string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Headers []Header
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Body []byte
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Header struct {
 | 
				
			||||||
 | 
						Name  string
 | 
				
			||||||
 | 
						Value string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -1,13 +0,0 @@
 | 
				
			|||||||
package http
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type Request struct {
 | 
					 | 
				
			||||||
	ConnectPath string // ip
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	Method   string
 | 
					 | 
				
			||||||
	Path     string
 | 
					 | 
				
			||||||
	Protocol string
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	Headers []Header
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	Body string
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@ -1,24 +0,0 @@
 | 
				
			|||||||
package http
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"errors"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var (
 | 
					 | 
				
			||||||
	ErrHeaderNotFound = errors.New("header not found")
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type Response struct {
 | 
					 | 
				
			||||||
	StatusCode int
 | 
					 | 
				
			||||||
	Headers    []Header
 | 
					 | 
				
			||||||
	Body       []byte
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (r *Response) GetHeader(name string) (Header, error) {
 | 
					 | 
				
			||||||
	for _, h := range r.Headers {
 | 
					 | 
				
			||||||
		if h.Name == name {
 | 
					 | 
				
			||||||
			return h, nil
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return Header{}, ErrHeaderNotFound
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@ -3,6 +3,7 @@ package http
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"bufio"
 | 
						"bufio"
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
 | 
						"crypto/tls"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
@ -14,19 +15,28 @@ import (
 | 
				
			|||||||
type HttpTransport struct{}
 | 
					type HttpTransport struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (t *HttpTransport) RoundTrip(req *Request) (*Response, error) {
 | 
					func (t *HttpTransport) RoundTrip(req *Request) (*Response, error) {
 | 
				
			||||||
	conn, err := net.Dial("tcp", req.ConnectPath)
 | 
						var err error
 | 
				
			||||||
	// conn, err := tls.Dial("tcp", connectPath, nil)
 | 
						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 {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, errors.New("connect " + req.ConnectPath)
 | 
							return nil, errors.New("connect " + req.ConnectPath)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	defer conn.Close()
 | 
						defer conn.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	debugWrap(conn, req, writeRequest)
 | 
						WriteRequestDebugWrap(conn, req, WriteRequest)
 | 
				
			||||||
	return readResponse(conn)
 | 
						return ReadResponseDebugWrap(conn, ReadResponse)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func writeRequest(w io.Writer, req *Request) {
 | 
					func WriteRequest(w io.Writer, req *Request) {
 | 
				
			||||||
	fmt.Fprintf(w, "%s %s %s\r\n", req.Method, req.Path, req.Protocol)
 | 
						fmt.Fprintf(w, "%s %s %s\r\n", req.Method, req.URL.Path, req.Protocol)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, header := range req.Headers {
 | 
						for _, header := range req.Headers {
 | 
				
			||||||
		fmt.Fprintf(w, "%s: %s\r\n", header.Name, header.Value)
 | 
							fmt.Fprintf(w, "%s: %s\r\n", header.Name, header.Value)
 | 
				
			||||||
@ -38,7 +48,7 @@ func writeRequest(w io.Writer, req *Request) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func debugWrap(w io.Writer, req *Request, f func(w io.Writer, req *Request)) {
 | 
					func WriteRequestDebugWrap(w io.Writer, req *Request, f func(w io.Writer, req *Request)) {
 | 
				
			||||||
	buffer := &bytes.Buffer{}
 | 
						buffer := &bytes.Buffer{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	f(buffer, req)
 | 
						f(buffer, req)
 | 
				
			||||||
@ -50,28 +60,31 @@ func debugWrap(w io.Writer, req *Request, f func(w io.Writer, req *Request)) {
 | 
				
			|||||||
	w.Write(buffer.Bytes())
 | 
						w.Write(buffer.Bytes())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func readResponse(r io.Reader) (*Response, error) {
 | 
					func ReadResponse(r io.Reader) (*Response, error) {
 | 
				
			||||||
	b := bufio.NewReader(r)
 | 
						b := bufio.NewReader(r)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if _, err := b.ReadString(' '); err != nil {
 | 
						protocol, err := b.ReadString(' ')
 | 
				
			||||||
		return nil, errors.New("read httpVersion")
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, errors.New("read protocol")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	statusCodeStr, err := b.ReadString(' ')
 | 
						protocol = protocol[:len(protocol)-1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						statusCode, err := b.ReadString(' ')
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, errors.New("read statusCode")
 | 
							return nil, errors.New("read statusCode")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if _, err := b.ReadString('\r'); err != nil {
 | 
						statusCode = statusCode[:len(statusCode)-1]
 | 
				
			||||||
		return nil, errors.New("read statusName")
 | 
					
 | 
				
			||||||
 | 
						statusMessage, err := b.ReadString('\r')
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, errors.New("read statusMessage")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						statusMessage = statusMessage[:len(statusMessage)-1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if _, err := b.ReadString('\n'); err != nil {
 | 
						if _, err := b.ReadString('\n'); err != nil {
 | 
				
			||||||
		return nil, errors.New("read LF")
 | 
							return nil, errors.New("read LF")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	statusCode, err := strconv.Atoi(statusCodeStr[:len(statusCodeStr)-1])
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, errors.New("read statusCode")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	headerStr := ""
 | 
						headerStr := ""
 | 
				
			||||||
	contentLengthStr := ""
 | 
						contentLengthStr := ""
 | 
				
			||||||
	headers := []Header{}
 | 
						headers := []Header{}
 | 
				
			||||||
@ -108,8 +121,27 @@ func readResponse(r io.Reader) (*Response, error) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return &Response{
 | 
						return &Response{
 | 
				
			||||||
		StatusCode: statusCode,
 | 
							Protocol:      protocol,
 | 
				
			||||||
		Headers:    headers,
 | 
							StatusCode:    statusCode,
 | 
				
			||||||
		Body:       body,
 | 
							StatusMessage: statusMessage,
 | 
				
			||||||
 | 
							Headers:       headers,
 | 
				
			||||||
 | 
							Body:          body,
 | 
				
			||||||
	}, nil
 | 
						}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ReadResponseDebugWrap(r io.Reader, f func(r io.Reader) (*Response, error)) (*Response, error) {
 | 
				
			||||||
 | 
						resp, err := f(r)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Println("----- Debug Info -----")
 | 
				
			||||||
 | 
						fmt.Printf("%s %s %s\n", resp.Protocol, resp.StatusCode, resp.StatusMessage)
 | 
				
			||||||
 | 
						for _, header := range resp.Headers {
 | 
				
			||||||
 | 
							fmt.Printf("%s: %s\n", header.Name, header.Value)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(resp.Body) > 0 {
 | 
				
			||||||
 | 
							fmt.Println(string(resp.Body))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Println("----- Debug End Info -----")
 | 
				
			||||||
 | 
						return resp, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -5,17 +5,20 @@ import (
 | 
				
			|||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"git.3crabs.ru/VLADIMIR/net/url"
 | 
				
			||||||
	"github.com/stretchr/testify/assert"
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Test_WriteRequest(t *testing.T) {
 | 
					func Test_WriteRequest(t *testing.T) {
 | 
				
			||||||
	b := &strings.Builder{}
 | 
						b := &strings.Builder{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	writeRequest(
 | 
						WriteRequest(
 | 
				
			||||||
		b,
 | 
							b,
 | 
				
			||||||
		&Request{
 | 
							&Request{
 | 
				
			||||||
			Method:   "GET",
 | 
								Method: "GET",
 | 
				
			||||||
			Path:     "/",
 | 
								URL: &url.URL{
 | 
				
			||||||
 | 
									Path: "/",
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
			Protocol: "HTTP/1.0",
 | 
								Protocol: "HTTP/1.0",
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
@ -28,10 +31,10 @@ func Test_ReadResponse(t *testing.T) {
 | 
				
			|||||||
		[]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!"),
 | 
							[]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)
 | 
						r, err := ReadResponse(b)
 | 
				
			||||||
	assert.Nil(t, err)
 | 
						assert.Nil(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	assert.Equal(t, 200, r.StatusCode)
 | 
						assert.Equal(t, "200", r.StatusCode)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	assert.Equal(t, "Date", r.Headers[0].Name)
 | 
						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, "Sat, 03 Feb 2024 16:40:29 GMT", r.Headers[0].Value)
 | 
				
			||||||
 | 
				
			|||||||
@ -2,7 +2,6 @@ package main
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.3crabs.ru/VLADIMIR/net/http"
 | 
						"git.3crabs.ru/VLADIMIR/net/http"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@ -21,9 +20,9 @@ func main() {
 | 
				
			|||||||
		DNS:       &CustomDNS{},
 | 
							DNS:       &CustomDNS{},
 | 
				
			||||||
		Transport: &http.HttpTransport{},
 | 
							Transport: &http.HttpTransport{},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	r, 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"},
 | 
				
			||||||
@ -33,9 +32,4 @@ func main() {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		panic(err)
 | 
							panic(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	fmt.Println(r.StatusCode)
 | 
					 | 
				
			||||||
	for _, header := range r.Headers {
 | 
					 | 
				
			||||||
		fmt.Println(header)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	fmt.Println(string(r.Body))
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										15
									
								
								url/examples/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								url/examples/main.go
									
									
									
									
									
										Normal file
									
								
							@ -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
									
								
								url/url.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								url/url.go
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user