Changed call to only expect and return binary headers when key ends with '-bin'

This commit is contained in:
murgatroid99 2015-03-17 18:13:55 -07:00
parent 61b75e081a
commit 3544e15cef
2 changed files with 25 additions and 13 deletions

View File

@ -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);

View File

@ -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);