Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 09e4972

Browse files
committed
[proxy]修复在Windows上URL特殊字符不能访问的问题
1 parent 81fbe2d commit 09e4972

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

teaconst/const.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package teaconst
22

33
const (
4-
TeaVersion = "0.1.9.1"
4+
TeaVersion = "0.1.9.2"
55

66
TeaProcessName = "teaweb" // 进程名
77
TeaProductName = "TeaWeb" // 产品名

teaproxy/http_serve_mux.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package teaproxy
22

33
import (
44
"net/http"
5-
"path/filepath"
65
)
76

87
// 自定义ServeMux
@@ -12,7 +11,7 @@ type HTTPServeMux struct {
1211

1312
func (this *HTTPServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
1413
// 解决因为URL中包含多个/而自动跳转的问题
15-
r.URL.Path = filepath.Clean(r.URL.Path)
14+
r.URL.Path = CleanPath(r.URL.Path)
1615

1716
this.ServeMux.ServeHTTP(w, r)
1817
}

teaproxy/request_backend.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"io"
1010
"net/http"
1111
"net/url"
12-
"path/filepath"
1312
"strings"
1413
"time"
1514
)
@@ -61,11 +60,10 @@ func (this *Request) callBackend(writer *ResponseWriter) error {
6160

6261
// new uri
6362
if this.backend.HasRequestURI() {
64-
uri := filepath.Clean(this.Format(this.backend.RequestPath()))
65-
63+
uri := this.Format(this.backend.RequestPath())
6664
u, err := url.ParseRequestURI(uri)
6765
if err == nil {
68-
this.raw.URL.Path = u.Path
66+
this.raw.URL.Path = CleanPath(u.Path)
6967
this.raw.URL.RawQuery = u.RawQuery
7068

7169
args := this.Format(this.backend.RequestArgs())

teaproxy/utils.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package teaproxy
2+
3+
// 清理Path中的多于信息
4+
func CleanPath(path string) string {
5+
l := len(path)
6+
if l == 0 {
7+
return "/"
8+
}
9+
result := []byte{'/'}
10+
isSlash := true
11+
for i := 0; i < l; i++ {
12+
if path[i] == '\\' || path[i] == '/' {
13+
if !isSlash {
14+
isSlash = true
15+
result = append(result, '/')
16+
}
17+
} else {
18+
isSlash = false
19+
result = append(result, path[i])
20+
}
21+
}
22+
return string(result)
23+
}

teaproxy/utils_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package teaproxy
2+
3+
import (
4+
"github.com/iwind/TeaGo/assert"
5+
"testing"
6+
)
7+
8+
func TestCleanPath(t *testing.T) {
9+
a := assert.NewAssertion(t)
10+
11+
a.IsTrue(CleanPath("") == "/")
12+
a.IsTrue(CleanPath("/hello/world") == "/hello/world")
13+
a.IsTrue(CleanPath("\\hello\\world") == "/hello/world")
14+
a.IsTrue(CleanPath("/\\hello\\//world") == "/hello/world")
15+
a.IsTrue(CleanPath("hello/world") == "/hello/world")
16+
a.IsTrue(CleanPath("/hello////world") == "/hello/world")
17+
}
18+
19+
func BenchmarkCleanPath(b *testing.B) {
20+
for i := 0; i < b.N; i++ {
21+
_ = CleanPath("/hello///world/very/long/very//long")
22+
}
23+
}

0 commit comments

Comments
 (0)