本文介绍了为什么我得到 Zlib::DataError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向我无法控制的远程服务器发帖.正在传递的参数绝对是问题的一个因素,但我很难弄清楚它为什么失败.看起来没有足够大的变化来创建错误.我从来没有直接处理过 zlib 压缩.有没有办法获得更好的错误输出?示例:

I am posting to a remote server that I do not have control over. The params that are being passed are definitely a factor in the problem but I am having a hard time figuring out why it is failing. It does not look like there is a big enough change to create an error. I have never handled zlib compression directly. Is there a way to get better error output? Example:

这通过了:

 {"email" => "[email protected]"}

这失败了:

 {"email" => "[email protected]"}

Ruby 2.2/Rails 4

Ruby 2.2 / Rails 4

错误:

 #<Zlib::Inflate:0x0000000618ea38 @dictionaries={}>
 #<Zlib::DataError: invalid block type>
 "invalid block type"
[ 0] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:377:in `inflate'",
[ 1] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:377:in `block in inflate_adapter'",
[ 2] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/protocol.rb:411:in `call_block'",
[ 3] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/protocol.rb:402:in `<<'",
[ 4] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/protocol.rb:106:in `read'",
[ 5] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:399:in `read'",
[ 6] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:317:in `read_chunked'",
[ 7] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:281:in `block in read_body_0'",
[ 8] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:260:in `inflater'",
[ 9] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:279:in `read_body_0'",
[10] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:201:in `read_body'",
[11] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:226:in `body'",
[12] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http/response.rb:163:in `reading_body'",
[13] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:1411:in `catch'",
[14] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:1411:in `transport_request'",
[15] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:1384:in `request'",
[16] "/home/pete/.rvm/gems/ruby-2.2.2@umbie/gems/rest-client-1.6.9/lib/restclient/net_http_ext.rb:51:in `request'",
[17] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:1377:in `block in request'",
[18] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:853:in `start'",
[19] "/home/pete/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/net/http.rb:1375:in `request'",
[20] "/home/pete/.rvm/gems/ruby-2.2.2@umbie/gems/rest-client-1.6.9/lib/restclient/net_http_ext.rb:51:in `request'"

在 response.rb 中抛出错误的方法

Method that throws error in response.rb

 def inflate_adapter(dest)
  if dest.respond_to?(:set_encoding)
    dest.set_encoding(Encoding::ASCII_8BIT)
  elsif dest.respond_to?(:force_encoding)
    dest.force_encoding(Encoding::ASCII_8BIT)
  end
  block = proc do |compressed_chunk|
    @inflate.inflate(compressed_chunk) do |chunk| # This is line 377
      dest << chunk
    end
  end

  Net::ReadAdapter.new(block)
end

