[Mono-list] initial HttpWebRequest/HttpWebResposne patch

Shahms E. King shahms@shahms.com
31 Aug 2002 22:32:07 -0700


--=-9FiNFFYYDAjXlb6XTL0Z
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Attached is a first run attempt at getting these to work, haven't done a
whole lot of testing, but the header properties get set correctly, the
HTTP version and response code are handled as well, what can I say, it
works, if not well.


--Shahms

--=-9FiNFFYYDAjXlb6XTL0Z
Content-Disposition: attachment; filename=mcs-HttpWeb.patch
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; name=mcs-HttpWeb.patch; charset=ISO-8859-1

? class/System/System.Net/.HttpWebResponse.cs.swp
Index: class/System/System.Net/HttpWebRequest.cs
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /mono/mcs/class/System/System.Net/HttpWebRequest.cs,v
retrieving revision 1.5
diff -u -r1.5 HttpWebRequest.cs
--- class/System/System.Net/HttpWebRequest.cs	28 May 2002 18:36:53 -0000	1.=
5
+++ class/System/System.Net/HttpWebRequest.cs	1 Sep 2002 05:31:23 -0000
@@ -56,9 +56,9 @@
 			this.requestUri =3D uri;
 			this.actualUri =3D uri;
 			this.webHeaders =3D new WebHeaderCollection (true);
-			// this.webHeaders.SetInternal ("Host", uri.Authority);
-			// this.webHeaders.SetInternal ("Date", DateTime.Now.ToUniversalTime ()=
.ToString ("r", null));
-			// this.webHeaders.SetInternal ("Expect", "100-continue");
+			this.webHeaders.SetInternal ("Host", uri.Authority);
+			this.webHeaders.SetInternal ("Date", DateTime.Now.ToUniversalTime ().To=
String ("r", null));
+			this.webHeaders.SetInternal ("Expect", "100-continue");
 			this.method =3D "GET";
 			this.version =3D HttpVersion.Version11;
 			this.proxy =3D GlobalProxySelection.Select;
@@ -451,7 +451,7 @@
 	=09
 		internal Stream GetRequestStreamInternal ()
 		{
-			this.requestStream =3D null;   // TODO: new HttpWebStream (this);
+			this.requestStream =3D new HttpWebStream (this);
 			return this.requestStream;
 		}
 	=09
@@ -512,7 +512,7 @@
 			if (requestEndEvent !=3D null) {
 				requestEndEvent.WaitOne ();
 			}
-			Stream responseStream =3D null; // TODO: new HttpWebStream (this);
+			Stream responseStream =3D new HttpWebStream (this);
  			this.webResponse =3D new HttpWebResponse (this.actualUri, method, resp=
onseStream);
  			return (WebResponse) this.webResponse;
 		}
@@ -557,16 +557,41 @@
 		// Private Classes
 	=09
 		// to catch the Close called on the NetworkStream
-		/*
-		internal class HttpWebStream : Stream
+		internal class HttpWebStream : NetworkStream
 		{
 			HttpWebRequest webRequest;
 		=09
-			internal HttpWebStream (HttpWebRequest webRequest)
-				: base (webRequest.RequestUri)
+			internal HttpWebStream (HttpWebRequest webRequest)=20
+				: base (HttpWebStream.CreateSocket (webRequest), true)
 			{
+				StreamWriter webWriter =3D null;
+				string headerValue =3D null;
+
+
+				webWriter =3D new StreamWriter (this);
+=09
+				webWriter.Write (webRequest.Method + " " + webRequest.actualUri.Absolu=
tePath + " HTTP/1.1\r\n");
+
+				foreach (string header in webRequest.webHeaders) {
+					headerValue =3D header + ": " + webRequest.webHeaders[header] + "\r\n=
";
+					webWriter.Write (headerValue);
+				}
+				webWriter.Write ("\r\n");
+				webWriter.Flush();
+
 				this.webRequest =3D webRequest;
 			}
+	=09
+			private static Socket CreateSocket (HttpWebRequest webRequest)
+			{
+				IPAddress hostAddr =3D Dns.Resolve (webRequest.actualUri.Host).Address=
List[0];
+				IPEndPoint endPoint =3D new IPEndPoint (hostAddr, webRequest.actualUri=
.Port);
+				Socket socket =3D new Socket(AddressFamily.InterNetwork, SocketType.St=
ream,
+					ProtocolType.Tcp);
+
+				socket.Connect (endPoint);
+				return socket;
+			}
 					   =09
 			public override void Close()=20
 			{
@@ -574,6 +599,5 @@
 				webRequest.Close ();
 			}
 		}	=09
-		*/
 	}
