Example #1
0
        /// <summary>
        /// Tries to parse a trailer header for the message
        /// </summary>
        /// <returns></returns>
        private bool TryAndParseTrailerHeaderLine(ref HttpMessage message, EventHandler <HttpMessageProgressEventArgs> onProgress, object stateObject)
        {
            // try and parse a token from the data
            HttpByteParserToken token = _parser.GetNextToken();

            if (token == null)
            {
                // change state to processing the body
                _state = Processing.Finished;

                // we're done with the headers
                return(true);
            }

            // the first line of any message is never empty
            if (token.IsEmpty)
            {
                // change state to processing the body
                _state = Processing.Finished;

                // we're done with the headers
                return(true);
            }

            // success! this line is a header
            string line = token.ToString(HttpUtils.Encoding);

            // parse the line into a new header
            HttpHeader header = HttpHeader.Parse(line);

            // save the header in the chunked body trailers
            _chunkedBody.TrailerHeaders.Add(header);

            return(true);
        }
Example #2
0
        /// <summary>
        /// Tries to parse a chunk size line for the message
        /// </summary>
        /// <returns></returns>
        private bool TryAndParseChunkSizeLine(ref HttpMessage message, EventHandler <HttpMessageProgressEventArgs> onProgress, object stateObject)
        {
            // try and parse a token from the data
            HttpByteParserToken token = _parser.GetNextToken();

            if (token == null)
            {
                return(false);
            }

            // the first line of any message is never empty
            if (token.IsEmpty)
            {
                return(false);
            }

            // success! this line is a header
            string line = token.ToString(HttpUtils.Encoding);

            // parse the size line
            HttpChunkSizeLine chunkSizeLine = HttpChunkSizeLine.Parse(line);

            // create a new chunk
            _chunk = new HttpChunk(chunkSizeLine, null);

            // change state to processing chunk data
            _state = Processing.ChunkData;

            return(true);
        }
Example #3
0
        /// <summary>
        /// Tries to parse the first line for the message
        /// </summary>
        /// <returns></returns>
        private bool TryAndParseFirstLine(ref HttpMessage message, EventHandler <HttpMessageProgressEventArgs> onProgress, object stateObject)
        {
            // try and parse a token from the data
            HttpByteParserToken token = _parser.GetNextToken();

            if (token == null)
            {
                return(false);
            }

            // the first line of any message is never empty
            if (token.IsEmpty)
            {
                return(false);
            }

            // success! store the first line of the message
            message._firstLine = token.ToString(HttpUtils.Encoding);

            // save the offset into the data where the headers start
            _headerOffsetStart = _parser.Index;

            // change state to processing headers!
            _state = Processing.HeaderLine;

            return(true);
        }
        /// <summary>
        /// Tries to parse out a line of text from the underlying byte array by searching for '\r\n' combinations.
        /// </summary>
        /// <returns></returns>
        public HttpByteParserToken GetNextToken()
        {
            HttpByteParserToken line = null;

            // Start searching at our last index, and work our way towards the end of our bytes (|| until we strike a valid CRLF combination)
            for (int i = _index; i < _bytes.Length; i++)
            {
                // if the current byte is the LF char
                if (_bytes[i] == (byte)'\n')
                {
                    // try backing up and seeing if the one before was a CR char (the length of it will be our current position - where we started
                    int length = i - _index;

                    // if there is a non-zero length, and the char matches (back the length up one more so that we do not include the CR char
                    if (length > 0 && _bytes[i - 1] == (byte)'\r')
                    {
                        length--;
                    }

                    // now that we have a line (and we have possibly excluded the '\r' from the line using the code above :P let's construct a new line around these bytes
                    line = new HttpByteParserToken(_bytes, _index, length);

                    // now push the index ahead so that we're sitting ahead of the '\n' that we stopped on
                    _index = i + 1;

                    // return the object
                    return(line);
                }
            }

            return(null);
        }
