From 70fa78c10d8afb3e06be84e3449fdfdeb2673765 Mon Sep 17 00:00:00 2001 From: HugoCasa Date: Fri, 20 Sep 2024 18:26:50 +0200 Subject: [PATCH] fix: allow no body in job requests (#4413) --- backend/windmill-api/src/http_triggers.rs | 25 +++++++++++++++++++++-- backend/windmill-api/src/lib.rs | 4 +--- backend/windmill-queue/src/jobs.rs | 13 +++++++++++- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/backend/windmill-api/src/http_triggers.rs b/backend/windmill-api/src/http_triggers.rs index b835c510c0..9e30743fbc 100644 --- a/backend/windmill-api/src/http_triggers.rs +++ b/backend/windmill-api/src/http_triggers.rs @@ -1,7 +1,7 @@ use axum::{ extract::{Path, Query}, response::IntoResponse, - routing::{any, delete, get, post}, + routing::{delete, get, post}, Extension, Json, Router, }; use http::{HeaderMap, StatusCode}; @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize}; use sql_builder::{bind::Bind, SqlBuilder}; use sqlx::prelude::FromRow; use std::collections::HashMap; +use tower_http::cors::CorsLayer; use windmill_audit::{audit_ee::audit_log, ActionKind}; use windmill_common::{ auth::fetch_authed_from_permissioned_as, @@ -34,7 +35,27 @@ lazy_static::lazy_static! { } pub fn routes_global_service() -> Router { - Router::new().route("/*path", any(route_job)) + let cors = CorsLayer::new() + .allow_methods([ + http::Method::GET, + http::Method::POST, + http::Method::DELETE, + http::Method::PUT, + http::Method::PATCH, + ]) + .allow_headers([http::header::CONTENT_TYPE, http::header::AUTHORIZATION]) + .allow_origin(tower_http::cors::Any); + Router::new() + .route( + "/*path", + get(route_job) + .post(route_job) + .delete(route_job) + .put(route_job) + .patch(route_job) + .head(|| async { "" }), + ) + .layer(cors) } pub fn workspaced_service() -> Router { diff --git a/backend/windmill-api/src/lib.rs b/backend/windmill-api/src/lib.rs index 2553596522..b5bba9f645 100644 --- a/backend/windmill-api/src/lib.rs +++ b/backend/windmill-api/src/lib.rs @@ -350,9 +350,7 @@ pub async fn run_server( ) .nest( "/r", - http_triggers::routes_global_service() - .layer(from_extractor::()) - .layer(cors), + http_triggers::routes_global_service().layer(from_extractor::()), ) .route("/version", get(git_v)) .route("/uptodate", get(is_up_to_date)) diff --git a/backend/windmill-queue/src/jobs.rs b/backend/windmill-queue/src/jobs.rs index a577ffe21e..68f415e7cf 100644 --- a/backend/windmill-queue/src/jobs.rs +++ b/backend/windmill-queue/src/jobs.rs @@ -2866,10 +2866,21 @@ where (content_type, extra, raw, wrap_body) }; - if content_type.is_none() || content_type.unwrap().starts_with("application/json") { + let no_content_type = content_type.is_none(); + if no_content_type || content_type.unwrap().starts_with("application/json") { let bytes = Bytes::from_request(req, _state) .await .map_err(IntoResponse::into_response)?; + if no_content_type && bytes.is_empty() { + if use_raw { + extra.insert("raw_string".to_string(), to_raw_value(&"".to_string())); + } + let mut args = HashMap::new(); + if wrap_body { + args.insert("body".to_string(), to_raw_value(&serde_json::json!({}))); + } + return Ok(PushArgsOwned { extra: Some(extra), args: args }); + } let str = String::from_utf8(bytes.to_vec()) .map_err(|e| Error::BadRequest(format!("invalid utf8: {}", e)).into_response())?;