mirror of https://github.com/grpc/grpc-node.git
Changed call to only expect and return binary headers when key ends with '-bin'
This commit is contained in:
parent
61b75e081a
commit
3544e15cef
34
ext/call.cc
34
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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue