diff --git a/userspace/falco/grpc_context.cpp b/userspace/falco/grpc_context.cpp new file mode 100644 index 00000000..f8559755 --- /dev/null +++ b/userspace/falco/grpc_context.cpp @@ -0,0 +1,35 @@ +/* +Copyright (C) 2016-2019 The Falco Authors + +This file is part of falco. + +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. +*/ + +#include "grpc_context.h" + +context::context(grpc::ServerContext* ctx): + m_ctx(ctx) +{ + // todo: set thread-specific prefix "[session id][request id]" +} + +void context::get_metadata(std::string key, std::string& val) +{ + const std::multimap& client_metadata = m_ctx->client_metadata(); + auto it = client_metadata.find(key); + if(it != client_metadata.end()) + { + val.assign(it->second.data(), it->second.size()); + } +} \ No newline at end of file diff --git a/userspace/falco/grpc_context.h b/userspace/falco/grpc_context.h new file mode 100644 index 00000000..dc471ccf --- /dev/null +++ b/userspace/falco/grpc_context.h @@ -0,0 +1,56 @@ +/* +Copyright (C) 2016-2019 The Falco Authors + +This file is part of falco. + +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. +*/ + +#pragma once + +#ifdef GRPC_INCLUDE_IS_GRPCPP +#include +#else +#include +#endif + +class context +{ +public: + context(grpc::ServerContext* ctx); + ~context() = default; + + void get_metadata(std::string key, std::string& val); + +private: + grpc::ServerContext* m_ctx = nullptr; +}; + +class stream_context : public context +{ +public: + stream_context(grpc::ServerContext* ctx): context(ctx) {} + ~stream_context() = default; + +private: + enum : char + { + STREAMING = 1, + SUCCESS, + ERROR + } m_status = STREAMING; + // Request-specific stream data + mutable void* m_stream = nullptr; + // Are there more responses to stream? + mutable bool m_has_more = false; +};