create mini http client

This commit is contained in:
Владимир Фёдоров 2024-02-03 23:26:09 +07:00
commit f0f1e83c60
7 changed files with 149 additions and 0 deletions

0
.gitignore vendored Normal file
View File

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.3crabs.ru/VLADIMIR/net
go 1.20

87
http/client.go Normal file
View File

@ -0,0 +1,87 @@
package http
import (
"bufio"
"errors"
"fmt"
"io"
"net"
"strconv"
"strings"
)
func Do(method string, url string) (*Response, error) {
path := "/"
arr := strings.Split(url, "/")
if len(arr) == 2 {
path = arr[1]
}
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
conn, err := net.Dial("tcp", arr[0])
if err != nil {
return nil, errors.New("connect " + arr[0])
}
defer conn.Close()
WriteRequest(conn, method, path, "HTTP/1.0")
return ReadResponse(conn)
}
func WriteRequest(w io.Writer, method string, path string, httpVersion string) {
fmt.Fprintf(w, "%s %s %s\r\n\r\n", method, path, httpVersion)
}
func ReadResponse(r io.Reader) (*Response, error) {
response := &Response{}
b := bufio.NewReader(r)
status, err := b.ReadString('\n')
if err != nil {
return response, errors.New("read status")
}
arr := strings.Split(status, " ")
response.StatusCode, err = strconv.Atoi(arr[1])
if err != nil {
return response, errors.New("read status code")
}
headerStr := ""
for {
headerStr, err = b.ReadString('\n')
if err != nil {
return response, errors.New("read header")
}
if headerStr == "\r\n" {
break
}
arr := strings.Split(headerStr, ": ")
value := strings.TrimSpace(arr[1])
response.Headers = append(response.Headers, &Header{Name: arr[0], Value: value})
}
contentTypeHeader, err := response.GetHeader("Content-Length")
if err != nil {
return response, errors.New("get Content-Length")
}
length, err := strconv.Atoi(contentTypeHeader.Value)
if err != nil {
return response, errors.New("read Content-Length")
}
body := make([]byte, 0, length)
for i := 0; i < length; i++ {
r, err := b.ReadByte()
if err != nil {
return response, errors.New("read body")
}
body = append(body, r)
}
response.Body = body
return response, nil
}

6
http/header.go Normal file
View File

@ -0,0 +1,6 @@
package http
type Header struct {
Name string
Value string
}

24
http/response.go Normal file
View File

@ -0,0 +1,24 @@
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 nil, ErrHeaderNotFound
}

15
test_client/main.go Normal file
View File

@ -0,0 +1,15 @@
package main
import (
"fmt"
"git.3crabs.ru/VLADIMIR/net/http"
)
func main() {
r, err := http.Do("GET", "127.0.0.1:8081")
if err != nil {
panic(err)
}
fmt.Println(r.StatusCode, string(r.Body))
}

14
test_server/main.go Normal file
View File

@ -0,0 +1,14 @@
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World!")
w.WriteHeader(200)
})
http.ListenAndServe(":8081", nil)
}