receive HTTP request success; still working on response

This commit is contained in:
soypat
2025-05-31 14:08:32 -03:00
parent 0008791bd8
commit 1ae8fab210
4 changed files with 119 additions and 54 deletions
+34 -8
View File
@@ -4,6 +4,8 @@ import (
"fmt"
"net"
"os"
"os/signal"
"syscall"
"github.com/soypat/lneto/http/httpraw"
)
@@ -17,27 +19,51 @@ func main() {
}
func run() error {
// Prepare GET request.
var hdr httpraw.Header
hdr.SetMethod("GET")
hdr.SetRequestURI("/")
hdr.SetProtocol("HTTP/1.1")
req, err := hdr.AppendRequest(nil)
if err != nil {
return err
}
fmt.Println("dialing...")
conn, err := net.DialTCP("tcp4", &net.TCPAddr{IP: []byte{192, 168, 10, 1}, Port: 1337}, &net.TCPAddr{IP: []byte{192, 168, 10, 2}, Port: 80})
if err != nil {
return err
}
defer conn.Close()
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
s := <-c
fmt.Println("terminating connection on signal", s.String())
conn.Close()
os.Exit(0)
}()
fmt.Println("reading...")
var hdr httpraw.Header
for {
conn.Write(req)
hdr.Reset(nil)
var needMore bool = true
for needMore {
_, err = hdr.ReadFromLimited(conn, 1024)
if err != nil {
return err
break
}
const asRequest = false
var ok bool
ok, err = hdr.TryParse(asRequest)
if ok {
const asResponse = true
needMore, err = hdr.TryParse(asResponse)
if needMore {
break
} else if err != nil {
return err
break
}
}
if err != nil {
return err
}
fmt.Println("got HTTP:\n", hdr.String())
return nil
}