diff options
author | Tim Emiola <tbetbetbe@users.noreply.github.com> | 2015-03-18 16:19:23 -0700 |
---|---|---|
committer | Tim Emiola <tbetbetbe@users.noreply.github.com> | 2015-03-18 16:19:23 -0700 |
commit | 6cf02edae146694e6e9378e0b6fb635439b9c140 (patch) | |
tree | acda2659a5f663466596053f223eff999529771e /src/node | |
parent | 33013ce9c94eab3fa9ea017902ad9f187fdcfd4a (diff) | |
parent | be64ca046ef299b28ac6ad1ef5d0e1717c5a224f (diff) |
Merge pull request #1075 from murgatroid99/node_binary_metadata
Node differentiate between binary and string headers
Diffstat (limited to 'src/node')
-rw-r--r-- | src/node/ext/call.cc | 34 | ||||
-rw-r--r-- | src/node/package.json | 2 | ||||
-rw-r--r-- | src/node/test/call_test.js | 4 | ||||
-rw-r--r-- | src/node/test/end_to_end_test.js | 18 |
4 files changed, 35 insertions, 23 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/package.json b/src/node/package.json index 1d0aa0e669..9f52f8c988 100644 --- a/src/node/package.json +++ b/src/node/package.json @@ -1,6 +1,6 @@ { "name": "grpc", - "version": "0.5.5", + "version": "0.6.0", "author": "Google Inc.", "description": "gRPC Library for Node", "homepage": "http://www.grpc.io/", 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); diff --git a/src/node/test/end_to_end_test.js b/src/node/test/end_to_end_test.js index c39364d410..60e9861bc8 100644 --- a/src/node/test/end_to_end_test.js +++ b/src/node/test/end_to_end_test.js @@ -138,21 +138,21 @@ describe('end-to-end', function() { client_batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true; call.startBatch(client_batch, function(err, response) { assert.ifError(err); - assert(response['send metadata']); - assert(response['client close']); - assert(response.hasOwnProperty('metadata')); - assert.strictEqual(response.metadata.server_key[0].toString(), - 'server_value'); - assert.deepEqual(response.status, {'code': grpc.status.OK, - 'details': status_text, - 'metadata': {}}); + assert.deepEqual(response,{ + 'send metadata': true, + 'client close': true, + metadata: {server_key: ['server_value']}, + status: {'code': grpc.status.OK, + 'details': status_text, + 'metadata': {}} + }); done(); }); server.requestCall(function(err, call_details) { var new_call = call_details['new call']; assert.notEqual(new_call, null); - assert.strictEqual(new_call.metadata.client_key[0].toString(), + assert.strictEqual(new_call.metadata.client_key[0], 'client_value'); var server_call = new_call.call; assert.notEqual(server_call, null); |