From 5e1bdcae2d7909e455675a34a157119a30765672 Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Wed, 4 May 2016 10:07:11 +0300 Subject: [PATCH] Issue #86: Fixed leading slash on Windows --- uri.go | 7 +------ uri_unix.go | 15 +++++++++++++++ uri_windows.go | 12 ++++++++++++ uri_windows_test.go | 12 ++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 uri_unix.go create mode 100644 uri_windows.go create mode 100644 uri_windows_test.go diff --git a/uri.go b/uri.go index 464ccd7..6249f44 100644 --- a/uri.go +++ b/uri.go @@ -268,12 +268,7 @@ func (u *URI) parse(host, uri []byte, h *RequestHeader) { func normalizePath(dst, src []byte) []byte { dst = dst[:0] - - // add leading slash - if len(src) == 0 || src[0] != '/' { - dst = append(dst, '/') - } - + dst = addLeadingSlash(dst, src) dst = decodeArgAppend(dst, src, false) // remove duplicate slashes diff --git a/uri_unix.go b/uri_unix.go new file mode 100644 index 0000000..631712a --- /dev/null +++ b/uri_unix.go @@ -0,0 +1,15 @@ +// +build !windows + +package fasthttp + +import "bytes" + + +func addLeadingSlash(dst, src []byte) []byte { + // add leading slash for unix paths + if len(src) == 0 || src[0] != '/' { + dst = append(dst, '/') + } + + return dst +} diff --git a/uri_windows.go b/uri_windows.go new file mode 100644 index 0000000..c7980c8 --- /dev/null +++ b/uri_windows.go @@ -0,0 +1,12 @@ +// +build windows + +package fasthttp + +func addLeadingSlash(dst, src []byte) []byte { + // zero length and "C:/" case + if len(src) == 0 || (len(src) > 2 && src[1] != ':') { + dst = append(dst, '/') + } + + return dst +} \ No newline at end of file diff --git a/uri_windows_test.go b/uri_windows_test.go new file mode 100644 index 0000000..61d1a2c --- /dev/null +++ b/uri_windows_test.go @@ -0,0 +1,12 @@ +// +build windows + +package fasthttp + +import "testing" + +func TestURIPathNormalizeIssue86(t *testing.T) { + // see https://github.com/valyala/fasthttp/issues/86 + var u URI + + testURIPathNormalize(t, &u, `C:\a\b\c\fs.go`, `C:\a\b\c\fs.go`) +}