This project is archived and is in readonly mode.

#2844 ✓resolved
Brad Sumersford

Bad Content Type Error in Rack 1.0 with first CGI Mongrel Request

Reported by Brad Sumersford | June 27th, 2009 @ 04:00 AM | in 3.x

This issue is similar to Tickets #2788 and #1875 with the same backtrace. I have been able to reproduce this reliably by sending an initial multipart request to mongrel upon a mongrel_rails (or cluster) startup.

Ruby => 1.8.6
OS => OS X Leopard (also a Linux Distro)
Rails => 2.3.2
Mongrel => 1.1.5

This issue has its origins in this change in Rails 2.3 in action_controller.rb from:

require 'action_controller/cgi_process'


# DEPRECATE: Remove CGI support
autoload :CgiRequest, 'action_controller/cgi_process'
autoload :CGIHandler, 'action_controller/cgi_process'

This causes this patch in /action_controller/cgi_ext/query_extension.rb to be lazy loaded:

# Neuter CGI parameter parsing.
def initialize_query
  # Fix some strange request environments.
  env_table['REQUEST_METHOD'] ||= 'GET'

  # POST assumes missing Content-Type is application/x-www-form-urlencoded.
  if env_table['CONTENT_TYPE'].blank? && env_table['REQUEST_METHOD'] == 'POST'
    env_table['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'

  @cookies = CGI::Cookie::parse(env_table['HTTP_COOKIE'] || env_table['COOKIE'])
  @params = {}

This has the implication of the original initialize_query running for the first request if it's a multipart request. The original ruby cgi method calls read on stdinput moving the offset to the end which causes the method parse_multipart to fail as it doesn't rewind the data before trying to parse it. After the first request, the Rails cgi extension has a chance to load making all subsequent multipart request work correctly. This also explains why passenger seems to be immune to this issue, as is starting the app via script/server as it does not process the request via cgi (mongrel_rails, though it does use mongrel).

This bug, although an isolated case, can be annoying for a production app of several mongrel instances that just listen for multipart submissions when the first one for each instance fails (Especially when you are watching the logs like a hawk :) ). I reviewed the commit 25fde77 which updates the rack version but does not address this issue.

The resolution to this may very well be that cgi support is deprecated so move on, but as this issue cost me many hours in rdebug I wanted it recorded for reference, and I would bet there are still a few mongrel_clusters still out there in the wild.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>