add client and transport
This commit is contained in:
		
							parent
							
								
									729015c4ea
								
							
						
					
					
						commit
						6d1bf72d9d
					
				
							
								
								
									
										107
									
								
								http/client.go
									
									
									
									
									
								
							
							
						
						
									
										107
									
								
								http/client.go
									
									
									
									
									
								
							@ -1,11 +1,6 @@
 | 
				
			|||||||
package http
 | 
					package http
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bufio"
 | 
					 | 
				
			||||||
	"errors"
 | 
					 | 
				
			||||||
	"io"
 | 
					 | 
				
			||||||
	"net"
 | 
					 | 
				
			||||||
	"strconv"
 | 
					 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -14,14 +9,12 @@ var (
 | 
				
			|||||||
	SP   = []byte(" ")
 | 
						SP   = []byte(" ")
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func getIP(domain string) string {
 | 
					type Client struct {
 | 
				
			||||||
	if domain == "test.ru" {
 | 
						DNS       DNS
 | 
				
			||||||
		return "127.0.0.1:8081"
 | 
						Transport RoundTripper
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return ""
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Do(method string, url string, headers []Header) (*Response, error) {
 | 
					func (c *Client) Do(method string, url string, headers []Header) (*Response, error) {
 | 
				
			||||||
	url = strings.TrimPrefix(url, "http://")
 | 
						url = strings.TrimPrefix(url, "http://")
 | 
				
			||||||
	url = strings.TrimPrefix(url, "https://")
 | 
						url = strings.TrimPrefix(url, "https://")
 | 
				
			||||||
	path := "/"
 | 
						path := "/"
 | 
				
			||||||
@ -33,87 +26,23 @@ func Do(method string, url string, headers []Header) (*Response, error) {
 | 
				
			|||||||
		path = "/" + path
 | 
							path = "/" + path
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	connectPath := getIP(arr[0])
 | 
						connectPath := ""
 | 
				
			||||||
	conn, err := net.Dial("tcp", connectPath)
 | 
						if c.DNS != nil {
 | 
				
			||||||
	// conn, err := tls.Dial("tcp", connectPath, nil)
 | 
							var err error
 | 
				
			||||||
	if err != nil {
 | 
							connectPath, err = c.DNS.GetIP(arr[0])
 | 
				
			||||||
		return nil, errors.New("connect " + connectPath)
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	defer conn.Close()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	DebugWrap(
 | 
						resp, err := c.Transport.RoundTrip(
 | 
				
			||||||
		conn,
 | 
					 | 
				
			||||||
		&Request{
 | 
							&Request{
 | 
				
			||||||
			Method:   method,
 | 
								ConnectPath: connectPath,
 | 
				
			||||||
			Path:     path,
 | 
								Method:      method,
 | 
				
			||||||
			Protocol: "HTTP/1.0",
 | 
								Path:        path,
 | 
				
			||||||
			Headers:  headers,
 | 
								Protocol:    "HTTP/1.0",
 | 
				
			||||||
 | 
								Headers:     headers,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		WriteRequest,
 | 
					 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
	return ReadResponse(conn)
 | 
						return resp, err
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func ReadResponse(r io.Reader) (*Response, error) {
 | 
					 | 
				
			||||||
	b := bufio.NewReader(r)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if _, err := b.ReadString(' '); err != nil {
 | 
					 | 
				
			||||||
		return nil, errors.New("read httpVersion")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	statusCodeStr, err := b.ReadString(' ')
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, errors.New("read statusCode")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if _, err := b.ReadString('\r'); err != nil {
 | 
					 | 
				
			||||||
		return nil, errors.New("read statusName")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if _, err := b.ReadString('\n'); err != nil {
 | 
					 | 
				
			||||||
		return nil, errors.New("read LF")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	statusCode, err := strconv.Atoi(statusCodeStr[:len(statusCodeStr)-1])
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, errors.New("read statusCode")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	headerStr := ""
 | 
					 | 
				
			||||||
	contentLengthStr := ""
 | 
					 | 
				
			||||||
	headers := []Header{}
 | 
					 | 
				
			||||||
	for {
 | 
					 | 
				
			||||||
		headerStr, err = b.ReadString('\n')
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return nil, errors.New("read header")
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		if headerStr == "\r\n" {
 | 
					 | 
				
			||||||
			break
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		arr := strings.Split(headerStr, ": ")
 | 
					 | 
				
			||||||
		value := strings.TrimSpace(arr[1])
 | 
					 | 
				
			||||||
		header := Header{Name: arr[0], Value: value}
 | 
					 | 
				
			||||||
		if header.Name == "Content-Length" {
 | 
					 | 
				
			||||||
			contentLengthStr = header.Value
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		headers = append(headers, header)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	length, err := strconv.Atoi(contentLengthStr)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, errors.New("read Content-Length")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	body := make([]byte, 0, length)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	for i := 0; i < length; i++ {
 | 
					 | 
				
			||||||
		r, err := b.ReadByte()
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return nil, errors.New("read body")
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		body = append(body, r)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return &Response{
 | 
					 | 
				
			||||||
		StatusCode: statusCode,
 | 
					 | 
				
			||||||
		Headers:    headers,
 | 
					 | 
				
			||||||
		Body:       body,
 | 
					 | 
				
			||||||
	}, nil
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										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)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										6
									
								
								http/header.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								http/header.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,6 @@
 | 
				
			|||||||
 | 
					package http
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Header struct {
 | 
				
			||||||
 | 
						Name  string
 | 
				
			||||||
 | 
						Value string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -1,6 +1,8 @@
 | 
				
			|||||||
package http
 | 
					package http
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Request struct {
 | 
					type Request struct {
 | 
				
			||||||
 | 
						ConnectPath string // ip
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Method   string
 | 
						Method   string
 | 
				
			||||||
	Path     string
 | 
						Path     string
 | 
				
			||||||
	Protocol string
 | 
						Protocol string
 | 
				
			||||||
@ -9,8 +11,3 @@ type Request struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	Body string
 | 
						Body string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
type Header struct {
 | 
					 | 
				
			||||||
	Name  string
 | 
					 | 
				
			||||||
	Value string
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										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)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										115
									
								
								http/transport.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										115
									
								
								http/transport.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,115 @@
 | 
				
			|||||||
 | 
					package http
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bufio"
 | 
				
			||||||
 | 
						"bytes"
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"net"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type HttpTransport struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (t *HttpTransport) RoundTrip(req *Request) (*Response, error) {
 | 
				
			||||||
 | 
						conn, err := net.Dial("tcp", req.ConnectPath)
 | 
				
			||||||
 | 
						// conn, err := tls.Dial("tcp", connectPath, nil)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, errors.New("connect " + req.ConnectPath)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer conn.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						debugWrap(conn, req, writeRequest)
 | 
				
			||||||
 | 
						return readResponse(conn)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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 debugWrap(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)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if _, err := b.ReadString(' '); err != nil {
 | 
				
			||||||
 | 
							return nil, errors.New("read httpVersion")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						statusCodeStr, err := b.ReadString(' ')
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, errors.New("read statusCode")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if _, err := b.ReadString('\r'); err != nil {
 | 
				
			||||||
 | 
							return nil, errors.New("read statusName")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if _, err := b.ReadString('\n'); err != nil {
 | 
				
			||||||
 | 
							return nil, errors.New("read LF")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						statusCode, err := strconv.Atoi(statusCodeStr[:len(statusCodeStr)-1])
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, errors.New("read statusCode")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						headerStr := ""
 | 
				
			||||||
 | 
						contentLengthStr := ""
 | 
				
			||||||
 | 
						headers := []Header{}
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
 | 
							headerStr, err = b.ReadString('\n')
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, errors.New("read header")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if headerStr == "\r\n" {
 | 
				
			||||||
 | 
								break
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							arr := strings.Split(headerStr, ": ")
 | 
				
			||||||
 | 
							value := strings.TrimSpace(arr[1])
 | 
				
			||||||
 | 
							header := Header{Name: arr[0], Value: value}
 | 
				
			||||||
 | 
							if header.Name == "Content-Length" {
 | 
				
			||||||
 | 
								contentLengthStr = header.Value
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							headers = append(headers, header)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						length, err := strconv.Atoi(contentLengthStr)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, errors.New("read Content-Length")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						body := make([]byte, 0, length)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for i := 0; i < length; i++ {
 | 
				
			||||||
 | 
							r, err := b.ReadByte()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, errors.New("read body")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							body = append(body, r)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return &Response{
 | 
				
			||||||
 | 
							StatusCode: statusCode,
 | 
				
			||||||
 | 
							Headers:    headers,
 | 
				
			||||||
 | 
							Body:       body,
 | 
				
			||||||
 | 
						}, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -11,7 +11,7 @@ import (
 | 
				
			|||||||
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",
 | 
				
			||||||
@ -28,7 +28,7 @@ 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)
 | 
				
			||||||
@ -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 DebugWrap(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,13 +1,27 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.3crabs.ru/VLADIMIR/net/http"
 | 
						"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() {
 | 
					func main() {
 | 
				
			||||||
	r, err := http.Do(
 | 
						client := http.Client{
 | 
				
			||||||
 | 
							DNS:       &CustomDNS{},
 | 
				
			||||||
 | 
							Transport: &http.HttpTransport{},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						r, err := client.Do(
 | 
				
			||||||
		"GET",
 | 
							"GET",
 | 
				
			||||||
		"http://test.ru",
 | 
							"http://test.ru",
 | 
				
			||||||
		[]http.Header{
 | 
							[]http.Header{
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user