public int Read(byte[] buffer, int position) { int read = 0; HeaderReaderState state = HeaderReaderState.Reading; for (int i = position; i < buffer.Length && state != HeaderReaderState.FoundSecondLF; i++) { char current = (char)buffer[i]; if (current == '\r') { switch (state) { case HeaderReaderState.Reading: state = HeaderReaderState.FoundFirstCR; break; case HeaderReaderState.FoundFirstCR: case HeaderReaderState.FoundFirstLF: state = HeaderReaderState.FoundSecondCR; break; } } else if (current == '\n') { switch (state) { case HeaderReaderState.Reading: case HeaderReaderState.FoundFirstCR: state = HeaderReaderState.FoundFirstLF; break; case HeaderReaderState.FoundFirstLF: case HeaderReaderState.FoundSecondCR: state = HeaderReaderState.FoundSecondLF; break; } } else { state = HeaderReaderState.Reading; } read++; } headers.Append(encoding.GetString(buffer, position, read)); // If we found the blank line if (state == HeaderReaderState.FoundSecondLF) { _headerComplete = true; // Back off the last two lines string tail = headers.ToString(headers.Length - 4, 4); if (tail[2] == '\r') { headers.Length -= 4; } else if (tail[2] == '\n') { headers.Length -= 2; } } return(read); }
public int Read(byte[] buffer, int position) { int count = 0; HeaderReaderState reading = HeaderReaderState.Reading; for (int i = position; (i < buffer.Length) && (reading != HeaderReaderState.FoundSecondLF); i++) { switch (((char)buffer[i])) { case '\r': switch (reading) { case HeaderReaderState.Reading: reading = HeaderReaderState.FoundFirstCR; break; case HeaderReaderState.FoundFirstCR: case HeaderReaderState.FoundFirstLF: reading = HeaderReaderState.FoundSecondCR; break; } break; case '\n': switch (reading) { case HeaderReaderState.Reading: case HeaderReaderState.FoundFirstCR: reading = HeaderReaderState.FoundFirstLF; break; case HeaderReaderState.FoundFirstLF: case HeaderReaderState.FoundSecondCR: reading = HeaderReaderState.FoundSecondLF; break; } break; default: reading = HeaderReaderState.Reading; break; } count++; } this.headers.Append(this.encoding.GetString(buffer, position, count)); if (reading == HeaderReaderState.FoundSecondLF) { this._headerComplete = true; string str = this.headers.ToString(this.headers.Length - 4, 4); if (str[2] == '\r') { this.headers.Length -= 4; return(count); } if (str[2] == '\n') { this.headers.Length -= 2; } } return(count); }