 # the presence and length of the body.
 
 parse_header_text = (header_text) ->
+  if exports.debug
+    util.log "parse_header_text(#{header_text})"
+
   header_lines = header_text.split("\n")
   headers = {}
   for line in header_lines

# "socket" application command).
class eslParser
  constructor: (@socket) ->
    @body_left = 0
    @buffer = ""

  # When capturing the body, buffer contains the current data
  # (text), and body_left contains how many bytes are left to
  # be read in the body.
  capture_body: (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
      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 = ""

    # Process the content
    @process @headers, body

  # For completeness provide an on_end() method.
  # TODO: it probably should make sure the buffer is empty?
  on_end: () ->
    if exports.debug
      util.log "Parser: end of stream"
      if @buffer.length > 0
        util.log "Buffer is not empty, left over: #{@buffer}"

#### ESL request
class eslRequest
