Skip to content

Commit c199e77

Browse files
authored
Merge pull request #20 from jdoubleu/fix-large-xdebug-chunks
Fix incomplete data from xdebug
2 parents d5e0fbf + 65f6efd commit c199e77

1 file changed

Lines changed: 38 additions & 8 deletions

File tree

xdebugproxy/xdebugproxy.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"fmt"
1313
"io"
1414
"net"
15+
"bytes"
16+
"strconv"
1517
)
1618

1719
const h = "%s"
@@ -97,12 +99,31 @@ func (p *Proxy) pipe(src, dst *net.TCPConn) {
9799
buff := make([]byte, 0xffff)
98100
for {
99101
n, err := src.Read(buff)
100-
if err != nil {
101-
p.pipeErrors <- err
102-
// make sure the other pipe will stop as well
103-
dst.Close()
102+
if p.handleError(err, dst) {
104103
return
105104
}
105+
// check for incomplete data from xdebug
106+
if isFromDebugger {
107+
header := bytes.Split(buff, []byte{0})
108+
sizeStr := string(header[0])
109+
sizeLen := len(sizeStr)
110+
size, err := strconv.Atoi(sizeStr)
111+
if p.handleError(err, dst) {
112+
return
113+
}
114+
115+
// whole message consists of [size NULL XML(data) NULL]
116+
packetLen := sizeLen + size + 2
117+
// read more data if buffer has not been filled with all expected data
118+
for n < packetLen {
119+
n2, err := src.Read(buff[n:])
120+
if p.handleError(err, dst) {
121+
return
122+
}
123+
124+
n += n2
125+
}
126+
}
106127
b := buff[:n]
107128
p.log(h, f)
108129
if p.Config.VeryVerbose {
@@ -142,10 +163,7 @@ func (p *Proxy) pipe(src, dst *net.TCPConn) {
142163

143164
// write out result
144165
n, err = dst.Write(b)
145-
if err != nil {
146-
p.pipeErrors <- err
147-
// make sure the other pipe will stop as well
148-
src.Close()
166+
if p.handleError(err, src) {
149167
return
150168
}
151169
if isFromDebugger {
@@ -155,3 +173,15 @@ func (p *Proxy) pipe(src, dst *net.TCPConn) {
155173
}
156174
}
157175
}
176+
177+
func (p *Proxy) handleError(err error, ch *net.TCPConn) bool {
178+
if err != nil {
179+
p.pipeErrors <- err
180+
// make sure the other pipe will stop as well
181+
ch.Close()
182+
183+
return true
184+
}
185+
186+
return false
187+
}

0 commit comments

Comments
 (0)