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
? class/System/System.Net/.HttpWebResponse.cs.swp
Index: class/System/System.Net/HttpWebRequest.cs
===================================================================
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 = uri;
this.actualUri = uri;
this.webHeaders = 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 ().ToString ("r", null));
+ this.webHeaders.SetInternal ("Expect", "100-continue");
this.method = "GET";
this.version = HttpVersion.Version11;
this.proxy = GlobalProxySelection.Select;
@@ -451,7 +451,7 @@
internal Stream GetRequestStreamInternal ()
{
- this.requestStream = null; // TODO: new HttpWebStream (this);
+ this.requestStream = new HttpWebStream (this);
return this.requestStream;
}
@@ -512,7 +512,7 @@
if (requestEndEvent != null) {
requestEndEvent.WaitOne ();
}
- Stream responseStream = null; // TODO: new HttpWebStream
(this);
+ Stream responseStream = new HttpWebStream (this);
this.webResponse = new HttpWebResponse (this.actualUri,
method, responseStream);
return (WebResponse) this.webResponse;
}
@@ -557,16 +557,41 @@
// Private Classes
// to catch the Close called on the NetworkStream
- /*
- internal class HttpWebStream : Stream
+ internal class HttpWebStream : NetworkStream
{
HttpWebRequest webRequest;
- internal HttpWebStream (HttpWebRequest webRequest)
- : base (webRequest.RequestUri)
+ internal HttpWebStream (HttpWebRequest webRequest)
+ : base (HttpWebStream.CreateSocket (webRequest), true)
{
+ StreamWriter webWriter = null;
+ string headerValue = null;
+
+
+ webWriter = new StreamWriter (this);
+
+ webWriter.Write (webRequest.Method + " " +
+webRequest.actualUri.AbsolutePath + " HTTP/1.1\r\n");
+
+ foreach (string header in webRequest.webHeaders) {
+ headerValue = header + ": " +
+webRequest.webHeaders[header] + "\r\n";
+ webWriter.Write (headerValue);
+ }
+ webWriter.Write ("\r\n");
+ webWriter.Flush();
+
this.webRequest = webRequest;
}
+
+ private static Socket CreateSocket (HttpWebRequest webRequest)
+ {
+ IPAddress hostAddr = Dns.Resolve
+(webRequest.actualUri.Host).AddressList[0];
+ IPEndPoint endPoint = new IPEndPoint (hostAddr,
+webRequest.actualUri.Port);
+ Socket socket = new Socket(AddressFamily.InterNetwork,
+SocketType.Stream,
+ ProtocolType.Tcp);
+
+ socket.Connect (endPoint);
+ return socket;
+ }
public override void Close()
{
@@ -574,6 +599,5 @@
webRequest.Close ();
}
}
- */
}
-}
\ No newline at end of file
+}
Index: class/System/System.Net/HttpWebResponse.cs
===================================================================
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 @@
using System;
using System.IO;
+using System.Text;
using System.Runtime.Serialization;
namespace System.Net
@@ -29,13 +30,46 @@
internal HttpWebResponse (Uri uri, string method, Stream
responseStream)
{
+ StringBuilder value = null;
+ string last = null;
+ string line = null;
+ string[] protocol, header;
+
this.uri = uri;
this.method = method;
this.responseStream = responseStream;
+ this.webHeaders = new WebHeaderCollection();
+
+ line = ReadHttpLine(responseStream);
+ protocol = line.Split (new char[] {' '});
+
+ switch (protocol[0]) {
+ case "HTTP/1.0":
+ this.version = HttpVersion.Version10;
+ break;
+ case "HTTP/1.1":
+ this.version = HttpVersion.Version11;
+ break;
+ default:
+ throw new WebException ("Unrecognized HTTP
+Version");
+ }
- // TODO: parse headers from responseStream
+ this.statusCode = int.Parse (protocol[1]);
+
+ while ((line = ReadHttpLine(responseStream)).Length != 0) {
+ header = line.Split (new char[] {':'}, 2);
+ if (header.Length == 2) { // new header
+ if (last != null) // not the first header
+ this.webHeaders[last] =
+value.ToString();
+ last = header[0];
+ value = new StringBuilder
+(header[1].TrimStart());
+ }
+ else
+ value.Append (header[0].TrimStart());
+ }
- this.statusCode = HttpStatusCode.OK;
+ this.webHeaders[last] = value.ToString(); // otherwise we miss
+the last header
+ // TODO: parse cookies from headers
}
protected HttpWebResponse (SerializationInfo serializationInfo,
StreamingContext streamingContext)
@@ -267,5 +301,22 @@
if (disposed)
throw new ObjectDisposedException (GetType
().FullName);
}
+
+ private static string ReadHttpLine (Stream stream)
+ {
+ StringBuilder line = new StringBuilder();
+ byte[] buf = new byte[1]; // one at a time to not snarf too
+much
+
+ while (stream.Read (buf, 0, buf.Length) != 0) {
+ if (buf[0] == '\r') {
+ stream.ReadByte (); //assuming it's \r\n
+ return line.ToString();
+ }
+
+ line.Append (Convert.ToChar(buf[0]));
+ }
+
+ return line.ToString();
+ }
}
-}
\ No newline at end of file
+}