CreateRequestor extracts the lifecycle event requestor data from an http.Request context.
(r *http.Request)
| 10 | |
| 11 | // CreateRequestor extracts the lifecycle event requestor data from an http.Request context. |
| 12 | func CreateRequestor(r *http.Request) *api.EventLifecycleRequestor { |
| 13 | ctx := r.Context() |
| 14 | requestor := &api.EventLifecycleRequestor{} |
| 15 | |
| 16 | // Normal requestor. |
| 17 | val, ok := ctx.Value(CtxUsername).(string) |
| 18 | if ok { |
| 19 | requestor.Username = val |
| 20 | } |
| 21 | |
| 22 | val, ok = ctx.Value(CtxProtocol).(string) |
| 23 | if ok { |
| 24 | requestor.Protocol = val |
| 25 | } |
| 26 | |
| 27 | requestor.Address = r.RemoteAddr |
| 28 | |
| 29 | // Forwarded requestor override. |
| 30 | val, ok = ctx.Value(CtxForwardedUsername).(string) |
| 31 | if ok { |
| 32 | requestor.Username = val |
| 33 | } |
| 34 | |
| 35 | val, ok = ctx.Value(CtxForwardedProtocol).(string) |
| 36 | if ok { |
| 37 | requestor.Protocol = val |
| 38 | } |
| 39 | |
| 40 | val, ok = ctx.Value(CtxForwardedAddress).(string) |
| 41 | if ok { |
| 42 | requestor.Address = val |
| 43 | } |
| 44 | |
| 45 | // Strip port from address. |
| 46 | host, _, err := net.SplitHostPort(requestor.Address) |
| 47 | if err == nil { |
| 48 | requestor.Address = host |
| 49 | } |
| 50 | |
| 51 | return requestor |
| 52 | } |
| 53 | |
| 54 | // SaveConnectionInContext can be set as the ConnContext field of a http.Server to set the connection |
| 55 | // in the request context for later use. |
no test coverage detected
searching dependent graphs…