Skip to content

Add support for Content-Disposition response header field #22

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

konstruktiv
Copy link

@konstruktiv konstruktiv commented May 18, 2025

PR to fix #21.

@amandasaurus
Copy link
Owner

That looks good. But rather than the argument being the entire value of the Content-Disposition header, should it be the value of the filename? I know I would probably have to constantly consult the docs to see what the correct format is. 😅

i.e. would this be a more ergonomic and nicer API:

cgi::binary_response(200, "application/octet-stream", "filename.bin", vec![1, 2]);

If the argument is set, then the header value can be constructed from format!("attachment; filename={}", …).

Is this possible? Desireable?

@konstruktiv
Copy link
Author

konstruktiv commented May 22, 2025

That is a nicer API, but it would miss the other parts of the Content-Disposition header field's scope (display inline or as attachment, multipart/forms).

Would it be an option to accept both in the same argument? I.e. a filename or the full header string starting with "content-disposition: " in case someone needs it. Using something like this:

if let Some(cd) = content_disposition {
    const HEADER_NAME: &str = "content-disposition: ";
    let arg_cleaned: String = cd.to_lowercase().trim().to_string();        
    let cd = if arg_cleaned.starts_with(HEADER_NAME) {
        arg_cleaned.split(HEADER_NAME).collect::<String>()
    } else {
       format!("attachment; filename=\"{}\"", cd)
    };
    response = response.header(http::header::CONTENT_DISPOSITION, cd);
}

The same could be done for Content-Type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support content disposition header field for binary response
2 participants