具有HTTPS,SSL客户端证书和Keep-Alive支持的Ruby的HTTP库?

我正在尝试用Ruby编写HTTPS客户端。 它将使用HTTPS连接到服务器,传递身份validation令牌(通过单独的登录过程获得)和SSL客户端证书。

我正在使用rest-client执行以下操作:

client = RestClient::Resource.new(url, :ssl_client_cert => OpenSSL::X509::Certificate.new(File.read('./certificate/client-2048.pem')), :ssl_client_key => OpenSSL::PKey::RSA.new(File.read('./certificate/client-2048.key'), ''), :verify_ssl => OpenSSL::SSL::VERIFY_NONE) # ... headers = { 'X-Application' => APP_KEY, 'X-Authentication' => @session_token, 'content-type' => 'application/json', 'Accept' => 'application/json' } response = client.post(request, headers) 

这是有效的,但我想做的是使用keep-alive,以避免每次我想提出请求时都要经历整个连接过程。 所涉及的延迟使得我正在编写的监控应用程序变得不那么有用。

但是,我似乎找不到的是一个提供以下内容的Ruby库:

  • HTTPS支持
  • SSL客户端证书支持
  • 活着

对于应该提供它的rest客户,有一个拉动请求 。 httparty具有persistent_httparty,但如果它支持SSL客户端证书,则没有相关文档 。

我可以fork rest-client,合并现在已经位腐烂的pull-request,并使用它。 但是我肯定在这里遗漏了一些东西……是否有现有的图书馆提供我正在寻找的东西? 或者一些httparty文档解释了SSL库证书的SSL客户端证书使用情况?

任何帮助将不胜感激。

有什么东西已经有效吗?

我相信HTTP客户端库Faraday是目前更多Ruby社区行动的焦点。

它附带一个:net_http_persistent适配器 ,它支持SSL客户端证书。 你可以这样做:

 ssl_options = { cert: OpenSSL::X509::Certificate.new(File.read('./certificate/client-2048.pem')), key: OpenSSL::PKey::RSA.new(File.read('./certificate/client-2048.key'), 'mypassword') } conn = Faraday.new(url: 'https://example.com', ssl: ssl_options) do |faraday| faraday.adapter = Faraday::Adapter::NetHttpPersistent end conn.get '/my-resource' 

HTTParty

根据规格:

…当scheme为https时提供PEM证书时产生的连接

  • 使用提供的PEM证书
  • 将validation证书

您可以使用pem classmethod以PEM格式提供客户端证书。

REST客户端

它并没有像所有那样死 – 拉里吉尔伯特( @L2G )仍在合并拉动请求并保持灯亮。 他在问题跟踪器中点头表示了他的普遍认可。 我怀疑它目前不在他的优先级队列的顶部。

发送该拉取请求的人@byroot 一直保持他的代码更新,所以在等待时你根本不需要做太多。

Ruby标准库的Net :: HTTP API满足您列出的要求:HTTPS支持,SSL客户端证书支持和Keep-Alive。

由于Net :: HTTP可以在没有块语义的情况下实例化和重用,因此也很容易将其包装在您自己的库中。

 #!/usr/bin/env ruby # # https://gist.github.com/sheldonh/4693e2eca35b62b22c55 require 'openssl' require 'net/http' require 'json' class Gist DEFAULT_OPTIONS = { use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_PEER, keep_alive_timeout: 30, cert: OpenSSL::X509::Certificate.new(File.read('./client.cert.pem')), key: OpenSSL::PKey::RSA.new(File.read('./client.key.pem')) } def initialize(http = nil) if http @http = http else @http = Net::HTTP.start("api.github.com", 443, DEFAULT_OPTIONS) end end def fetch(id, file) response = @http.request Net::HTTP::Get.new "/gists/#{id}" JSON.parse(response.body)["files"][file]["content"] end end gist = Gist.new puts gist.fetch "fc5b5c42ff2e22171f09", "gistfile1.txt" # Lorem ipsum puts gist.fetch "4693e2eca35b62b22c55", "gist.rb" # This script 

你尝试过机械化吗?

根据示例,您可以通过这样的客户端认证:

 require 'rubygems' require 'mechanize' # create Mechanize instance agent = Mechanize.new # set the path of the certificate file agent.cert = 'example.cer' # set the path of the private key file agent.key = 'example.key' # get the login form & fill it out with the username/password login_form = agent.get("http://example.com/login_page").form('Login') login_form.Userid = 'TestUser' login_form.Password = 'TestPassword' # submit login form agent.submit(login_form, login_form.buttons.first) 

根据此主题 ,您可能需要强制机械化才能使用SSLV3:

 page = Mechanize.new{|a| a.ssl_version, a.verify_mode = 'SSLv3', OpenSSL::SSL::VERIFY_NONE}.get "https://yourHTTPSurl"