ソースを参照

* HttpListenerRequestTest.cs: Added test for HasEntityBody.
* HttpListener2Test.cs: Removed CWL.
* HttpListenerRequest.cs: Removed dead code. USe String.Length to
check for zero-length string. Use instance field for is_chunked, and
also set it for GET/HEAD/DELETE. Fixed HasEntityBody to return true
when Content-Length > 0, or Transfer-Encoding is chunked.
* System_test.dll.sources: Added HttpListenerRequestTest.cs.

svn path=/trunk/mcs/; revision=86666

Gert Driesen 18 年 前
コミット
affbed9b01

+ 4 - 0
mcs/class/System/ChangeLog

@@ -1,3 +1,7 @@
+2007-09-29  Gert Driesen  <[email protected]>
+
+	* System_test.dll.sources: Added HttpListenerRequestTest.cs.
+
 2007-09-27  Atsushi Enomoto  <[email protected]>
 
 	* System.dll.sources : added ITypeDiscoveryService.cs.

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

@@ -1,3 +1,10 @@
+2007-09-30  Gert Driesen  <[email protected]>
+
+	* HttpListenerRequest.cs: Removed dead code. USe String.Length to
+	check for zero-length string. Use instance field for is_chunked, and
+	also set it for GET/HEAD/DELETE. Fixed HasEntityBody to return true
+	when Content-Length > 0, or Transfer-Encoding is chunked.
+
 2007-09-28  Marek Habersack  <[email protected]>
 
 	* HttpListenerRequest.cs: make sure 'method' is always stored in

+ 8 - 22
mcs/class/System/System.Net/HttpListenerRequest.cs

