XenAPI.HTTP.ReadHttpHeaders C# (CSharp) Method

ReadHttpHeaders() private static method

Read HTTP headers, doing any redirects as necessary
private static ReadHttpHeaders ( Stream &stream, IWebProxy proxy, bool nodelay, int timeout_ms ) : bool
stream Stream
proxy IWebProxy
nodelay bool
timeout_ms int
return bool
        private static bool ReadHttpHeaders(ref Stream stream, IWebProxy proxy, bool nodelay, int timeout_ms)
        {
            string response = ReadLine(stream);
            int code = getResultCode(response);

            switch (code)
            {
                case 200:
                    break;

                case 302:
                    string url = "";
                    while (true)
                    {
                        response = ReadLine(stream);
                        if (response.StartsWith("Location: "))
                            url = response.Substring(10);
                        if (response.Equals("\r\n") || response.Equals("\n") || response.Equals(""))
                            break;
                    }
                    Uri redirect = new Uri(url.Trim());
                    stream.Close();
                    stream = ConnectStream(redirect, proxy, nodelay, timeout_ms);
                    return true; // headers need to be sent again

                default:
                    if (response.EndsWith("\r\n"))
                        response = response.Substring(0, response.Length - 2);
                    else if (response.EndsWith("\n"))
                        response = response.Substring(0, response.Length - 1);
                    stream.Close();
                    throw new BadServerResponseException(string.Format("Received error code {0} from the server", response));
            }

            while (true)
            {
                string line = ReadLine(stream);
                if (System.Text.RegularExpressions.Regex.Match(line, "^\\s*$").Success)
                    break;
            }

            return false;
        }