 # "socket" application command).
 class eslParser
   constructor: (@socket) ->
-    @body_left = 0
+    @body_length = 0
     @buffer = ""
 
   # When capturing the body, buffer contains the current data
-  # (text), and body_left contains how many bytes are left to
+  # (text), and body_length contains how many bytes are expected to
   # be read in the body.
   capture_body: (data) ->
+    @buffer += data
+
     # As long as the whole body hasn't been received, keep
     # adding the new data into the buffer.
-    if data.length < @body_left
-      @buffer    += data
-      @body_left -= data.length
+    if @buffer.length < @body_length
       return
 
     # Consume the body once it has been fully received.
-    body = @buffer + data.substring(0,@body_left)
-    extra = data.substring(@body_left)
-    @body_left = 0
-    @buffer = ""
+    body = @buffer.substring(0,@body_length)
+    @buffer = @buffer.substring(@body_length)
+    @body_length = 0
 
     # Process the content
     @process @headers, body

# the presence and length of the body.

parse_header_text = (header_text) ->
  header_lines = header_text.split("\n")
  headers = {}
  for line in header_lines


    # Re-parse whatever data was left after the body was
    # fully consumed.
    @capture_headers ''

  # Capture headers, meaning up to the first blank line.
  capture_headers: (data) ->
    @buffer += data

    # Wait until we reach the end of the header.
    header_end = @buffer.indexOf("\n\n")
    if header_end < 0
      return

    # Consume the headers
    header_text = @buffer.substring(0,header_end)
    @buffer = @buffer.substring(header_end+2)

    # Parse the header lines
    @headers = parse_header_text(header_text)

    # Figure out whether a body is expected
    if @headers["Content-Length"]
      @body_length = @headers["Content-Length"]
      # Parse the body (and eventually process)
      @capture_body ''

    else
      # Process the (header-only) content
      @process @headers
      @headers = {}

      # Re-parse whatever data was left after these headers
      # were fully consumed.
      @capture_headers ''

  # Dispatch incoming data into the header or body parsers.
  on_data: (data) ->
    if exports.debug
      util.log "on_data(#{data})"

    # Capture the body as needed
    if @body_length > 0
      return @capture_body data
    else
      return @capture_headers data