-}
\ No newline at end of file
+}
Index: class/System/System.Net/HttpWebResponse.cs
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /mono/mcs/class/System/System.Net/HttpWebResponse.cs,v
retrieving revision 1.4
diff -u -r1.4 HttpWebResponse.cs
--- class/System/System.Net/HttpWebResponse.cs	28 May 2002 18:36:53 -0000	1=
.4
+++ class/System/System.Net/HttpWebResponse.cs	1 Sep 2002 05:31:24 -0000
@@ -7,6 +7,7 @@
=20
 using System;
 using System.IO;
+using System.Text;
 using System.Runtime.Serialization;
=20
 namespace System.Net=20
@@ -29,13 +30,46 @@
 	=09
 		internal HttpWebResponse (Uri uri, string method, Stream responseStream)=
=20
 		{=20
+			StringBuilder value =3D null;
+			string last =3D null;
+			string line =3D null;
+			string[] protocol, header;
+
 			this.uri =3D uri;
 			this.method =3D method;
 			this.responseStream =3D responseStream;
+			this.webHeaders =3D new WebHeaderCollection();
+
+			line =3D ReadHttpLine(responseStream);
+			protocol =3D line.Split (new char[] {' '});
+		=09
+			switch (protocol[0]) {
+				case "HTTP/1.0":
+					this.version =3D HttpVersion.Version10;
+					break;
+				case "HTTP/1.1":
+					this.version =3D HttpVersion.Version11;
+					break;
+				default:
+					throw new WebException ("Unrecognized HTTP Version");
+			}
 		=09
-			// TODO: parse headers from responseStream
+			this.statusCode =3D int.Parse (protocol[1]);
+
+			while ((line =3D ReadHttpLine(responseStream)).Length !=3D 0) {
+				header =3D line.Split (new char[] {':'}, 2);
+				if (header.Length =3D=3D 2) { // new header
+					if (last !=3D null) // not the first header
+						this.webHeaders[last] =3D value.ToString();
+					last =3D header[0];
+					value =3D new StringBuilder (header[1].TrimStart());
+				}
+				else
+					value.Append (header[0].TrimStart());
+			}
 		=09
-			this.statusCode =3D HttpStatusCode.OK;
+			this.webHeaders[last] =3D value.ToString(); // otherwise we miss the la=
st header
+			// TODO: parse cookies from headers
 		}
 	=09
 		protected HttpWebResponse (SerializationInfo serializationInfo, Streamin=
gContext streamingContext)
@@ -267,5 +301,22 @@
 			if (disposed)
 				throw new ObjectDisposedException (GetType ().FullName);
 		}
+
+		private static string ReadHttpLine (Stream stream)
+		{
+			StringBuilder line =3D new StringBuilder();
+			byte[] buf =3D new byte[1]; // one at a time to not snarf too much
+		=09
+			while (stream.Read (buf, 0, buf.Length) !=3D 0) {
+				if (buf[0] =3D=3D '\r') {
+					stream.ReadByte (); //assuming it's \r\n
+					return line.ToString();
+				}
+
+				line.Append (Convert.ToChar(buf[0]));
+			}
+		=09
+			return line.ToString();
+		}
 	}=09
-}
\ No newline at end of file
+}

--=-9FiNFFYYDAjXlb6XTL0Z


--=-9FiNFFYYDAjXlb6XTL0Z--