+ accept_header = request.headers["HTTP_ACCEPT"]
+ if accept_header.nil?
+ # e.g. unit tests don't set an Accept: header by default, force XML in this case
+ request.format = "xml"
+ return
+ end
+
+ req_mimetypes = []
+
+ # Some clients (JOSM) send Accept headers which cannot be parsed by Rails, example: *; q=.2
+ # To be fair, JOSM's Accept header doesn't adhere to RFC 7231, section 5.3.1, et al. either
+ # As a workaround for backwards compatibility, we're assuming XML format
+ begin
+ req_mimetypes = Mime::Type.parse(accept_header)
+ rescue Mime::Type::InvalidMimeType
+ request.format = "xml"
+ return
+ end
+
+ # req_mimetypes contains all Accept header MIME types with descending priority
+ req_mimetypes.each do |mime|
+ if mime.symbol == :xml
+ request.format = "xml"
+ break
+ end
+
+ if mime.symbol == :json
+ request.format = "json"
+ break
+ end
+
+ # Any format, not explicitly requesting XML or JSON -> assume XML as default
+ if mime == "*/*"
+ request.format = "xml"
+ break
+ end
+ end