diff --git a/BUILD b/BUILD index d339201..8138354 100644 --- a/BUILD +++ b/BUILD @@ -15,7 +15,6 @@ cc_library( ], ) -# Targets for building base library cc_library( name = "proxy_wasm_intrinsics", srcs = [ @@ -29,10 +28,6 @@ cc_library( "proxy_wasm_intrinsics.h", ], visibility = ["//visibility:public"], - deps = [ - ":proxy_wasm_intrinsics_cc_proto", - "@com_google_protobuf//:protobuf", - ], ) cc_proto_library( @@ -51,27 +46,32 @@ proto_library( ], ) -# Targets for build lite library +# include lite protobuf support cc_library( name = "proxy_wasm_intrinsics_lite", - srcs = [ - "proxy_wasm_intrinsics.cc", - ], - hdrs = [ - "proxy_wasm_api.h", - "proxy_wasm_common.h", - "proxy_wasm_enums.h", - "proxy_wasm_externs.h", - "proxy_wasm_intrinsics.h", - ], - copts = ["-DPROXY_WASM_PROTOBUF_LITE"], + hdrs = ["proxy_wasm_intrinsics_lite.h"], + copts = ["-DPROXY_WASM_PROTOBUF_LITE=1"], visibility = ["//visibility:public"], deps = [ + ":proxy_wasm_intrinsics", ":proxy_wasm_intrinsics_lite_cc_proto", "@com_google_protobuf//:protobuf", ], ) +# include full protobuf support +cc_library( + name = "proxy_wasm_intrinsics_full", + hdrs = ["proxy_wasm_intrinsics_full.h"], + copts = ["-DPROXY_WASM_PROTOBUF_FULL=1"], + visibility = ["//visibility:public"], + deps = [ + ":proxy_wasm_intrinsics", + ":proxy_wasm_intrinsics_cc_proto", + "@com_google_protobuf//:protobuf", + ], +) + cc_proto_library( name = "proxy_wasm_intrinsics_lite_cc_proto", deps = [":proxy_wasm_intrinsics_lite_proto"], diff --git a/proxy_wasm_api.h b/proxy_wasm_api.h index 05efc86..3f7bb0f 100644 --- a/proxy_wasm_api.h +++ b/proxy_wasm_api.h @@ -188,6 +188,7 @@ struct Tuple3Hash { using HeaderStringPairs = std::vector>; +#ifdef PROXY_WASM_PROTOBUF class GrpcCallHandlerBase { public: GrpcCallHandlerBase() {} @@ -262,6 +263,7 @@ class GrpcStreamHandler : public GrpcStreamHandlerBase { virtual void onReceive(size_t body_size) = 0; }; +#endif // Behavior supported by all contexts. class ContextBase { @@ -323,16 +325,25 @@ class RootContext : public ContextBase { // Low level HTTP/gRPC interface. virtual void onHttpCallResponse(uint32_t token, uint32_t headers, size_t body_size, uint32_t trailers); +#ifdef PROXY_WASM_PROTOBUF virtual void onGrpcReceiveInitialMetadata(uint32_t token, uint32_t headers); virtual void onGrpcReceiveTrailingMetadata(uint32_t token, uint32_t trailers); virtual void onGrpcReceive(uint32_t token, size_t body_size); virtual void onGrpcClose(uint32_t token, GrpcStatus status); +#else + // Without protobuf support gRCP is disabled. + virtual void onGrpcReceiveInitialMetadata(uint32_t, uint32_t) {} + virtual void onGrpcReceiveTrailingMetadata(uint32_t, uint32_t) {} + virtual void onGrpcReceive(uint32_t, size_t) {} + virtual void onGrpcClose(uint32_t, GrpcStatus) {} +#endif // Default high level HTTP/gRPC interface. NB: overriding the low level // interface will disable this interface. Returns false on setup error. WasmResult httpCall(StringView uri, const HeaderStringPairs &request_headers, StringView request_body, const HeaderStringPairs &request_trailers, uint32_t timeout_milliseconds, HttpCallCallback callback); +#ifdef PROXY_WASM_PROTOBUF // NB: the message is the response if status == OK and an error message // otherwise. Returns false on setup error. WasmResult grpcSimpleCall(StringView service, StringView service_name, StringView method_name, @@ -365,18 +376,23 @@ class RootContext : public ContextBase { WasmResult grpcStreamHandler(StringView service, StringView service_name, StringView method_name, const HeaderStringPairs &initial_metadata, std::unique_ptr handler); +#endif private: +#ifdef PROXY_WASM_PROTOBUF friend class GrpcCallHandlerBase; friend class GrpcStreamHandlerBase; +#endif bool onDoneBase() override { return onDone(); } const std::string root_id_; std::unordered_map http_calls_; +#ifdef PROXY_WASM_PROTOBUF std::unordered_map simple_grpc_calls_; std::unordered_map> grpc_calls_; std::unordered_map> grpc_streams_; +#endif }; RootContext *getRoot(StringView root_id); @@ -1183,6 +1199,7 @@ inline Histogram *Histogram::New(StringView name, std::vector({toMetricTag(descriptors)...})); } +#ifdef PROXY_WASM_PROTOBUF inline WasmResult grpcCall(StringView service, StringView service_name, StringView method_name, const HeaderStringPairs &initial_metadata, const google::protobuf::MessageLite &request, @@ -1216,6 +1233,8 @@ inline WasmResult grpcSend(uint32_t token, StringView message, bool end_stream) return proxy_grpc_send(token, message.data(), message.size(), end_stream ? 1 : 0); } +#endif + inline WasmResult RootContext::httpCall(StringView uri, const HeaderStringPairs &request_headers, StringView request_body, const HeaderStringPairs &request_trailers, @@ -1238,6 +1257,7 @@ inline void RootContext::onHttpCallResponse(uint32_t token, uint32_t headers, si } } +#ifdef PROXY_WASM_PROTOBUF inline WasmResult RootContext::grpcSimpleCall(StringView service, StringView service_name, StringView method_name, const HeaderStringPairs &initial_metadata, @@ -1401,6 +1421,7 @@ inline WasmResult RootContext::grpcStreamHandler(StringView service, StringView } return result; } +#endif inline WasmResult ContextBase::setEffectiveContext() { return proxy_set_effective_context(id_); } diff --git a/proxy_wasm_intrinsics.h b/proxy_wasm_intrinsics.h index 4898341..ef24f4d 100644 --- a/proxy_wasm_intrinsics.h +++ b/proxy_wasm_intrinsics.h @@ -40,9 +40,12 @@ template using Optional = std::optional; #include "proxy_wasm_common.h" #include "proxy_wasm_enums.h" #include "proxy_wasm_externs.h" -#ifndef PROXY_WASM_PROTOBUF_LITE +#ifdef PROXY_WASM_PROTOBUF_FULL +#define PROXY_WASM_PROTOBUF 1 #include "proxy_wasm_intrinsics.pb.h" -#else +#endif +#ifdef PROXY_WASM_PROTOBUF_LITE +#define PROXY_WASM_PROTOBUF 1 #include "proxy_wasm_intrinsics_lite.pb.h" #endif #include "proxy_wasm_api.h" diff --git a/proxy_wasm_intrinsics_full.h b/proxy_wasm_intrinsics_full.h new file mode 100644 index 0000000..a60c815 --- /dev/null +++ b/proxy_wasm_intrinsics_full.h @@ -0,0 +1,25 @@ +/* + * Copyright 2016-2019 Envoy Project Authors + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * API Available to WASM modules. + */ +// NOLINT(namespace-envoy) + +#pragma once +#define PROXY_WASM_PROTOBUF_FULL 1 +#include "proxy_wasm_intrinsics.h" diff --git a/proxy_wasm_intrinsics_lite.h b/proxy_wasm_intrinsics_lite.h new file mode 100644 index 0000000..1e4ebe5 --- /dev/null +++ b/proxy_wasm_intrinsics_lite.h @@ -0,0 +1,25 @@ +/* + * Copyright 2016-2019 Envoy Project Authors + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * API Available to WASM modules. + */ +// NOLINT(namespace-envoy) + +#pragma once +#define PROXY_WASM_PROTOBUF_LITE 1 +#include "proxy_wasm_intrinsics.h"