-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
84 lines (78 loc) · 2.04 KB
/
Copy pathcontext.go
File metadata and controls
84 lines (78 loc) · 2.04 KB
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
package web
import (
"context"
"net/http"
)
// Context is the app-facing HTTP context contract.
type Context interface {
Context() context.Context
Method() string
Path() string
URI() string
Scheme() string
Host() string
Param(name string) string
Query(name string) string
Header(name string) string
Cookie(name string) (*http.Cookie, error)
RealIP() string
Request() *http.Request
SetRequest(request *http.Request)
Response() Response
ResponseWriter() http.ResponseWriter
SetResponseWriter(writer http.ResponseWriter)
Bind(target any) error
Set(key string, value any)
Get(key string) any
AddHeader(name string, value string)
SetHeader(name string, value string)
SetCookie(cookie *http.Cookie)
JSON(code int, payload any) error
Blob(code int, contentType string, body []byte) error
File(path string) error
Text(code int, body string) error
HTML(code int, body string) error
NoContent(code int) error
Redirect(code int, url string) error
StatusCode() int
Native() any
}
type contextSetter interface {
SetContext(context.Context)
}
type rawRequester interface {
RawRequest() *http.Request
}
// BindContext attaches ctx to the request-scoped web.Context without forcing
// callers to replace the underlying *http.Request when the adapter can carry
// an override more cheaply.
func BindContext(target Context, ctx context.Context) {
if target == nil {
return
}
if ctx == nil {
ctx = context.Background()
}
if setter, ok := target.(contextSetter); ok {
setter.SetContext(ctx)
return
}
req := target.Request()
if req == nil {
return
}
target.SetRequest(req.WithContext(ctx))
}
// RawRequest returns the adapter's underlying request when available, without
// forcing a context-rebound clone. Callers should prefer Context() for scoped
// execution state and use RawRequest only when they need direct request body or
// header access.
func RawRequest(target Context) *http.Request {
if target == nil {
return nil
}
if requester, ok := target.(rawRequester); ok {
return requester.RawRequest()
}
return target.Request()
}