Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
2004-11-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
	* ChunkStream.cs: simplified condition for WantMore property.

	* WebConnection.cs: in Connect(), finish any pending reads we might
	have for chunked data. Ignore possible blank lines at the very
	beginning of the server response. Honor user set KeepAlive in
	HttpWebRequest for HTTP/1.1 connections. Thanks to Eyal Alayuf for
	his suggestions and code.

svn path=/trunk/mcs/; revision=35934
  • Loading branch information
gonzalop committed Nov 10, 2004
1 parent 56e8a49 commit ccc7c27
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
10 changes: 10 additions & 0 deletions mcs/class/System/System.Net/ChangeLog
@@ -1,3 +1,13 @@
2004-11-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>

* ChunkStream.cs: simplified condition for WantMore property.

* WebConnection.cs: in Connect(), finish any pending reads we might
have for chunked data. Ignore possible blank lines at the very
beginning of the server response. Honor user set KeepAlive in
HttpWebRequest for HTTP/1.1 connections. Thanks to Eyal Alayuf for
his suggestions and code.

2004-11-01 Gonzalo Paniagua Javier <gonzalo@ximian.com>

* HttpWebResponse.cs: don't lock up when the cookie received ends with
Expand Down
2 changes: 1 addition & 1 deletion mcs/class/System/System.Net/ChunkStream.cs
Expand Up @@ -167,7 +167,7 @@ void InternalWrite (byte [] buffer, ref int offset, int size)
}

public bool WantMore {
get { return (chunkRead != chunkSize || chunkSize != 0 || (chunkSize == 0 && state != State.None)); }
get { return (chunkRead != chunkSize || chunkSize != 0 || state != State.None); }
}

public int ChunkLeft {
Expand Down
38 changes: 30 additions & 8 deletions mcs/class/System/System.Net/WebConnection.cs
Expand Up @@ -94,17 +94,18 @@ void Connect ()
lock (this) {
if (socket != null && socket.Connected && status == WebExceptionStatus.Success) {
// Take the chunked stream to the expected state (State.None)
while (chunkedRead && chunkStream.WantMore && Read (buffer, 0, buffer.Length) > 0);
reused = true;
return;
if (CompleteChunkedRead ()) {
reused = true;
return;
}
}

reused = false;
if (socket != null) {
socket.Close();
socket = null;
}

chunkStream = null;
IPHostEntry hostEntry = sPoint.HostEntry;

Expand Down Expand Up @@ -218,6 +219,7 @@ bool CreateStream (HttpWebRequest request)
{
try {
NetworkStream serverStream = new NetworkStream (socket, false);

if (request.Address.Scheme == Uri.UriSchemeHttps) {
ssl = true;
EnsureSSLStreamAvailable ();
Expand Down Expand Up @@ -301,7 +303,6 @@ static void ReadDone (IAsyncResult result)
return;
}

//Console.WriteLine (System.Text.Encoding.Default.GetString (cnc.buffer, 0, nread + cnc.position));
int pos = -1;
nread += cnc.position;
if (cnc.readState == ReadState.None) {
Expand Down Expand Up @@ -399,12 +400,19 @@ int GetResponse (byte [] buffer, int max)
string line = null;
bool lineok = false;
bool isContinue = false;
bool emptyFirstLine = false;
do {
if (readState == ReadState.None) {
lineok = ReadLine (buffer, ref pos, max, ref line);
if (!lineok)
return -1;

if (line == null) {
emptyFirstLine = true;
continue;
}
emptyFirstLine = false;

readState = ReadState.Status;

string [] parts = line.Split (' ');
Expand All @@ -429,6 +437,7 @@ int GetResponse (byte [] buffer, int max)
return pos;
}

emptyFirstLine = false;
if (readState == ReadState.Status) {
readState = ReadState.Headers;
Data.Headers = new WebHeaderCollection ();
Expand Down Expand Up @@ -482,7 +491,7 @@ int GetResponse (byte [] buffer, int max)
return pos;
}
}
} while (isContinue == true);
} while (emptyFirstLine || isContinue);

return -1;
}
Expand All @@ -502,7 +511,6 @@ void InitConnection (object state, bool notUsed)
keepAlive = request.KeepAlive;
Data = new WebConnectionData ();
Data.request = request;

Connect ();
if (status != WebExceptionStatus.Success) {
request.SetWriteStreamError (status);
Expand Down Expand Up @@ -559,7 +567,7 @@ internal void NextRead ()
busy = false;
string header = (sPoint.UsesProxy) ? "Proxy-Connection" : "Connection";
string cncHeader = (Data.Headers != null) ? Data.Headers [header] : null;
bool keepAlive = (Data.Version == HttpVersion.Version11);
bool keepAlive = (Data.Version == HttpVersion.Version11 && this.keepAlive);
if (cncHeader != null) {
cncHeader = cncHeader.ToLower ();
keepAlive = (this.keepAlive && cncHeader.IndexOf ("keep-alive") != -1);
Expand Down Expand Up @@ -678,6 +686,20 @@ internal int EndRead (IAsyncResult result)
return nstream.EndRead (result);
}

bool CompleteChunkedRead()
{
if (!chunkedRead || chunkStream == null)
return true;

while (chunkStream.WantMore) {
int nbytes = nstream.Read (buffer, 0, buffer.Length);
if (nbytes <= 0)
return false; // Socket was disconnected
chunkStream.Write(buffer, 0, nbytes);
}

return true;
}
internal IAsyncResult BeginWrite (byte [] buffer, int offset, int size, AsyncCallback cb, object state)
{
IAsyncResult result = null;
Expand Down

0 comments on commit ccc7c27

Please sign in to comment.