-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
handler.go
59 lines (52 loc) · 1.57 KB
/
handler.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
package interpreter
import (
"net/http"
"strings"
"github.com/pkg/errors"
"github.com/ysugimoto/falco/interpreter/exception"
"github.com/ysugimoto/falco/interpreter/limitations"
"github.com/ysugimoto/falco/interpreter/variable"
)
// Implements http.Handler
func (i *Interpreter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
i.Debugger.Message("Request Incoming =========>")
defer i.Debugger.Message("<========= Request finished")
// Prevent deadlock if simulator is a backend for itself.
if strings.Contains(r.Header.Get("Fastly-FF"), variable.FALCO_SERVER_HOSTNAME) {
http.Error(w, "loop detected", http.StatusServiceUnavailable)
return
}
i.lock.Lock()
defer i.lock.Unlock()
if err := i.ProcessInit(r); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
handleError := func(err error) {
// If debug is true, print with stacktrace
i.process.Error = err
if re, ok := errors.Cause(err).(*exception.Exception); ok {
i.Debugger.Message(re.Error())
} else {
i.Debugger.Message(err.Error())
}
}
if err := i.ProcessRecv(); err != nil {
handleError(err)
} else if err := limitations.CheckFastlyResponseLimit(i.ctx.Response); err != nil {
handleError(err)
}
i.process.Restarts = i.ctx.Restarts
i.process.Backend = i.ctx.Backend
if i.process.Error != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
out, err := i.process.Finalize(i.ctx.Response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(out) // nolint:errcheck
}