Package list golang-github-go-kit-kit / 8599541
transport/http: PopulateRequestContext This RequestFunc moves many HTTP request parameters to the context under specific keys. If wired in to the transport, those values are available to subsequent endpoints or service methods that receive the context. This can be used to e.g. log transport (HTTP) details in a service logging middleware. Peter Bourgon 4 years ago
2 changed file(s) with 117 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
0 package http
1
2 import (
3 "fmt"
4 "net/http"
5 "net/http/httptest"
6
7 "golang.org/x/net/context"
8 )
9
10 func ExamplePopulateRequestContext() {
11 handler := NewServer(
12 context.Background(),
13 func(ctx context.Context, request interface{}) (response interface{}, err error) {
14 fmt.Println("Method", ctx.Value(ContextKeyRequestMethod).(string))
15 fmt.Println("RequestPath", ctx.Value(ContextKeyRequestPath).(string))
16 fmt.Println("RequestURI", ctx.Value(ContextKeyRequestURI).(string))
17 fmt.Println("X-Request-ID", ctx.Value(ContextKeyRequestXRequestID).(string))
18 return struct{}{}, nil
19 },
20 func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil },
21 func(context.Context, http.ResponseWriter, interface{}) error { return nil },
22 ServerBefore(PopulateRequestContext),
23 )
24
25 server := httptest.NewServer(handler)
26 defer server.Close()
27
28 req, _ := http.NewRequest("PATCH", fmt.Sprintf("%s/search?q=sympatico", server.URL), nil)
29 req.Header.Set("X-Request-Id", "a1b2c3d4e5")
30 http.DefaultClient.Do(req)
31
32 // Output:
33 // Method PATCH
34 // RequestPath /search
35 // RequestURI /search?q=sympatico
36 // X-Request-ID a1b2c3d4e5
37 }
2121 // clients, after a request has been made, but prior to it being decoded.
2222 type ClientResponseFunc func(context.Context, *http.Response) context.Context
2323
24 // SetContentType returns a ResponseFunc that sets the Content-Type header to
25 // the provided value.
24 // SetContentType returns a ServerResponseFunc that sets the Content-Type header
25 // to the provided value.
2626 func SetContentType(contentType string) ServerResponseFunc {
2727 return SetResponseHeader("Content-Type", contentType)
2828 }
2929
30 // SetResponseHeader returns a ResponseFunc that sets the specified header.
30 // SetResponseHeader returns a ServerResponseFunc that sets the given header.
3131 func SetResponseHeader(key, val string) ServerResponseFunc {
3232 return func(ctx context.Context, w http.ResponseWriter) context.Context {
3333 w.Header().Set(key, val)
3535 }
3636 }
3737
38 // SetRequestHeader returns a RequestFunc that sets the specified header.
38 // SetRequestHeader returns a RequestFunc that sets the given header.
3939 func SetRequestHeader(key, val string) RequestFunc {
4040 return func(ctx context.Context, r *http.Request) context.Context {
4141 r.Header.Set(key, val)
4242 return ctx
4343 }
4444 }
45
46 // PopulateRequestContext is a RequestFunc that populates several values into
47 // the context from the HTTP request. Those values may be extracted using the
48 // corresponding ContextKey type in this package.
49 func PopulateRequestContext(ctx context.Context, r *http.Request) context.Context {
50 for k, v := range map[contextKey]string{
51 ContextKeyRequestMethod: r.Method,
52 ContextKeyRequestURI: r.RequestURI,
53 ContextKeyRequestPath: r.URL.Path,
54 ContextKeyRequestProto: r.Proto,
55 ContextKeyRequestHost: r.Host,
56 ContextKeyRequestRemoteAddr: r.RemoteAddr,
57 ContextKeyRequestXForwardedFor: r.Header.Get("X-Forwarded-For"),
58 ContextKeyRequestXForwardedProto: r.Header.Get("X-Forwarded-Proto"),
59 ContextKeyRequestAuthorization: r.Header.Get("Authorization"),
60 ContextKeyRequestReferer: r.Header.Get("Referer"),
61 ContextKeyRequestUserAgent: r.Header.Get("User-Agent"),
62 ContextKeyRequestXRequestID: r.Header.Get("X-Request-Id"),
63 } {
64 ctx = context.WithValue(ctx, k, v)
65 }
66 return ctx
67 }
68
69 type contextKey int
70
71 const (
72 // ContextKeyRequestMethod is populated in the context by
73 // PopulateRequestContext. Its value is r.Method.
74 ContextKeyRequestMethod contextKey = iota
75
76 // ContextKeyRequestURI is populated in the context by
77 // PopulateRequestContext. Its value is r.RequestURI.
78 ContextKeyRequestURI
79
80 // ContextKeyRequestPath is populated in the context by
81 // PopulateRequestContext. Its value is r.URL.Path.
82 ContextKeyRequestPath
83
84 // ContextKeyRequestProto is populated in the context by
85 // PopulateRequestContext. Its value is r.Proto.
86 ContextKeyRequestProto
87
88 // ContextKeyRequestHost is populated in the context by
89 // PopulateRequestContext. Its value is r.Host.
90 ContextKeyRequestHost
91
92 // ContextKeyRequestRemoteAddr is populated in the context by
93 // PopulateRequestContext. Its value is r.RemoteAddr.
94 ContextKeyRequestRemoteAddr
95
96 // ContextKeyRequestXForwardedFor is populated in the context by
97 // PopulateRequestContext. Its value is r.Header.Get("X-Forwarded-For").
98 ContextKeyRequestXForwardedFor
99
100 // ContextKeyRequestXForwardedProto is populated in the context by
101 // PopulateRequestContext. Its value is r.Header.Get("X-Forwarded-Proto").
102 ContextKeyRequestXForwardedProto
103
104 // ContextKeyRequestAuthorization is populated in the context by
105 // PopulateRequestContext. Its value is r.Header.Get("Authorization").
106 ContextKeyRequestAuthorization
107
108 // ContextKeyRequestReferer is populated in the context by
109 // PopulateRequestContext. Its value is r.Header.Get("Referer").
110 ContextKeyRequestReferer
111
112 // ContextKeyRequestUserAgent is populated in the context by
113 // PopulateRequestContext. Its value is r.Header.Get("User-Agent").
114 ContextKeyRequestUserAgent
115
116 // ContextKeyRequestXRequestID is populated in the context by
117 // PopulateRequestContext. Its value is r.Header.Get("X-Request-Id").
118 ContextKeyRequestXRequestID
119 )