Project

General

Profile

Feature #3622 ยป net.http.continue.patch

Proposed Net::HTTP that waits for a continue response - drbrain (Eric Hodel), 07/28/2010 03:08 PM

View differences:

lib/net/http.rb (working copy)
517 517
      @started = false
518 518
      @open_timeout = nil
519 519
      @read_timeout = 60
520
      @continue_timeout = 0.5
520 521
      @debug_output = nil
521 522
      @use_ssl = false
522 523
      @ssl_context = nil
......
570 571
      @read_timeout = sec
571 572
    end
572 573

  
574
    # Seconds to wait for 100 Continue response.  If the HTTP object does not
575
    # receive a response in this many seconds it sends the request body.
576
    attr_reader :continue_timeout
577

  
578
    # Setter for the continue_timeout attribute.
579
    def continue_timout=(sec)
580
      @socket.continue_timeout = sec if @socket
581
      @continue_timeout = sec
582
    end
583

  
573 584
    # returns true if the HTTP session is started.
574 585
    def started?
575 586
      @started
......
1190 1201

  
1191 1202
    def transport_request(req)
1192 1203
      begin_transport req
1193
      req.exec @socket, @curr_http_version, edit_path(req.path)
1204
      res = req.exec @socket, @curr_http_version, edit_path(req.path)
1194 1205
      begin
1195 1206
        res = HTTPResponse.read_new(@socket)
1196
      end while res.kind_of?(HTTPContinue)
1207
      end while res.kind_of?(HTTPContinue) unless res
1197 1208
      res.reading_body(@socket, req.response_body_permitted?) {
1198 1209
        yield res if block_given?
1199 1210
      }
......
1740 1751
      delete 'Transfer-Encoding'
1741 1752
      supply_default_content_type
1742 1753
      write_header sock, ver, path
1754

  
1755
      res = wait_for_continue sock, ver
1756
      return res if res
1757

  
1743 1758
      sock.write body
1759

  
1760
      nil
1744 1761
    end
1745 1762

  
1746 1763
    def send_request_with_body_stream(sock, ver, path, f)
......
1750 1767
      end
1751 1768
      supply_default_content_type
1752 1769
      write_header sock, ver, path
1770

  
1771
      res = wait_for_continue sock, ver
1772
      return res if res
1773

  
1753 1774
      if chunked?
1754 1775
        while s = f.read(1024)
1755 1776
          sock.write(sprintf("%x\r\n", s.length) << s << "\r\n")
......
1760 1781
          sock.write s
1761 1782
        end
1762 1783
      end
1784

  
1785
      nil
1763 1786
    end
1764 1787

  
1765 1788
    def supply_default_content_type
......
1768 1791
      set_content_type 'application/x-www-form-urlencoded'
1769 1792
    end
1770 1793

  
1794
    ##
1795
    # Waits up to the continue timeout for a response from the server provided
1796
    # we're speaking HTTP 1.1 and are expecting a 100-continue response.
1797

  
1798
    def wait_for_continue(sock, ver)
1799
      if ver >= '1.1' and @header['expect'].include?('100-continue') then
1800
        if IO.select [sock.io], nil, nil, sock.continue_timeout then
1801
          res = HTTPResponse.read_new sock
1802
          return res unless res.kind_of?(Net::HTTPContinue)
1803
        end
1804
      end
1805

  
1806
      nil
1807
    end
1808

  
1771 1809
    def write_header(sock, ver, path)
1772 1810
      buf = "#{@method} #{path} HTTP/#{ver}\r\n"
1773 1811
      each_capitalized do |k,v|
lib/net/protocol.rb (working copy)
50 50
    def initialize(io)
51 51
      @io = io
52 52
      @read_timeout = 60
53
      @continue_timeout = 0.5
53 54
      @debug_output = nil
54 55
      @rbuf = ''
55 56
    end
56 57

  
57 58
    attr_reader :io
58 59
    attr_accessor :read_timeout
60
    attr_accessor :continue_timeout
59 61
    attr_accessor :debug_output
60 62

  
61 63
    def inspect