Backport #8176

Error when using body stream with chunked

Added by shek hei wong about 1 year ago. Updated 9 months ago.

[ruby-core:53775]
Status:Closed
Priority:Normal
Assignee:Usaku NAKAMURA

Description

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

Associated revisions

Revision 42461
Added by Usaku NAKAMURA 9 months ago

  • lib/net/http.rb (Net::HTTP#sendrequestwithbodystream): use String#bytesize instead of String#length. reported by shekhei (shek hei wong) at [Backport #8176].

History

#1 Updated by Yui NARUSE 9 months ago

  • Tracker changed from Bug to Backport
  • Project changed from ruby-trunk to Backport93
  • Status changed from Open to Assigned
  • Assignee set to Usaku NAKAMURA
  • Priority changed from High to Normal
  • Target version deleted (next minor)

r35281 fixes it but it is too large.
So 1.9.3 should fix this as shekhei says.

#2 Updated by Usaku NAKAMURA 9 months ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r42461.
shek hei , thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • lib/net/http.rb (Net::HTTP#sendrequestwithbodystream): use String#bytesize instead of String#length. reported by shekhei (shek hei wong) at [Backport #8176].

Also available in: Atom PDF