Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed Nov 22, 2023
1 parent 9c3f0b7 commit 40712fb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
7 changes: 1 addition & 6 deletions pkg/protocol/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import (
"github.com/cloudwego/hertz/internal/bytesconv"
"github.com/cloudwego/hertz/internal/bytestr"
"github.com/cloudwego/hertz/internal/nocopy"
"github.com/cloudwego/hertz/pkg/common/hlog"
)

// AcquireURI returns an empty URI instance from the pool.
Expand Down Expand Up @@ -388,11 +387,7 @@ func getScheme(rawURL []byte) (scheme, path []byte) {
return nil, rawURL
}
case c == ':':
if i == 0 {
hlog.Errorf("error happened when try to parse the rawURL(%s): missing protocol scheme", rawURL)
return nil, nil
}
return rawURL[:i], rawURL[i+1:]
return checkSchemeWhenCharIsColon(i, path)
default:
// we have encountered an invalid character,
// so there is no valid scheme
Expand Down
10 changes: 10 additions & 0 deletions pkg/protocol/uri_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

package protocol

import "github.com/cloudwego/hertz/pkg/common/hlog"

func addLeadingSlash(dst, src []byte) []byte {
// add leading slash for unix paths
if len(src) == 0 || src[0] != '/' {
Expand All @@ -52,3 +54,11 @@ func addLeadingSlash(dst, src []byte) []byte {

return dst
}

func checkSchemeWhenCharIsColon(i int, rawURL []byte) (scheme, path []byte) {
if i == 0 {
hlog.Errorf("error happened when try to parse the rawURL(%s): missing protocol scheme", rawURL)
return
}
return rawURL[:i], rawURL[i+1:]
}
16 changes: 16 additions & 0 deletions pkg/protocol/uri_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

package protocol

import "github.com/cloudwego/hertz/pkg/common/hlog"

func addLeadingSlash(dst, src []byte) []byte {
// zero length and "C:/" case
isDisk := len(src) > 2 && src[1] == ':'
Expand All @@ -53,3 +55,17 @@ func addLeadingSlash(dst, src []byte) []byte {

return dst
}

func checkSchemeWhenCharIsColon(i int, rawURL []byte) (scheme, path []byte) {
if i == 0 {
hlog.Errorf("error happened when trying to parse the rawURL(%s): missing protocol scheme", rawURL)
return
}

if i+2 < len(rawURL) && rawURL[i+1] == '/' {
if rawURL[i+2] == '/' {
return rawURL[:i], rawURL[i+1:]
}
return nil, rawURL
}
}

Check failure on line 71 in pkg/protocol/uri_windows.go

View workflow job for this annotation

GitHub Actions / ut-windows (1.18)

missing return
16 changes: 15 additions & 1 deletion pkg/protocol/uri_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package protocol

import "testing"
import (
"testing"

"github.com/cloudwego/hertz/pkg/common/test/assert"
)

func TestURIPathNormalizeIssue86(t *testing.T) {
t.Parallel()
Expand All @@ -26,3 +30,13 @@ func TestURIPathNormalizeIssue86(t *testing.T) {
testURIPathNormalize(t, &u, "/..\\..\\..\\..\\..\\", "/")
testURIPathNormalize(t, &u, "/..%5c..%5cfoo", "/foo")
}

func TestGetScheme(t *testing.T) {
scheme, path := getScheme([]byte("E:/file.go"))
assert.DeepEqual(t, "", string(scheme))
assert.DeepEqual(t, "E:/file.go", string(path))

scheme, path = getScheme([]byte("https://foo.com"))
assert.DeepEqual(t, "https", string(scheme))
assert.DeepEqual(t, "//foo.com", string(path))
}

0 comments on commit 40712fb

Please sign in to comment.