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 headers, specify length #338

Merged
merged 5 commits into from Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion message.c
Expand Up @@ -77,7 +77,11 @@ void kafka_message_new(zval *return_value, const rd_kafka_message_t *message TSR
if (header_response != RD_KAFKA_RESP_ERR_NO_ERROR) {
break;
}
rdkafka_add_assoc_string(&headers_array, header_name, (char*)header_value);
#if PHP_MAJOR_VERSION >= 7
add_assoc_stringl(&headers_array, header_name, (const char*)header_value, header_size);
#else
add_assoc_stringl(&headers_array, header_name, (const char*)header_value, header_size, 1);
#endif
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could define rdkafka_add_assoc_stringl() in php_rdkafka_priv.h, like we did for rdkafka_add_assoc_string()

}
zend_update_property(NULL, return_value, ZEND_STRL("headers"), &headers_array TSRMLS_CC);
zval_ptr_dtor(&headers_array);
Expand Down
9 changes: 7 additions & 2 deletions tests/message_headers.phpt
Expand Up @@ -44,6 +44,7 @@ $headers = [
'key2' => 'value2',
'key3' => 'value3',
],
['gzencoded' => gzencode('gzdata')],
[],
null,
['key'],
Expand Down Expand Up @@ -80,6 +81,9 @@ while (true) {

$headersString = isset($msg->headers) ? $msg->headers : [];
array_walk($headersString, function(&$value, $key) {
if ('gzencoded' === $key) {
$value = gzdecode($value);
}
$value = "{$key}: {$value}";
});
if (empty($headersString)) {
Expand All @@ -90,9 +94,10 @@ while (true) {
printf("Got message: %s | Headers: %s\n", $msg->payload, $headersString);
}
--EXPECT--
5 messages delivered
6 messages delivered
Got message: message 0 | Headers: key: value
Got message: message 1 | Headers: key1: value1, key2: value2, key3: value3
Got message: message 2 | Headers: none
Got message: message 2 | Headers: gzencoded: gzdata
Got message: message 3 | Headers: none
Got message: message 4 | Headers: none
Got message: message 5 | Headers: none
2 changes: 1 addition & 1 deletion topic.c
Expand Up @@ -599,7 +599,7 @@ PHP_METHOD(RdKafka__ProducerTopic, producev)
header_key,
-1, // Auto detect header title length
Z_STRVAL_P(ZEVAL(header_value)),
-1 // Auto detect header value length
Z_STRLEN_P(ZEVAL(header_value))
);
}
} else {
Expand Down