Pārlūkot izejas kodu

finished WebRequest.cs

svn path=/trunk/mcs/; revision=4524
Lawrence Pit 23 gadi atpakaļ
vecāks
revīzija
3576832989

+ 9 - 0
mcs/class/System/System.Net/ChangeLog

@@ -1,3 +1,12 @@
+2002-05-11  Lawrence Pit <[email protected]>
+
+	* Finished WebRequest.cs
+	* FileWebRequest.cs and HttpWebRequest.cs stubs added
+
+2002-05-09  Lawrence Pit <[email protected]>
+
+	* Rewrote IPAddress.Parse method, passing all unit tests
+
 2002-05-09  Lawrence Pit <[email protected]>
 
 	* fixed bug in IPEndPoint.Equals method

+ 44 - 0
mcs/class/System/System.Net/FileWebRequest.cs

@@ -0,0 +1,44 @@
+//
+// System.Net.FileWebRequest
+//
+// Author:
+//   Lawrence Pit ([email protected])
+//
+
+using System;
+using System.Collections;
+using System.IO;
+using System.Runtime.Serialization;
+
+namespace System.Net 
+{
+	[Serializable]
+	public class FileWebRequest : WebRequest, ISerializable
+	{
+		private Uri uri;
+		
+		// Constructors
+		
+		internal FileWebRequest (Uri uri) 
+		{ 
+			this.uri = uri;
+		}		
+		
+		protected FileWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) 
+		{
+			throw new NotSupportedException ();
+		}
+		
+		// Properties
+		
+
+
+		// Methods
+		
+		void ISerializable.GetObjectData (SerializationInfo serializationInfo,
+		   				  StreamingContext streamingContext)
+		{
+			throw new NotSupportedException ();
+		}
+	}
+}

+ 44 - 0
mcs/class/System/System.Net/HttpWebRequest.cs

@@ -0,0 +1,44 @@
+//
+// System.Net.HttpWebRequest
+//
+// Author:
+//   Lawrence Pit ([email protected])
+//
+
+using System;
+using System.Collections;
+using System.IO;
+using System.Runtime.Serialization;
+
+namespace System.Net 
+{
+	[Serializable]
+	public class HttpWebRequest : WebRequest, ISerializable
+	{
+		private Uri uri;
+		
+		// Constructors
+		
+		internal HttpWebRequest (Uri uri) 
+		{ 
+			this.uri = uri;
+		}		
+		
+		protected HttpWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) 
+		{
+			throw new NotSupportedException ();
+		}
+		
+		// Properties
+		
+
+
+		// Methods
+		
+		void ISerializable.GetObjectData (SerializationInfo serializationInfo,
+		   				  StreamingContext streamingContext)
+		{
+			throw new NotSupportedException ();
+		}
+	}
+}

+ 74 - 8
mcs/class/System/System.Net/WebRequest.cs

@@ -6,6 +6,8 @@
 //
 
 using System;
+using System.Collections;
+using System.Collections.Specialized;
 using System.IO;
 using System.Runtime.Serialization;
 
@@ -14,11 +16,41 @@ namespace System.Net
 	[Serializable]
 	public abstract class WebRequest : MarshalByRefObject, ISerializable
 	{
+		private static HybridDictionary prefixes;
+		
+		static WebRequest () {
+			prefixes = new HybridDictionary (3, true);
+			RegisterPrefix ("file", new FileWebRequestCreator ());
+			RegisterPrefix ("http", new HttpWebRequestCreator ());
+			RegisterPrefix ("https", new HttpWebRequestCreator ());
+		}
+		
+		internal class HttpWebRequestCreator : IWebRequestCreate
+		{
+			internal HttpWebRequestCreator () { }
+			
+			public WebRequest Create (Uri uri) 
+			{
+				return new HttpWebRequest (uri);
+			}
+		}
+
+		internal class FileWebRequestCreator : IWebRequestCreate
+		{
+			internal FileWebRequestCreator () { }
+			
+			public WebRequest Create (Uri uri) 
+			{
+				return new FileWebRequest (uri);
+			}
+		}
+
+		
 		// Constructors
 		
-		protected WebRequest () {}		
+		protected WebRequest () { }		
 		
-		protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
+		protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) 
 		{
 			throw new NotSupportedException ();
 		}
@@ -99,20 +131,18 @@ namespace System.Net
 			return Create (new Uri (requestUriString));
 		}
 				
-		[MonoTODO]
 		public static WebRequest Create (Uri requestUri) 
 		{
 			if (requestUri == null)
 				throw new ArgumentNullException ("requestUri");
-			throw new NotImplementedException ();
+			return GetCreator (requestUri.AbsoluteUri).Create (requestUri);
 		}
 		
-		[MonoTODO]
 		public static WebRequest CreateDefault (Uri requestUri)
 		{
 			if (requestUri == null)
 				throw new ArgumentNullException ("requestUri");
-			throw new NotImplementedException ();			
+			return GetCreator (requestUri.Scheme).Create (requestUri);
 		}
 
 		public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)
@@ -141,10 +171,46 @@ namespace System.Net
 			throw new NotSupportedException ();
 		}
 
