diff --git a/README.md b/README.md index fe4e2aa1..006aa537 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,7 @@ Any comments, questions, issues reports and PRs are welcome. Please open issues/PRs or reach me [@mathetake](https://twitter.com/mathetake). ## TODOs -- docs -- support get/set shared queue -- support get/set shared data -- support get/set property -- support metrics -- support gRPC -- enhance error handling + ## references diff --git a/examples/README.md b/examples/README.md index 1cc84a0b..4e2be86d 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,7 +1,24 @@ Theses are the proxy-wasm-go reimplementation of examples in https://github.com/proxy-wasm/proxy-wasm-rust-sdk/tree/master/examples. -Before you try an example, you must install tinygo(0.12.0 or above) for compiling your programs to wasm binary. -Please follow the official instruction [here](https://tinygo.org/getting-started/macos/). +## requirements -Currently they are tested against `docker.io/istio/proxyv2:1.5.0` image \ No newline at end of file +- TinyGo(0.14.0+): https://tinygo.org/ +- GetEnvoy: https://www.getenvoy.io/install/ + +To download compatible envoyproxy, run +```bash +getenvoy fetch wasm:1.15 +``` + +## build + +```bash +tinygo build -o ./${example}/wasm.wasm -wasm-abi=generic -target wasm ./${example}/main.go +``` + +## run + +```bash +getenvoy run wasm:1.15 -- -c ./${example}/envoy.yaml +``` diff --git a/examples/helloworld/README.md b/examples/helloworld/README.md index 6ed198f2..561ccc47 100644 --- a/examples/helloworld/README.md +++ b/examples/helloworld/README.md @@ -3,14 +3,6 @@ this example handles http request/response headers events and log all headers. -### build && run -``` -tinygo build -o wasm.wasm -wasm-abi=generic -target wasm ./main.go && docker-compose up | grep OnTick -``` - -now you can see something like: - - ```bash proxy_1 | [2020-03-25 09:04:56.149][1][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127096149656000 proxy_1 | [2020-03-25 09:04:56.154][1][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_root_id: OnTick on 1, it's 1585127096154901000 diff --git a/examples/helloworld/docker-compose.yml b/examples/helloworld/docker-compose.yml deleted file mode 100644 index d36a0c33..00000000 --- a/examples/helloworld/docker-compose.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: '2' -services: - proxy: - image: docker.io/istio/proxyv2:1.5.0 # TODO: replace with envoyproxy upstream - entrypoint: /usr/local/bin/envoy - command: -c /etc/envoy.yaml -l debug --service-cluster proxy - volumes: - - ./envoy.yaml:/etc/envoy.yaml - - ./wasm.wasm:/etc/envoy_filter_http_wasm_example.wasm - ports: - - "18000:80" - - "18001:8001" diff --git a/examples/helloworld/envoy.yaml b/examples/helloworld/envoy.yaml index 3ddd53fa..b2b29f32 100644 --- a/examples/helloworld/envoy.yaml +++ b/examples/helloworld/envoy.yaml @@ -3,8 +3,8 @@ static_resources: - name: main address: socket_address: - address: 0.0.0.0 - port_value: 80 + address: 127.0.0.1 + port_value: 1234 filter_chains: - filters: - name: envoy.http_connection_manager @@ -33,7 +33,7 @@ static_resources: runtime: "envoy.wasm.runtime.v8" code: local: - filename: "/etc/envoy_filter_http_wasm_example.wasm" + filename: "./helloworld/wasm.wasm" allow_precompiled: true - name: envoy.router config: {} diff --git a/examples/helloworld/wasm.wasm b/examples/helloworld/wasm.wasm deleted file mode 100755 index 1b7b63a4..00000000 Binary files a/examples/helloworld/wasm.wasm and /dev/null differ diff --git a/examples/http_auth_random/README.md b/examples/http_auth_random/README.md index f9721d9a..f32966e5 100644 --- a/examples/http_auth_random/README.md +++ b/examples/http_auth_random/README.md @@ -1,14 +1,6 @@ ## http_auth_random -this example authorize requests depending - on the hash values of a response from http://httpbin.org/uuid. - -### build && run -``` -tinygo build -o wasm.wasm -wasm-abi=generic -target wasm ./main.go && docker-compose up -``` - -now you can make requests authorized randomly: +this example authorize requests depending on the hash values of a response from http://httpbin.org/uuid. ```bash $ curl localhost:18000/uuid -v diff --git a/examples/http_auth_random/docker-compose.yml b/examples/http_auth_random/docker-compose.yml deleted file mode 100644 index d36a0c33..00000000 --- a/examples/http_auth_random/docker-compose.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: '2' -services: - proxy: - image: docker.io/istio/proxyv2:1.5.0 # TODO: replace with envoyproxy upstream - entrypoint: /usr/local/bin/envoy - command: -c /etc/envoy.yaml -l debug --service-cluster proxy - volumes: - - ./envoy.yaml:/etc/envoy.yaml - - ./wasm.wasm:/etc/envoy_filter_http_wasm_example.wasm - ports: - - "18000:80" - - "18001:8001" diff --git a/examples/http_auth_random/envoy.yaml b/examples/http_auth_random/envoy.yaml index 50f406ce..cc224f66 100644 --- a/examples/http_auth_random/envoy.yaml +++ b/examples/http_auth_random/envoy.yaml @@ -3,8 +3,8 @@ static_resources: - name: main address: socket_address: - address: 0.0.0.0 - port_value: 80 + address: 127.0.0.1 + port_value: 18000 filter_chains: - filters: - name: envoy.http_connection_manager @@ -33,7 +33,7 @@ static_resources: runtime: "envoy.wasm.runtime.v8" code: local: - filename: "/etc/envoy_filter_http_wasm_example.wasm" + filename: "./http_auth_random/wasm.wasm" allow_precompiled: true - name: envoy.router config: {} diff --git a/examples/http_auth_random/main.go b/examples/http_auth_random/main.go index b95a3a5c..03368de8 100644 --- a/examples/http_auth_random/main.go +++ b/examples/http_auth_random/main.go @@ -22,7 +22,7 @@ func newContext(contextID uint32) runtime.HttpContext { } // override default -func (ctx *httpHeaders) OnHttpRequestHeaders(_ int) runtime.Action { +func (ctx *httpHeaders) OnHttpRequestHeaders(_ int, _ bool) runtime.Action { hs, st := ctx.GetHttpRequestHeaders() if st != runtime.StatusOk { runtime.LogCritical("failed to get request headers") diff --git a/examples/http_auth_random/wasm.wasm b/examples/http_auth_random/wasm.wasm deleted file mode 100755 index 891f7e43..00000000 Binary files a/examples/http_auth_random/wasm.wasm and /dev/null differ diff --git a/examples/http_headers/README.md b/examples/http_headers/README.md index bd210334..6184c809 100644 --- a/examples/http_headers/README.md +++ b/examples/http_headers/README.md @@ -3,14 +3,6 @@ this example handles http request/response headers events and log all headers. -### build && run -``` -tinygo build -o wasm.wasm -wasm-abi=generic -target wasm ./main.go && docker-compose up | grep header: -``` - -now you can send requests and see headers logged: - - ```bash proxy_1 | [2020-03-25 09:09:24.937][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: request header: :authority: localhost:18000 proxy_1 | [2020-03-25 09:09:24.937][16][info][wasm] [external/envoy/source/extensions/common/wasm/context.cc:1077] wasm log my_plugin my_root_id: request header: :path: / diff --git a/examples/http_headers/docker-compose.yml b/examples/http_headers/docker-compose.yml deleted file mode 100644 index d36a0c33..00000000 --- a/examples/http_headers/docker-compose.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: '2' -services: - proxy: - image: docker.io/istio/proxyv2:1.5.0 # TODO: replace with envoyproxy upstream - entrypoint: /usr/local/bin/envoy - command: -c /etc/envoy.yaml -l debug --service-cluster proxy - volumes: - - ./envoy.yaml:/etc/envoy.yaml - - ./wasm.wasm:/etc/envoy_filter_http_wasm_example.wasm - ports: - - "18000:80" - - "18001:8001" diff --git a/examples/http_headers/envoy.yaml b/examples/http_headers/envoy.yaml index 3ddd53fa..01e8ab66 100644 --- a/examples/http_headers/envoy.yaml +++ b/examples/http_headers/envoy.yaml @@ -3,8 +3,8 @@ static_resources: - name: main address: socket_address: - address: 0.0.0.0 - port_value: 80 + address: 127.0.0.1 + port_value: 18000 filter_chains: - filters: - name: envoy.http_connection_manager @@ -33,7 +33,7 @@ static_resources: runtime: "envoy.wasm.runtime.v8" code: local: - filename: "/etc/envoy_filter_http_wasm_example.wasm" + filename: "./http_headers/wasm.wasm" allow_precompiled: true - name: envoy.router config: {} diff --git a/examples/http_headers/main.go b/examples/http_headers/main.go index 4b3b25e1..902123cc 100644 --- a/examples/http_headers/main.go +++ b/examples/http_headers/main.go @@ -21,7 +21,7 @@ func newContext(contextID uint32) runtime.HttpContext { } // override -func (ctx *httpHeaders) OnHttpRequestHeaders(_ int) runtime.Action { +func (ctx *httpHeaders) OnHttpRequestHeaders(_ int, _ bool) runtime.Action { hs, st := ctx.GetHttpRequestHeaders() if st != runtime.StatusOk { runtime.LogCritical("failed to get request headers") @@ -34,7 +34,7 @@ func (ctx *httpHeaders) OnHttpRequestHeaders(_ int) runtime.Action { } // override -func (ctx *httpHeaders) OnHttpResponseHeaders(_ int) runtime.Action { +func (ctx *httpHeaders) OnHttpResponseHeaders(_ int, _ bool) runtime.Action { hs, st := ctx.GetHttpResponseHeaders() if st != runtime.StatusOk { runtime.LogCritical("failed to get request headers") diff --git a/examples/http_headers/wasm.wasm b/examples/http_headers/wasm.wasm deleted file mode 100755 index 48df5239..00000000 Binary files a/examples/http_headers/wasm.wasm and /dev/null differ diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..f2c1554c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/mathetake/proxy-wasm-go + +go 1.14 diff --git a/runtime/abi.go b/runtime/abi.go index 75306618..2b699b46 100644 --- a/runtime/abi.go +++ b/runtime/abi.go @@ -14,5 +14,5 @@ package runtime -//go:export proxy_abi_version_0_1_0 +//export proxy_abi_version_0_1_0 func proxyABIVersion010() {} diff --git a/runtime/abi_alloc.go b/runtime/abi_alloc.go index 1c4bcfff..5338bd98 100644 --- a/runtime/abi_alloc.go +++ b/runtime/abi_alloc.go @@ -14,7 +14,7 @@ package runtime -//go:export malloc +//export malloc func malloc(size uint) *byte { buf := make([]byte, size) return &buf[0] diff --git a/runtime/abi_configuration.go b/runtime/abi_configuration.go index 3db8f7d7..2fb0e534 100644 --- a/runtime/abi_configuration.go +++ b/runtime/abi_configuration.go @@ -14,7 +14,7 @@ package runtime -//go:export proxy_on_vm_start +//export proxy_on_vm_start func proxyOnVMStart(rootContextID uint32, vmConfigurationSize int) bool { ctx, ok := currentState.rootContexts[rootContextID] if !ok { @@ -24,7 +24,7 @@ func proxyOnVMStart(rootContextID uint32, vmConfigurationSize int) bool { return ctx.OnVMStart(vmConfigurationSize) } -//go:export proxy_on_configure +//export proxy_on_configure func proxyOnConfigure(rootContextID uint32, pluginConfigurationSize int) bool { ctx, ok := currentState.rootContexts[rootContextID] if !ok { diff --git a/runtime/abi_hostcalls.go b/runtime/abi_hostcalls.go index ff0b8178..cbb3f253 100644 --- a/runtime/abi_hostcalls.go +++ b/runtime/abi_hostcalls.go @@ -14,83 +14,77 @@ package runtime -//go:export proxy_log +//export proxy_log func proxyLog(logLevel LogLevel, messageData *byte, messageSize int) Status -//go:export proxy_get_configuration -func proxyGetConfiguration(returnBufferData **byte, returnBufferSize *int) Status - -//go:export proxy_set_property +//export proxy_set_property func proxySetProperty(pathData *byte, pathSize int, valueData *byte, valueSize int) -//go:export proxy_get_property +//export proxy_get_property func proxyGetProperty(pathData *byte, pathSize int, returnValueData **byte, returnValueSize *int) -//go:export proxy_continue_request -func proxyContinueRequest() Status - -//go:export proxy_continue_response -func proxyContinueResponse() Status - -//go:export proxy_send_local_response +//export proxy_send_local_response func proxySendLocalResponse(statusCode uint32, statusCodeDetailData *byte, statusCodeDetailsSize int, bodyData *byte, bodySize int, headersData *byte, headersSize int, grpcStatus int32) Status -//go:export proxy_clear_route_cache -func proxyClearRouteCache() Status - -//go:export proxy_get_shared_data +//export proxy_get_shared_data func proxyGetSharedData(keyData *byte, keySize int, returnValueData **byte, returnValueSize *byte, returnCas *uint32) Status -//go:export proxy_set_shared_data +//export proxy_set_shared_data func proxySetSharedData(keyData *byte, keySize int, valueData *byte, valueSize int, cas uint32) Status -//go:export proxy_register_shared_queue +//export proxy_register_shared_queue func proxyRegisterSharedQueue(nameData *byte, nameSize uint, returnID *uint32) Status -//go:export proxy_resolve_shared_queue +//export proxy_resolve_shared_queue func proxyResolveSharedQueue(vmIDData *byte, vmIDSize uint, nameData *byte, nameSize uint, returnID *uint32) Status -//go:export proxy_dequeue_shared_queue +//export proxy_dequeue_shared_queue func proxyDequeueSharedQueue(queueID uint32, returnValueData **byte, returnValueSize *byte) Status -//go:export proxy_enqueue_shared_queue +//export proxy_enqueue_shared_queue func proxyEnqueueSharedQueue(queueID uint32, valueData *byte, valueSize uint) Status -//go:export proxy_get_header_map_value +//export proxy_get_header_map_value func proxyGetHeaderMapValue(mapType MapType, keyData *byte, keySize int, returnValueData **byte, returnValueSize *int) Status -//go:export proxy_add_header_map_value +//export proxy_add_header_map_value func proxyAddHeaderMapValue(mapType MapType, keyData *byte, keySize int, valueData *byte, valueSize int) Status -//go:export proxy_replace_header_map_value +//export proxy_replace_header_map_value func proxyReplaceHeaderMapValue(mapType MapType, keyData *byte, keySize int, valueData *byte, valueSize int) Status -//go:export proxy_remove_header_map_value +//export proxy_continue_stream +func proxyContinueStream(streamType StreamType) Status + +//export proxy_close_stream +func proxyCloseStream(streamType StreamType) Status + +//export proxy_remove_header_map_value func proxyRemoveHeaderMapValue(mapType MapType, keyData *byte, keySize int) Status -//go:export proxy_get_header_map_pairs +//export proxy_get_header_map_pairs func proxyGetHeaderMapPairs(mapType MapType, returnValueData **byte, returnValueSize *int) Status -//go:export proxy_set_header_map_pairs +//export proxy_set_header_map_pairs func proxySetHeaderMapPairs(mapType MapType, mapData *byte, mapSize int) Status -//go:export proxy_get_buffer_bytes +//export proxy_get_buffer_bytes func proxyGetBufferBytes(bt BufferType, start int, maxSize int, returnBufferData **byte, returnBufferSize *int) Status -//go:export proxy_http_call +//export proxy_http_call func proxyHttpCall(upstreamData *byte, upstreamSize int, headerData *byte, headerSize int, bodyData *byte, bodySize int, trailersData *byte, trailersSize int, timeout uint32, calloutIDPtr *uint32, ) Status -//go:export proxy_set_tick_period_milliseconds +//export proxy_set_tick_period_milliseconds func proxySetTickPeriodMilliseconds(period uint32) Status -//go:export proxy_get_current_time_nanoseconds +//export proxy_get_current_time_nanoseconds func proxyGetCurrentTimeNanoseconds(returnTime *int64) Status -//go:export proxy_set_effective_context +//export proxy_set_effective_context func proxySetEffectiveContext(contextID uint32) Status -//go:export proxy_done +//export proxy_done func proxyDone() Status diff --git a/runtime/abi_l4.go b/runtime/abi_l4.go index be73143b..299d2654 100644 --- a/runtime/abi_l4.go +++ b/runtime/abi_l4.go @@ -14,7 +14,7 @@ package runtime -//go:export proxy_on_new_connection +//export proxy_on_new_connection func proxyOnNewConnection(contextID uint32) Action { ctx, ok := currentState.streamContexts[contextID] if !ok { @@ -24,7 +24,7 @@ func proxyOnNewConnection(contextID uint32) Action { return ctx.OnNewConnection() } -//go:export proxy_on_downstream_data +//export proxy_on_downstream_data func proxyOnDownstreamData(contextID uint32, dataSize int, endOfStream bool) Action { ctx, ok := currentState.streamContexts[contextID] if !ok { @@ -34,7 +34,7 @@ func proxyOnDownstreamData(contextID uint32, dataSize int, endOfStream bool) Act return ctx.OnDownstreamData(dataSize, endOfStream) } -//go:export on_downstream_connection_close +//export on_downstream_connection_close func proxyOnDownstreamConnectionClose(contextID uint32, pType PeerType) { ctx, ok := currentState.streamContexts[contextID] if !ok { @@ -44,7 +44,7 @@ func proxyOnDownstreamConnectionClose(contextID uint32, pType PeerType) { ctx.OnDownStreamClose(pType) } -//go:export proxy_on_upstream_data +//export proxy_on_upstream_data func proxyOnUpstreamData(contextID uint32, dataSize int, endOfStream bool) Action { ctx, ok := currentState.streamContexts[contextID] if !ok { @@ -54,7 +54,7 @@ func proxyOnUpstreamData(contextID uint32, dataSize int, endOfStream bool) Actio return ctx.OnUpstreamData(dataSize, endOfStream) } -//go:export proxy_on_upstream_connection_close +//export proxy_on_upstream_connection_close func proxyOnUpstreamConnectionClose(contextID uint32, pType PeerType) { ctx, ok := currentState.streamContexts[contextID] if !ok { diff --git a/runtime/abi_l7.go b/runtime/abi_l7.go index c0261b9a..923e1486 100644 --- a/runtime/abi_l7.go +++ b/runtime/abi_l7.go @@ -14,18 +14,18 @@ package runtime -//go:export proxy_on_request_headers -func proxyOnRequestHeaders(contextID uint32, numHeaders int) Action { +//export proxy_on_request_headers +func proxyOnRequestHeaders(contextID uint32, numHeaders int, endOfStream bool) Action { ctx, ok := currentState.httpContexts[contextID] if !ok { panic("invalid context on proxy_on_request_headers") } currentState.setActiveContextID(contextID) - return ctx.OnHttpRequestHeaders(numHeaders) + return ctx.OnHttpRequestHeaders(numHeaders, endOfStream) } -//go:export proxy_on_request_body +//export proxy_on_request_body func proxyOnRequestBody(contextID uint32, bodySize int, endOfStream bool) Action { ctx, ok := currentState.httpContexts[contextID] if !ok { @@ -35,7 +35,7 @@ func proxyOnRequestBody(contextID uint32, bodySize int, endOfStream bool) Action return ctx.OnHttpRequestBody(bodySize, endOfStream) } -//go:export proxy_on_request_trailers +//export proxy_on_request_trailers func proxyOnRequestTrailers(contextID uint32, numTrailers int) Action { ctx, ok := currentState.httpContexts[contextID] if !ok { @@ -45,17 +45,17 @@ func proxyOnRequestTrailers(contextID uint32, numTrailers int) Action { return ctx.OnHttpRequestTrailers(numTrailers) } -//go:export proxy_on_response_headers -func proxyOnHttpResponseHeaders(contextID uint32, numHeaders int) Action { +//export proxy_on_response_headers +func proxyOnHttpResponseHeaders(contextID uint32, numHeaders int, endOfStream bool) Action { ctx, ok := currentState.httpContexts[contextID] if !ok { panic("invalid context id on proxy_on_response_headers") } currentState.setActiveContextID(contextID) - return ctx.OnHttpResponseHeaders(numHeaders) + return ctx.OnHttpResponseHeaders(numHeaders, endOfStream) } -//go:export proxy_on_response_body +//export proxy_on_response_body func proxyOnResponseBody(contextID uint32, bodySize int, endOfStream bool) Action { ctx, ok := currentState.httpContexts[contextID] if !ok { @@ -65,7 +65,7 @@ func proxyOnResponseBody(contextID uint32, bodySize int, endOfStream bool) Actio return ctx.OnHttpResponseBody(bodySize, endOfStream) } -//go:export proxy_on_response_trailers +//export proxy_on_response_trailers func proxyOnResponseTrailers(contextID uint32, numTrailers int) Action { ctx, ok := currentState.httpContexts[contextID] if !ok { @@ -75,7 +75,7 @@ func proxyOnResponseTrailers(contextID uint32, numTrailers int) Action { return ctx.OnHttpResponseTrailers(numTrailers) } -//go:export proxy_on_http_call_response +//export proxy_on_http_call_response func proxyOnHttpCallResponse(_, calloutID uint32, numHeaders, bodySize, numTrailers int) { ctxID, ok := currentState.callOuts[calloutID] if !ok { diff --git a/runtime/abi_lifecycle.go b/runtime/abi_lifecycle.go index e6973054..441dfb6e 100644 --- a/runtime/abi_lifecycle.go +++ b/runtime/abi_lifecycle.go @@ -14,7 +14,7 @@ package runtime -//go:export proxy_on_context_create +//export proxy_on_context_create func proxyOnContextCreate(contextID uint32, rootContextID uint32) { if rootContextID == 0 { currentState.createRootContext(contextID) @@ -27,7 +27,7 @@ func proxyOnContextCreate(contextID uint32, rootContextID uint32) { } } -//go:export proxy_on_done +//export proxy_on_done func proxyOnDone(contextID uint32) bool { if ctx, ok := currentState.streamContexts[contextID]; ok { currentState.setActiveContextID(contextID) @@ -43,7 +43,7 @@ func proxyOnDone(contextID uint32) bool { } } -//go:export proxy_on_log +//export proxy_on_log func proxyOnLog(contextID uint32) { if ctx, ok := currentState.streamContexts[contextID]; ok { currentState.setActiveContextID(contextID) @@ -59,7 +59,7 @@ func proxyOnLog(contextID uint32) { } } -//go:export proxy_on_delete +//export proxy_on_delete func proxyOnDelete(contextID uint32) { if _, ok := currentState.streamContexts[contextID]; ok { delete(currentState.streamContexts, contextID) diff --git a/runtime/abi_queue.go b/runtime/abi_queue.go index 5afea57a..2144183c 100644 --- a/runtime/abi_queue.go +++ b/runtime/abi_queue.go @@ -14,7 +14,7 @@ package runtime -//go:export proxy_on_queue_ready +//export proxy_on_queue_ready func ProxyOnQueueReady(contextID, queueID uint32) { ctx, ok := currentState.rootContexts[contextID] if !ok { diff --git a/runtime/abi_timers.go b/runtime/abi_timers.go index 59fc6296..922237db 100644 --- a/runtime/abi_timers.go +++ b/runtime/abi_timers.go @@ -14,7 +14,7 @@ package runtime -//go:export proxy_on_tick +//export proxy_on_tick func ProxyOnTick(rootContextID uint32) { ctx, ok := currentState.rootContexts[rootContextID] if !ok { diff --git a/runtime/abi_types.go b/runtime/abi_types.go index 266d8298..31bad415 100644 --- a/runtime/abi_types.go +++ b/runtime/abi_types.go @@ -70,4 +70,15 @@ const ( BufferTypeDownstreamData BufferType = 2 BufferTypeUpstreamData BufferType = 3 BufferTypeHttpCallResponseBody BufferType = 4 + BufferTypeGrpcReceiveBuffer BufferType = 5 + BufferTypeVmConfiguration BufferType = 6 + BufferTypePluginConfiguration BufferType = 7 + BufferTypeCallData BufferType = 8 +) + +type StreamType uint32 + +const ( + StreamTypeRequest StreamType = 0 + StreamTypeResponse StreamType = 1 ) diff --git a/runtime/context.go b/runtime/context.go index 78880a8d..845449c3 100644 --- a/runtime/context.go +++ b/runtime/context.go @@ -34,7 +34,7 @@ type RootContext interface { Context OnVMStart(vmConfigurationSize int) bool OnConfigure(pluginConfigurationSize int) bool - GetConfiguration() ([]byte, Status) + GetPluginConfiguration(dataSize int) ([]byte, Status) SetTickPeriod(period uint32) Status OnQueueReady(queueID uint32) OnTick() @@ -58,7 +58,7 @@ type HttpContext interface { Context // request - OnHttpRequestHeaders(numHeaders int) Action + OnHttpRequestHeaders(numHeaders int, endOfStream bool) Action GetHttpRequestHeaders() ([][2]string, Status) SetHttpRequestHeaders(headers [][2]string) Status GetHttpRequestHeader(key string) (string, Status) @@ -80,7 +80,7 @@ type HttpContext interface { ResumeHttpRequest() Status // response - OnHttpResponseHeaders(numHeaders int) Action + OnHttpResponseHeaders(numHeaders int, endOfStream bool) Action GetHttpResponseHeaders() ([][2]string, Status) SetHttpResponseHeaders(headers [][2]string) Status GetHttpResponseHeader(key string) (string, Status) @@ -102,7 +102,6 @@ type HttpContext interface { ResumeHttpResponse() Status SendHttpResponse(statusCode uint32, headers [][2]string, body string) Status - ClearHttpRouteCache() Status OnLog() } @@ -176,8 +175,8 @@ func (d *DefaultContext) OnConfigure(_ int) bool { } // impl RootContext -func (d *DefaultContext) GetConfiguration() ([]byte, Status) { - return getConfiguration() +func (d *DefaultContext) GetPluginConfiguration(dataSize int) ([]byte, Status) { + return getBuffer(BufferTypePluginConfiguration, 0, dataSize) } // impl RootContext @@ -223,7 +222,7 @@ func (d *DefaultContext) GetUpstreamData(start, maxSize int) ([]byte, Status) { func (d *DefaultContext) OnUpstreamStreamClose(_ PeerType) {} // impl HttpContext -func (d *DefaultContext) OnHttpRequestHeaders(_ int) Action { +func (d *DefaultContext) OnHttpRequestHeaders(_ int, _ bool) Action { return ActionContinue } @@ -304,11 +303,11 @@ func (d *DefaultContext) AddHttpRequestTrailer(key, value string) Status { // impl HttpContext func (d *DefaultContext) ResumeHttpRequest() Status { - return proxyContinueRequest() + return proxyContinueStream(StreamTypeRequest) } // impl HttpContext -func (d *DefaultContext) OnHttpResponseHeaders(_ int) Action { +func (d *DefaultContext) OnHttpResponseHeaders(_ int, _ bool) Action { return ActionContinue } @@ -390,15 +389,10 @@ func (d *DefaultContext) AddHttpResponseTrailer(key, value string) Status { // impl HttpContext func (d *DefaultContext) ResumeHttpResponse() Status { - return proxyContinueResponse() + return proxyContinueStream(StreamTypeResponse) } // impl HttpContext func (d *DefaultContext) SendHttpResponse(statusCode uint32, headers [][2]string, body string) Status { return sendHttpResponse(statusCode, headers, body) } - -// impl HttpContext -func (d *DefaultContext) ClearHttpRouteCache() Status { - return proxyClearRouteCache() -} diff --git a/runtime/hostcalls.go b/runtime/hostcalls.go index 83750b67..fa5a1a6b 100644 --- a/runtime/hostcalls.go +++ b/runtime/hostcalls.go @@ -95,26 +95,6 @@ func getBuffer(bufType BufferType, start, maxSize int) ([]byte, Status) { } } -// TODO: not tested yet -func getConfiguration() ([]byte, Status) { - var retData *byte - var retSize int - switch st := proxyGetConfiguration(&retData, &retSize); st { - case StatusOk: - // is this correct handling...? - if retData == nil { - return nil, StatusNotFound - } - return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{ - Data: uintptr(unsafe.Pointer(retData)), - Len: uintptr(retSize), - Cap: uintptr(retSize), - })), st - default: - return nil, st - } -} - func sendHttpResponse(statusCode uint32, headers [][2]string, body string) Status { shs := serializeMap(headers) hp := &shs[0]