120 lines
2.3 KiB
Go
120 lines
2.3 KiB
Go
package http
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
"io"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
CRLF = []byte("\r\n")
|
|
SP = []byte(" ")
|
|
)
|
|
|
|
func getIP(domain string) string {
|
|
if domain == "test.ru" {
|
|
return "127.0.0.1:8081"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func 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 := getIP(arr[0])
|
|
conn, err := net.Dial("tcp", connectPath)
|
|
// conn, err := tls.Dial("tcp", connectPath, nil)
|
|
if err != nil {
|
|
return nil, errors.New("connect " + connectPath)
|
|
}
|
|
defer conn.Close()
|
|
|
|
DebugWrap(
|
|
conn,
|
|
&Request{
|
|
Method: method,
|
|
Path: path,
|
|
Protocol: "HTTP/1.0",
|
|
Headers: headers,
|
|
},
|
|
WriteRequest,
|
|
)
|
|
return ReadResponse(conn)
|
|
}
|
|
|
|
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
|
|
}
|