Skip to content

Commit e4f9d03

Browse files
maskedekenmmmray
andauthored
splithttp Read() using blocking mode (#3473)
* blocking splithttp read * Add testcase * simplify conditions --------- Co-authored-by: mmmray <[email protected]>
1 parent 7acd5a6 commit e4f9d03

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

transport/internet/splithttp/upload_queue.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,19 @@ func (h *UploadQueue) Close() error {
4747
}
4848

4949
func (h *UploadQueue) Read(b []byte) (int, error) {
50-
if h.closed && len(h.heap) == 0 && len(h.pushedPackets) == 0 {
50+
if h.closed {
5151
return 0, io.EOF
5252
}
5353

54-
needMorePackets := false
54+
if len(h.heap) == 0 {
55+
packet, more := <-h.pushedPackets
56+
if !more {
57+
return 0, io.EOF
58+
}
59+
heap.Push(&h.heap, packet)
60+
}
5561

56-
if len(h.heap) > 0 {
62+
for len(h.heap) > 0 {
5763
packet := heap.Pop(&h.heap).(Packet)
5864
n := 0
5965

@@ -81,18 +87,12 @@ func (h *UploadQueue) Read(b []byte) (int, error) {
8187
return 0, newError("packet queue is too large")
8288
}
8389
heap.Push(&h.heap, packet)
84-
needMorePackets = true
85-
}
86-
} else {
87-
needMorePackets = true
88-
}
89-
90-
if needMorePackets {
91-
packet, more := <-h.pushedPackets
92-
if !more {
93-
return 0, io.EOF
90+
packet2, more := <-h.pushedPackets
91+
if !more {
92+
return 0, io.EOF
93+
}
94+
heap.Push(&h.heap, packet2)
9495
}
95-
heap.Push(&h.heap, packet)
9696
}
9797

9898
return 0, nil
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package splithttp_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/xtls/xray-core/common"
7+
. "github.com/xtls/xray-core/transport/internet/splithttp"
8+
)
9+
10+
func Test_regression_readzero(t *testing.T) {
11+
q := NewUploadQueue(10)
12+
q.Push(Packet{
13+
Payload: []byte("x"),
14+
Seq: 0,
15+
})
16+
buf := make([]byte, 20)
17+
n, err := q.Read(buf)
18+
common.Must(err)
19+
if n != 1 {
20+
t.Error("n=", n)
21+
}
22+
}

0 commit comments

Comments
 (0)