加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 我做错了还是net / http中的错误?

发布时间:2020-12-17 02:05:12 所属栏目:百科 来源:网络整理
导读:我在NginX / Phusion Passenger服务器下使用 Ruby Net :: HTTP,试图将JSON字符串发送到服务器.看来我的POST只在作为’application / json’发送时,过早地关闭了到服务器的会话.即: 在服务器端: 127.0.0.1 - - [03/Sep/2013 07:47:14] "POST /path/to/submi
我在NginX / Phusion Passenger服务器下使用 Ruby Net :: HTTP,试图将JSON字符串发送到服务器.看来我的POST只在作为’application / json’发送时,过早地关闭了到服务器的会话.即:

在服务器端:

127.0.0.1 - - [03/Sep/2013 07:47:14] "POST /path/to/submit " 200 45 0.0013
pid=12893 thr=47197563169088 file=ext/nginx/HelperAgent.cpp:933 time=2013-09-03 07:47:14.830 ]: Uncaught exception in PassengerServer client thread:
exception: Cannot read response from backend process: Connection reset by peer (104)
backtrace:
 in 'void Client::forwardResponse(Passenger::SessionPtr&,Passenger::FileDescriptor&,const Passenger::AnalyticsLogPtr&)' (HelperAgent.cpp:698)
 in 'void Client::handleRequest(Passenger::FileDescriptor&)' (HelperAgent.cpp:859)
 in 'void Client::threadMain()' (HelperAgent.cpp:952)

客户端调试会话是:

opening connection to hostname.com...
opened
<- "POST /path/to/submit HTTP/1.1rnContent-Type: application/jsonrnAccept: application/jsonrnUser-Agent: agent+testrnConnection: closernHost: hostname.comrnContent-Length: 660rnrn"
<- "[{json string}]"
-> "HTTP/1.1 301 Moved Permanentlyrn"
-> "Server: nginx/1.2.6rn"
-> "Date: Tue,03 Sep 2013 14:47:15 GMTrn"
-> "Content-Type: text/htmlrn"
-> "Content-Length: 184rn"
-> "Connection: closern"
-> "Location: https://hostname.com/path/to/submitrn"
-> "rn"
reading 184 bytes...
-> "<html>rn<head><title>301 Moved Permanently</title></head>rn<body bgcolor="white">rn<center><h1>301 Moved Permanently</h1></center>rn<hr><center>nginx/1.2.6</center>rn</body>rn</html>rn"
read 184 bytes
Conn close
opening connection to hostname.com...
opened
<- "POST /path/to/submit HTTP/1.1rnContent-Type: application/jsonrnAccept: application/jsonrnUser-Agent: agent+testrnConnection: closernHost: hostname.comrnContent-Length: 660rnrn"
<- "[{json string}]"
-> "HTTP/1.1 200 OKrn"
-> "Content-Type: text/html;charset=utf-8rn"
-> "Content-Length: 45rn"
-> "Connection: closern"
-> "Status: 200rn"
-> "X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.17rn"
-> "X-Frame-Options: SAMEORIGINrn"
-> "X-XSS-Protection: 1; mode=blockrn"
-> "X-Content-Type-Options: nosniffrn"
-> "Server: nginx/1.2.6 + Phusion Passenger 3.0.17 (mod_rails/mod_rack)rn"
-> "Strict-Transport-Security: max-age=31536000rn"
-> "X-Frame-Options: DENYrn"
-> "rn"
reading 45 bytes...
-> "some url string"  <-- this is suspicous to me.
read 45 bytes
Conn close

我用来提交数据的代码是:

private
def _http_client(method = 'get',location = nil,limit = 10) 
    raise ArgumentError,'FATAL: too many HTTP redirects attempted...' if limit == 0
    response = nil 

    if location.nil?
        if @uri.nil?
            raise ArgumentError 'FATAL: no location specified. Quitting.'
        end 
    else
        @uri = URI.parse(location)
    end 

    client = Net::HTTP.new(@uri.host,@uri.port)
    client.open_timeout    = 15 # in seconds
    client.read_timeout    = 15 # in seconds
    initheader             = {'content-type' => 'application/json','accept'       => 'application/json','user-agent'   => UA,}
    client.set_debug_output $stderr  # XXX TEMPORARY

    if @uri.scheme == 'https'
        client.use_ssl = true
        client.verify_mode = 0 
    end 

    if method == 'post'
        serialized = JSON.generate(@payload)
        response   = client.send_request('POST',@uri.path,serialized,initheader = initheader)
        puts serialized
    end 

# Only intended for pings. In order to do
# full GETs (including query_params) this
# will need to be refactored a little.
    if method == 'get'
        response = client.get(@uri.path)
    end 

    case response
        when Net::HTTPSuccess
            @status = true
        when Net::HTTPMovedPermanently
            @status = false
            limit = limit - 1 
            _http_client(method,response['location'],limit)
        when Net::HTTPClientError
            $stderr.puts 'Client error.'
            @status = false
        when Net::HTTPServerError
            $stderr.puts 'Server error.'
            @status = false
        else
            $stderr.puts 'No idea what the server returned.'
            @status = false
    end 

    unless response.nil?
        @response  = {:code => response.code,:body => (response.body || 'OK')}
        @code      = @response[:code].to_s
        @http_body = @response[:body].to_s
    else
        $stderr.puts 'response from server was empty. :('
    end 

    _send_status
end

我正在使用:

> Ruby版本:1.9.3p448
> NginX 1.2.6
>乘客3.0.17

我很确定我可能做错了什么,但我是Ruby的新手,也是这种网络编程的新手.

上面的代码片段使用send_request方法,但我也尝试过post和post2,结果相同.

我真的很怀疑,只要在我的initheader中使用’application / json’,一切都会中断.当我删除它,一切正常.

解决方法

尝试升级到Phusion Passenger 4.它可以更好地处理I / O异步,并且在许多情况下可以避免过早的连接关闭问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读