Skip to content

Commit 3a2daa0

Browse files
author
drovosek229
committed
XHTTP client: Fix edge-case issue for packet-up mode (XTLS#5020)
XTLS#4952 (comment)
1 parent 178d89d commit 3a2daa0

3 files changed

Lines changed: 25 additions & 22 deletions

File tree

common/buf/buffer.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const (
1313
Size = 8192
1414
)
1515

16+
var ErrBufferFull = errors.New("buffer is full")
17+
1618
var zero = [Size * 10]byte{0}
1719

1820
var pool = bytespool.GetPool(Size)
@@ -266,13 +268,16 @@ func (b *Buffer) IsFull() bool {
266268
func (b *Buffer) Write(data []byte) (int, error) {
267269
nBytes := copy(b.v[b.end:], data)
268270
b.end += int32(nBytes)
271+
if nBytes < len(data) {
272+
return nBytes, ErrBufferFull
273+
}
269274
return nBytes, nil
270275
}
271276

272277
// WriteByte writes a single byte into the buffer.
273278
func (b *Buffer) WriteByte(v byte) error {
274279
if b.IsFull() {
275-
return errors.New("buffer full")
280+
return ErrBufferFull
276281
}
277282
b.v[b.end] = v
278283
b.end++

transport/internet/splithttp/dialer.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -489,15 +489,16 @@ func (w uploadWriter) Write(b []byte) (int, error) {
489489
}
490490
*/
491491

492-
buffer := buf.New()
493-
n, err := buffer.Write(b)
494-
if err != nil {
495-
return 0, err
496-
}
492+
buffer := buf.MultiBufferContainer{}
493+
common.Must2(buffer.Write(b))
497494

498-
err = w.WriteMultiBuffer([]*buf.Buffer{buffer})
499-
if err != nil {
500-
return 0, err
495+
var writed int
496+
for _, buff := range buffer.MultiBuffer {
497+
err := w.WriteMultiBuffer(buf.MultiBuffer{buff})
498+
if err != nil {
499+
return writed, err
500+
}
501+
writed += int(buff.Len())
501502
}
502-
return n, nil
503+
return writed, nil
503504
}

transport/internet/splithttp/splithttp_test.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package splithttp_test
22

33
import (
4+
"bytes"
45
"context"
56
"crypto/rand"
67
"fmt"
@@ -421,18 +422,12 @@ func Test_maxUpload(t *testing.T) {
421422
},
422423
}
423424

424-
var uploadSize int
425+
uploadReceived := make([]byte, 10001)
425426
listen, err := ListenXH(context.Background(), net.LocalHostIP, listenPort, streamSettings, func(conn stat.Connection) {
426427
go func(c stat.Connection) {
427428
defer c.Close()
428-
var b [10240]byte
429429
c.SetReadDeadline(time.Now().Add(2 * time.Second))
430-
n, err := c.Read(b[:])
431-
if err != nil {
432-
return
433-
}
434-
435-
uploadSize = n
430+
io.ReadFull(c, uploadReceived)
436431

437432
common.Must2(c.Write([]byte("Response")))
438433
}(conn)
@@ -441,10 +436,12 @@ func Test_maxUpload(t *testing.T) {
441436
ctx := context.Background()
442437

443438
conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
439+
common.Must(err)
444440

445441
// send a slightly too large upload
446-
var upload [10001]byte
447-
_, err = conn.Write(upload[:])
442+
upload := make([]byte, 10001)
443+
rand.Read(upload)
444+
_, err = conn.Write(upload)
448445
common.Must(err)
449446

450447
var b [10240]byte
@@ -455,8 +452,8 @@ func Test_maxUpload(t *testing.T) {
455452
}
456453
common.Must(conn.Close())
457454

458-
if uploadSize > 10000 || uploadSize == 0 {
459-
t.Error("incorrect upload size: ", uploadSize)
455+
if !bytes.Equal(upload, uploadReceived) {
456+
t.Error("incorrect upload", upload, uploadReceived)
460457
}
461458

462459
common.Must(listen.Close())

0 commit comments

Comments
 (0)