Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing Error with Post/Put requests (Passenger Headers)

I've run into a weird problem and after a bunch of research can't get any closer. I've got several forms that upload files via Carrierwave. When I upload the information, part of the route gets cut off (I think).

For example, I have a multi-part form submitting to:

https:/domain/programs/223/add_file as POST

but on submission I get the error

No route matches [POST] "/223/add_file"

even though what's in my address bar is the complete route. And if submit the complete route as a GET request it works fine. When I run rake routes the route shows up just fine.

Here is a subset of my route:

resources :programs do
  match "add_file" => "programs#add_file"

If it matters, I'm running Rails 3.2.2 with Passenger on Apache. The problem only happens on this production server, never in development.

Any ideas? I'm stuck on this one as it effects multiple routes and I've tried defining a custom route just for that form with no luck.

Update: When I remove multi-part => true or the file_field_tag from the form it fixes the problem. It's still an issue but seems to be less about routing than about the form with file uploads.

like image 805
Jason Mellen Avatar asked Jan 02 '13 02:01

Jason Mellen


1 Answers

Create passenger_extension.rb in the lib folder with this code:

Passenger 3

module PhusionPassenger
  module Utils

    protected

    NULL = "\0".freeze

    def split_by_null_into_hash(data)
      args = data.split(NULL, -1)
      args.pop
      headers_hash = Hash.new
      args.each_slice(2).to_a.each do |pair|
        headers_hash[pair.first] = pair.last unless headers_hash.keys.include? pair.first
      end
      return headers_hash
    end

  end
end

Passenger 5

module PhusionPassenger
  module Utils

    # Utility functions that can potentially be accelerated by native_support functions.
    module NativeSupportUtils
      extend self

      NULL = "\0".freeze

      class ProcessTimes < Struct.new(:utime, :stime)
      end

      def split_by_null_into_hash(data)
        args = data.split(NULL, -1)
        args.pop
        headers_hash = Hash.new
        args.each_slice(2).to_a.each do |pair|
          headers_hash[pair.first] = pair.last unless headers_hash.keys.include? pair.first
        end
        return headers_hash
      end

      def process_times
        times = Process.times
        return ProcessTimes.new((times.utime * 1_000_000).to_i,
          (times.stime * 1_000_000).to_i)
      end
    end

  end # module Utils
end # module PhusionPassenger

And then in 'config/application.rb' do:

class Application < Rails::Application
  ...
  config.autoload_paths += %W(#{config.root}/lib)
  require 'passenger_extension'
end

And then restart a webserver.

NOTICE: I'm not sure that this doesn't break any other functionality so use it on your own risk and please let me know if you find any harm from this approach.

like image 130
trushkevich Avatar answered Sep 28 '22 08:09

trushkevich