Ver Fonte

Merge pull request #2663 from esdrubal/islocal

[System] Fixes HttpListenerRequest.IsLocal.
Alexis Christoforides há 9 anos atrás
pai
commit
8004de2b79

+ 1 - 1
mcs/class/System/System.Net/HttpListenerRequest.cs

@@ -403,7 +403,7 @@ namespace System.Net {
 		}
 
 		public bool IsLocal {
-			get { return IPAddress.IsLoopback (RemoteEndPoint.Address); }
+			get { return LocalEndPoint.Address.Equals (RemoteEndPoint.Address); }
 		}
 
 		public bool IsSecureConnection {

+ 12 - 2
mcs/class/System/Test/System.Net/HttpListener2Test.cs

@@ -81,13 +81,23 @@ namespace MonoTests.System.Net {
 
 		public static MyNetworkStream CreateNS (int port)
 		{
-			return CreateNS (port, 5000);
+			return CreateNS (IPAddress.Loopback, port, 5000);
 		}
 
 		public static MyNetworkStream CreateNS (int port, int timeout_ms)
+		{
+			return CreateNS (IPAddress.Loopback, port, timeout_ms);
+		}
+
+		public static MyNetworkStream CreateNS (IPAddress ip, int port)
+		{
+			return CreateNS (ip, port, 5000);
+		}
+
+		public static MyNetworkStream CreateNS (IPAddress ip, int port, int timeout_ms)
 		{
 			Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-			sock.Connect (new IPEndPoint (IPAddress.Loopback, port));
+			sock.Connect (new IPEndPoint (ip, port));
 			sock.SendTimeout = timeout_ms;
 			sock.ReceiveTimeout = timeout_ms;
 			return new MyNetworkStream (sock);

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

@@ -32,6 +32,7 @@ using System.IO;
 using System.Net;
 using System.Net.Sockets;
 using System.Text;
+using System.Collections.Generic;
 
 using NUnit.Framework;
 
@@ -190,6 +191,26 @@ namespace MonoTests.System.Net
 			listener.Close ();
 		}
 
+		[Test]
+		public void HttpRequestIsLocal ()
+		{
+			var ips = new List<IPAddress> (Dns.GetHostAddresses (Dns.GetHostName ()));
+			ips.Add (IPAddress.Loopback);
+			foreach (var ip in ips) {
+				if (ip.AddressFamily != AddressFamily.InterNetwork)
+					continue;
+
+				HttpListener listener = HttpListener2Test.CreateAndStartListener (
+					"http://" + ip + ":9000/HttpRequestIsLocal/");
+				NetworkStream ns = HttpListener2Test.CreateNS (ip, 9000);
+				HttpListener2Test.Send (ns, "GET /HttpRequestIsLocal/ HTTP/1.0\r\n\r\n");
+				HttpListenerContext ctx = listener.GetContext ();
+				HttpListenerRequest request = ctx.Request;
+				Assert.AreEqual (true, request.IsLocal, "IP " + ip + " is not local");
+				listener.Close ();
+			}
+		}
+
 		[Test] // #29927
 		public void HttpRequestUriUnescape ()
 		{