Reduce code complexity
@@ -108,14 +108,21 @@ func (p *Proxy) applyWebsockets(req *http.Request) error {
return diags
}
- if wss := bodyContent.Blocks.OfType("websockets"); len(wss) == 1 {
+ wss := bodyContent.Blocks.OfType("websockets")
+ if len(wss) != 1 {
+ return nil
+ }
+
ctx := req.Context()
ctx = context.WithValue(ctx, request.AllowWebsockets, true)
*req = *req.WithContext(ctx)
// This method needs the 'request.AllowWebsockets' flag in the 'req.context'.
- if eval.IsUpgradeRequest(req) {
+ if !eval.IsUpgradeRequest(req) {
+ return nil
+ }
+
bodyContent, _, diags = wss[0].Body.PartialContent(config.WebsocketsInlineSchema)
if diags.HasErrors() {
return diags
@@ -124,7 +131,11 @@ func (p *Proxy) applyWebsockets(req *http.Request) error {
return err
}
- if attr, ok := bodyContent.Attributes["timeout"]; ok {
+ attr, ok := bodyContent.Attributes["timeout"]
+ if !ok {
+ return nil
+ }
+
val, diags := attr.Expr.Value(nil)
if diags.HasErrors() {
return diags
@@ -139,9 +150,6 @@ func (p *Proxy) applyWebsockets(req *http.Request) error {
ctx = context.WithValue(ctx, request.WebsocketsTimeout, timeout)
*req = *req.WithContext(ctx)
- }
- }
- }
return nil
}
