Rails / Mongrel 是否可以接受带有值为 URL 的文本字段的表单 POST

发布于 2024-07-13 13:30:42 字数 3217 浏览 8 评论 0原文

在我的 Rails 应用程序中,我尝试创建一个允许用户创建书签的表单。

<% 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>

正如你所看到的,这是一个完全正常的形式。


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

可以看出,请求从未到达我的控制器代码。

此后我发现提交任何字段值以字符串“http://”开头的表单都会导致问题。 在解决问题之前放置任何其他字符或空格。 我也在其他 Rails 应用程序(在同一服务器上)中尝试过此操作,结果相同。

因此,可以通过在字符串开头插入额外的空格并在服务器上删除它来解决该问题。

但如果可能的话,我更希望仅在服务器端进行修复。

我希望这只是服务器上的一个简单的错误配置。

我在基于 CPanel 的共享托管安排上运行 Rails 2.1.0。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔书几行 2024-07-20 13:30:43

为什么不用 form_for 而不是 form_tag 呢? 我刚刚完成了与此类似的工作,并且在发布完整网址时从未遇到过问题。

我遇到的一个问题是,您向我们展示了提交表单,但没有任何控制器代码很可能是中断所在的地方。

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.

拿命拼未来 2024-07-20 13:30:42

堆栈跟踪显示它在崩溃时尚未加载任何 Rails 代码,因此这可能不是您的代码或 Rails 本身的问题。 我的猜测是,您的主机正在使用类似 mod_security 之类的东西,它认为您的请求看起来很可疑,并且正在破坏您的数据。

我在 Apache 上通过 Mongrel 和 Passenger 通过表单输入 URL 时没有遇到任何问题,尽管这是在自我管理的专用服务器上。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文