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

Rest get body #561

Merged
merged 3 commits into from
Oct 5, 2023
Merged
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
38 changes: 25 additions & 13 deletions plugins/zenoh-plugin-rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ fn with_extended_string<R, F: FnMut(&mut String) -> R>(
result
}

async fn query(req: Request<(Arc<Session>, String)>) -> tide::Result<Response> {
async fn query(mut req: Request<(Arc<Session>, String)>) -> tide::Result<Response> {
log::trace!("Incoming GET request: {:?}", req);

let first_accept = match req.header("accept") {
Expand Down Expand Up @@ -374,6 +374,7 @@ async fn query(req: Request<(Arc<Session>, String)>) -> tide::Result<Response> {
},
))
} else {
let body = req.body_bytes().await.unwrap_or_default();
let url = req.url();
let key_expr = match path_to_key_expr(url.path(), &req.state().1) {
Ok(ke) => ke,
Expand All @@ -397,14 +398,15 @@ async fn query(req: Request<(Arc<Session>, String)>) -> tide::Result<Response> {
QueryConsolidation::from(zenoh::query::ConsolidationMode::Latest)
};
let raw = selector.decode().any(|(k, _)| k.as_ref() == RAW_KEY);
match req
.state()
.0
.get(&selector)
.consolidation(consolidation)
.res()
.await
{
let mut query = req.state().0.get(&selector).consolidation(consolidation);
if !body.is_empty() {
Mallets marked this conversation as resolved.
Show resolved Hide resolved
let encoding: Encoding = req
.content_type()
.map(|m| m.to_string().into())
.unwrap_or_default();
query = query.with_value(Value::from(body).encoding(encoding));
}
match query.res().await {
Ok(receiver) => {
if raw {
Ok(to_raw_response(receiver).await)
Expand Down Expand Up @@ -439,7 +441,7 @@ async fn write(mut req: Request<(Arc<Session>, String)>) -> tide::Result<Respons
};
let encoding: Encoding = req
.content_type()
.map(|m| m.essence().to_owned().into())
.map(|m| m.to_string().into())
.unwrap_or_default();

// @TODO: Define the right congestion control value
Expand Down Expand Up @@ -481,16 +483,26 @@ pub async fn run(runtime: Runtime, conf: Config) -> ZResult<()> {
app.with(
tide::security::CorsMiddleware::new()
.allow_methods(
"GET, PUT, PATCH, DELETE"
"GET, POST, PUT, PATCH, DELETE"
.parse::<http_types::headers::HeaderValue>()
.unwrap(),
)
.allow_origin(tide::security::Origin::from("*"))
.allow_credentials(false),
);

app.at("/").get(query).put(write).patch(write).delete(write);
app.at("*").get(query).put(write).patch(write).delete(write);
app.at("/")
.get(query)
.post(query)
.put(write)
.patch(write)
.delete(write);
app.at("*")
.get(query)
.post(query)
.put(write)
.patch(write)
.delete(write);

if let Err(e) = app.listen(conf.http_port).await {
log::error!("Unable to start http server for REST: {:?}", e);
Expand Down