From 5129c1e4ffaff7100e85793eefd0660fcefb8663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Wed, 13 Aug 2025 14:02:26 +0000 Subject: [PATCH 1/4] XHTTP: Fix packet up writer --- common/buf/buffer.go | 7 ++++++- transport/internet/splithttp/dialer.go | 10 ++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/common/buf/buffer.go b/common/buf/buffer.go index 2aa60e6aa85a..72f3248b2643 100644 --- a/common/buf/buffer.go +++ b/common/buf/buffer.go @@ -13,6 +13,8 @@ const ( Size = 8192 ) +var ErrBufferFull = errors.New("buffer is full") + var zero = [Size * 10]byte{0} var pool = bytespool.GetPool(Size) @@ -258,13 +260,16 @@ func (b *Buffer) IsFull() bool { func (b *Buffer) Write(data []byte) (int, error) { nBytes := copy(b.v[b.end:], data) b.end += int32(nBytes) + if nBytes < len(data) { + return nBytes, ErrBufferFull + } return nBytes, nil } // WriteByte writes a single byte into the buffer. func (b *Buffer) WriteByte(v byte) error { if b.IsFull() { - return errors.New("buffer full") + return ErrBufferFull } b.v[b.end] = v b.end++ diff --git a/transport/internet/splithttp/dialer.go b/transport/internet/splithttp/dialer.go index dee5f4863b0b..9133f9a0c659 100644 --- a/transport/internet/splithttp/dialer.go +++ b/transport/internet/splithttp/dialer.go @@ -489,15 +489,17 @@ func (w uploadWriter) Write(b []byte) (int, error) { } */ - buffer := buf.New() + buffer := buf.MultiBufferContainer{} n, err := buffer.Write(b) if err != nil { return 0, err } - err = w.WriteMultiBuffer([]*buf.Buffer{buffer}) - if err != nil { - return 0, err + for _, buff := range buffer.MultiBuffer { + err := w.WriteMultiBuffer(buf.MultiBuffer{buff}) + if err != nil { + return 0, err + } } return n, nil } From 1258dca3ff722b0addf35d7e08fa4b93f87e1837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Thu, 14 Aug 2025 09:39:21 +0000 Subject: [PATCH 2/4] Return writed From annoying request --- transport/internet/splithttp/dialer.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/transport/internet/splithttp/dialer.go b/transport/internet/splithttp/dialer.go index 9133f9a0c659..c0e35422b86b 100644 --- a/transport/internet/splithttp/dialer.go +++ b/transport/internet/splithttp/dialer.go @@ -495,11 +495,13 @@ func (w uploadWriter) Write(b []byte) (int, error) { return 0, err } + var writed int for _, buff := range buffer.MultiBuffer { err := w.WriteMultiBuffer(buf.MultiBuffer{buff}) if err != nil { - return 0, err + return writed, err } + writed += int(buff.Len()) } - return n, nil + return writed, nil } From 2654aa5c7ba46131eceff5b83e7975e95959b936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Thu, 14 Aug 2025 09:41:13 +0000 Subject: [PATCH 3/4] refine --- transport/internet/splithttp/dialer.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/transport/internet/splithttp/dialer.go b/transport/internet/splithttp/dialer.go index c0e35422b86b..a5bb0138c84b 100644 --- a/transport/internet/splithttp/dialer.go +++ b/transport/internet/splithttp/dialer.go @@ -490,10 +490,7 @@ func (w uploadWriter) Write(b []byte) (int, error) { */ buffer := buf.MultiBufferContainer{} - n, err := buffer.Write(b) - if err != nil { - return 0, err - } + common.Must2(buffer.Write(b)) var writed int for _, buff := range buffer.MultiBuffer { From b5e3b5bb8345834c679cf814734d99a67c516ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Thu, 14 Aug 2025 10:04:09 +0000 Subject: [PATCH 4/4] Refactor Test_maxUpload logic --- .../internet/splithttp/splithttp_test.go | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/transport/internet/splithttp/splithttp_test.go b/transport/internet/splithttp/splithttp_test.go index 1db54255eccd..c1bb8580de18 100644 --- a/transport/internet/splithttp/splithttp_test.go +++ b/transport/internet/splithttp/splithttp_test.go @@ -1,6 +1,7 @@ package splithttp_test import ( + "bytes" "context" "crypto/rand" "fmt" @@ -421,18 +422,12 @@ func Test_maxUpload(t *testing.T) { }, } - var uploadSize int + uploadReceived := make([]byte, 10001) listen, err := ListenXH(context.Background(), net.LocalHostIP, listenPort, streamSettings, func(conn stat.Connection) { go func(c stat.Connection) { defer c.Close() - var b [10240]byte c.SetReadDeadline(time.Now().Add(2 * time.Second)) - n, err := c.Read(b[:]) - if err != nil { - return - } - - uploadSize = n + io.ReadFull(c, uploadReceived) common.Must2(c.Write([]byte("Response"))) }(conn) @@ -441,10 +436,12 @@ func Test_maxUpload(t *testing.T) { ctx := context.Background() conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings) + common.Must(err) // send a slightly too large upload - var upload [10001]byte - _, err = conn.Write(upload[:]) + upload := make([]byte, 10001) + rand.Read(upload) + _, err = conn.Write(upload) common.Must(err) var b [10240]byte @@ -455,8 +452,8 @@ func Test_maxUpload(t *testing.T) { } common.Must(conn.Close()) - if uploadSize > 10000 || uploadSize == 0 { - t.Error("incorrect upload size: ", uploadSize) + if !bytes.Equal(upload, uploadReceived) { + t.Error("incorrect upload", upload, uploadReceived) } common.Must(listen.Close())