-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
56 lines (47 loc) · 1.25 KB
/
utils.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
package mixed
import (
"net/http"
echo "github.com/labstack/echo/v4"
)
// copyContext creates a new echo.Context with the same Request, Path, Params and Handler
// but with a Response that contains an in-memory ResponseWriter
func copyContext(c echo.Context, preserveKeys []string) echo.Context {
cc := c.Echo().AcquireContext()
cc.SetRequest(c.Request())
cc.SetPath(c.Path())
cc.SetParamNames(c.ParamNames()...)
cc.SetParamValues(c.ParamValues()...)
cc.SetHandler(c.Handler())
for _, key := range preserveKeys {
if val := c.Get(key); val != nil {
cc.Set(key, val)
}
}
rw := tempResponseWriter{
header: make(http.Header),
Content: []byte{},
}
resp := echo.NewResponse(&rw, c.Echo())
cc.SetResponse(resp)
return cc
}
// copyResponse copy c2 headers and content into c1
func copyResponse(c1, c2 echo.Context, preserveKeys []string) {
for _, key := range preserveKeys {
if val := c2.Get(key); val != nil {
c1.Set(key, val)
}
}
for k, v := range c2.Response().Header() {
for _, vv := range v {
c1.Response().Header().Set(k, vv)
}
}
if c2.Response().Status > 0 {
c1.Response().WriteHeader(c2.Response().Status)
}
_, err := c1.Response().Write(c2.Response().Writer.(*tempResponseWriter).Content)
if err != nil {
panic(err)
}
}