Forum: Ruby-core [ruby-trunk - Bug #8176][Open] Error when using body stream with chunked

2ef55be154c86cf63f593c0083385141?d=identicon&s=25 shekhei (shek hei wong) (Guest)
on 2013-03-27 19:08
(Received via mailing list)
Issue #8176 has been reported by shekhei (shek hei  wong).

----------------------------------------
Bug #8176: Error when using body stream with chunked
https://bugs.ruby-lang.org/issues/8176

Author: shekhei (shek hei  wong)
Status: Open
Priority: High
Assignee:
Category:
Target version: next minor
ruby -v: 1.9.3-p392


When using body stream and chunk in 1.9.3-p392, when it calls

    def send_request_with_body_stream(sock, ver, path, f)
      unless content_length() or chunked?
        raise ArgumentError,
            "Content-Length not given and Transfer-Encoding is not
`chunked'"
      end
      supply_default_content_type
      write_header sock, ver, path
      wait_for_continue sock, ver if sock.continue_timeout
      if chunked?
        while s = f.read(1024)
             sock.write(sprintf("%x\r\n", s.length) << s << "\r\n")
        end
        sock.write "0\r\n\r\n"
      else
        while s = f.read(1024)
          sock.write s
        end
      end
    end

it often causes the receiving server to close the connection as the
length is apparently wrong.

In Ruby2, the code has been changed to using bytesize instead of length,
after quite a bit of debugging,

        while s = f.read(1024)
             puts sprintf("%x\r\n", s.length)
             puts sprintf("%x\r\n", s.bytesize)
             sock.write(sprintf("%x\r\n", s.length) << s << "\r\n")
        end

quite a number of times, the two values are not matching. And when i
change the code to use s.bytesize instead of s.length, the problem never
appears again
This topic is locked and can not be replied to.