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 MIME::Type.parse handling of single media with a q value #4918
Conversation
@@ -69,6 +69,12 @@ class MimeTypeTest < ActiveSupport::TestCase | |||
assert_equal expect, Mime::Type.parse(accept) | |||
end | |||
|
|||
test "parse single media range with q" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please also add a test that handles multiple types with q=
? Your changes also updated this part of the code, but you haven't added any tests for it. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@josevalim Just above that line (line 66) there is a pre-existing test that handles multiple types with q=
:
test "parse with q" do
accept = "text/xml,application/xhtml+xml,text/yaml; q=0.3,application/xml,text/html; q=0.8,image/png,text/plain; q=0.5,application/pdf,*/*; q=0.2"
expect = [Mime::HTML, Mime::XML, Mime::PNG, Mime::PDF, Mime::TEXT, Mime::YAML, Mime::ALL]
assert_equal expect, Mime::Type.parse(accept)
end
This test passes before and after my change. Or am I misunderstanding your request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was it, thanks! Merging.
Just wanted to say thanks for taking the time and doing the effort scottwb! I hope your work fixes it once and for all :) |
Fix MIME::Type.parse handling of single media with a q value
Here's a freedom patch that works for this for now: class << Mime::Type
Q_SEPARATOR_REGEXP = /;\s*q=/
def parse_with_q_fix(accept_header)
if accept_header !~ /,/
accept_header = accept_header.split(Q_SEPARATOR_REGEXP).first
end
parse_without_q_fix accept_header
end
alias_method_chain :parse, :q_fix
end |
This fix is in Rails master (4.0) or Rails stable (3.2.x)? If not, can this please be merged into 3.2.x? |
As far as I know, it only got merged into edge (4.0). I was gonna work up some additional pull requests for the various 3.x branches one of these days. It shouldn't be too hard if you wanna take a stab at it. The changes are pretty small and should be straightforward to apply to Rails 3. On Feb 16, 2012, at 23:47, Aditya Sanghi reply@reply.github.com wrote:
|
Fine. In the mood for taking on some stabbing. |
Backporting #4918 with one added test for googlebot accept header as I saw it
Based on rails#4918. Related to rails#4127.
what version was this merged into. I'm still on 3.0.9 |
@designwaves I am pretty sure it was merged into 3.2.x. Not sure what 'x' is, but when I discussed it with some of the maintainers they said they did not plan any functional updates to 3.0 and 3.1 at the time so it would only be relevant to apply it to 3.2 and later. |
Scott, Is there a monkey patch that I can cut and paste into 3.0.9? |
@scottwb @designwaves Above, you can see this was merged into 3.2 stable. |
@designwaves I posted a while back about some monkey-patches I was using on 3.0.7, which should probably work for you on 3.0.9: https://gist.github.com/1754727, however I think I like the one pasted above by @pheld better. |
sweet thank you so much for helping out with this. We are getting enough traffic now that this is becoming an issue and wanted to patch it. Awesome work man! |
I used the Gist and it fixed all my issues. The monkey patch above only fixed one but your code fixed all. thank you for that. It really helped out. |
Has this been fixed for 3.2? I saw that it was merged into master, but I'm using 3.2.13 and I'm having the same issue.
seems to be happening with bots (as mentioned above). Here's the user agent that triggered it
replicated with
|
It seems to be included in 3-2-stable so maybe it is in one 3.2.x release. |
@rafaelfranca doesn't seem so. I just tried 3.2.18, which is the latest version as shown in the RAILS_VERSION file under the 3.2-stable branch and I still get the same error... |
Maybe it was reverted because caused some regression. |
…ader given by googlebot
Based on rails#4918. Related to rails#4127. Conflicts: actionpack/test/dispatch/mime_type_test.rb
Based on rails#4918. Related to rails#4127. Conflicts: actionpack/test/dispatch/mime_type_test.rb
An HTTP request header like this:
Is valid according to the HTTP spec, but is not handled well by
Mime::Type.parse
. It is not uncommon to receive requests like this from bots and, according to one source, the PSP's browser. The failed parsing typically manifests itself as aMissingTemplate
error.This change fixes issue #736, which is not currently open, but should be.
The fix should be easy to also apply to 3.x branches. I'd be happy to help with that if necessary.