@@ -68,10 +68,6 @@ namespace System.Net {
 
 		static char [] separators = new char [] { ' ' };
 
-#if false
-		static readonly string [] methods = new string [] { "GET", "POST", "HEAD",
-								"PUT", "CONNECT", "MKCOL" };
-#endif
 		internal void SetRequestLine (string req)
 		{
 			string [] parts = req.Split (separators, 3);
@@ -94,17 +90,6 @@ namespace System.Net {
 				context.ErrorMessage = "(Invalid verb)";
 				return;
 			}
-			
-#if false
-			//
-			// According to bug #80504 we should allow any verbs to go
-			// through.
-			//
-			if (Array.IndexOf (methods, method) == -1) {
-				context.ErrorMessage = "Invalid request line (verb).";
-				return;
-			}
-#endif
 
 			raw_url = parts [1];
 			if (parts [2].Length != 8 || !parts [2].StartsWith ("HTTP/")) {
@@ -147,12 +132,12 @@ namespace System.Net {
 		internal void FinishInitialization ()
 		{
 			string host = UserHostName;
-			if (version > HttpVersion.Version10 && (host == null || host == "")) {
+			if (version > HttpVersion.Version10 && (host == null || host.Length == 0)) {
 				context.ErrorMessage = "Invalid host name";
 				return;
 			}
 
-			if (host == null || host == "")
+			if (host == null || host.Length == 0)
 				host = UserHostAddress;
 
 			int colon = host.IndexOf (':');
@@ -172,9 +157,6 @@ namespace System.Net {
 
 			CreateQueryString (url.Query);
 
-			if (method == "GET" || method == "HEAD" || method == "DELETE")
-				return;
-
 			string t_encoding = null;
 			if (version >= HttpVersion.Version11) {
 				t_encoding = Headers ["Transfer-Encoding"];
@@ -185,7 +167,11 @@ namespace System.Net {
 				}
 			}
 
-			bool is_chunked = (t_encoding == "chunked");
+			is_chunked = (t_encoding == "chunked");
+
+			if (method == "GET" || method == "HEAD" || method == "DELETE")
+				return;
+
 			if (!is_chunked && !cl_set) {
 				context.Connection.SendError (null, 411);
 				return;
@@ -331,7 +317,7 @@ namespace System.Net {
 		}
 
 		public bool HasEntityBody {
-			get { return !(method == "GET" || method == "HEAD" || content_length <= 0 || is_chunked); }
+			get { return (content_length > 0 || is_chunked); }
 		}
 
 		public NameValueCollection Headers {

+ 1 - 0
mcs/class/System/System_test.dll.sources

@@ -187,6 +187,7 @@ System.Net/HttpListenerTest.cs
 System.Net/HttpListenerBasicIdentityTest.cs
 System.Net/HttpListenerPrefixCollectionTest.cs
 System.Net/HttpListener2Test.cs
+System.Net/HttpListenerRequestTest.cs
 System.Net/IPAddressTest.cs
 System.Net/IPEndPointTest.cs
 System.Net/ServicePointManagerTest.cs

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

@@ -1,3 +1,8 @@
+2007-09-30  Gert Driesen  <[email protected]>
+
+	* HttpListenerRequestTest.cs: Added test for HasEntityBody.
+	* HttpListener2Test.cs: Removed CWL.
+
 2007-08-29  Atsushi Enomoto  <[email protected]>
 
 	* IPAddressTest.cs : added an address with scope id to parse/format

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

@@ -163,7 +163,6 @@ namespace MonoTests.System.Net {
 				string response = Receive (ns, 512);
 				ns.Close ();
 				listener.Close ();
-				Console.WriteLine ("Response is: {0}", response);
 				Assert.AreEqual (true, response.StartsWith ("HTTP/1.1 400"), String.Format ("Failed on {0}", (int) b));
 			}
 		}

+ 149 - 0
mcs/class/System/Test/System.Net/HttpListenerRequestTest.cs

@@ -0,0 +1,149 @@
+//
+// HttpListenerRequestTest.cs - Unit tests for System.Net.HttpListenerRequest
+//
+// Author:
+//	Gert Driesen ([email protected])
+//
+// Copyright (C) 2007 Gert Driesen
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.IO;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+
+using NUnit.Framework;
+
+namespace MonoTests.System.Net
+{
+	[TestFixture]
+#if TARGET_JVM
+	[Ignore ("The class HttpListener is not supported")]
+#endif
+	public class HttpListenerRequestTest
+	{
+		[Test]
+		public void HasEntityBody ()
+		{
+			HttpListenerContext ctx;
+			HttpListenerRequest request;
+			NetworkStream ns;
+
+			HttpListener listener = HttpListener2Test.CreateAndStartListener (
+				"http://127.0.0.1:9000/HasEntityBody/");
+
+			// POST with non-zero Content-Lenth
+			ns = HttpListener2Test.CreateNS (9000);
+			HttpListener2Test.Send (ns, "POST /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
+			ctx = listener.GetContext ();
+			request = ctx.Request;
+			Assert.IsTrue (request.HasEntityBody, "#A");
+			HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
+
+			// POST with zero Content-Lenth
+			ns = HttpListener2Test.CreateNS (9000);
+			HttpListener2Test.Send (ns, "POST /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
+			ctx = listener.GetContext ();
+			request = ctx.Request;
+			Assert.IsFalse (request.HasEntityBody, "#B");
+			HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
+
+			// POST with chunked encoding
+			ns = HttpListener2Test.CreateNS (9000);
+			HttpListener2Test.Send (ns, "POST /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
+			ctx = listener.GetContext ();
+			request = ctx.Request;
+			Assert.IsTrue (request.HasEntityBody, "#C");
+			HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
+
+			// GET with no Content-Length
+			ns = HttpListener2Test.CreateNS (9000);
+			HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
+			ctx = listener.GetContext ();
+			request = ctx.Request;
+			Assert.IsFalse (request.HasEntityBody, "#D");
+			HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
+
+			// GET with non-zero Content-Length
+			ns = HttpListener2Test.CreateNS (9000);
+			HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n");
+			ctx = listener.GetContext ();
+			request = ctx.Request;
+			Assert.IsTrue (request.HasEntityBody, "#E");
+			HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
+
+			// GET with zero Content-Length
+			ns = HttpListener2Test.CreateNS (9000);
+			HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n");
+			ctx = listener.GetContext ();
+			request = ctx.Request;
+			Assert.IsFalse (request.HasEntityBody, "#F");
+			HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
+
+			// GET with chunked encoding
+			ns = HttpListener2Test.CreateNS (9000);
+			HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
+			ctx = listener.GetContext ();
+			request = ctx.Request;
+			Assert.IsTrue (request.HasEntityBody, "#G");
+			HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
+
+			// PUT with non-zero Content-Lenth
+			ns = HttpListener2Test.CreateNS (9000);
+			HttpListener2Test.Send (ns, "PUT /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
+			ctx = listener.GetContext ();
+			request = ctx.Request;
+			Assert.IsTrue (request.HasEntityBody, "#H");
+
+			// PUT with zero Content-Lenth
+			ns = HttpListener2Test.CreateNS (9000);
+			HttpListener2Test.Send (ns, "PUT /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
+			ctx = listener.GetContext ();
+			request = ctx.Request;
+			Assert.IsFalse (request.HasEntityBody, "#I");
+
+			// INVALID with non-zero Content-Lenth
+			ns = HttpListener2Test.CreateNS (9000);
+			HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
+			ctx = listener.GetContext ();
+			request = ctx.Request;
+			Assert.IsTrue (request.HasEntityBody, "#J");
+
+			// INVALID with zero Content-Lenth
+			ns = HttpListener2Test.CreateNS (9000);
+			HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
+			ctx = listener.GetContext ();
+			request = ctx.Request;
+			Assert.IsFalse (request.HasEntityBody, "#K");
+
+			// INVALID with chunked encoding
+			ns = HttpListener2Test.CreateNS (9000);
+			HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
+			ctx = listener.GetContext ();
+			request = ctx.Request;
+			Assert.IsTrue (request.HasEntityBody, "#L");
+
+			listener.Close ();
+		}
+	}
+}