Skip to content

Commit

Permalink
fix: adminspace encodings (#1182)
Browse files Browse the repository at this point in the history
* fix(adminspace-encoding): explicit set of encoding for adminspace replies

Signed-off-by: Gabriele Baldoni <[email protected]>

* fix: using TEXT_PLAIN with simple strings

Signed-off-by: Gabriele Baldoni <[email protected]>

* fix: better matching of encodings in REST plugin

Signed-off-by: Gabriele Baldoni <[email protected]>

* chore: addressing comments

Signed-off-by: Gabriele Baldoni <[email protected]>

---------

Signed-off-by: Gabriele Baldoni <[email protected]>
  • Loading branch information
gabrik authored Jun 25, 2024
1 parent 755cdc2 commit 8f94b08
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
11 changes: 10 additions & 1 deletion plugins/zenoh-plugin-rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,19 @@ fn payload_to_json(payload: &ZBytes, encoding: &Encoding) -> serde_json::Value {
&Encoding::APPLICATION_JSON | &Encoding::TEXT_JSON | &Encoding::TEXT_JSON5 => {
payload
.deserialize::<serde_json::Value>()
.unwrap_or_else(|_| {
.unwrap_or_else(|e| {
tracing::warn!("Encoding is JSON but data is not JSON, converting to base64, Error: {e:?}");
serde_json::Value::String(base64_encode(&Cow::from(payload)))
})
}
&Encoding::TEXT_PLAIN | &Encoding::ZENOH_STRING => serde_json::Value::String(
payload
.deserialize::<String>()
.unwrap_or_else(|e| {
tracing::warn!("Encoding is String but data is not String, converting to base64, Error: {e:?}");
base64_encode(&Cow::from(payload))
}),
),
// otherwise convert to JSON string
_ => serde_json::Value::String(base64_encode(&Cow::from(payload))),
}
Expand Down
21 changes: 15 additions & 6 deletions zenoh/src/net/runtime/adminspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,11 @@ zenoh_build{{version="{}"}} 1
.openmetrics_text(),
);

if let Err(e) = query.reply(reply_key, metrics).wait() {
if let Err(e) = query
.reply(reply_key, metrics)
.encoding(Encoding::TEXT_PLAIN)
.wait()
{
tracing::error!("Error sending AdminSpace reply: {:?}", e);
}
}
Expand All @@ -681,6 +685,7 @@ fn routers_linkstate_data(context: &AdminContext, query: Query) {

if let Err(e) = query
.reply(reply_key, tables.hat_code.info(&tables, WhatAmI::Router))
.encoding(Encoding::TEXT_PLAIN)
.wait()
{
tracing::error!("Error sending AdminSpace reply: {:?}", e);
Expand All @@ -699,6 +704,7 @@ fn peers_linkstate_data(context: &AdminContext, query: Query) {

if let Err(e) = query
.reply(reply_key, tables.hat_code.info(&tables, WhatAmI::Peer))
.encoding(Encoding::TEXT_PLAIN)
.wait()
{
tracing::error!("Error sending AdminSpace reply: {:?}", e);
Expand Down Expand Up @@ -770,7 +776,11 @@ fn plugins_data(context: &AdminContext, query: Query) {
let status = serde_json::to_value(status).unwrap();
match ZBytes::try_from(status) {
Ok(zbuf) => {
if let Err(e) = query.reply(key, zbuf).wait() {
if let Err(e) = query
.reply(key, zbuf)
.encoding(Encoding::APPLICATION_JSON)
.wait()
{
tracing::error!("Error sending AdminSpace reply: {:?}", e);
}
}
Expand All @@ -782,8 +792,6 @@ fn plugins_data(context: &AdminContext, query: Query) {

#[cfg(feature = "plugins")]
fn plugins_status(context: &AdminContext, query: Query) {
use crate::bytes::{Serialize, ZSerde};

let key_expr = query.key_expr();
let guard = context.runtime.plugins_manager();
let mut root_key = format!(
Expand All @@ -798,7 +806,8 @@ fn plugins_status(context: &AdminContext, query: Query) {
if let Ok(key_expr) = KeyExpr::try_from(plugin_path_key.clone()) {
if query.key_expr().intersects(&key_expr) {
if let Err(e) = query
.reply(key_expr, ZSerde.serialize(plugin.path()))
.reply(key_expr, plugin.path())
.encoding(Encoding::TEXT_PLAIN)
.wait()
{
tracing::error!("Error sending AdminSpace reply: {:?}", e);
Expand All @@ -824,7 +833,7 @@ fn plugins_status(context: &AdminContext, query: Query) {
if let Ok(key_expr) = KeyExpr::try_from(response.key) {
match ZBytes::try_from(response.value) {
Ok(zbuf) => {
if let Err(e) = query.reply(key_expr, zbuf).wait() {
if let Err(e) = query.reply(key_expr, zbuf).encoding(Encoding::APPLICATION_JSON).wait() {
tracing::error!("Error sending AdminSpace reply: {:?}", e);
}
},
Expand Down

0 comments on commit 8f94b08

Please sign in to comment.