|
| 1 | +// Package core provides utility methods that help convert proxy events |
| 2 | +// into an http.Request and http.ResponseWriter |
| 3 | +package core |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "context" |
| 8 | + "encoding/base64" |
| 9 | + "encoding/json" |
| 10 | + "errors" |
| 11 | + "fmt" |
| 12 | + "log" |
| 13 | + "net/http" |
| 14 | + "net/url" |
| 15 | + "os" |
| 16 | + "strings" |
| 17 | + |
| 18 | + "github.com/aws/aws-lambda-go/events" |
| 19 | + "github.com/aws/aws-lambda-go/lambdacontext" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + // FuContextHeader is the custom header key used to store the |
| 24 | + // Function Url context. To access the Context properties use the |
| 25 | + // GetFunctionUrlContext method of the RequestAccessorFu object. |
| 26 | + FuContextHeader = "X-GoLambdaProxy-Fu-Context" |
| 27 | +) |
| 28 | + |
| 29 | +// RequestAccessorV2 objects give access to custom API Gateway properties |
| 30 | +// in the request. |
| 31 | +type RequestAccessorFu struct { |
| 32 | + stripBasePath string |
| 33 | +} |
| 34 | + |
| 35 | +// GetAPIGatewayContextV2 extracts the API Gateway context object from a |
| 36 | +// request's custom header. |
| 37 | +// Returns a populated events.APIGatewayProxyRequestContext object from |
| 38 | +// the request. |
| 39 | +func (r *RequestAccessorFu) GetFunctionUrlContext(req *http.Request) (events.LambdaFunctionURLRequestContext, error) { |
| 40 | + if req.Header.Get(APIGwContextHeader) == "" { |
| 41 | + return events.LambdaFunctionURLRequestContext{}, errors.New("No context header in request") |
| 42 | + } |
| 43 | + context := events.LambdaFunctionURLRequestContext{} |
| 44 | + err := json.Unmarshal([]byte(req.Header.Get(FuContextHeader)), &context) |
| 45 | + if err != nil { |
| 46 | + log.Println("Erorr while unmarshalling context") |
| 47 | + log.Println(err) |
| 48 | + return events.LambdaFunctionURLRequestContext{}, err |
| 49 | + } |
| 50 | + return context, nil |
| 51 | +} |
| 52 | + |
| 53 | +// StripBasePath instructs the RequestAccessor object that the given base |
| 54 | +// path should be removed from the request path before sending it to the |
| 55 | +// framework for routing. This is used when the Lambda is configured with |
| 56 | +// base path mappings in custom domain names. |
| 57 | +func (r *RequestAccessorFu) StripBasePath(basePath string) string { |
| 58 | + if strings.Trim(basePath, " ") == "" { |
| 59 | + r.stripBasePath = "" |
| 60 | + return "" |
| 61 | + } |
| 62 | + |
| 63 | + newBasePath := basePath |
| 64 | + if !strings.HasPrefix(newBasePath, "/") { |
| 65 | + newBasePath = "/" + newBasePath |
| 66 | + } |
| 67 | + |
| 68 | + if strings.HasSuffix(newBasePath, "/") { |
| 69 | + newBasePath = newBasePath[:len(newBasePath)-1] |
| 70 | + } |
| 71 | + |
| 72 | + r.stripBasePath = newBasePath |
| 73 | + |
| 74 | + return newBasePath |
| 75 | +} |
| 76 | + |
| 77 | +// ProxyEventToHTTPRequest converts an API Gateway proxy event into a http.Request object. |
| 78 | +// Returns the populated http request with additional two custom headers for the stage variables and API Gateway context. |
| 79 | +// To access these properties use the GetAPIGatewayStageVars and GetAPIGatewayContext method of the RequestAccessor object. |
| 80 | +func (r *RequestAccessorFu) ProxyEventToHTTPRequest(req events.LambdaFunctionURLRequest) (*http.Request, error) { |
| 81 | + httpRequest, err := r.EventToRequest(req) |
| 82 | + if err != nil { |
| 83 | + log.Println(err) |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + return addToHeaderFu(httpRequest, req) |
| 87 | +} |
| 88 | + |
| 89 | +// EventToRequestWithContext converts an API Gateway proxy event and context into an http.Request object. |
| 90 | +// Returns the populated http request with lambda context, stage variables and APIGatewayProxyRequestContext as part of its context. |
| 91 | +// Access those using GetAPIGatewayContextFromContext, GetStageVarsFromContext and GetRuntimeContextFromContext functions in this package. |
| 92 | +func (r *RequestAccessorFu) EventToRequestWithContext(ctx context.Context, req events.LambdaFunctionURLRequest) (*http.Request, error) { |
| 93 | + httpRequest, err := r.EventToRequest(req) |
| 94 | + if err != nil { |
| 95 | + log.Println(err) |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + return addToContextFu(ctx, httpRequest, req), nil |
| 99 | +} |
| 100 | + |
| 101 | +// EventToRequest converts an API Gateway proxy event into an http.Request object. |
| 102 | +// Returns the populated request maintaining headers |
| 103 | +func (r *RequestAccessorFu) EventToRequest(req events.LambdaFunctionURLRequest) (*http.Request, error) { |
| 104 | + decodedBody := []byte(req.Body) |
| 105 | + if req.IsBase64Encoded { |
| 106 | + base64Body, err := base64.StdEncoding.DecodeString(req.Body) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + decodedBody = base64Body |
| 111 | + } |
| 112 | + |
| 113 | + path := req.RawPath |
| 114 | + |
| 115 | + // if RawPath empty is, populate from request context |
| 116 | + if len(path) == 0 { |
| 117 | + path = req.RequestContext.HTTP.Path |
| 118 | + } |
| 119 | + |
| 120 | + if r.stripBasePath != "" && len(r.stripBasePath) > 1 { |
| 121 | + if strings.HasPrefix(path, r.stripBasePath) { |
| 122 | + path = strings.Replace(path, r.stripBasePath, "", 1) |
| 123 | + } |
| 124 | + } |
| 125 | + if !strings.HasPrefix(path, "/") { |
| 126 | + path = "/" + path |
| 127 | + } |
| 128 | + serverAddress := "https://" + req.RequestContext.DomainName |
| 129 | + if customAddress, ok := os.LookupEnv(CustomHostVariable); ok { |
| 130 | + serverAddress = customAddress |
| 131 | + } |
| 132 | + path = serverAddress + path |
| 133 | + |
| 134 | + if len(req.RawQueryString) > 0 { |
| 135 | + path += "?" + req.RawQueryString |
| 136 | + } else if len(req.QueryStringParameters) > 0 { |
| 137 | + values := url.Values{} |
| 138 | + for key, value := range req.QueryStringParameters { |
| 139 | + values.Add(key, value) |
| 140 | + } |
| 141 | + path += "?" + values.Encode() |
| 142 | + } |
| 143 | + |
| 144 | + httpRequest, err := http.NewRequest( |
| 145 | + strings.ToUpper(req.RequestContext.HTTP.Method), |
| 146 | + path, |
| 147 | + bytes.NewReader(decodedBody), |
| 148 | + ) |
| 149 | + |
| 150 | + if err != nil { |
| 151 | + fmt.Printf("Could not convert request %s:%s to http.Request\n", req.RequestContext.HTTP.Method, req.RequestContext.HTTP.Path) |
| 152 | + log.Println(err) |
| 153 | + return nil, err |
| 154 | + } |
| 155 | + |
| 156 | + httpRequest.RemoteAddr = req.RequestContext.HTTP.SourceIP |
| 157 | + |
| 158 | + for _, cookie := range req.Cookies { |
| 159 | + httpRequest.Header.Add("Cookie", cookie) |
| 160 | + } |
| 161 | + |
| 162 | + for headerKey, headerValue := range req.Headers { |
| 163 | + for _, val := range strings.Split(headerValue, ",") { |
| 164 | + httpRequest.Header.Add(headerKey, strings.Trim(val, " ")) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + httpRequest.RequestURI = httpRequest.URL.RequestURI() |
| 169 | + |
| 170 | + return httpRequest, nil |
| 171 | +} |
| 172 | + |
| 173 | +func addToHeaderFu(req *http.Request, functionUrlRequest events.LambdaFunctionURLRequest) (*http.Request, error) { |
| 174 | + apiGwContext, err := json.Marshal(functionUrlRequest.RequestContext) |
| 175 | + if err != nil { |
| 176 | + log.Println("Could not Marshal API GW context for custom header") |
| 177 | + return req, err |
| 178 | + } |
| 179 | + req.Header.Add(APIGwContextHeader, string(apiGwContext)) |
| 180 | + return req, nil |
| 181 | +} |
| 182 | + |
| 183 | +func addToContextFu(ctx context.Context, req *http.Request, functionUrlRequest events.LambdaFunctionURLRequest) *http.Request { |
| 184 | + lc, _ := lambdacontext.FromContext(ctx) |
| 185 | + rc := requestContextFu{lambdaContext: lc, functionUrlProxyContext: functionUrlRequest.RequestContext} |
| 186 | + ctx = context.WithValue(ctx, ctxKey{}, rc) |
| 187 | + return req.WithContext(ctx) |
| 188 | +} |
| 189 | + |
| 190 | +// GetAPIGatewayV2ContextFromContext retrieve APIGatewayProxyRequestContext from context.Context |
| 191 | +func GetFunctionUrlContextFromContext(ctx context.Context) (events.LambdaFunctionURLRequestContext, bool) { |
| 192 | + v, ok := ctx.Value(ctxKey{}).(requestContextFu) |
| 193 | + return v.functionUrlProxyContext, ok |
| 194 | +} |
| 195 | + |
| 196 | +// GetRuntimeContextFromContextV2 retrieve Lambda Runtime Context from context.Context |
| 197 | +func GetRuntimeContextFromContextFu(ctx context.Context) (*lambdacontext.LambdaContext, bool) { |
| 198 | + v, ok := ctx.Value(ctxKey{}).(requestContextFu) |
| 199 | + return v.lambdaContext, ok |
| 200 | +} |
| 201 | + |
| 202 | +type requestContextFu struct { |
| 203 | + lambdaContext *lambdacontext.LambdaContext |
| 204 | + functionUrlProxyContext events.LambdaFunctionURLRequestContext |
| 205 | +} |
0 commit comments