github.com/tetratelabs/proxy-wasm-go-sdk@v0.23.1-0.20240517021853-021aa9cf78e8/proxywasm/internal/abi_callback_l7.go (about) 1 // Copyright 2020-2021 Tetrate 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package internal 16 17 import ( 18 "time" 19 20 "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" 21 ) 22 23 //export proxy_on_request_headers 24 func proxyOnRequestHeaders(contextID uint32, numHeaders int, endOfStream bool) types.Action { 25 if recordTiming { 26 defer logTiming("proxyOnRequestHeaders", time.Now()) 27 } 28 ctx, ok := currentState.httpContexts[contextID] 29 if !ok { 30 panic("invalid context on proxy_on_request_headers") 31 } 32 33 currentState.setActiveContextID(contextID) 34 return ctx.OnHttpRequestHeaders(numHeaders, endOfStream) 35 } 36 37 //export proxy_on_request_body 38 func proxyOnRequestBody(contextID uint32, bodySize int, endOfStream bool) types.Action { 39 if recordTiming { 40 defer logTiming("proxyOnRequestBody", time.Now()) 41 } 42 ctx, ok := currentState.httpContexts[contextID] 43 if !ok { 44 panic("invalid context on proxy_on_request_body") 45 } 46 currentState.setActiveContextID(contextID) 47 return ctx.OnHttpRequestBody(bodySize, endOfStream) 48 } 49 50 //export proxy_on_request_trailers 51 func proxyOnRequestTrailers(contextID uint32, numTrailers int) types.Action { 52 if recordTiming { 53 defer logTiming("proxyOnRequestTrailers", time.Now()) 54 } 55 ctx, ok := currentState.httpContexts[contextID] 56 if !ok { 57 panic("invalid context on proxy_on_request_trailers") 58 } 59 currentState.setActiveContextID(contextID) 60 return ctx.OnHttpRequestTrailers(numTrailers) 61 } 62 63 //export proxy_on_response_headers 64 func proxyOnResponseHeaders(contextID uint32, numHeaders int, endOfStream bool) types.Action { 65 if recordTiming { 66 defer logTiming("proxyOnResponseHeaders", time.Now()) 67 } 68 ctx, ok := currentState.httpContexts[contextID] 69 if !ok { 70 panic("invalid context id on proxy_on_response_headers") 71 } 72 currentState.setActiveContextID(contextID) 73 return ctx.OnHttpResponseHeaders(numHeaders, endOfStream) 74 } 75 76 //export proxy_on_response_body 77 func proxyOnResponseBody(contextID uint32, bodySize int, endOfStream bool) types.Action { 78 if recordTiming { 79 defer logTiming("proxyOnResponseBody", time.Now()) 80 } 81 ctx, ok := currentState.httpContexts[contextID] 82 if !ok { 83 panic("invalid context id on proxy_on_response_headers") 84 } 85 currentState.setActiveContextID(contextID) 86 return ctx.OnHttpResponseBody(bodySize, endOfStream) 87 } 88 89 //export proxy_on_response_trailers 90 func proxyOnResponseTrailers(contextID uint32, numTrailers int) types.Action { 91 if recordTiming { 92 defer logTiming("proxyOnResponseTrailers", time.Now()) 93 } 94 ctx, ok := currentState.httpContexts[contextID] 95 if !ok { 96 panic("invalid context id on proxy_on_response_headers") 97 } 98 currentState.setActiveContextID(contextID) 99 return ctx.OnHttpResponseTrailers(numTrailers) 100 } 101 102 //export proxy_on_http_call_response 103 func proxyOnHttpCallResponse(pluginContextID, calloutID uint32, numHeaders, bodySize, numTrailers int) { 104 if recordTiming { 105 defer logTiming("proxyOnHttpCallResponse", time.Now()) 106 } 107 root, ok := currentState.pluginContexts[pluginContextID] 108 if !ok { 109 panic("http_call_response on invalid plugin context") 110 } 111 112 cb := root.httpCallbacks[calloutID] 113 if cb == nil { 114 panic("invalid callout id") 115 } 116 117 ctxID := cb.callerContextID 118 currentState.setActiveContextID(ctxID) 119 delete(root.httpCallbacks, calloutID) 120 121 // Check if the context is already deleted. 122 // For example, if the connection expired before the call response arrival, 123 // proxy_on_http_call_response is called AFTER ProxyOnDelete is called for the context id. 124 // In that case, if the callback continues response or make local reply, then the subsequent 125 // callbacks (for example OnHttpResponseHeaders) would follow and result in calling callback 126 // for already-deleted context id. See https://github.com/tetratelabs/proxy-wasm-go-sdk/issues/261 for detail. 127 if _, ok := currentState.contextIDToRootID[ctxID]; ok { 128 ProxySetEffectiveContext(ctxID) 129 cb.callback(numHeaders, bodySize, numTrailers) 130 } 131 }