Ejemplo n.º 1
0
        /// <summary>
        /// Read the response header
        /// </summary>
        /// <param name="reader">The data reader to read from</param>
        /// <param name="strictParsing">Use strict parsing rules</param>
        /// <param name="logger">The logger to write errors to</param>
        /// <exception cref="EndOfStreamException">Thrown when stream ends</exception>
        /// <returns>The http response header</returns>
        public static HttpResponseHeader ReadResponseHeader(DataReader reader, bool strictParsing, Logger logger)
        {
            // Read just first 4 chars
            string header = reader.ReadLine(BinaryEncoding.Instance, TextLineEnding.LineFeed, 4);

            // If no more data left then we end here
            if (header.Length == 0)
            {
                throw new EndOfStreamException();
            }

            if (strictParsing)
            {
                CheckLineEnding(header);
            }

            if (header.Equals("http", StringComparison.OrdinalIgnoreCase))
            {
                // Read to end of line
                header = header + reader.ReadLine();

                HttpVersion ver    = new HttpVersion(1, 0);
                string[]    values = header.Trim().Split(new char[] { ' ' }, 3);

                if (values.Length < 2)
                {
                    throw new HttpStreamParserException(String.Format(Properties.Resources.HttpParser_ResponseHeaderInvalid, header));
                }

                if (!HttpVersion.TryParse(values[0], out ver))
                {
                    throw new HttpStreamParserException(Properties.Resources.HttpParser_InvalidHttpVersionString);
                }

                int responseCode;

                if (!int.TryParse(values[1], out responseCode))
                {
                    throw new HttpStreamParserException(Properties.Resources.HttpParser_InvalidHttpResponseCode);
                }

                return(new HttpResponseHeader(reader, ReadHeaders(reader, strictParsing, logger),
                                              responseCode, values.Length > 2 ? values[2] : String.Empty, ver));
            }
            else
            {
                // Case where server probably responded with simple response even when we sent a full one
                return(new HttpResponseHeader(reader, header));
            }
        }
        /// <summary>
        /// Read a request header
        /// </summary>
        /// <param name="reader">The reader to use</param>
        /// <param name="strictParsing">Use strict parsing rules</param>
        /// <param name="logger">The logger to write errors to</param>
        /// <param name="prefix">Some prefix characters, if null then just reads whole line</param>
        /// <exception cref="EndOfStreamException">Thrown when stream ends</exception>
        /// <returns>The request header object</returns>
        public static HttpRequestHeader ReadRequestHeader(DataReader reader, bool strictParsing, Logger logger, char[] prefix)
        {
            string header;

            if (prefix != null)
            {
                header = new string(prefix) + reader.ReadLine();
            }
            else
            {
                header = reader.ReadLine();
            }

            if (header.Length == 0)
            {
                throw new EndOfStreamException();
            }

            if (strictParsing)
            {
                CheckLineEnding(header);
            }

            // Let us default to version unknown
            HttpVersion       ver     = HttpVersion.VersionUnknown;
            List <HttpHeader> headers = new List <HttpHeader>();

            string[] values = header.Trim().Split(new char[] { ' ' }, 3);
            if (values.Length < 2)
            {
                throw new HttpStreamParserException(String.Format(Properties.Resources.HttpParser_RequestHeaderInvalid, header));
            }

            if (values.Length > 2)
            {
                if (!HttpVersion.TryParse(values[2], out ver))
                {
                    throw new HttpStreamParserException(Properties.Resources.HttpParser_InvalidHttpVersionString);
                }

                headers.AddRange(ReadHeaders(reader, strictParsing, logger));
            }

            return(new HttpRequestHeader(reader, headers, values[0], values[1], ver));
        }