第三方库:

  require 'net/http'
  require 'uri'
  require 'cgi'
  require 'rubygems'
  require 'json'
  module ConnectionHelper
    class Connection
      attr_accessor :practiceid
      attr_reader :token

      def initialize(version, key, secret, practiceid=nil)
        uri = URI.parse('https://api.remote_url.com/')
        @connection = Net::HTTP.new(uri.host, uri.port)
        @connection.use_ssl = true
        # Monkey patch to make Net::HTTP do proper SSL verification.
        # Background reading:
        # http://stackoverflow.com/a/9238221
        # http://blog.spiderlabs.com/2013/06/a-friday-afternoon-troubleshooting-ruby-openssl-its-a-trap.html
        def @connection.proper_ssl_context!
          ssl_context = OpenSSL::SSL::SSLContext.new
          ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
          cert_store = OpenSSL::X509::Store.new
          cert_store.set_default_paths
          ssl_context.cert_store = cert_store
          @ssl_context = ssl_context
        end
        @connection.proper_ssl_context!
        # End monkey patch
        @version = version
        @key = key
        @secret = secret
        @practiceid = practiceid

        authenticate
      end

      def authenticate            # :nodoc:
        auth_paths = {
          'v1' => 'oauth',
          'preview1' => 'oauthpreview',
          'openpreview1' => 'oauthopenpreview',
        }
        request = Net::HTTP::Post.new("/#{auth_paths[@version]}/token")
        request.basic_auth(@key, @secret)
        request.set_form_data({'grant_type' => 'client_credentials'})
        response = @connection.request(request)
        authorization = JSON.parse(response.body)
        @token = authorization['access_token']
      end

      def path_join(*args)        # :nodoc:
        head = '^/+'
        tail = '/+$'
        # add a slash to each slash-trimmed string, grab the non-empty ones, and join them up
        return args.map { |arg| '/' + arg.to_s.gsub(/#{head}|#{tail}/, '') }.select { |x| !x.empty? }.join('')
      end

      def call(request, body, headers, secondcall=false)
        begin
          request.set_form_data(body)
          headers.each {
            |k, v|
            request[k] = v
          }
          request['authorization'] = "Bearer #{@token}"
          response = @connection.request(request)

          if response.code == '401' && !secondcall
            authenticate
            return call(request, body, headers, secondcall=true)
          end
          return JSON.parse(response.body)
        rescue Exception => e
          puts e.message
        end
      end

      def POST(path, parameters=nil, headers=nil)
        url = path
        parameters ||= {}
        headers ||= {}
        request = Net::HTTP::Post.new(path_join(@version, @practiceid, url))
        return call(request, parameters, headers)
      end

    private :authenticate, :path_join, :call
    end
  end

更新

当 Web 视图 API 测试器失败时,我能够得到有效响应.我在下面发布了一个好的和坏的回应.远程服务器端似乎存在验证错误,由于某种原因,zlib 无法解析它们的响应.

I am able to get a valid response from the web view API tester when it fails. I have posted a good and bad response below. It would seem that there is a validation error on the remote servers end and for some reason their response can not be parsed by zlib.

差评:

 #Header
 Content-Type: application/json
 Date: Wed, 22 Jun 2016 19:39:59 GMT
 Nncoection: close
 Pragma: No-cache
 Server: Apache
 Vary: Accept-Encoding
 X-Mashery-Message-Id: 524sd54sd-21sd-s5d4-89ds-54sd54sd4s5d4
 X-Mashery-Responder: some_site.com
 Transfer-Encoding: chunked
 Connection: keep-alive

 #Body
 {
  "fields": ["email", "mobilephone", "workphone", "zip"],
  "error": "Data for one or more of the fields listed above are required to successfully create or find a patient record. Note: invalid phone numbers are ignored."
 }

良好的反应:

 #Header
 Cache-Control: no-cache, no-store
 Content-Type: application/json
 Date: Wed, 22 Jun 2016 19:41:19 GMT
 Expires: Mon, 06 Jan 1975 16:00:00 GMT
 Nncoection: close
 Pragma: No-cache
 Server: Apache
 Vary: Accept-Encoding
 X-Mashery-Message-Id: s5ad4as5d-5s4d-5s4d-3545-s5d4s5d453s4d
 X-Mashery-Responder: some_site.com
 Content-Length: 22
 Connection: keep-alive

 #Body
 [{
   "personid": "4131"
 }]

我不知道这是否重要,但没有接受编码",它默认为:gzip;q=1.0,deflate;q=0.6,identity;q=0.3".

I do not know if it matters but there is no "accept-encoding" and it defaults to: "gzip;q=1.0,deflate;q=0.6,identity;q=0.3".

推荐答案

net/http 不知道如何处理响应.解决方案是将请求accept-encoding"设置为identity".

The net/http did not know how to handle the response. The solution was to set the request 'accept-encoding' to 'identity'.

    request.set_form_data(body)
    headers.each {
      |k, v|
      request[k] = v
    }

    request["User-Agent"] = "Mozilla/5.0"
    request["accept-encoding"] = "identity"
    request['authorization'] = "Bearer #{@token}"

这篇关于为什么我得到 Zlib::DataError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 19:23