input = $input; Loop\addReadStream($this->input, function () { while (($c = fgetc($this->input)) !== false && $c !== '') { $this->buffer .= $c; switch ($this->parsingMode) { case self::PARSE_HEADERS: if ($this->buffer === "\r\n") { $this->parsingMode = self::PARSE_BODY; $this->contentLength = (int)$this->headers['Content-Length']; $this->buffer = ''; } else if (substr($this->buffer, -2) === "\r\n") { $parts = explode(':', $this->buffer); $this->headers[$parts[0]] = trim($parts[1]); $this->buffer = ''; } break; case self::PARSE_BODY: if (strlen($this->buffer) === $this->contentLength) { $msg = new Message(MessageBody::parse($this->buffer), $this->headers); $this->emit('message', [$msg]); $this->parsingMode = self::PARSE_HEADERS; $this->headers = []; $this->buffer = ''; } break; } } }); } }