如何使用SSL客户端证书获得HTTPS请求以使用Ruby EventMachine?
问题描述:
我试图访问使用Ruby EventMachine进行SSL证书认证的HTTPS Web服务,但我没有得到它的工作。如何使用SSL客户端证书获得HTTPS请求以使用Ruby EventMachine?
我已经写以下简单的代码块,以测试它端至端:
require 'rubygems'
require 'em-http'
EventMachine.run do
url = 'https://foobar.com/'
ssl_opts = {:private_key_file => '/tmp/private.key',
:cert_chain_file => '/tmp/ca.pem',
:verify_peer => false}
http = EventMachine::HttpRequest.new(url).get :ssl => ssl_opts
http.callback do
p http.response_header.status
p http.response_header
p http.response
EventMachine.stop
end
http.errback do
EventMachine.stop
fail "Request failed"
end
end
运行以上输出<SSL_incomp>
接着凸起RuntimeError消息。我试过运行:verify_peer
设置为true和false,它给了我同样的错误。如果没有:ssl
选项,则运行EventMachine::HttpRequest#get
的操作相同。
我也尝试将请求发送到GMail(https://mail.google.com),而没有:ssl
选项(即纯粹的HTTPS没有证书),并且工作,输出状态代码200,标题和正文。
我试图做与卷曲的Web服务相同的请求和工作原理:
curl --silent --cert /tmp/private.key --cacert /tmp/ca.pem https://foobar.com/
我在想,我要么使用EM-HTTP请求宝石或EventMachine的错误或者说,SSL文件的格式与curl协同工作,但不支持EventMachine。
我有人知道如何解决上面的例子或提供一个类似的例子直接使用EventMachine将不胜感激!
答
传递给curl的文件--cert
的文件同时包含证书和密钥(除非单独传递--key
)。只需使用/tmp/private.key
作为参数都:private_key_file
和:cert_chain_file
关于这一问题,露出下面的错误(而不是只打印出SSL_incomp)补丁的详细信息,请参阅http://github.com/eventmachine/eventmachine/issues/#issue/115。