Skip to content

Commit 0341290

Browse files
committed
TASK: prevent cache misses from crashing the whole application
1 parent 9e37d1b commit 0341290

5 files changed

Lines changed: 64 additions & 48 deletions

File tree

dummypathmapper/dummypathmapper.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ func (p *PathMapper) Initialize(c *config.Config, l *logger.Logger, m *pathmappi
3333
}
3434

3535
// ApplyMappingToTextProtocol change file path in xDebug text protocol
36-
func (p *PathMapper) ApplyMappingToTextProtocol(message []byte) []byte {
37-
return message
36+
func (p *PathMapper) ApplyMappingToTextProtocol(message []byte) ([]byte, error) {
37+
return message, nil
3838
}
3939

4040
// ApplyMappingToXML change file path in xDebug XML protocol
41-
func (p *PathMapper) ApplyMappingToXML(message []byte) []byte {
42-
return message
41+
func (p *PathMapper) ApplyMappingToXML(message []byte) ([]byte, error) {
42+
return message, nil
4343
}

errorhandler/errorhanlder.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

flowpathmapper/flowpathmapper.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package flowpathmapper
66

77
import (
88
"github.com/dfeyer/flow-debugproxy/config"
9-
"github.com/dfeyer/flow-debugproxy/errorhandler"
109
"github.com/dfeyer/flow-debugproxy/logger"
1110
"github.com/dfeyer/flow-debugproxy/pathmapperfactory"
1211
"github.com/dfeyer/flow-debugproxy/pathmapping"
@@ -16,9 +15,9 @@ import (
1615
"io/ioutil"
1716
"os"
1817
"regexp"
18+
"runtime"
1919
"strconv"
2020
"strings"
21-
"runtime"
2221
)
2322

2423
const (
@@ -63,32 +62,34 @@ func (p *PathMapper) Initialize(c *config.Config, l *logger.Logger, m *pathmappi
6362
}
6463

6564
// ApplyMappingToTextProtocol change file path in xDebug text protocol
66-
func (p *PathMapper) ApplyMappingToTextProtocol(message []byte) []byte {
67-
return p.doTextPathMapping(message)
65+
func (p *PathMapper) ApplyMappingToTextProtocol(message []byte) ([]byte, error) {
66+
return p.doTextPathMapping(message), nil
6867
}
6968

7069
// ApplyMappingToXML change file path in xDebug XML protocol
71-
func (p *PathMapper) ApplyMappingToXML(message []byte) []byte {
70+
func (p *PathMapper) ApplyMappingToXML(message []byte) ([]byte, error) {
7271
message = p.doXMLPathMapping(message)
7372

7473
// update xml length count
7574
s := strings.Split(string(message), "\x00")
7675
i, err := strconv.Atoi(s[0])
77-
errorhandler.PanicHandling(err, p.logger)
76+
if err != nil {
77+
return nil, err
78+
}
7879
l := len(s[1])
7980
if i != l {
8081
message = bytes.Replace(message, []byte(strconv.Itoa(i)), []byte(strconv.Itoa(l)), 1)
8182
}
8283

83-
return message
84+
return message, nil
8485
}
8586

8687
func (p *PathMapper) doTextPathMapping(message []byte) []byte {
8788
var processedMapping = map[string]string{}
8889
for _, match := range regexpPhpFile.FindAllStringSubmatch(string(message), -1) {
8990
originalPath := match[1]
9091
if runtime.GOOS == "windows" {
91-
originalPath = strings.Replace(originalPath,"//","", 1)
92+
originalPath = strings.Replace(originalPath, "//", "", 1)
9293
}
9394
path := p.mapPath(originalPath)
9495
p.logger.Debug("doTextPathMapping %s >>> %s", path, originalPath)
@@ -183,7 +184,10 @@ func (p *PathMapper) readOriginalPathFromCache(path, basePath string) string {
183184
}
184185
p.logger.Debug("readOriginalPathFromCache %s", localPath)
185186
dat, err := ioutil.ReadFile(localPath)
186-
errorhandler.PanicHandling(err, p.logger)
187+
if err != nil {
188+
p.logger.Warn(err.Error())
189+
return localPath
190+
}
187191
match := regexpPathAndFilename.FindStringSubmatch(string(dat))
188192
if len(match) == 2 {
189193
originalPath := match[1]

main.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
package main
66

77
import (
8+
golog "log"
9+
810
"github.com/dfeyer/flow-debugproxy/config"
911

10-
"github.com/dfeyer/flow-debugproxy/errorhandler"
1112
"github.com/dfeyer/flow-debugproxy/logger"
1213
"github.com/dfeyer/flow-debugproxy/pathmapperfactory"
1314
"github.com/dfeyer/flow-debugproxy/pathmapping"
@@ -84,13 +85,20 @@ func main() {
8485
Config: c,
8586
}
8687

87-
laddr, raddr, listener := setupNetworkConnection(cli.String("xdebug"), cli.String("ide"), log)
88+
laddr, raddr, listener, err := setupNetworkConnection(cli.String("xdebug"), cli.String("ide"))
89+
if err != nil {
90+
log.Warn(err.Error())
91+
os.Exit(1)
92+
}
8893

8994
log.Info("Debugger from %v\nIDE from %v\n", laddr, raddr)
9095

9196
pathMapping := &pathmapping.PathMapping{}
9297
pathMapper, err := pathmapperfactory.Create(c, pathMapping, log)
93-
errorhandler.PanicHandling(err, log)
98+
if err != nil {
99+
log.Warn(err.Error())
100+
os.Exit(1)
101+
}
94102

95103
for {
96104
conn, err := listener.AcceptTCP()
@@ -109,18 +117,28 @@ func main() {
109117
}
110118
}
111119

112-
app.Run(os.Args)
120+
err := app.Run(os.Args)
121+
if err != nil {
122+
golog.Fatal(err)
123+
return
124+
}
113125
}
114126

115-
func setupNetworkConnection(xdebugAddr string, ideAddr string, log *logger.Logger) (*net.TCPAddr, *net.TCPAddr, *net.TCPListener) {
127+
func setupNetworkConnection(xdebugAddr string, ideAddr string) (*net.TCPAddr, *net.TCPAddr, *net.TCPListener, error) {
116128
laddr, err := net.ResolveTCPAddr("tcp", xdebugAddr)
117-
errorhandler.PanicHandling(err, log)
129+
if err != nil {
130+
return nil, nil, nil, err
131+
}
118132

119133
raddr, err := net.ResolveTCPAddr("tcp", ideAddr)
120-
errorhandler.PanicHandling(err, log)
134+
if err != nil {
135+
return nil, nil, nil, err
136+
}
121137

122138
listener, err := net.ListenTCP("tcp", laddr)
123-
errorhandler.PanicHandling(err, log)
139+
if err != nil {
140+
return nil, nil, nil, err
141+
}
124142

125-
return laddr, raddr, listener
143+
return laddr, raddr, listener, nil
126144
}

xdebugproxy/xdebugproxy.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
"github.com/dfeyer/flow-debugproxy/logger"
1010
"github.com/dfeyer/flow-debugproxy/pathmapping"
1111

12+
"bytes"
1213
"fmt"
1314
"io"
1415
"net"
15-
"bytes"
1616
"strconv"
1717
)
1818

@@ -21,8 +21,8 @@ const h = "%s"
2121
// XDebugProcessorPlugin process message in xDebug protocol
2222
type XDebugProcessorPlugin interface {
2323
Initialize(c *config.Config, l *logger.Logger, m *pathmapping.PathMapping)
24-
ApplyMappingToTextProtocol(message []byte) []byte
25-
ApplyMappingToXML(message []byte) []byte
24+
ApplyMappingToTextProtocol(message []byte) ([]byte, error)
25+
ApplyMappingToXML(message []byte) ([]byte, error)
2626
}
2727

2828
// Proxy represents a pair of connections and their state
@@ -135,18 +135,30 @@ func (p *Proxy) pipe(src, dst *net.TCPConn) {
135135
}
136136
// extract command name
137137
if isFromDebugger {
138-
b = p.PathMapper.ApplyMappingToXML(b)
138+
b, err = p.PathMapper.ApplyMappingToXML(b)
139+
if p.handleError(err, dst) {
140+
return
141+
}
139142
// post processors
140143
for _, d := range p.postProcessors {
141144
processor = d
142-
b = processor.ApplyMappingToXML(b)
145+
b, err = processor.ApplyMappingToXML(b)
146+
if p.handleError(err, dst) {
147+
return
148+
}
143149
}
144150
} else {
145-
b = p.PathMapper.ApplyMappingToTextProtocol(b)
151+
b, err = p.PathMapper.ApplyMappingToTextProtocol(b)
152+
if p.handleError(err, dst) {
153+
return
154+
}
146155
// post processors
147156
for _, d := range p.postProcessors {
148157
processor = d
149-
b = processor.ApplyMappingToTextProtocol(b)
158+
b, err = processor.ApplyMappingToTextProtocol(b)
159+
if p.handleError(err, dst) {
160+
return
161+
}
150162
}
151163
}
152164

@@ -184,4 +196,4 @@ func (p *Proxy) handleError(err error, ch *net.TCPConn) bool {
184196
}
185197

186198
return false
187-
}
199+
}

0 commit comments

Comments
 (0)