-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileServerHandlerT.ServeHTTP.go
140 lines (117 loc) · 4.33 KB
/
FileServerHandlerT.ServeHTTP.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
package stonelizard
import (
"os"
"fmt"
"strings"
"runtime"
"net/http"
"path/filepath"
)
func (fs FileServerHandlerT) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var err error
var httpstat int
var extAuth ExtAuthT
var authparms map[string]interface{}
var qry, header string
var ok bool
var etag string
var fi os.FileInfo
var exported string
defer func() {
// Tries to survive to any panic from the application.
// Additionally, tries to extract useful data from the panic message and log it.
// This is needed because the panic message is too long and have newlines in it,
// when such messages reach the system log, only the first line goes into the log.
// In my experience, the most needed data is where in my code the panic ocurred,
// so here we search for the first line referencing a GOLANG sourcecode which is not
// a system package and select it to put in the log
// TODO: add the error message to the log too
if panicerr := recover(); panicerr != nil {
const size = 64 << 10
var buf []byte
var srcs, srcs2 []string
var src string
var i int
buf = make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
srcs = strings.Split(string(buf),"\n")
for _, src = range srcs {
if gosrcRE.MatchString(src) && (!gorootRE.MatchString(src)) {
srcs2 = append(srcs2,gosrcFNameRE.FindStringSubmatch(src)[1])
}
}
src = strings.Join(srcs2,", ")
if len(src) == 0 {
for i=len(srcs)-1; i>0; i-- {
Goose.Serve.Logf(1,"panic loop %d/%d", i, len(srcs))
src = srcs[i]
if len(src) > 0 {
break
}
}
}
Goose.Serve.Logf(1,"panic (%s): calling %s -> %s @ %s", panicerr, r.URL.Path, src)
}
}()
Goose.Serve.Logf(2,"svc.Access: %s from %s with level %d", r.RequestURI, r.RemoteAddr, fs.svc.Access)
if fs.svc.Access != AccessNone && fs.svc.Access != AccessInfo {
r.ParseForm()
authparms = map[string]interface{}{}
for qry, _ = range r.Form {
authparms[qry] = r.Form[qry][0]
}
for header, _ = range r.Header {
authparms[header] = r.Header[header][0]
}
// Goose.Serve.Fatalf(0,"authparms: %#v", authparms)
if extAuth, ok = fs.svc.Authorizer.(ExtAuthT); ok {
httpstat, _, err = extAuth.ExtAuthorize(fs.svc.ch, fs.path, authparms, w, r, fs.svc.SavePending)
} else {
httpstat, _, err = fs.svc.Authorizer.Authorize(fs.path, nil, r.RemoteAddr, r.TLS, fs.svc.SavePending)
}
if err != nil {
Goose.Serve.Logf(1,"svc.Access: %s from %s with level %d error: %s", r.RequestURI, r.RemoteAddr, fs.svc.Access, err)
w.WriteHeader(httpstat)
w.Write([]byte(fmt.Sprintf("%s",err)))
return
}
}
if fs.exported == "" {
fs.exported = "."
}
if (len(fs.path) > len(r.URL.Path)) || (fs.path != r.URL.Path[:len(fs.path)]) {
Goose.Serve.Logf(1,"Invalid path %s", r.URL.Path)
w.WriteHeader(http.StatusBadRequest)
// w.Write([]byte(fmt.Sprintf("%s",err)))
// w.Write([]byte(fs.exported + "/" + r.URL.Path))
return
}
exported, err = filepath.Abs(fs.exported)
Goose.Serve.Logf(4,"exported: [%s], path: [%s], err:[%s]", exported, r.URL.Path[len(fs.path):], err)
if err != nil {
Goose.Serve.Logf(1,"Error normalizing exported path %s: %s", exported, err)
w.WriteHeader(http.StatusInternalServerError)
// w.Write([]byte(fmt.Sprintf("%s",err)))
// w.Write([]byte(fs.exported + "/" + r.URL.Path))
return
}
fi, err = os.Stat(exported + "/" + r.URL.Path[len(fs.path):])
if err != nil {
Goose.Serve.Logf(1,"Error stating path %s: %s", exported + "/" + r.URL.Path[len(fs.path):], err)
w.WriteHeader(http.StatusInternalServerError)
// w.Write([]byte(fmt.Sprintf("%s",err)))
// w.Write([]byte(exported + "/" + r.URL.Path))
return
}
etag = `"` + fi.ModTime().Format("20060102150405") + `"`
w.Header().Set("Etag", etag)
if match := r.Header.Get("If-None-Match"); match != "" {
if strings.Contains(match, etag) {
w.WriteHeader(http.StatusNotModified)
return
}
}
Goose.Serve.Logf(4,"Calling handler for %#v", r.URL)
r.URL.Path = r.URL.Path[len(fs.path):]
fs.hnd.ServeHTTP(w,r)
}