2010-05-06 Marek Habersack <mhabersack@novell.com>
authorMarek Habersack <grendel@twistedcode.net>
Fri, 7 May 2010 23:02:46 +0000 (23:02 -0000)
committerMarek Habersack <grendel@twistedcode.net>
Fri, 7 May 2010 23:02:46 +0000 (23:02 -0000)
* HttpResponse.cs: implemented the following 4.0 methods:
RedirectPermanent, RedirectToRoute, RedirectToRoutePermanent and
RemoveOutputCacheItem.

2010-05-06  Marek Habersack  <mhabersack@novell.com>

* RouteCollection.cs: GetVirtualPath throws ArgumentException
when named route is not found in the collection.

2010-05-06  Marek Habersack  <mhabersack@novell.com>

* OutputCacheModule.cs: added a 2.0 internal property to return
the internal provider used in this profile.

* OutputCache.cs: added internal RemoveFromProvider method to
remove items from the indicated provider.

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

mcs/class/System.Web.Routing/System.Web.Routing/ChangeLog
mcs/class/System.Web.Routing/System.Web.Routing/RouteCollection.cs
mcs/class/System.Web/System.Web.Caching/ChangeLog
mcs/class/System.Web/System.Web.Caching/OutputCache.cs
mcs/class/System.Web/System.Web.Caching/OutputCacheModule.cs
mcs/class/System.Web/System.Web/ChangeLog
mcs/class/System.Web/System.Web/HttpResponse.cs
mcs/class/System.Web/Test/System.Web/HttpResponseTest.cs

index 637f9f96e079358d363812148ed91811e4ae0472..d390205e05c62f656832acc598453392a33b2f04 100644 (file)
@@ -1,3 +1,8 @@
+2010-05-06  Marek Habersack  <mhabersack@novell.com>
+
+       * RouteCollection.cs: GetVirtualPath throws ArgumentException
+       when named route is not found in the collection.
+
 2010-05-05  Marek Habersack  <mhabersack@novell.com>
 
        * UrlRoutingModule.cs: PostMapRequestHandler is obsolete in 4.0
