(
req: Request<Incoming>,
state: Arc<CallbackServerState>,
)
| 295 | } |
| 296 | |
| 297 | async fn handle_callback_request( |
| 298 | req: Request<Incoming>, |
| 299 | state: Arc<CallbackServerState>, |
| 300 | ) -> CallbackResponse { |
| 301 | let method = req.method().clone(); |
| 302 | let path = req.uri().path().to_string(); |
| 303 | debug!(method = %method, path = %path, "callback request received"); |
| 304 | |
| 305 | let allowed_origin = state.allowed_origin.as_deref(); |
| 306 | if path != "/callback" { |
| 307 | return empty_response(StatusCode::NOT_FOUND, allowed_origin); |
| 308 | } |
| 309 | |
| 310 | if let Some(expected_origin) = allowed_origin { |
| 311 | let request_origin = req |
| 312 | .headers() |
| 313 | .get(ORIGIN) |
| 314 | .and_then(|value| value.to_str().ok()); |
| 315 | if request_origin != Some(expected_origin) { |
| 316 | debug!( |
| 317 | request_origin = ?request_origin, |
| 318 | allowed_origin = expected_origin, |
| 319 | "callback origin mismatch" |
| 320 | ); |
| 321 | let _ = state.take_sender(); |
| 322 | return json_response( |
| 323 | StatusCode::FORBIDDEN, |
| 324 | allowed_origin, |
| 325 | r#"{"ok":false,"error":"origin not allowed"}"#, |
| 326 | ); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | if method == Method::OPTIONS { |
| 331 | return empty_response(StatusCode::NO_CONTENT, allowed_origin); |
| 332 | } |
| 333 | |
| 334 | if method != Method::POST { |
| 335 | return json_response( |
| 336 | StatusCode::METHOD_NOT_ALLOWED, |
| 337 | allowed_origin, |
| 338 | r#"{"error":"method not allowed"}"#, |
| 339 | ); |
| 340 | } |
| 341 | |
| 342 | let body = match req.into_body().collect().await { |
| 343 | Ok(body) => body.to_bytes(), |
| 344 | Err(error) => { |
| 345 | debug!(error = %error, "failed to read callback body"); |
| 346 | let _ = state.take_sender(); |
| 347 | return json_response( |
| 348 | StatusCode::BAD_REQUEST, |
| 349 | allowed_origin, |
| 350 | r#"{"ok":false,"error":"invalid request body"}"#, |
| 351 | ); |
| 352 | } |
| 353 | }; |
| 354 |
no test coverage detected