         # File lib/em/protocols/httpclient.rb, line 161
    161:       def receive_data data
    162:         while data and data.length > 0
    163:           case @read_state
    164:           when :base
    165:             # Perform any per-request initialization here and don't consume any data.
    166:             @data = ""
    167:             @headers = []
    168:             @content_length = nil # not zero
    169:             @content = ""
    170:             @status = nil
    171:             @read_state = :header
    172:             @connection_close = nil
    173:           when :header
    174:             ary = data.split( /\r?\n/m, 2 )
    175:             if ary.length == 2
    176:               data = ary.last
    177:               if ary.first == ""
    178:                 if (@content_length and @content_length > 0) || @connection_close
    179:                   @read_state = :content
    180:                 else
    181:                   dispatch_response
    182:                   @read_state = :base
    183:                 end
    184:               else
    185:                 @headers << ary.first
    186:                 if @headers.length == 1
    187:                   parse_response_line
    188:                 elsif ary.first =~ /\Acontent-length:\s*/i
    189:                   # Only take the FIRST content-length header that appears,
    190:                   # which we can distinguish because @content_length is nil.
    191:                   # TODO, it's actually a fatal error if there is more than one
    192:                   # content-length header, because the caller is presumptively
    193:                   # a bad guy. (There is an exploit that depends on multiple
    194:                   # content-length headers.)
    195:                   @content_length ||= $'.to_i
    196:                 elsif ary.first =~ /\Aconnection:\s*close/i
    197:                   @connection_close = true
    198:                 end
    199:               end
    200:             else
    201:               @data << data
    202:               data = ""
    203:             end
    204:           when :content
    205:             # If there was no content-length header, we have to wait until the connection
    206:             # closes. Everything we get until that point is content.
    207:             # TODO: Must impose a content-size limit, and also must implement chunking.
    208:             # Also, must support either temporary files for large content, or calling
    209:             # a content-consumer block supplied by the user.
    210:             if @content_length
    211:               bytes_needed = @content_length - @content.length
    212:               @content += data[0, bytes_needed]
    213:               data = data[bytes_needed..-1] || ""
    214:               if @content_length == @content.length
    215:                 dispatch_response
    216:                 @read_state = :base
    217:               end
    218:             else
    219:               @content << data
    220:               data = ""
    221:             end
    222:           end
    223:         end
    224:       end