path.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2013 Julien Schmidt. All rights reserved.
  2. // Based on the path package, Copyright 2009 The Go Authors.
  3. // Use of this source code is governed by a BSD-style license that can be found
  4. // at https://github.com/julienschmidt/httprouter/blob/master/LICENSE.
  5. package gin
  6. // cleanPath is the URL version of path.Clean, it returns a canonical URL path
  7. // for p, eliminating . and .. elements.
  8. //
  9. // The following rules are applied iteratively until no further processing can
  10. // be done:
  11. // 1. Replace multiple slashes with a single slash.
  12. // 2. Eliminate each . path name element (the current directory).
  13. // 3. Eliminate each inner .. path name element (the parent directory)
  14. // along with the non-.. element that precedes it.
  15. // 4. Eliminate .. elements that begin a rooted path:
  16. // that is, replace "/.." by "/" at the beginning of a path.
  17. //
  18. // If the result of this process is an empty string, "/" is returned.
  19. func cleanPath(p string) string {
  20. const stackBufSize = 128
  21. // Turn empty string into "/"
  22. if p == "" {
  23. return "/"
  24. }
  25. // Reasonably sized buffer on stack to avoid allocations in the common case.
  26. // If a larger buffer is required, it gets allocated dynamically.
  27. buf := make([]byte, 0, stackBufSize)
  28. n := len(p)
  29. // Invariants:
  30. // reading from path; r is index of next byte to process.
  31. // writing to buf; w is index of next byte to write.
  32. // path must start with '/'
  33. r := 1
  34. w := 1
  35. if p[0] != '/' {
  36. r = 0
  37. if n+1 > stackBufSize {
  38. buf = make([]byte, n+1)
  39. } else {
  40. buf = buf[:n+1]
  41. }
  42. buf[0] = '/'
  43. }
  44. trailing := n > 1 && p[n-1] == '/'
  45. // A bit more clunky without a 'lazybuf' like the path package, but the loop
  46. // gets completely inlined (bufApp calls).
  47. // loop has no expensive function calls (except 1x make) // So in contrast to the path package this loop has no expensive function
  48. // calls (except make, if needed).
  49. for r < n {
  50. switch {
  51. case p[r] == '/':
  52. // empty path element, trailing slash is added after the end
  53. r++
  54. case p[r] == '.' && r+1 == n:
  55. trailing = true
  56. r++
  57. case p[r] == '.' && p[r+1] == '/':
  58. // . element
  59. r += 2
  60. case p[r] == '.' && p[r+1] == '.' && (r+2 == n || p[r+2] == '/'):
  61. // .. element: remove to last /
  62. r += 3
  63. if w > 1 {
  64. // can backtrack
  65. w--
  66. if len(buf) == 0 {
  67. for w > 1 && p[w] != '/' {
  68. w--
  69. }
  70. } else {
  71. for w > 1 && buf[w] != '/' {
  72. w--
  73. }
  74. }
  75. }
  76. default:
  77. // Real path element.
  78. // Add slash if needed
  79. if w > 1 {
  80. bufApp(&buf, p, w, '/')
  81. w++
  82. }
  83. // Copy element
  84. for r < n && p[r] != '/' {
  85. bufApp(&buf, p, w, p[r])
  86. w++
  87. r++
  88. }
  89. }
  90. }
  91. // Re-append trailing slash
  92. if trailing && w > 1 {
  93. bufApp(&buf, p, w, '/')
  94. w++
  95. }
  96. // If the original string was not modified (or only shortened at the end),
  97. // return the respective substring of the original string.
  98. // Otherwise return a new string from the buffer.
  99. if len(buf) == 0 {
  100. return p[:w]
  101. }
  102. return string(buf[:w])
  103. }
  104. // Internal helper to lazily create a buffer if necessary.
  105. // Calls to this function get inlined.
  106. func bufApp(buf *[]byte, s string, w int, c byte) {
  107. b := *buf
  108. if len(b) == 0 {
  109. // No modification of the original string so far.
  110. // If the next character is the same as in the original string, we do
  111. // not yet have to allocate a buffer.
  112. if s[w] == c {
  113. return
  114. }
  115. // Otherwise use either the stack buffer, if it is large enough, or
  116. // allocate a new buffer on the heap, and copy all previous characters.
  117. length := len(s)
  118. if length > cap(b) {
  119. *buf = make([]byte, length)
  120. } else {
  121. *buf = (*buf)[:length]
  122. }
  123. b = *buf
  124. copy(b, s[:w])
  125. }
  126. b[w] = c
  127. }