mirror of
https://github.com/soypat/lneto.git
synced 2026-08-17 13:23:30 +00:00
Further improvements to httphi API (#173)
* add Exchange.WriteBodyString * Mux.MaxPathValues and other improvements * Mux PathValue improvemnt and fixes * MuxSlice more method muxing improvements * diagram out interesting approach to form parsing for clanker * refactor RequestParseForm and achieve greatness in API design * explicit naming of headerCapacityKV value in kvBuffer.Reset * fix Mux bug not matching paths correctly; httpraw HTTP V1 naming applied * rename many examples,use httphi in examples,remove useless maxAwaitingConn field * add ipv4.String * add ipv4 UnspecifiedAddr and BroadcastAddr * add ethernet.String
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/soypat/lneto/http/httpraw"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("DONE")
|
||||
}
|
||||
|
||||
func run() error {
|
||||
var port int
|
||||
flag.IntVar(&port, "lport", 13337, "Local port over which to hit server")
|
||||
flag.Parse()
|
||||
// Prepare GET request.
|
||||
var hdr httpraw.HeaderV1
|
||||
hdr.SetMethod("GET")
|
||||
hdr.SetRequestTarget("/")
|
||||
hdr.SetProtocol("HTTP/1.1")
|
||||
req, err := hdr.AppendRequest(nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(time.Now().Format("15:04:05.000"), "dialing...")
|
||||
conn, err := net.DialTCP("tcp4", &net.TCPAddr{IP: []byte{192, 168, 10, 1}, Port: port}, &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(time.Now().Format("15:04:05.000"), "writing...")
|
||||
conn.Write(req)
|
||||
hdr.Reset(nil, 0) // No preallocation. Both key/value and raw buffer will grow and allocate.
|
||||
var needMore bool = true
|
||||
for needMore {
|
||||
_, err = hdr.ReadFromLimited(conn, 1024)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
const asResponse = true
|
||||
needMore, err = hdr.TryParse(asResponse)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(time.Now().Format("15:04:05.000"), "got HTTP:\n", hdr.String())
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user