class EventMachine::Protocols::HttpClient2::Request
@private
def initialize conn, args
def initialize conn, args @conn = conn @args = args @header_lines = [] @headers = {} @blanks = 0 @chunk_trailer = nil @chunking = nil end
def process_header
def process_header unless @header_lines.first =~ HttpResponseRE @conn.close_connection @internal_error = :bad_request end @version = $1.dup @status = $2.dup.to_i clen = nil chunks = nil @header_lines.each_with_index do |e,ix| if ix > 0 hdr,val = e.split(ColonRE,2) (@headers[hdr.downcase] ||= []) << val end if clen == nil and e =~ ClenRE clen = $1.dup.to_i end if e =~ ChunkedRE chunks = true end end if clen # If the content length is zero we should not call set_text_mode, # because a value of zero will make it wait forever, hanging the # connection. Just return success instead, with empty content. if clen == 0 then @content = "" @conn.pop_request succeed(self) else @conn.set_text_mode clen end elsif chunks @chunking = true else # Chunked transfer, multipart, or end-of-connection. # For end-of-connection, we need to go the unbind # method and suppress its desire to fail us. p "NO CLEN" p @args[:uri] p @header_lines @internal_error = :unsupported_clen @conn.close_connection end end
def receive_chunk_header ln
Cf RFC 2616 pgh 3.6.1 for the format of HTTP chunks.
--
def receive_chunk_header ln if ln.length > 0 chunksize = ln.to_i(16) if chunksize > 0 @conn.set_text_mode(ln.to_i(16)) else @content = @content ? @content.join : '' @chunk_trailer = true end else # We correctly come here after each chunk gets read. # p "Got A BLANK chunk line" end end
def receive_chunk_trailer ln
--
def receive_chunk_trailer ln if ln.length == 0 @conn.pop_request succeed(self) else p "Received chunk trailer line" end end
def receive_chunked_text text
We get a single chunk. Append it to the incoming content and switch back to line mode.
--
def receive_chunked_text text # p "RECEIVED #{text.length} CHUNK" (@content ||= []) << text end
def receive_header_line ln
Allow no more than 100 lines in the header.
Allow up to ten blank lines before we get a real response line.
--
def receive_header_line ln if ln.length == 0 if @header_lines.length > 0 process_header else @blanks += 1 if @blanks > 10 @conn.close_connection end end else @header_lines << ln if @header_lines.length > 100 @internal_error = :bad_header @conn.close_connection end end end
def receive_line ln
--
def receive_line ln if @chunk_trailer receive_chunk_trailer(ln) elsif @chunking receive_chunk_header(ln) else receive_header_line(ln) end end
def receive_sized_text text
specified by the content-length header.
At the present time, we only handle contents that have a length
--
def receive_sized_text text @content = text @conn.pop_request succeed(self) end
def receive_text text
def receive_text text @chunking ? receive_chunked_text(text) : receive_sized_text(text) end
def send_request
def send_request az = @args[:authorization] and az = "Authorization: #{az}\r\n" r = [ "#{@args[:verb]} #{@args[:uri]} HTTP/#{@args[:version] || "1.1"}\r\n", "Host: #{@args[:host_header] || "_"}\r\n", az || "", "\r\n" ] @conn.send_data r.join end