aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar murgatroid99 <mlumish@google.com>2015-03-17 18:13:55 -0700
committerGravatar murgatroid99 <mlumish@google.com>2015-03-17 18:13:55 -0700
commitda771517c67c1ed09606a603d73b17380c650a1e (patch)
treefd118233a56fcc60fc64f69bfee5c82a85e0b53f
parent7370136427bb974ece569885c4e3f526d310a6ef (diff)
Changed call to only expect and return binary headers when key ends with '-bin'
-rw-r--r--src/node/ext/call.cc34
-rw-r--r--src/node/test/call_test.js4
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<FunctionTemplate> Call::fun_tpl;
+bool EndsWith(const char *str, const char *substr) {
+ return strcmp(str+strlen(str)-strlen(substr), substr) == 0;
+}
bool CreateMetadataArray(Handle<Object> metadata, grpc_metadata_array *array,
shared_ptr<Resources> resources) {
@@ -99,14 +102,19 @@ bool CreateMetadataArray(Handle<Object> metadata, grpc_metadata_array *array,
Handle<Value> 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<Value> *handle = new Persistent<Value>();
- NanAssignPersistent(*handle, value);
- resources->handles.push_back(unique_ptr<PersistentHolder>(
- 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<Value> *handle = new Persistent<Value>();
+ NanAssignPersistent(*handle, value);
+ resources->handles.push_back(unique_ptr<PersistentHolder>(
+ new PersistentHolder(handle)));
+ continue;
+ }
+ }
+ if (value->IsString()) {
Handle<String> string_value = value->ToString();
NanUtf8String *utf8_value = new NanUtf8String(string_value);
resources->strings.push_back(unique_ptr<NanUtf8String>(utf8_value));
@@ -146,9 +154,13 @@ Handle<Value> ParseMetadata(const grpc_metadata_array *metadata_array) {
array = NanNew<Array>(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);