Last active
March 10, 2017 14:17
-
-
Save bradgessler/0958e3f8a17c358263c5 to your computer and use it in GitHub Desktop.
MIddleware for making legacy versions of Internet Explorer (anything below 11) work with rails. I release this code under MIT license.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ./config/initializers/rewrite_ie_header.rb | |
# Don't forget to include the middleware file! If you have a ./lib/middleware folder in rails | |
# a `require './lib/middleware/rewrite_ie_header.rb'` should do the trick | |
# | |
# IE has a really stupid accept header, so here we scope its domain of mime-types that it can negotiate, otherwise we'll | |
# end up with weird bugs where if you hit /mcp/:id, IE will download a .ppt file instead of the .html content. | |
# | |
# Issue documented in more detail at https://github.com/rails/rails/issues/9940. | |
Rails.application.config.middleware.use RewriteIEAcceptHeader do |ie_mime_types| | |
ie_mime_types.register Mime::HTML | |
ie_mime_types.register Mime::JSON | |
ie_mime_types.register Mime::ALL | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# IE pukes a bunch of mime-types when we post forms at the server in a random order. | |
# so basically you can't count on those headers. | |
class RewriteIEAcceptHeader | |
def initialize(app) | |
@app = app | |
yield self | |
end | |
def call(env) | |
if crappy_browser?(env) && (mime_type = detect_mime_type(env)) | |
env["HTTP_ACCEPT"] = mime_type.to_s | |
end | |
@app.call(env) | |
end | |
def register(mime_type) | |
mime_types << mime_type | |
end | |
private | |
def mime_types | |
@mime_types ||= [] | |
end | |
def detect_mime_type(env) | |
# PPT Presenter ajax calls don't have an ACCEPT header. However still have a MSIE mimetype. | |
# Old Mime::Type in rails2 didn't care that this was nil. In Rails3 it explodes. | |
Mime::Type.parse(env["HTTP_ACCEPT"] || "").find{ |m| mime_types.include?(m) } | |
end | |
# Detect if the user agent is IE trying to put or post... | |
def crappy_browser?(env) | |
env["HTTP_USER_AGENT"] =~ /msie/i | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment