IICuX
February 16th, 2010, 02:37 PM
hi all, i have trouble with my application, i writen my personal cache using sockets and streams, but i have big trouble with unpack GZip, this string int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length); is trouble, when i try read from stream, i have an Exception, that the GZip header magic number is not corrent. InvalidDataException. here is my code...
public long WriteURL()
{
long num1 = 0;
string host = new Uri(_url).Host;
IPHostEntry ipAddress = Dns.GetHostEntry(host);
IPEndPoint ip = new IPEndPoint(ipAddress.AddressList[0], 80);
using (Socket s = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
s.Connect(ip);
using (NetworkStream n = new NetworkStream(s))
{
if (HttpRequestType == "POST")
{
//
}
bool saveItAtCache = false;
bool existsAtCache = false;
byte[] cachedFile = null;
string ext = Path.GetExtension(_url).ToLower();
if (!_url.Contains(".php") && ".gif.jpg.swf.js.css.png.html".IndexOf(ext) != -1 && ext != "")
{
saveItAtCache = true;
cachedFile = cache.GetFile(_url);
existsAtCache = (cachedFile != null);
}
if (existsAtCache)
{
writeSuccess(cachedFile.Length, null);
socket.Send(cachedFile);
}
else
{
SendRequest(n, new[] { HttpQuery });
Dictionary<string, string> headers = new Dictionary<string, string>();
while (true)
{
string line = ReadLine(n);
if (Equals(line.Length, 0))
{
break;
}
int index = line.IndexOf(':');
if (!headers.ContainsKey(line.Substring(0, index)))
{
headers.Add(line.Substring(0, index), line.Substring(index + 2));
}
}
string str = "";
string contentEncoding;
if (headers.TryGetValue("Content-Encoding", out contentEncoding))
{
Stream responseStream = n;
if (contentEncoding.Equals("gzip"))
{
responseStream = new GZipStream(responseStream, CompressionMode.Decompress, true);
}
else if (contentEncoding.Equals("deflate"))
{
responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
}
var memStream = new MemoryStream();
var respBuffer = new byte[4096];
try
{
int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
while (bytesRead > 0)
{
memStream.Write(respBuffer, 0, bytesRead);
bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
}
}
finally
{
responseStream.Close();
}
str = encoding.GetString(memStream.ToArray());
ManageCookies(_headers["Set-Cookie"], _headers["Host"]);
cachedFile = encoding.GetBytes(str);
if (saveItAtCache)
{
cache.Store(_url, cachedFile);
}
num1 = str.Length;
}
else
{
while (true)
{
string line = ReadLine(n);
if (line == null)
{
str.Remove(str.Length - 1, 1);
break;
}
num1 += line.Length;
str += line;
}
}
ManageCookies(_headers["Set-Cookie"], _headers["Host"]);
byte[] buffer3 = encoding.GetBytes(str);
writeSuccess(buffer3.Length, _headers["Set-Cookie"]);
socket.Send(buffer3);
}
}
}
return num1;
}
void SendRequest(Stream stream, IEnumerable<string> request)
{
if (request != null)
{
foreach (var r in request)
{
byte[] data = encoding.GetBytes(r);
stream.Write(data, 0, data.Length);
stream.Write(LineTerminator, 0, 2);
}
stream.Write(LineTerminator, 0, 2);
// Eat response
string response = ReadLine(stream);
Console.WriteLine(response);
}
}
string ReadLine(Stream stream)
{
List<byte> lineBuffer = new List<byte>();
while (true)
{
try
{
int b = stream.ReadByte();
if (b == -1)
{
return null;
}
if (b == 10)
break;
if (b!=13)
lineBuffer.Add((byte)b);
}
catch (Exception)
{
return null;
}
}
return encoding.GetString(lineBuffer.ToArray());
}
public long WriteURL()
{
long num1 = 0;
string host = new Uri(_url).Host;
IPHostEntry ipAddress = Dns.GetHostEntry(host);
IPEndPoint ip = new IPEndPoint(ipAddress.AddressList[0], 80);
using (Socket s = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
s.Connect(ip);
using (NetworkStream n = new NetworkStream(s))
{
if (HttpRequestType == "POST")
{
//
}
bool saveItAtCache = false;
bool existsAtCache = false;
byte[] cachedFile = null;
string ext = Path.GetExtension(_url).ToLower();
if (!_url.Contains(".php") && ".gif.jpg.swf.js.css.png.html".IndexOf(ext) != -1 && ext != "")
{
saveItAtCache = true;
cachedFile = cache.GetFile(_url);
existsAtCache = (cachedFile != null);
}
if (existsAtCache)
{
writeSuccess(cachedFile.Length, null);
socket.Send(cachedFile);
}
else
{
SendRequest(n, new[] { HttpQuery });
Dictionary<string, string> headers = new Dictionary<string, string>();
while (true)
{
string line = ReadLine(n);
if (Equals(line.Length, 0))
{
break;
}
int index = line.IndexOf(':');
if (!headers.ContainsKey(line.Substring(0, index)))
{
headers.Add(line.Substring(0, index), line.Substring(index + 2));
}
}
string str = "";
string contentEncoding;
if (headers.TryGetValue("Content-Encoding", out contentEncoding))
{
Stream responseStream = n;
if (contentEncoding.Equals("gzip"))
{
responseStream = new GZipStream(responseStream, CompressionMode.Decompress, true);
}
else if (contentEncoding.Equals("deflate"))
{
responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
}
var memStream = new MemoryStream();
var respBuffer = new byte[4096];
try
{
int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
while (bytesRead > 0)
{
memStream.Write(respBuffer, 0, bytesRead);
bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
}
}
finally
{
responseStream.Close();
}
str = encoding.GetString(memStream.ToArray());
ManageCookies(_headers["Set-Cookie"], _headers["Host"]);
cachedFile = encoding.GetBytes(str);
if (saveItAtCache)
{
cache.Store(_url, cachedFile);
}
num1 = str.Length;
}
else
{
while (true)
{
string line = ReadLine(n);
if (line == null)
{
str.Remove(str.Length - 1, 1);
break;
}
num1 += line.Length;
str += line;
}
}
ManageCookies(_headers["Set-Cookie"], _headers["Host"]);
byte[] buffer3 = encoding.GetBytes(str);
writeSuccess(buffer3.Length, _headers["Set-Cookie"]);
socket.Send(buffer3);
}
}
}
return num1;
}
void SendRequest(Stream stream, IEnumerable<string> request)
{
if (request != null)
{
foreach (var r in request)
{
byte[] data = encoding.GetBytes(r);
stream.Write(data, 0, data.Length);
stream.Write(LineTerminator, 0, 2);
}
stream.Write(LineTerminator, 0, 2);
// Eat response
string response = ReadLine(stream);
Console.WriteLine(response);
}
}
string ReadLine(Stream stream)
{
List<byte> lineBuffer = new List<byte>();
while (true)
{
try
{
int b = stream.ReadByte();
if (b == -1)
{
return null;
}
if (b == 10)
break;
if (b!=13)
lineBuffer.Add((byte)b);
}
catch (Exception)
{
return null;
}
}
return encoding.GetString(lineBuffer.ToArray());
}