ruby - Webrick 代理 + cURL 返回错误请求

标签 ruby curl proxy webrick

我要疯了,我有一个非常简单的 WEBrickProxy(WEBrick 1.3.1,带有 Ruby 1.9.3),我想用curl 尝试一下。这是代理:

require 'webrick'
require 'webrick/httpproxy'

server = WEBrick::HTTPProxyServer.new(:BindAddress => "localhost", :Port => 8888)
trap('INT') { server.shutdown }
server.start

这是 cURL 命令

curl --proxy localhost:8888 http://www.google.de -v

但是curl命令总是返回错误的请求:

* About to connect() to proxy localhost port 8888 (#0)
*   Trying ::1...
* connected
* Connected to localhost (::1) port 8888 (#0)
> GET http://www.google.de HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: www.google.de
> Accept: */*
> Proxy-Connection: Keep-Alive
> 
< HTTP/1.1 400 Bad Request 
< Content-Type: text/html; charset=ISO-8859-1
< Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-10-12)
< Date: Mon, 18 Mar 2013 13:44:27 GMT
< Content-Length: 295
< Connection: close
< 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
  <HEAD><TITLE>Bad Request</TITLE></HEAD>
  <BODY>
    <H1>Bad Request</H1>
    bad URI `http://www.google.de'.
    <HR>
    <ADDRESS>
     WEBrick/1.3.1 (Ruby/1.9.3/2012-10-12) at
     23tuxmb.local:8888
    </ADDRESS>
  </BODY>
</HTML>
* Closing connection #0

curl --version 在我的 Mac OS X 10.8 上返回

curl 7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smtp smtps telnet tftp 
Features: AsynchDNS GSS-Negotiate IPv6 Largefile NTLM NTLM_WB SSL libz

我找不到错误所在,几周前我尝试了这段代码,我记得它有效。

奇怪的是,当我将 Mac 配置为全局使用该代理时(在系统设置 -> 网络 -> 高级 -> 代理 -> Web 代理内),一切正常。 Chrome 或任何其他应用程序的请求通过代理进行路由(我在代理的终端日志中看到了这一点)。

那么,有人可以重现这个问题吗?这是 curl 错误吗?或者 Webrick 相关问题?

编辑

更多信息:当curl尝试连接时,ruby脚本本身的输出是

[2013-03-18 17:16:32] ERROR bad URI `http://www.amazon.de'.
localhost - - [18/Mar/2013:17:16:32 CET] "GET http://www.amazon.de HTTP/1.1" 400 286
- -> http://www.amazon.de

谢谢!

最佳答案

如果您在请求的 URL 中添加尾部斜杠,那么它可以工作吗?即像这样的命令行:

curl --proxy localhost:8888 http://www.google.de/ -v

(这是 bug #1206 中报告的一个curl bug,并在 git 和下一个版本中修复...)

关于ruby - Webrick 代理 + cURL 返回错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15478461/

相关文章:

ruby - 如何使用 gsub 将字符串中的所有数字转换为十六进制?

java - Spring OAuth2 : InsufficientAuthenticationException

bash - Git post-receive hook,将 curl 提交消息发送到 Discord Webhook

java - 代理错误 502 : The proxy server received an invalid response from an upstream server

java - HttpClient 的代理身份验证问题

ruby-on-rails - 使用Minitest,需要 'minitest_helper'加载错误

Ruby Mechanize 连接超时

ruby-on-rails - Ruby 将单引号转换为 XML 中的双引号

docker - 为Druid构建Docker镜像时出错

python - 使用 ssl 模块的 HTTPS 代理隧道