index d656feb32576d787b1e6e434fa5dbdfb95ec1f67..f74525043e32b9a3469ead5b2fa8d0ee67984e17 100644 (file)
@@ -155,15 +155,19 @@ namespace System.Web.Routing
                {
                        if (requestContext == null)
                                throw new ArgumentNullException ("httpContext");
-
+#if !NET_4_0
                        if (Count == 0)
                                return null;
-
+#endif
                        VirtualPathData vp = null;
                        if (!String.IsNullOrEmpty (name)) {
                                RouteBase rb = this [name];
                                if (rb != null)
                                        vp = rb.GetVirtualPath (requestContext, values);
+#if NET_4_0
+                               else
+                                       throw new ArgumentException ("A route named '" + name + "' could not be found in the route collection.", "name");
+#endif
                        } else {
                                foreach (RouteBase rb in this) {
                                        vp = rb.GetVirtualPath (requestContext, values);
index 9de482e55bffb4c8de80c94e113744562f4f6342..7be050f506d1460f421f86f0bf36106099fd7f47 100644 (file)
@@ -1,3 +1,11 @@
+2010-05-06  Marek Habersack  <mhabersack@novell.com>
+
+       * OutputCacheModule.cs: added a 2.0 internal property to return
+       the internal provider used in this profile.
+
+       * OutputCache.cs: added internal RemoveFromProvider method to
+       remove items from the indicated provider.
+
 2010-02-23  Marek Habersack  <mhabersack@novell.com>
 
        * CachedVaryBy.cs: made serializable for 4.0+ (necessary for
index 24aa7819ffe093cdd53517b086bcd4afff40e75a..fd1eee678393e75d98053efc2d79082738b6f850 100644 (file)
@@ -27,6 +27,7 @@
 //
 
 using System.Configuration;
+using System.Configuration.Provider;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Security.Permissions;
@@ -93,7 +94,7 @@ namespace System.Web.Caching
                                !(data is FileResponseElement) &&
                                !(data is SubstitutionResponseElement);
                }
-
+               
                static void Init ()
                {
                        if (initialized)
@@ -132,5 +133,24 @@ namespace System.Web.Caching
 
                        return ret;
                }
+
+               internal static void RemoveFromProvider (string key, string providerName)
+               {
+                       if (providerName == null)
+                               return;
+
+                       OutputCacheProviderCollection providers = Providers;
+                       OutputCacheProvider provider;
+                       
+                       if (providers == null || providers.Count == 0)
+                               provider = null;
+                       else
+                               provider = providers [providerName];
+
+                       if (provider == null)
+                               throw new ProviderException ("Provider '" + providerName + "' was not found.");
+
+                       provider.Remove (key);
+               }
        }
 }
index 00fea61b66089e399d604a204d5526ad98bcbeb8..d1a6cd3486ce3bbe5688fb1ac711cee58d200d77 100644 (file)
@@ -50,7 +50,11 @@ namespace System.Web.Caching
                static object keysCacheLock = new object ();
                Dictionary <string, string> keysCache;
                Dictionary <string, string> entriesToInvalidate;
-
+#if !NET_4_0
+               internal OutputCacheProvider InternalProvider {
+                       get { return provider; }
+               }
+#endif
                public OutputCacheModule ()
                {
                }
index 3398ab7dedbf47ffd48936fd55b5456bf3626411..e26168ce5ac9e0e804baf032c4ea2ba56a942875 100644 (file)
@@ -1,3 +1,9 @@
+2010-05-06  Marek Habersack  <mhabersack@novell.com>
+
+       * HttpResponse.cs: implemented the following 4.0 methods:
+       RedirectPermanent, RedirectToRoute, RedirectToRoutePermanent and
+       RemoveOutputCacheItem.
+
 2010-05-05  Marek Habersack  <mhabersack@novell.com>
 
        * HttpRequest.cs: implemented new 4.0 property - RequestContext
index 951bed2646d50837317b3f9b22f64d5d319de6b3..a1ae238839987f6368f54daccd7247767134af2b 100644 (file)
@@ -43,6 +43,10 @@ using System.Security.Permissions;
 using System.Web.Hosting;
 using System.Web.SessionState;
 
+#if NET_4_0
+using System.Web.Routing;
+#endif
+
 namespace System.Web
 {      
        // CAS - no InheritanceDemand here as the class is sealed
@@ -832,12 +836,7 @@ namespace System.Web
                        AppendHeader ("PICS-Label", value);
                }
 
-               public void Redirect (string url)
-               {
-                       Redirect (url, true);
-               }
-
-               public void Redirect (string url, bool endResponse)
+               void Redirect (string url, bool endResponse, int code)
                {
                        if (url == null)
                                throw new ArgumentNullException ("url");
@@ -852,7 +851,7 @@ namespace System.Web
                        ClearHeaders ();
                        ClearContent ();
                        
-                       StatusCode = 302;
+                       StatusCode = code;
                        url = ApplyAppPathModifier (url);
 
                        bool isFullyQualified;
@@ -888,7 +887,105 @@ namespace System.Web
                                End ();
                        is_request_being_redirected = false;
                }
+               
+               public void Redirect (string url)
+               {
+                       Redirect (url, true);
+               }
+
+               public void Redirect (string url, bool endResponse)
+               {
+                       Redirect (url, endResponse, 302);
+               }
+#if NET_4_0
+               public void RedirectPermanent (string url)
+               {
+                       RedirectPermanent (url, true);
+               }
+
+               public void RedirectPermanent (string url, bool endResponse)
+               {
+                       Redirect (url, endResponse, 301);
+               }
+
+               public void RedirectToRoute (object routeValues)
+               {
+                       RedirectToRoute ("RedirectToRoute", null, new RouteValueDictionary (routeValues), 302, true);
+               }
+
+               public void RedirectToRoute (RouteValueDictionary routeValues)
+               {
+                       RedirectToRoute ("RedirectToRoute", null, routeValues, 302, true);
+               }
+
+               public void RedirectToRoute (string routeName)
+               {
+                       RedirectToRoute ("RedirectToRoute", routeName, null, 302, true);
+               }
+
+               public void RedirectToRoute (string routeName, object routeValues)
+               {
+                       RedirectToRoute ("RedirectToRoute", routeName, new RouteValueDictionary (routeValues), 302, true);
+               }
+
+               public void RedirectToRoute (string routeName, RouteValueDictionary routeValues)
+               {
+                       RedirectToRoute ("RedirectToRoute", routeName, routeValues, 302, true);
+               }
+
+               public void RedirectToRoutePermanent (object routeValues)
+               {
+                       RedirectToRoute ("RedirectToRoutePermanent", null, new RouteValueDictionary (routeValues), 301, false);
+               }
+
+               public void RedirectToRoutePermanent (RouteValueDictionary routeValues)
+               {
+                       RedirectToRoute ("RedirectToRoutePermanent", null, routeValues, 301, false);
+               }
+
+               public void RedirectToRoutePermanent (string routeName)
+               {
+                       RedirectToRoute ("RedirectToRoutePermanent", routeName, null, 301, false);
+               }
+
+               public void RedirectToRoutePermanent (string routeName, object routeValues)
+               {
+                       RedirectToRoute ("RedirectToRoutePermanent", routeName, new RouteValueDictionary (routeValues), 301, false);
+               }               
 
+               public void RedirectToRoutePermanent (string routeName, RouteValueDictionary routeValues)
+               {
+                       RedirectToRoute ("RedirectToRoutePermanent", routeName, routeValues, 301, false);
+               }
+               
+               void RedirectToRoute (string callerName, string routeName, RouteValueDictionary routeValues, int redirectCode, bool endResponse)
+               {
+                       HttpContext ctx = context ?? HttpContext.Current;
+                       HttpRequest req = ctx != null ? ctx.Request : null;
+                       
+                       if (req == null)
+                               // Let's emulate .NET
+                               throw new NullReferenceException ();
+                       
+                       VirtualPathData vpd = RouteTable.Routes.GetVirtualPath (req.RequestContext, routeName, routeValues);
+                       string redirectUrl = vpd != null ? vpd.VirtualPath : null;
+                       if (String.IsNullOrEmpty (redirectUrl))
+                               throw new InvalidOperationException ("No matching route found for RedirectToRoute");
+
+                       Redirect (redirectUrl, true, redirectCode);
+               }
+
+               public static void RemoveOutputCacheItem (string path, string providerName)
+               {
+                       if (path == null)
+                               throw new ArgumentNullException ("path");
+
+                       if (path.Length > 0 && path [0] != '/')
+                               throw new ArgumentException ("Invalid path for HttpResponse.RemoveOutputCacheItem: '" + path + "'. An absolute virtual path is expected");
+
+                       OutputCache.RemoveFromProvider (path, providerName);
+               }
+#endif
                public static void RemoveOutputCacheItem (string path)
                {
                        if (path == null)
@@ -900,7 +997,19 @@ namespace System.Web
                        if (path [0] != '/')
                                throw new ArgumentException ("'" + path + "' is not an absolute virtual path.");
 
-                       HttpRuntime.InternalCache.Remove (path);
+#if NET_4_0
+                       RemoveOutputCacheItem (path, OutputCache.DefaultProviderName);
+#else
+                       HttpContext context = HttpContext.Current;
+                       HttpApplication app_instance = context != null ? context.ApplicationInstance : null;
+                       HttpModuleCollection modules = app_instance != null ? app_instance.Modules : null;
+                       OutputCacheModule ocm = modules != null ? modules.Get ("OutputCache") as OutputCacheModule : null;
+                       OutputCacheProvider internalProvider = ocm != null ? ocm.InternalProvider : null;
+                       if (internalProvider == null)
+                               return;
+
+                       internalProvider.Remove (path);
+#endif
                }
 
                public void SetCookie (HttpCookie cookie)
index 52ab2c680602c59fc8f7f5ca228896fd277c99f4..5e60b5f4316724d9da5348f0520c9398d5f8eacc 100644 (file)
@@ -32,9 +32,14 @@ using System.Collections.Specialized;
 using System.IO;
 using System.Text;
 using System.Web;
+#if NET_4_0
+using System.Web.Routing;
+#endif
 
 using NUnit.Framework;
 
+using MonoTests.Common;
+
 namespace MonoTests.System.Web {
 
        public class FakeHttpWorkerRequest2 : HttpWorkerRequest {
@@ -61,7 +66,7 @@ namespace MonoTests.System.Web {
                
                public override string GetRawUrl()
                {
-                       return "GetRawUrl";
+                       return "/GetRawUrl";
                }
                
                public override string GetHttpVerbName()
@@ -588,6 +593,51 @@ namespace MonoTests.System.Web {
                        Assert.AreEqual (0, f.UnknownResponseHeaders.Count, "#C1");
 #endif
                }
+#if NET_4_0
+               [Test]
+               public void RedirectPermanent ()
+               {
+                       FakeHttpWorkerRequest2 request;
+                       HttpContext context = Cook (1, out request);
+                       AssertExtensions.Throws<ArgumentNullException> (() => {
+                               context.Response.RedirectPermanent (null);
+                       }, "#A1");
+
+                       AssertExtensions.Throws<ArgumentException> (() => {
+                               context.Response.RedirectPermanent ("http://invalid\nurl.com");
+                       }, "#A2");
+
+                       AssertExtensions.Throws<ArgumentNullException> (() => {
+                               context.Response.RedirectPermanent (null, true);
+                       }, "#A3");
+
+                       AssertExtensions.Throws<ArgumentException> (() => {
+                               context.Response.RedirectPermanent ("http://invalid\nurl.com", true);
+                       }, "#A4");
+               }
+
+               [Test]
+               public void RedirectToRoute ()
+               {
+                       var resp = new HttpResponse (new StringWriter ());
+                       // Ho, ho, ho!
+                       AssertExtensions.Throws<NullReferenceException> (() => {
+                               resp.RedirectToRoute ("SomeRoute");
+                       }, "#A1");
+
+                       FakeHttpWorkerRequest2 request;
+                       HttpContext context = Cook (1, out request);
+
+                       // From RouteCollection.GetVirtualPath
+                       AssertExtensions.Throws<ArgumentException> (() => {
+                               context.Response.RedirectToRoute ("SomeRoute");
+                       }, "#A2");
+
+                       AssertExtensions.Throws<InvalidOperationException> (() => {
+                               context.Response.RedirectToRoute (new { productId = "1", category = "widgets" });
+                       }, "#A3");
+               }
+#endif
        }
 
        [TestFixture]