Skip to content

Commit

Permalink
Add windows support to normalizePath
Browse files Browse the repository at this point in the history
This is probably still not 100% sure and there are still many bugs with
FS on windows. But it's a slight improvement.

Fixes #1226
  • Loading branch information
erikdubbelboer committed Feb 28, 2022
1 parent f0b0cfe commit 6b5bc7b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
34 changes: 19 additions & 15 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ var (
)

var (
strSlash = []byte("/")
strSlashSlash = []byte("//")
strSlashDotDot = []byte("/..")
strSlashDotSlash = []byte("/./")
strSlashDotDotSlash = []byte("/../")
strCRLF = []byte("\r\n")
strHTTP = []byte("http")
strHTTPS = []byte("https")
strHTTP10 = []byte("HTTP/1.0")
strHTTP11 = []byte("HTTP/1.1")
strColon = []byte(":")
strColonSlashSlash = []byte("://")
strColonSpace = []byte(": ")
strCommaSpace = []byte(", ")
strGMT = []byte("GMT")
strSlash = []byte("/")
strSlashSlash = []byte("//")
strSlashDotDot = []byte("/..")
strSlashDotSlash = []byte("/./")
strSlashDotDotSlash = []byte("/../")
strBackSlashDotDot = []byte(`\..`)
strBackSlashDotBackSlash = []byte(`\.\`)
strSlashDotDotBackSlash = []byte(`/..\`)
strBackSlashDotDotBackSlash = []byte(`\..\`)
strCRLF = []byte("\r\n")
strHTTP = []byte("http")
strHTTPS = []byte("https")
strHTTP10 = []byte("HTTP/1.0")
strHTTP11 = []byte("HTTP/1.1")
strColon = []byte(":")
strColonSlashSlash = []byte("://")
strColonSpace = []byte(": ")
strCommaSpace = []byte(", ")
strGMT = []byte("GMT")

strResponseContinue = []byte("HTTP/1.1 100 Continue\r\n\r\n")

Expand Down
55 changes: 55 additions & 0 deletions uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"path/filepath"
"strconv"
"sync"
)
Expand Down Expand Up @@ -634,6 +635,60 @@ func normalizePath(dst, src []byte) []byte {
b = b[:nn+1]
}

if filepath.Separator == '\\' {
// remove \.\ parts
b = dst
for {
n := bytes.Index(b, strBackSlashDotBackSlash)
if n < 0 {
break
}
nn := n + len(strSlashDotSlash) - 1
copy(b[n:], b[nn:])
b = b[:len(b)-nn+n]
}

// remove /foo/..\ parts
for {
n := bytes.Index(b, strSlashDotDotBackSlash)
if n < 0 {
break
}
nn := bytes.LastIndexByte(b[:n], '/')
if nn < 0 {
nn = 0
}
n += len(strSlashDotDotBackSlash) - 1
copy(b[nn:], b[n:])
b = b[:len(b)-n+nn]
}

// remove /foo\..\ parts
for {
n := bytes.Index(b, strBackSlashDotDotBackSlash)
if n < 0 {
break
}
nn := bytes.LastIndexByte(b[:n], '/')
if nn < 0 {
nn = 0
}
n += len(strBackSlashDotDotBackSlash) - 1
copy(b[nn:], b[n:])
b = b[:len(b)-n+nn]
}

// remove trailing \foo\..
n := bytes.LastIndex(b, strBackSlashDotDot)
if n >= 0 && n+len(strSlashDotDot) == len(b) {
nn := bytes.LastIndexByte(b[:n], '/')
if nn < 0 {
return append(dst[:0], strSlash...)
}
b = b[:nn+1]
}
}

return b
}

Expand Down

0 comments on commit 6b5bc7b

Please sign in to comment.