WebHeaderCollection ReadHeaders (HttpWebRequest request, Stream stream, out byte [] retBuffer, out int status)
{
retBuffer = null;
status = 200;
byte [] buffer = new byte [1024];
MemoryStream ms = new MemoryStream ();
bool gotStatus = false;
WebHeaderCollection headers = null;
while (true) {
int n = stream.Read (buffer, 0, 1024);
if (n == 0) {
HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders");
return null;
}
ms.Write (buffer, 0, n);
int start = 0;
string str = null;
headers = new WebHeaderCollection ();
while (ReadLine (ms.GetBuffer (), ref start, (int) ms.Length, ref str)) {
if (str == null) {
if (ms.Length - start > 0) {
retBuffer = new byte [ms.Length - start];
Buffer.BlockCopy (ms.GetBuffer (), start, retBuffer, 0, retBuffer.Length);
}
return headers;
}
if (gotStatus) {
headers.Add (str);
continue;
}
int spaceidx = str.IndexOf (' ');
if (spaceidx == -1) {
HandleError (WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders2");
return null;
}
status = (int) UInt32.Parse (str.Substring (spaceidx + 1, 3));
gotStatus = true;
}
}
}