-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlpath.go
More file actions
31 lines (28 loc) · 942 Bytes
/
Copy pathurlpath.go
File metadata and controls
31 lines (28 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package httpservefile
import (
"net/http"
)
// sanitizeURLPathPrefix make sure result will start and end with slash (`/`) character.
func sanitizeURLPathPrefix(urlPathPrefix string) string {
if len(urlPathPrefix) == 0 {
urlPathPrefix = "/"
}
if urlPathPrefix[0] != '/' {
urlPathPrefix = "/" + urlPathPrefix
}
if urlPathPrefix[len(urlPathPrefix)-1] != '/' {
urlPathPrefix = urlPathPrefix + "/"
}
return urlPathPrefix
}
// extractTargetContentPath return URL path after urlPathPrefixLen as targetContentPath.
// The defaultContentPath will be return if targetContentPath is empty or slash.
func extractTargetContentPath(r *http.Request, urlPathPrefixLen int, defaultContentPath string) (targetContentPath string) {
if len(r.URL.Path) > urlPathPrefixLen {
targetContentPath = r.URL.Path[urlPathPrefixLen:]
}
if (targetContentPath == "/") || (targetContentPath == "") {
targetContentPath = defaultContentPath
}
return
}