|
| 1 | +// Package core provides utility methods that help convert ALB 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 | + "os" |
| 15 | + "strings" |
| 16 | + |
| 17 | + "github.com/aws/aws-lambda-go/events" |
| 18 | + "github.com/aws/aws-lambda-go/lambdacontext" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + // FnURLContextHeader is the custom header key used to store the |
| 23 | + // Function URL context. To access the Context properties use the |
| 24 | + // GetContext method of the RequestAccessorFnURL object. |
| 25 | + FnURLContextHeader = "X-GoLambdaProxy-FnURL-Context" |
| 26 | +) |
| 27 | + |
| 28 | +// RequestAccessorFnURL objects give access to custom Function URL properties |
| 29 | +// in the request. |
| 30 | +type RequestAccessorFnURL struct { |
| 31 | + stripBasePath string |
| 32 | +} |
| 33 | + |
| 34 | +// GetALBContext extracts the ALB context object from a request's custom header. |
| 35 | +// Returns a populated events.ALBTargetGroupRequestContext object from the request. |
| 36 | +func (r *RequestAccessorFnURL) GetContext(req *http.Request) (events.LambdaFunctionURLRequestContext, error) { |
| 37 | + if req.Header.Get(FnURLContextHeader) == "" { |
| 38 | + return events.LambdaFunctionURLRequestContext{}, errors.New("no context header in request") |
| 39 | + } |
| 40 | + context := events.LambdaFunctionURLRequestContext{} |
| 41 | + err := json.Unmarshal([]byte(req.Header.Get(FnURLContextHeader)), &context) |
| 42 | + if err != nil { |
| 43 | + log.Println("Error while unmarshalling context") |
| 44 | + log.Println(err) |
| 45 | + return events.LambdaFunctionURLRequestContext{}, err |
| 46 | + } |
| 47 | + return context, nil |
| 48 | +} |
| 49 | + |
| 50 | +// StripBasePath instructs the RequestAccessor object that the given base |
| 51 | +// path should be removed from the request path before sending it to the |
| 52 | +// framework for routing. This is used when API Gateway is configured with |
| 53 | +// base path mappings in custom domain names. |
| 54 | +func (r *RequestAccessorFnURL) StripBasePath(basePath string) string { |
| 55 | + if strings.Trim(basePath, " ") == "" { |
| 56 | + r.stripBasePath = "" |
| 57 | + return "" |
| 58 | + } |
| 59 | + |
| 60 | + newBasePath := basePath |
| 61 | + if !strings.HasPrefix(newBasePath, "/") { |
| 62 | + newBasePath = "/" + newBasePath |
| 63 | + } |
| 64 | + |
| 65 | + if strings.HasSuffix(newBasePath, "/") { |
| 66 | + newBasePath = newBasePath[:len(newBasePath)-1] |
| 67 | + } |
| 68 | + |
| 69 | + r.stripBasePath = newBasePath |
| 70 | + |
| 71 | + return newBasePath |
| 72 | +} |
| 73 | + |
| 74 | +// FunctionURLEventToHTTPRequest converts an a Function URL event into a http.Request object. |
| 75 | +// Returns the populated http request with additional custom header for the Function URL context. |
| 76 | +// To access these properties use the GetContext method of the RequestAccessorFnURL object. |
| 77 | +func (r *RequestAccessorFnURL) FunctionURLEventToHTTPRequest(req events.LambdaFunctionURLRequest) (*http.Request, error) { |
| 78 | + httpRequest, err := r.EventToRequest(req) |
| 79 | + if err != nil { |
| 80 | + log.Println(err) |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + return addToHeaderFnURL(httpRequest, req) |
| 84 | +} |
| 85 | + |
| 86 | +// FunctionURLEventToHTTPRequestWithContext converts a Function URL event and context into an http.Request object. |
| 87 | +// Returns the populated http request with lambda context, Function URL RequestContext as part of its context. |
| 88 | +func (r *RequestAccessorFnURL) FunctionURLEventToHTTPRequestWithContext(ctx context.Context, req events.LambdaFunctionURLRequest) (*http.Request, error) { |
| 89 | + httpRequest, err := r.EventToRequest(req) |
| 90 | + if err != nil { |
| 91 | + log.Println(err) |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + return addToContextFnURL(ctx, httpRequest, req), nil |
| 95 | +} |
| 96 | + |
| 97 | +// EventToRequest converts a Function URL event into an http.Request object. |
| 98 | +// Returns the populated request maintaining headers |
| 99 | +func (r *RequestAccessorFnURL) EventToRequest(req events.LambdaFunctionURLRequest) (*http.Request, error) { |
| 100 | + decodedBody := []byte(req.Body) |
| 101 | + if req.IsBase64Encoded { |
| 102 | + base64Body, err := base64.StdEncoding.DecodeString(req.Body) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + decodedBody = base64Body |
| 107 | + } |
| 108 | + |
| 109 | + path := req.RawPath |
| 110 | + if r.stripBasePath != "" && len(r.stripBasePath) > 1 { |
| 111 | + if strings.HasPrefix(path, r.stripBasePath) { |
| 112 | + path = strings.Replace(path, r.stripBasePath, "", 1) |
| 113 | + } |
| 114 | + } |
| 115 | + if !strings.HasPrefix(path, "/") { |
| 116 | + path = "/" + path |
| 117 | + } |
| 118 | + |
| 119 | + serverAddress := "https://" + req.RequestContext.DomainName |
| 120 | + if customAddress, ok := os.LookupEnv(CustomHostVariable); ok { |
| 121 | + serverAddress = customAddress |
| 122 | + } |
| 123 | + |
| 124 | + path = serverAddress + path + "?" + req.RawQueryString |
| 125 | + |
| 126 | + httpRequest, err := http.NewRequest( |
| 127 | + strings.ToUpper(req.RequestContext.HTTP.Method), |
| 128 | + path, |
| 129 | + bytes.NewReader(decodedBody), |
| 130 | + ) |
| 131 | + |
| 132 | + if err != nil { |
| 133 | + fmt.Printf("Could not convert request %s:%s to http.Request\n", req.RequestContext.HTTP.Method, req.RawPath) |
| 134 | + log.Println(err) |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + |
| 138 | + for header, val := range req.Headers { |
| 139 | + httpRequest.Header.Add(header, val) |
| 140 | + } |
| 141 | + |
| 142 | + httpRequest.RemoteAddr = req.RequestContext.HTTP.SourceIP |
| 143 | + httpRequest.RequestURI = httpRequest.URL.RequestURI() |
| 144 | + |
| 145 | + return httpRequest, nil |
| 146 | +} |
| 147 | + |
| 148 | +func addToHeaderFnURL(req *http.Request, fnUrlRequest events.LambdaFunctionURLRequest) (*http.Request, error) { |
| 149 | + ctx, err := json.Marshal(fnUrlRequest.RequestContext) |
| 150 | + if err != nil { |
| 151 | + log.Println("Could not Marshal Function URL context for custom header") |
| 152 | + return req, err |
| 153 | + } |
| 154 | + req.Header.Set(FnURLContextHeader, string(ctx)) |
| 155 | + return req, nil |
| 156 | +} |
| 157 | + |
| 158 | +// adds context data to http request so we can pass |
| 159 | +func addToContextFnURL(ctx context.Context, req *http.Request, fnUrlRequest events.LambdaFunctionURLRequest) *http.Request { |
| 160 | + lc, _ := lambdacontext.FromContext(ctx) |
| 161 | + rc := requestContextFnURL{lambdaContext: lc, fnUrlContext: fnUrlRequest.RequestContext} |
| 162 | + ctx = context.WithValue(ctx, ctxKey{}, rc) |
| 163 | + return req.WithContext(ctx) |
| 164 | +} |
| 165 | + |
| 166 | +type requestContextFnURL struct { |
| 167 | + lambdaContext *lambdacontext.LambdaContext |
| 168 | + fnUrlContext events.LambdaFunctionURLRequestContext |
| 169 | +} |
0 commit comments