Project

General

Profile

Feature #5605 ยป 0001-net-http-use-IO.copy_stream-for-requests-using-body_.patch

normalperson (Eric Wong), 11/10/2011 09:55 AM

View differences:

lib/net/http.rb
1928 1928

  
1929 1929
    private
1930 1930

  
1931
    class Chunker
1932
      def initialize(sock)
1933
        @sock = sock
1934
        @prev = nil
1935
      end
1936

  
1937
      def write(buf)
1938
        # avoid memcpy() of buf, buf can huge and eat memory bandwidth
1939
        @sock.write("#{buf.bytesize.to_s(16)}\r\n")
1940
        rv = @sock.write(buf)
1941
        @sock.write("\r\n")
1942
        rv
1943
      end
1944

  
1945
      def finish
1946
        @sock.write("0\r\n\r\n")
1947
      end
1948
    end
1949

  
1931 1950
    def send_request_with_body(sock, ver, path, body)
1932 1951
      self.content_length = body.bytesize
1933 1952
      delete 'Transfer-Encoding'
......
1946 1965
      write_header sock, ver, path
1947 1966
      wait_for_continue sock, ver if sock.continue_timeout
1948 1967
      if chunked?
1949
        while s = f.read(1024)
1950
          sock.write(sprintf("%x\r\n", s.length) << s << "\r\n")
1951
        end
1952
        sock.write "0\r\n\r\n"
1968
        chunker = Chunker.new(sock)
1969
        IO.copy_stream(f, chunker)
1970
        chunker.finish
1953 1971
      else
1954
        while s = f.read(1024)
1955
          sock.write s
1956
        end
1972
        # copy_stream can sendfile() to sock.io unless we use SSL.
1973
        # If sock.io is an SSLSocket, copy_stream will hit SSL_write()
1974
        IO.copy_stream(f, sock.io)
1957 1975
      end
1958 1976
    end
1959 1977

  
1960
-