1 // 2 // Copyright 2017 gRPC authors. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #include <grpc/support/port_platform.h> 18 19 #include "src/core/ext/filters/workarounds/workaround_utils.h" 20 21 #include <grpc/support/alloc.h> 22 #include <grpc/support/log.h> 23 24 user_agent_parser ua_parser[GRPC_MAX_WORKAROUND_ID]; 25 destroy_user_agent_md(void * user_agent_md)26static void destroy_user_agent_md(void* user_agent_md) { 27 gpr_free(user_agent_md); 28 } 29 grpc_parse_user_agent(grpc_mdelem md)30grpc_workaround_user_agent_md* grpc_parse_user_agent(grpc_mdelem md) { 31 grpc_workaround_user_agent_md* user_agent_md = 32 static_cast<grpc_workaround_user_agent_md*>( 33 grpc_mdelem_get_user_data(md, destroy_user_agent_md)); 34 35 if (nullptr != user_agent_md) { 36 return user_agent_md; 37 } 38 user_agent_md = static_cast<grpc_workaround_user_agent_md*>( 39 gpr_malloc(sizeof(grpc_workaround_user_agent_md))); 40 for (int i = 0; i < GRPC_MAX_WORKAROUND_ID; i++) { 41 if (ua_parser[i]) { 42 user_agent_md->workaround_active[i] = ua_parser[i](md); 43 } 44 } 45 grpc_mdelem_set_user_data(md, destroy_user_agent_md, (void*)user_agent_md); 46 47 return user_agent_md; 48 } 49 grpc_register_workaround(uint32_t id,user_agent_parser parser)50void grpc_register_workaround(uint32_t id, user_agent_parser parser) { 51 GPR_ASSERT(id < GRPC_MAX_WORKAROUND_ID); 52 ua_parser[id] = parser; 53 } 54