I’m creating simple proxy server but I faced a strange situation, I’ve following code :
var clientRequestStream = _tcpClient.GetStream();
var requestHeader = clientRequestStream.GetUtf8String();
GetUtf8String is a extension method for Stream class which reads stream (contains HttpRequest headers). I need to extract those headers to access Host and Requested Url. Once reading NetworkStream is done. I need to perform seek operation and set its clientRequestStream.Position = 0; because I’ve to read that stream and write it on another remote NetworkStream.
I don’t know how should I solve this problem.Any advice will be helpful.
Edit: I also tried copy NetworkStream to MemoryStream then perform seek operation on MemoryStream, There is no exception but when I want to read from NetworkStream its buffer always is always empty.
Also I used reflector to see what happens inside Stream.CopyTo. See below code :
private void InternalCopyTo(Stream destination, int bufferSize)
{
int num;
byte[] buffer = new byte[bufferSize];
while ((num = this.Read(buffer, 0, buffer.Length)) != 0)
{
destination.Write(buffer, 0, num);
}
}
This is what CopyTo doing. Even if I use CopyTo Problem is still unresolved. Because it reads source (Here NetworkStream) to the end. I there another way to handle this situation?
Are you reading from this stream until the end? If so, I suggest you just copy the entire contents into a
MemoryStream, then you can seek on that to your heart’s content. In .NET 4 it’s particularly easy withStream.CopyTo:It makes sense for
NetworkStreamnot to be seekable – it’s just a stream of data that a server is giving to you. Unless you can tell the server to rewind (which only even makes sense in some situations) there’s no way of seeking unless something buffers as much data as you need to rewind – which is basically what copying to aMemoryStreamdoes, in a pretty brute-force fashion.