mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-14 15:56:44 +03:00
Added option to disable path normalization (#649)
This commit is contained in:
committed by
Erik Dubbelboer
parent
c6037ab3b4
commit
ee8450036e
@@ -48,6 +48,15 @@ type URI struct {
|
||||
queryArgs Args
|
||||
parsedQueryArgs bool
|
||||
|
||||
// Path values are sent as-is without normalization
|
||||
//
|
||||
// Disabled path normalization may be useful for proxying incoming requests
|
||||
// to servers that are expecting paths to be forwarded as-is.
|
||||
//
|
||||
// By default path values are normalized, i.e.
|
||||
// extra slashes are removed, special characters are encoded.
|
||||
DisablePathNormalizing bool
|
||||
|
||||
fullURI []byte
|
||||
requestURI []byte
|
||||
|
||||
@@ -71,6 +80,7 @@ func (u *URI) CopyTo(dst *URI) {
|
||||
|
||||
u.queryArgs.CopyTo(&dst.queryArgs)
|
||||
dst.parsedQueryArgs = u.parsedQueryArgs
|
||||
dst.DisablePathNormalizing = u.DisablePathNormalizing
|
||||
|
||||
// fullURI and requestURI shouldn't be copied, since they are created
|
||||
// from scratch on each FullURI() and RequestURI() call.
|
||||
@@ -215,6 +225,7 @@ func (u *URI) Reset() {
|
||||
u.host = u.host[:0]
|
||||
u.queryArgs.Reset()
|
||||
u.parsedQueryArgs = false
|
||||
u.DisablePathNormalizing = false
|
||||
|
||||
// There is no need in u.fullURI = u.fullURI[:0], since full uri
|
||||
// is calculated on each call to FullURI().
|
||||
@@ -387,7 +398,12 @@ func normalizePath(dst, src []byte) []byte {
|
||||
|
||||
// RequestURI returns RequestURI - i.e. URI without Scheme and Host.
|
||||
func (u *URI) RequestURI() []byte {
|
||||
dst := appendQuotedPath(u.requestURI[:0], u.Path())
|
||||
var dst []byte
|
||||
if u.DisablePathNormalizing {
|
||||
dst = append(u.requestURI[:0], u.PathOriginal()...)
|
||||
} else {
|
||||
dst = appendQuotedPath(u.requestURI[:0], u.Path())
|
||||
}
|
||||
if u.queryArgs.Len() > 0 {
|
||||
dst = append(dst, '?')
|
||||
dst = u.queryArgs.AppendBytes(dst)
|
||||
|
||||
+10
@@ -184,6 +184,16 @@ func testURIPathNormalize(t *testing.T, u *URI, requestURI, expectedPath string)
|
||||
}
|
||||
}
|
||||
|
||||
func TestURINoNormalization(t *testing.T) {
|
||||
var u URI
|
||||
irregularPath := "/aaa%2Fbbb%2F%2E.%2Fxxx"
|
||||
u.Parse(nil, []byte(irregularPath))
|
||||
u.DisablePathNormalizing = true
|
||||
if string(u.RequestURI()) != irregularPath {
|
||||
t.Fatalf("Unexpected path %q. Expected %q.", u.Path(), irregularPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestURICopyTo(t *testing.T) {
|
||||
var u URI
|
||||
var copyU URI
|
||||
|
||||
Reference in New Issue
Block a user