-		[MonoTODO]		
 		public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)
 		{
-			throw new NotImplementedException ();			
+			if (prefix == null)
+				throw new ArgumentNullException("prefix");
+			if (creator == null)
+				throw new ArgumentNullException("creator");			
+			
+			lock (prefixes.SyncRoot) {
+				if (prefixes.Contains (prefix))
+					return false;
+				prefixes.Add (prefix.ToLower (), creator);
+			}
+			return true;
+		}
+		
+		private static IWebRequestCreate GetCreator (string prefix)
+		{
+			int longestPrefix = -1;
+			IWebRequestCreate creator = null;
+
+			prefix = prefix.ToLower ();
+
+			IDictionaryEnumerator e = prefixes.GetEnumerator ();
+			while (e.MoveNext ()) {
+				string key = e.Key as string;
+
+				if (key.Length <= longestPrefix) 
+					continue;
+				
+				if (!prefix.StartsWith (key))
+					continue;					
+					
+				longestPrefix = key.Length;
+				creator = (IWebRequestCreate) e.Value;
+			}
+			
+			if (creator == null) 
+				throw new NotSupportedException (prefix);
+				
+			return creator;
 		}
 	}
 }

+ 1 - 0
mcs/class/System/Test/System.Net/AllTests.cs

@@ -27,6 +27,7 @@ namespace MonoTests.System.Net {
                                 suite.AddTest (CookieCollectionTest.Suite);
                                 suite.AddTest (CredentialCacheTest.Suite);
                                 //suite.AddTest (CookieContainerTest.Suite);
+                                suite.AddTest (WebRequestTest.Suite);
 				return suite;
                         }
                 }

+ 4 - 0
mcs/class/System/Test/System.Net/ChangeLog

@@ -1,3 +1,7 @@
+2002-05-11  Lawrence Pit <[email protected]>
+
+	* WebRequestTest.cs: added
+
 2002-05-09  Lawrence Pit <[email protected]>
 
 	* Added tests to IPAddressTest.cs

+ 122 - 0
mcs/class/System/Test/System.Net/WebRequestTest.cs

@@ -0,0 +1,122 @@
+//
+// WebRequestTest.cs - NUnit Test Cases for System.Net.WebRequest
+//
+// Author:
+//   Lawrence Pit ([email protected])
+//
+
+using NUnit.Framework;
+using System;
+using System.Net;
+using System.Collections;
+using System.Security;
+using System.Security.Permissions;
+
+namespace MonoTests.System.Net
+{
+
+public class WebRequestTest : TestCase
+{
+        public WebRequestTest () :
+                base ("[MonoTests.System.Net.WebRequestTest]") {}
+
+        public WebRequestTest (string name) : base (name) {}
+
+        protected override void SetUp () {}
+
+        protected override void TearDown () {}
+
+        public static ITest Suite
+        {
+                get {
+                        return new TestSuite (typeof (WebRequestTest));
+                }
+        }
+        
+        public void TestAll ()
+        {
+		WebRequest req = WebRequest.Create ("http://www.contoso.com");
+		Assert ("#1", req is HttpWebRequest);
+		req = WebRequest.Create ("https://www.contoso.com");
+		Assert ("#2", req is HttpWebRequest);
+		req = WebRequest.Create ("file://www.contoso.com");
+		Assert ("#3", req is FileWebRequest);
+		
+		WebRequest.RegisterPrefix ("http://www.contoso.com", new TestWebRequestCreator ());
+		bool ret = WebRequest.RegisterPrefix ("http://WWW.contoso.com", new TestWebRequestCreator ());
+		AssertEquals ("#4a", false, ret);
+		ret = WebRequest.RegisterPrefix ("http://www.contoso.com/foo/bar", new TestWebRequestCreator2 ());
+		AssertEquals ("#4b", true, ret);
+		ret = WebRequest.RegisterPrefix ("http://www", new TestWebRequestCreator3 ());
+		AssertEquals ("#4c", true, ret);
+
+		req = WebRequest.Create ("http://WWW.contoso.com");
+		Assert ("#5", req is TestWebRequest); 
+
+		req = WebRequest.Create ("http://WWW.contoso.com/foo/bar/index.html");
+		Assert ("#6", req is TestWebRequest2); 
+		
+		req = WebRequest.Create ("http://WWW.x.com");
+		Assert ("#7", req is TestWebRequest3); 
+
+		req = WebRequest.Create ("http://WWW.c");
+		Assert ("#8", req is TestWebRequest3); 
+
+		req = WebRequest.CreateDefault (new Uri("http://WWW.contoso.com"));
+		Assert ("#9", req is HttpWebRequest);
+
+		try {
+			req = WebRequest.Create ("tcp://www.contoso.com");
+			Fail ("#10 should have failed with NotSupportedException");			
+		} catch (NotSupportedException) {			
+		}		
+	}
+	
+	internal class TestWebRequestCreator : IWebRequestCreate
+	{
+		internal TestWebRequestCreator () { }
+		
+		public WebRequest Create (Uri uri)
+		{
+			return new TestWebRequest ();
+		}
+	}
+	
+	internal class TestWebRequest : WebRequest
+	{
+		internal TestWebRequest () { }
+	}
+
+	internal class TestWebRequestCreator2 : IWebRequestCreate
+	{
+		internal TestWebRequestCreator2 () { }
+		
+		public WebRequest Create (Uri uri)
+		{
+			return new TestWebRequest2 ();
+		}
+	}
+	
+	internal class TestWebRequest2 : WebRequest
+	{
+		internal TestWebRequest2 () { }
+	}
+
+	internal class TestWebRequestCreator3 : IWebRequestCreate
+	{
+		internal TestWebRequestCreator3 () { }
+		
+		public WebRequest Create (Uri uri)
+		{
+			return new TestWebRequest3 ();
+		}
+	}
+	
+	internal class TestWebRequest3 : WebRequest
+	{
+		internal TestWebRequest3 () { }
+	}
+}
+
+}
+