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

fix: support non-lower-case Content-Type header provided by app #516

Merged
merged 3 commits into from Oct 12, 2022
Merged

fix: support non-lower-case Content-Type header provided by app #516

merged 3 commits into from Oct 12, 2022

Conversation

vladimir-mencl-eresearch
Copy link
Contributor

The changes in #511 change what headers get set by PDFKit and make all headers set by PDFKit lower-case.

However, the changes also affect code depending on headers set by the app - which for Rack 2.x apps will be 'Content-Type'.

To address this, the code should check if Content-Type is present and in that case use it, otherwise default to content-type.

As the code also sets this header (changing the content type to 'application/pdf'), it should set the same header that the original value is retrieved from.

So decide on the exact header name first, store it in the content_type_header variable, and use it to index the headers dict.

Fix #515.

The changes in #511 change what headers get set by PDFKit
and make all headers set by PDFKit lower-case.

However, the changes also affect code depending on headers
set by the app - which for Rack 2.x apps will be 'Content-Type'.

To address this, the code should check if Content-Type is present
and in that case use it, otherwise default to content-type.

As the code also sets this header (changing the content type
to 'application/pdf'), it should set the same header that
the original value is retrieved from.

So decide on the exact header name first, store it in the
'content_type_header' variable, and use it to index the
headers dict.

Fix #515.
@vladimir-mencl-eresearch
Copy link
Contributor Author

@serene , please let me know whether happy to merge this PR or whether any changes and/or clarification are needed.

@vladimir-mencl-eresearch
Copy link
Contributor Author

Hi @serene , this (the issue fixed here and described in #515 ) is blocking our rollout of 0.8.7. Would you be able to review this PR - and if OK, merge and cut a release? Thanks a lot in advance. Cheers, Vlad

@serene
Copy link
Contributor

serene commented Oct 11, 2022

@vladimir-mencl-eresearch Please add a test that covers this case

As rack 3.x outright rejects mixed case headers, this test has to be marked
as pending - but passes (without the pending flag) with rack 2.x.
@vladimir-mencl-eresearch
Copy link
Contributor Author

Hi @serene ,

Thanks, I've just added the tests.

Note that as rack 3.x outright rejects mixed case headers, the test for the mixed case scenario had to be marked as pending - but passes (without the pending flag) with rack 2.x.

Cheers,
Vlad

@vladimir-mencl-eresearch
Copy link
Contributor Author

Hi @serene ,

I've made the pending flag conditional on Rack version >= 3.0.0 - so on Rack 2.2.4, it actually runs the test and records it as correctly passing, while on Rack 3.0.0, the test is flagged as pending - and records the exception raised by Rack (Rack::Lint::LintError: uppercase character in header name: Content-Type), but does not cause the test-suite to fail.

Please let me know whether you're happy with this.

Cheers,
Vlad

@serene serene merged commit 089e5de into pdfkit:master Oct 12, 2022
@exviva
Copy link

exviva commented Oct 16, 2022

I got bitten by this too, I'm also setting 'Content-Disposition' to attachment in my controller, but with pdfkit 0.8.7 it reverts to inline. Is the idea here that we should be using lowercase headers from now on?

@vladimir-mencl-eresearch
Copy link
Contributor Author

Hi @exviva ,

Yes, that's correct, the code now checks for and updates the content-disposition header under the lower-case name content-disposition.

If you set that from your app, the easiest would be to update the app to set the header lowercase.

I made this PR because Rack 2.x apps do not set the content-type header explicitly and Rack 2.x implicitly sets it as Content-Type.

While the trick I used hear could also be extended to Content-Disposition, if you control it from within the app, it might be easiest to change it in the app.

Please let me know if it works for you.

Cheers,
Vlad

@exviva
Copy link

exviva commented Oct 17, 2022

Gotcha, thanks a lot @vladimir-mencl-eresearch!

@vladimir-mencl-eresearch vladimir-mencl-eresearch deleted the fix-content-type-case branch November 3, 2022 14:34
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.

Changes in #511 break PDFKit for rack 2.x apps
4 participants