public static function parseHeaders(string $rawHeaders) : array
    {
        if (empty(trim($rawHeaders))) {
            throw new InvalidArgumentException('$rawHeaders cannot be whitespace');
        }

        $headers = [];
        $rawHeaders = preg_replace("/\r\n[\t ]+/", ' ', trim($rawHeaders));
        $fields = explode("\r\n", $rawHeaders);
        foreach ($fields as $field) {
            $match = null;
            if (preg_match('/([^:]+): (.+)/m', $field, $match)) {
                $key = $match[1];
                // convert 'some-header' to 'Some-Header'
                $key = strtolower(trim($key));
                $key = ucwords(preg_replace('/[\s-]/', ' ', $key));
                $key = strtr($key, ' ', '-');

                $value = trim($match[2]);

                if (!array_key_exists($key, $headers)) {
                    $headers[$key] = $value;
                    continue;
                }

                if (!is_array($headers[$key])) {
                    $headers[$key] = [$headers[$key]];
                }

                $headers[$key][] = $value;
                continue;
            }

            if (preg_match('#([A-Za-z]+) +([^ ]+) +HTTP/([\d.]+)#', $field, $match)) {
                $headers = self::addRequestDataToHeaders($match, $headers);
                continue;
            }

            if (preg_match('#HTTP/([\d.]+) +(\d{3}) +(.*)#', $field, $match)) {
                $headers = self::addResponseDataToHeaders($match, $headers);
                continue;
            }

            throw new Exception("Unsupported header format: {$field}");
        }

        return $headers;
    }