-
Notifications
You must be signed in to change notification settings - Fork 35
/
pathinfo.go
146 lines (131 loc) · 3.33 KB
/
pathinfo.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package gold
import (
"errors"
"net"
"net/url"
"os"
_path "path"
"path/filepath"
"strings"
"time"
)
type pathInfo struct {
Obj *url.URL
URI string
Base string
Path string
Root string
File string
FileType string
ParentURI string
AclURI string
AclFile string
MetaURI string
MetaFile string
Extension string
MaybeRDF bool
IsDir bool
Exists bool
ModTime time.Time
Size int64
}
func (req *httpRequest) pathInfo(path string) (*pathInfo, error) {
res := &pathInfo{}
if len(path) == 0 {
return nil, errors.New("missing resource path")
}
// hack - if source URI contains "one%2b+%2btwo" then it is
// normally decoded to "one+ +two", but Go parses it to
// "one+++two", so we replace the plus with a blank space
// strings.Replace(path, "+", "%20", -1)
p, err := url.Parse(path)
if err != nil {
return nil, err
}
res.Base = p.Scheme + "://" + p.Host
res.Root = req.Server.Config.DataRoot
// include host and port if running in vhosts mode
host, port, _ := net.SplitHostPort(p.Host)
if len(host) == 0 {
host = p.Host
}
if len(port) > 0 {
host += ":" + port
}
if req.Server.Config.Vhosts {
res.Root = req.Server.Config.DataRoot + host + "/"
res.Base = p.Scheme + "://" + host
}
// p.Path = p.String()[len(p.Scheme+"://"+p.Host):]
if strings.HasPrefix(p.Path, "/") && len(p.Path) > 0 {
p.Path = strings.TrimLeft(p.Path, "/")
}
if len(p.Path) == 0 {
res.URI = p.String() + "/"
} else {
res.URI = p.String()
}
res.Obj = p
res.File = p.Path
res.Path = p.Path
if req.Server.Config.Vhosts {
res.File = res.Root + p.Path
} else if len(req.Server.Config.DataRoot) > 0 {
res.File = req.Server.Config.DataRoot + p.Path
}
res.Exists = true
res.IsDir = false
// check if file exits first
if stat, err := os.Stat(res.File); os.IsNotExist(err) {
res.Exists = false
} else {
res.ModTime = stat.ModTime()
res.Size = stat.Size()
// Add missing trailing slashes for dirs
if stat.IsDir() {
res.IsDir = true
if !strings.HasSuffix(res.Path, "/") && len(res.Path) > 1 {
res.Path += "/"
res.File += "/"
res.URI += "/"
}
} else {
res.FileType, res.Extension, res.MaybeRDF = MimeLookup(res.File)
if len(res.FileType) == 0 {
res.FileType, err = GuessMimeType(res.File)
if err != nil {
req.Server.debug.Println(err)
}
}
}
}
if len(res.Extension) == 0 {
res.Extension = _path.Ext(res.File)
}
if strings.HasSuffix(res.Path, "/") {
if filepath.Dir(filepath.Dir(res.Path)) == "." {
res.ParentURI = res.Base + "/"
} else {
res.ParentURI = res.Base + "/" + filepath.Dir(filepath.Dir(res.Path)) + "/"
}
} else {
res.ParentURI = res.Base + "/" + filepath.Dir(res.Path) + "/"
}
if strings.HasSuffix(res.Path, req.Server.Config.ACLSuffix) {
res.AclURI = res.URI
res.AclFile = res.File
res.MetaURI = res.URI
res.MetaFile = res.File
} else if strings.HasSuffix(res.Path, req.Server.Config.MetaSuffix) {
res.AclURI = res.URI + req.Server.Config.ACLSuffix
res.AclFile = res.File + req.Server.Config.ACLSuffix
res.MetaURI = res.URI
res.MetaFile = res.File
} else {
res.AclURI = res.URI + req.Server.Config.ACLSuffix
res.AclFile = res.File + req.Server.Config.ACLSuffix
res.MetaURI = res.URI + req.Server.Config.MetaSuffix
res.MetaFile = res.File + req.Server.Config.MetaSuffix
}
return res, nil
}