tower-rs / tower-http

HTTP specific Tower utilities.
675 stars 156 forks source link

Using CorsLayer with axum blocks all http requests OPTIONS method #497

Closed daltoncoder closed 2 weeks ago

daltoncoder commented 1 month ago

Bug Report

Version

tower-http v0.5.2

Platform

Linux 6.9.3-arch1-1

Description

It seems using CorsLayer as a layer on an axum server stops any request with an OPTIONS method from ever hitting handler and responds with a default blank 200 response. Issue happens when using CorsLayer::permissive() and very_permissive(). I once was hitting the issue when manually adding with_methods() but currently can not recreate. Here is minimal snippet of code that will reproduce the bug. If you send any request with OPTIONS method the handler will never get hit.

use axum::{routing::any, Router};
use tower_http::cors::CorsLayer;

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/", any(root))
        .layer(CorsLayer::permissive());

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

async fn root() -> String {
    println!("in the handler");
    "Hello".to_string()
}
jplatte commented 1 month ago

This is intended, not a bug. The middleware handles OPTIONS requests without calling the inner service. Why do you want to write a custom handler for it?