Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug that header values are not null-terminated #970

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions e2e/both.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,27 @@ describe('Consumer/Producer', function() {
];
run_headers_test(done, headers);
});

it('should be able to produce and consume messages with one header with NULL byte', function(done) {
var b = Buffer.alloc(3);
b.writeUInt8(1, 0);
b.writeUInt8(0, 1);
b.writeUInt8(1, 2);
var headers = [
{ key1: b },
];
run_headers_test(done, headers);
});

it('should be able to produce and consume messages with one header with partial UTF-8 sequences byte', function(done) {
//first three bytes encode the euro sign '€' in UTF-8, the fourth byte marks the beginning of a three byte UTF-8 code,
//followed by a single byte UTF-8 code encoding the dollar sign, therefore the fourth byte is a partial UTF-8 sequence
var b = Buffer.from([0xE2, 0x82, 0xAC, 0xE2, 0x24]);
var headers = [
{ key1: b },
];
run_headers_test(done, headers);
});

it('should be able to produce and consume messages with multiple headers value as string: consumeLoop', function(done) {
var headers = [
Expand Down Expand Up @@ -621,11 +642,14 @@ describe('Consumer/Producer', function() {
var messageKey = Object.keys(messageHeaders[i]);
t.equal(messageKey.length, 1, 'Expected only one Header key');
t.equal(expectedKey, messageKey[0], 'Expected key does not match message key');
var expectedValue = Buffer.isBuffer(expectedHeaders[i][expectedKey]) ?
expectedHeaders[i][expectedKey].toString() :
expectedHeaders[i][expectedKey];
var actualValue = messageHeaders[i][expectedKey].toString();
t.equal(expectedValue, actualValue, 'invalid message header');
var expectedValue = expectedHeaders[i][expectedKey];
var actualValue = messageHeaders[i][expectedKey];
if (Buffer.isBuffer(expectedValue)) {
t.deepStrictEqual(expectedValue, actualValue, 'invalid message header');
} else {
actualValue = actualValue.toString();
t.equal(expectedValue, actualValue, 'invalid message header')
}
}
}

Expand Down
29 changes: 24 additions & 5 deletions src/producer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,9 @@ NAN_METHOD(Producer::NodeProduce) {
v8::Local<v8::Array> v8Headers = v8::Local<v8::Array>::Cast(info[6]);

if (v8Headers->Length() >= 1) {
const void* header_buffer_data;
size_t header_buffer_size;

for (unsigned int i = 0; i < v8Headers->Length(); i++) {
v8::Local<v8::Object> header = Nan::Get(v8Headers, i).ToLocalChecked()
->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
Expand All @@ -571,16 +574,32 @@ NAN_METHOD(Producer::NodeProduce) {
Nan::GetCurrentContext()).ToLocalChecked();
Nan::MaybeLocal<v8::String> v8Key = Nan::To<v8::String>(
Nan::Get(props, 0).ToLocalChecked());
Nan::MaybeLocal<v8::String> v8Value = Nan::To<v8::String>(
Nan::Get(header, v8Key.ToLocalChecked()).ToLocalChecked());

v8::Local<v8::Value> v8Value =
Nan::Get(header, v8Key.ToLocalChecked()).ToLocalChecked();

if (node::Buffer::HasInstance(v8Value)) {
v8::Local<v8::Object> key_buffer_object =
(v8Value->ToObject(Nan::GetCurrentContext())).ToLocalChecked();

header_buffer_data = node::Buffer::Data(v8Value);
header_buffer_size = node::Buffer::Length(v8Value);
} else {
v8::Local<v8::String> v8String =
Nan::To<v8::String>(v8Value).ToLocalChecked();

Nan::Utf8String uValue(v8String);
std::string value(*uValue, uValue.length());

header_buffer_data = value.data();
header_buffer_size = value.length();
}

Nan::Utf8String uKey(v8Key.ToLocalChecked());
std::string key(*uKey);

Nan::Utf8String uValue(v8Value.ToLocalChecked());
std::string value(*uValue);
headers.push_back(
RdKafka::Headers::Header(key, value.c_str(), value.size()));
RdKafka::Headers::Header(key, header_buffer_data, header_buffer_size));
}
}
}
Expand Down