2017-06-27 23:54:42 +03:00
|
|
|
// Copyright 2013 Julien Schmidt. All rights reserved.
|
|
|
|
// Based on the path package, Copyright 2009 The Go Authors.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found
|
2017-06-27 23:58:49 +03:00
|
|
|
// at https://github.com/julienschmidt/httprouter/blob/master/LICENSE.
|
2015-03-31 22:39:06 +03:00
|
|
|
|
|
|
|
package gin
|
|
|
|
|
2017-07-04 05:08:41 +03:00
|
|
|
// cleanPath is the URL version of path.Clean, it returns a canonical URL path
|
2015-03-31 22:39:06 +03:00
|
|
|
// for p, eliminating . and .. elements.
|
|
|
|
//
|
|
|
|
// The following rules are applied iteratively until no further processing can
|
|
|
|
// be done:
|
|
|
|
// 1. Replace multiple slashes with a single slash.
|
|
|
|
// 2. Eliminate each . path name element (the current directory).
|
|
|
|
// 3. Eliminate each inner .. path name element (the parent directory)
|
|
|
|
// along with the non-.. element that precedes it.
|
|
|
|
// 4. Eliminate .. elements that begin a rooted path:
|
|
|
|
// that is, replace "/.." by "/" at the beginning of a path.
|
|
|
|
//
|
2017-08-16 06:55:50 +03:00
|
|
|
// If the result of this process is an empty string, "/" is returned.
|
2015-05-30 15:45:13 +03:00
|
|
|
func cleanPath(p string) string {
|
2020-01-16 17:40:59 +03:00
|
|
|
const stackBufSize = 128
|
2015-03-31 22:39:06 +03:00
|
|
|
// Turn empty string into "/"
|
|
|
|
if p == "" {
|
|
|
|
return "/"
|
|
|
|
}
|
|
|
|
|
2020-01-07 12:37:18 +03:00
|
|
|
// Reasonably sized buffer on stack to avoid allocations in the common case.
|
|
|
|
// If a larger buffer is required, it gets allocated dynamically.
|
|
|
|
buf := make([]byte, 0, stackBufSize)
|
|
|
|
|
2015-03-31 22:39:06 +03:00
|
|
|
n := len(p)
|
|
|
|
|
|
|
|
// Invariants:
|
|
|
|
// reading from path; r is index of next byte to process.
|
|
|
|
// writing to buf; w is index of next byte to write.
|
|
|
|
|
|
|
|
// path must start with '/'
|
|
|
|
r := 1
|
|
|
|
w := 1
|
|
|
|
|
|
|
|
if p[0] != '/' {
|
|
|
|
r = 0
|
2020-01-07 12:37:18 +03:00
|
|
|
|
|
|
|
if n+1 > stackBufSize {
|
|
|
|
buf = make([]byte, n+1)
|
|
|
|
} else {
|
|
|
|
buf = buf[:n+1]
|
|
|
|
}
|
2015-03-31 22:39:06 +03:00
|
|
|
buf[0] = '/'
|
|
|
|
}
|
|
|
|
|
2018-06-23 06:06:27 +03:00
|
|
|
trailing := n > 1 && p[n-1] == '/'
|
2015-03-31 22:39:06 +03:00
|
|
|
|
|
|
|
// A bit more clunky without a 'lazybuf' like the path package, but the loop
|
|
|
|
// gets completely inlined (bufApp). So in contrast to the path package this
|
|
|
|
// loop has no expensive function calls (except 1x make)
|
|
|
|
|
|
|
|
for r < n {
|
|
|
|
switch {
|
|
|
|
case p[r] == '/':
|
|
|
|
// empty path element, trailing slash is added after the end
|
|
|
|
r++
|
|
|
|
|
|
|
|
case p[r] == '.' && r+1 == n:
|
|
|
|
trailing = true
|
|
|
|
r++
|
|
|
|
|
|
|
|
case p[r] == '.' && p[r+1] == '/':
|
|
|
|
// . element
|
2018-08-10 15:50:23 +03:00
|
|
|
r += 2
|
2015-03-31 22:39:06 +03:00
|
|
|
|
|
|
|
case p[r] == '.' && p[r+1] == '.' && (r+2 == n || p[r+2] == '/'):
|
|
|
|
// .. element: remove to last /
|
2018-08-10 15:50:23 +03:00
|
|
|
r += 3
|
2015-03-31 22:39:06 +03:00
|
|
|
|
|
|
|
if w > 1 {
|
|
|
|
// can backtrack
|
|
|
|
w--
|
|
|
|
|
2020-01-07 12:37:18 +03:00
|
|
|
if len(buf) == 0 {
|
2015-03-31 22:39:06 +03:00
|
|
|
for w > 1 && p[w] != '/' {
|
|
|
|
w--
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for w > 1 && buf[w] != '/' {
|
|
|
|
w--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// real path element.
|
|
|
|
// add slash if needed
|
|
|
|
if w > 1 {
|
|
|
|
bufApp(&buf, p, w, '/')
|
|
|
|
w++
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy element
|
|
|
|
for r < n && p[r] != '/' {
|
|
|
|
bufApp(&buf, p, w, p[r])
|
|
|
|
w++
|
|
|
|
r++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-append trailing slash
|
|
|
|
if trailing && w > 1 {
|
|
|
|
bufApp(&buf, p, w, '/')
|
|
|
|
w++
|
|
|
|
}
|
|
|
|
|
2020-01-07 12:37:18 +03:00
|
|
|
if len(buf) == 0 {
|
2015-03-31 22:39:06 +03:00
|
|
|
return p[:w]
|
|
|
|
}
|
|
|
|
return string(buf[:w])
|
|
|
|
}
|
|
|
|
|
2017-08-16 06:55:50 +03:00
|
|
|
// internal helper to lazily create a buffer if necessary.
|
2015-03-31 22:39:06 +03:00
|
|
|
func bufApp(buf *[]byte, s string, w int, c byte) {
|
2020-01-07 12:37:18 +03:00
|
|
|
b := *buf
|
|
|
|
if len(b) == 0 {
|
2015-03-31 22:39:06 +03:00
|
|
|
if s[w] == c {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-07 12:37:18 +03:00
|
|
|
if l := len(s); l > cap(b) {
|
|
|
|
*buf = make([]byte, len(s))
|
|
|
|
} else {
|
|
|
|
*buf = (*buf)[:l]
|
|
|
|
}
|
|
|
|
b = *buf
|
|
|
|
|
|
|
|
copy(b, s[:w])
|
2015-03-31 22:39:06 +03:00
|
|
|
}
|
2020-01-07 12:37:18 +03:00
|
|
|
b[w] = c
|
2015-03-31 22:39:06 +03:00
|
|
|
}
|