Example #5
0
        /// <summary>
        /// Tries to parse a header line for the message
        /// </summary>
        /// <returns></returns>
        private bool TryAndParseHeaderLine(ref HttpMessage message, EventHandler <HttpMessageProgressEventArgs> onProgress, object stateObject)
        {
            // try and parse a token from the data
            HttpByteParserToken token = _parser.GetNextToken();

            if (token == null)
            {
                return(false);
            }

            // the first line of any message is never empty
            if (token.IsEmpty)
            {
                // save the offset into the data where the headers end
                _headerOffsetEnd = _parser.Index;

                // determine if the body is chunked, as we have all the headers now we can determine how the message is going to come in
                if (message.IsChunked)
                {
                    // change state to processing a chunk size line
                    _state = Processing.ChunkSizeLine;
                }
                else
                {
                    // change state to processing the body
                    _state = Processing.Body;
                }

                // notify the callback that we have received the headers
                this.OnProgress(onProgress, this, new HttpMessageProgressEventArgs(message, true, new byte[] {}, _receivedBytes.Length, stateObject));

                // we're done with the headers
                return(true);
            }

            // success! this line is a header
            string line = token.ToString(HttpUtils.Encoding);

            // parse the line into a new header
            HttpHeader header = HttpHeader.Parse(line);

            // save the header
            message.Headers.Add(header);

            return(true);
        }
Example #6
0
        /// <summary>
        /// Splits the byte array into an array of chunks using the specified character separator
        /// </summary>
        /// <param name="separator"></param>
        /// <returns></returns>
        public HttpByteParserToken[] Split(char separator)
        {
            ArrayList list = new ArrayList();

            int pos = 0;

            while (pos < _bytes.Length)
            {
                int i = IndexOf(separator, pos);

                if (i < 0)
                {
                    break;
                }

                list.Add(Substring(pos, i - pos));
                pos = i + 1;

                while (this[pos] == (byte)separator && pos < _bytes.Length)
                {
                    pos++;
                }
            }

            if (pos < _bytes.Length)
            {
                list.Add(Substring(pos));
            }

            int n = list.Count;

            HttpByteParserToken[] result = new HttpByteParserToken[n];

            for (int i = 0; i < n; i++)
            {
                result[i] = (HttpByteParserToken)list[i];
            }

            return(result);
        }
		/// <summary>
		/// Tries to parse out a line of text from the underlying byte array by searching for '\r\n' combinations.
		/// </summary>
		/// <returns></returns>
		public HttpByteParserToken GetNextToken() 
		{
			HttpByteParserToken line = null;
			// Start searching at our last index, and work our way towards the end of our bytes (|| until we strike a valid CRLF combination)						
			for (int i = _index; i < _bytes.Length; i++) 
			{
				// if the current byte is the LF char
				if (_bytes[i] == (byte)'\n') 
				{
					// try backing up and seeing if the one before was a CR char (the length of it will be our current position - where we started
					int length = i - _index;
					
					// if there is a non-zero length, and the char matches (back the length up one more so that we do not include the CR char
					if (length > 0 && _bytes[i-1] == (byte)'\r')
						length--;					

					// now that we have a line (and we have possibly excluded the '\r' from the line using the code above :P let's construct a new line around these bytes
					line = new HttpByteParserToken(_bytes, _index, length);					

                    // now push the index ahead so that we're sitting ahead of the '\n' that we stopped on
					_index = i+1;

					// return the object
					return line;
				}
			}
			
			return null;
		}
		/// <summary>
		/// Splits the byte array into an array of chunks using the specified character separator
		/// </summary>
		/// <param name="separator"></param>
		/// <returns></returns>
		public HttpByteParserToken[] Split(char separator) 
		{
			ArrayList list = new ArrayList();

			int pos = 0;

			while (pos <  _bytes.Length) 
			{
				int i = IndexOf(separator, pos);

				if (i < 0)
					break;

				list.Add(Substring(pos, i-pos));
				pos = i+1;

				while (this[pos] == (byte)separator && pos <  _bytes.Length)
					pos++;
			}

			if (pos <  _bytes.Length)
				list.Add(Substring(pos));

			int n = list.Count;
			HttpByteParserToken[] result = new HttpByteParserToken[n];
            
			for (int i = 0; i < n; i++)
				result[i] = (HttpByteParserToken)list[i];
            
			return result;
		}