From da771517c67c1ed09606a603d73b17380c650a1e Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Tue, 17 Mar 2015 18:13:55 -0700 Subject: Changed call to only expect and return binary headers when key ends with '-bin' --- src/node/ext/call.cc | 34 +++++++++++++++++++++++----------- src/node/test/call_test.js | 4 ++-- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/node/ext/call.cc b/src/node/ext/call.cc index afb6541783..8cc3e38cd9 100644 --- a/src/node/ext/call.cc +++ b/src/node/ext/call.cc @@ -75,6 +75,9 @@ using v8::Value; NanCallback *Call::constructor; Persistent Call::fun_tpl; +bool EndsWith(const char *str, const char *substr) { + return strcmp(str+strlen(str)-strlen(substr), substr) == 0; +} bool CreateMetadataArray(Handle metadata, grpc_metadata_array *array, shared_ptr resources) { @@ -99,14 +102,19 @@ bool CreateMetadataArray(Handle metadata, grpc_metadata_array *array, Handle value = values->Get(j); grpc_metadata *current = &array->metadata[array->count]; current->key = **utf8_key; - if (::node::Buffer::HasInstance(value)) { - current->value = ::node::Buffer::Data(value); - current->value_length = ::node::Buffer::Length(value); - Persistent *handle = new Persistent(); - NanAssignPersistent(*handle, value); - resources->handles.push_back(unique_ptr( - new PersistentHolder(handle))); - } else if (value->IsString()) { + // Only allow binary headers for "-bin" keys + if (EndsWith(current->key, "-bin")) { + if (::node::Buffer::HasInstance(value)) { + current->value = ::node::Buffer::Data(value); + current->value_length = ::node::Buffer::Length(value); + Persistent *handle = new Persistent(); + NanAssignPersistent(*handle, value); + resources->handles.push_back(unique_ptr( + new PersistentHolder(handle))); + continue; + } + } + if (value->IsString()) { Handle string_value = value->ToString(); NanUtf8String *utf8_value = new NanUtf8String(string_value); resources->strings.push_back(unique_ptr(utf8_value)); @@ -146,9 +154,13 @@ Handle ParseMetadata(const grpc_metadata_array *metadata_array) { array = NanNew(size_map[elem->key]); metadata_object->Set(key_string, array); } - array->Set(index_map[elem->key], - MakeFastBuffer( - NanNewBufferHandle(elem->value, elem->value_length))); + if (EndsWith(elem->key, "-bin")) { + array->Set(index_map[elem->key], + MakeFastBuffer( + NanNewBufferHandle(elem->value, elem->value_length))); + } else { + array->Set(index_map[elem->key], NanNew(elem->value)); + } index_map[elem->key] += 1; } return NanEscapeScope(metadata_object); diff --git a/src/node/test/call_test.js b/src/node/test/call_test.js index 7b2b36ae37..98158ffff3 100644 --- a/src/node/test/call_test.js +++ b/src/node/test/call_test.js @@ -142,8 +142,8 @@ describe('call', function() { assert.doesNotThrow(function() { var batch = {}; batch[grpc.opType.SEND_INITIAL_METADATA] = { - 'key1': [new Buffer('value1')], - 'key2': [new Buffer('value2')] + 'key1-bin': [new Buffer('value1')], + 'key2-bin': [new Buffer('value2')] }; call.startBatch(batch, function(err, resp) { assert.ifError(err); -- cgit v1.2.3