views:

591

answers:

2

In my Rails app I am attempting to create a form that allows users to create a bookmark.

<% form_tag( contents_path ) do %>
  <input name='item_type' value="Bookmark" type="hidden" /></p>
  <h3>Create New Bookmark</h3>
  <p>Title:<input name='item[title]' type="text" /></p>
  <p>URL:<input name='item[url]' type="text" /></p>
  <%= submit_tag 'Create' %>
<% end %>


Edit: For clarity, here is the actual html generated by the above template:

<form action="/contents" method="post"><div style="margin:0;padding:0"><input name="authenticity_token" type="hidden" value="cc709c404365f1a5754a7bf0c3fe79ce9ec9f96b" /></div>
  <input name='item_type' value="Bookmark" type="hidden" /></p>
  <h3>Create New Bookmark</h3>
  <p>Title:<input name='item[title]' type="text" /></p>
  <p>URL:<input name='item[url]' type="text" /></p>
  <input name="commit" type="submit" value="Create" />
</form>

As you can see it's a perfectly normal form.


It works great unless an actual URL is entered, in which case the server never responds and I get a message in my mongrel.log file

Error reading HTTP body: #<RuntimeError: Socket read return nil>
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel/http_request.rb:105:in `read_socket'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel/http_request.rb:77:in `read_body'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel/http_request.rb:55:in `initialize'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:149:in `new'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:149:in `process_client'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:285:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:285:in `initialize'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:285:in `new'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:285:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:268:in `initialize'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:268:in `new'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:268:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel/configurator.rb:282:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel/configurator.rb:281:in `each'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel/configurator.rb:281:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/mongrel_rails:128:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel/command.rb:212:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/mongrel_rails:281
/usr/bin/mongrel_rails:16:in `load'
/usr/bin/mongrel_rails:16

As can be seen the request never reaches my controller code.

I have since discovered that submitting any form where a field value starts with the string "http:/" causes the problem. putting any other characters or white-space before it cures the problem. I have also tried this in other Rails apps (on the same server) with the same result.

So it is possible to work around the problem by inserting extra space at the beginning of the string and stripping it on the server.

But I would prefer a server side only fix if one is possible.

I'm hoping that it is just a simple misconfiguration on the server.

I'm running under Rails 2.1.0 on a CPanel based shared hosting arrangement.

+1  A: 

why not a form_for instead of form_tag? I just worked through something similar to this and never had an issue posting full urls.

One problem I have is you're showing us the submission form but none of the controller code that's more than likely where the break is.

I didn't post the controller code because when it fails it never reaches the controller, in fact I removed all the controller code except for writing a message to flash and doing a redirect_to :back
Noel Walters
The reason I didn't use form_for is because I don't have a prototype object at the time the form is created. The page contains several forms like this that are hidden until an associated "Create New Widget" icon is clicked.
Noel Walters
+4  A: 

The stack trace shows it hasn't loaded any of the Rails code when it falls over, so it's probably not a problem with your code or Rails itself. My guess is that your host is using something like mod_security that thinks your request looks suspicious and is mangling the data on you.

I've had no problems with having URLs entered through forms both through Mongrel and Passenger on Apache, though that's been on a self-managed dedicated server.

Luke
Thanks, mod_security gives me something to chase, I'll ask the tech support if they use anything like that. Also I'll try setting up a simple CGI program and see if that suffers from the same problem.
Noel Walters
Looks like you are correct, this problem is caused by mod_security. Hosting company support department is on the case.
Noel Walters