Merge pull request #2530 from lambdageek/monoerror-mono_string_new
[mono.git] / mcs / class / System.Web / System.Web / HttpServerUtility.cs
index 685394484595c92409983010a60512d9e53833e4..9f94dcec23f85e2fef61c1f59fcbcdd426405967 100644 (file)
@@ -8,7 +8,7 @@
 //
 
 //
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -35,9 +35,13 @@ using System.Web.UI;
 using System.Web.Util;
 using System.Collections.Specialized;
 using System.Security.Permissions;
+using System.Text;
+using System.Threading;
+using System.Web.Configuration;
+using System.Web.SessionState;
 
-namespace System.Web {
-
+namespace System.Web
+{
        //
        // Methods exposed through HttpContext.Server property
        //
@@ -85,12 +89,17 @@ namespace System.Web {
                        Execute (path, writer, true);
                }
 
-#if NET_2_0
-               public
-#else
-               internal
-#endif
-               void Execute (string path, TextWriter writer, bool preserveQuery)
+               public void Execute (string path, bool preserveForm)
+               {
+                       Execute (path, null, preserveForm);
+               }
+
+               public void Execute (string path, TextWriter writer, bool preserveForm)
+               {                       
+                       Execute (path, writer, preserveForm, false);
+               }
+
+               void Execute (string path, TextWriter writer, bool preserveForm, bool isTransfer)
                {
                        if (path == null)
                                throw new ArgumentNullException ("path");
@@ -98,47 +107,88 @@ namespace System.Web {
                        if (path.IndexOf (':') != -1)
                                throw new ArgumentException ("Invalid path.");
 
-                       HttpRequest request = context.Request;
-                       string oldQuery = request.QueryStringRaw;
+                       string queryString = null;
                        int qmark = path.IndexOf ('?');
                        if (qmark != -1) {
-                               request.QueryStringRaw = path.Substring (qmark + 1);
+                               queryString = path.Substring (qmark + 1);
                                path = path.Substring (0, qmark);
-                       } else if (!preserveQuery) {
-                               request.QueryStringRaw = "";
+                       }
+
+                       string exePath = UrlUtils.Combine (context.Request.BaseVirtualDir, path);
+                       bool cookieless = false;
+                       SessionStateSection config = WebConfigurationManager.GetWebApplicationSection ("system.web/sessionState") as SessionStateSection;
+                       cookieless = SessionStateModule.IsCookieLess (context, config);
+                       
+                       if (cookieless)
+                               exePath = UrlUtils.RemoveSessionId (VirtualPathUtility.GetDirectory (exePath), exePath);
+                       
+                       IHttpHandler handler = context.ApplicationInstance.GetHandler (context, exePath, true);
+                       Execute (handler, writer, preserveForm, exePath, queryString, isTransfer, true);
+               }
+
+               internal void Execute (IHttpHandler handler, TextWriter writer, bool preserveForm, string exePath, string queryString, bool isTransfer, bool isInclude)
+               {
+                       // If the target handler is not Page, the transfer must not occur.
+                       // InTransit == true means we're being called from Transfer
+                       bool is_static = (handler is StaticFileHandler);
+                       if (isTransfer && !(handler is Page) && !is_static)
+                               throw new HttpException ("Transfer is only allowed to .aspx and static files");
+
+                       HttpRequest request = context.Request;
+                       string oldQuery = request.QueryStringRaw;
+                       if (queryString != null) {
+                               request.QueryStringRaw = queryString;
+                       } else if (!preserveForm) {
+                               request.QueryStringRaw = String.Empty;
                        }
 
                        HttpResponse response = context.Response;
-                       WebROCollection oldForm = null;
-                       if (!preserveQuery) {
-                               oldForm = request.Form as WebROCollection;
+                       WebROCollection oldForm = request.Form as WebROCollection;
+                       if (!preserveForm) {
                                request.SetForm (new WebROCollection ());
                        }
 
                        TextWriter output = writer;
                        if (output == null)
                                output = response.Output;
-
-                       string oldFilePath = request.FilePath;
-                       request.SetCurrentExePath (UrlUtils.Combine (request.BaseVirtualDir, path));
-                       IHttpHandler handler = context.ApplicationInstance.GetHandler (context);
-                       TextWriter previous = null;
+                       
+                       TextWriter previous = response.SetTextWriter (output);
+                       string oldExePath = request.CurrentExecutionFilePath;
+                       bool oldIsInclude = context.IsProcessingInclude;
                        try {
-                               previous = response.SetTextWriter (output);
+                               context.PushHandler (handler);
+                               if (is_static) // Not sure if this should apply to Page too
+                                       request.SetFilePath (exePath);
+
+                               request.SetCurrentExePath (exePath);
+                               context.IsProcessingInclude = isInclude;
+                               
                                if (!(handler is IHttpAsyncHandler)) {
                                        handler.ProcessRequest (context);
                                } else {
                                        IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler) handler;
                                        IAsyncResult ar = asyncHandler.BeginProcessRequest (context, null, null);
-                                       ar.AsyncWaitHandle.WaitOne ();
+                                       WaitHandle asyncWaitHandle = ar != null ? ar.AsyncWaitHandle : null;
+                                       if (asyncWaitHandle != null)
+                                               asyncWaitHandle.WaitOne ();
                                        asyncHandler.EndProcessRequest (ar);
                                }
                        } finally {
-                               request.SetCurrentExePath (oldFilePath);
-                               request.QueryStringRaw = oldQuery;
+                               if (oldQuery != request.QueryStringRaw) {
+                                       if (oldQuery != null && oldQuery.Length > 0) {
+                                               oldQuery = oldQuery.Substring (1); // Ignore initial '?'
+                                               request.QueryStringRaw = oldQuery; // which is added here.
+                                       } else
+                                               request.QueryStringRaw = String.Empty;
+                               }
+                               
                                response.SetTextWriter (previous);
-                               if (!preserveQuery)
+                               if (!preserveForm)
                                        request.SetForm (oldForm);
+
+                               context.PopHandler ();
+                               request.SetCurrentExePath (oldExePath);
+                               context.IsProcessingInclude = oldIsInclude;
                        }
                }
 
@@ -174,51 +224,161 @@ namespace System.Web {
                        return context.Request.MapPath (path);
                }
 
+               
+               public void TransferRequest (string path)
+               {
+                       TransferRequest (path, false, null, null);
+               }
+               
+               public void TransferRequest (string path, bool preserveForm)
+               {
+                       TransferRequest (path, preserveForm, null, null);
+               }
+
+               [MonoTODO ("Always throws PlatformNotSupportedException.")]
+               public void TransferRequest (string path, bool preserveForm, string method, NameValueCollection headers)
+               {
+                       throw new PlatformNotSupportedException ();
+               }
+               
                public void Transfer (string path)
                {
-                       // If it's a page and a postback, don't pass form data
-                       // See bug #65613.
-                       bool preserveForm = true;
-                       if (context.Handler is Page) {
-                               Page page = (Page) context.Handler;
-                               preserveForm = !page.IsPostBack;
-                       }
+                       Transfer (path, true);
+               }
 
-                       Transfer (path, preserveForm);
+               public void Transfer (string path, bool preserveForm) {
+                       Execute (path, null, preserveForm, true);
+                       context.Response.End ();
                }
 
-               public void Transfer (string path, bool preserveForm)
+               public void Transfer (IHttpHandler handler, bool preserveForm)
                {
-                       Execute (path, null, preserveForm);
+                       if (handler == null)
+                               throw new ArgumentNullException ("handler");
+
+                       // TODO: see the MS doc and search for "enableViewStateMac": this method is not
+                       // allowed for pages when preserveForm is true and the page IsCallback property
+                       // is true.
+                       Execute (handler, null, preserveForm, context.Request.CurrentExecutionFilePath, null, true, true);
                        context.Response.End ();
                }
-#if NET_2_0
-               [MonoTODO]
-               public void Transfer (IHttpHandler handler, bool preserveForm)
+
+               public void Execute (IHttpHandler handler, TextWriter writer, bool preserveForm)
                {
-                       throw new NotImplementedException ();
+                       if (handler == null)
+                               throw new ArgumentNullException ("handler");
+
+                       Execute (handler, writer, preserveForm, context.Request.CurrentExecutionFilePath, null, false, true);
                }
-#endif
+
+               public static byte[] UrlTokenDecode (string input)
+               {
+                       if (input == null)
+                               throw new ArgumentNullException ("input");
+                       if (input.Length < 1)
+                               return new byte[0];
+                       byte[] bytes = Encoding.ASCII.GetBytes (input);
+                       int inputLength = input.Length - 1;
+                       int equalsCount = (int)(((char)bytes[inputLength]) - 0x30);
+                       char[] ret = new char[inputLength + equalsCount];
+                       int i = 0;
+                       for (; i < inputLength; i++) {
+                               switch ((char)bytes[i]) {
+                                       case '-':
+                                               ret[i] = '+';
+                                               break;
+
+                                       case '_':
+                                               ret[i] = '/';
+                                               break;
+
+                                       default:
+                                               ret[i] = (char)bytes[i];
+                                               break;
+                               }
+                       }
+                       while (equalsCount > 0) {
+                               ret[i++] = '=';
+                               equalsCount--;
+                       }
+                       
+                       return Convert.FromBase64CharArray (ret, 0, ret.Length);
+               }
+
+               public static string UrlTokenEncode (byte[] input)
+               {
+                       if (input == null)
+                               throw new ArgumentNullException ("input");
+                       if (input.Length < 1)
+                               return String.Empty;
+                       string base64 = Convert.ToBase64String (input);
+                       int retlen;
+                       if (base64 == null || (retlen = base64.Length) == 0)
+                               return String.Empty;
+
+                       // MS.NET implementation seems to process the base64
+                       // string before returning. It replaces the chars:
+                       //
+                       //  + with -
+                       //  / with _
+                       //
+                       // Then removes trailing ==, which may appear in the
+                       // base64 string, and replaces them with a single digit
+                       // that's the count of removed '=' characters (0 if none
+                       // were removed)
+                       int equalsCount = 0x30;
+                       while (retlen > 0 && base64[retlen - 1] == '=') {
+                               equalsCount++;
+                               retlen--;
+                       }
+                       char[] chars = new char[retlen + 1];
+                       chars[retlen] = (char)equalsCount;
+                       for (int i = 0; i < retlen; i++) {
+                               switch (base64[i]) {
+                                       case '+':
+                                               chars[i] = '-';
+                                               break;
+
+                                       case '/':
+                                               chars[i] = '_';
+                                               break;
+                                       
+                                       default:
+                                               chars[i] = base64[i];
+                                               break;
+                               }
+                       }
+                       return new string (chars);
+               }
+
                public string UrlDecode (string s)
                {
-                       return HttpUtility.UrlDecode (s);
+                       HttpRequest request = context.Request;
+                       if(request != null)
+                               return HttpUtility.UrlDecode (s, request.ContentEncoding);
+                       else
+                               return HttpUtility.UrlDecode (s);
                }
 
                public void UrlDecode (string s, TextWriter output)
                {
                        if (s != null)
-                               output.Write (HttpUtility.UrlDecode (s));
+                               output.Write (UrlDecode (s));
                }
 
                public string UrlEncode (string s)
                {
-                       return HttpUtility.UrlEncode (s);
+                       HttpResponse response = context.Response;
+                       if (response != null)
+                               return HttpUtility.UrlEncode (s, response.ContentEncoding);
+                       else
+                               return HttpUtility.UrlEncode (s);
                }
 
                public void UrlEncode (string s, TextWriter output)
                {
                        if (s != null)
-                               output.Write (HttpUtility.UrlEncode (s));
+                               output.Write (UrlEncode (s));
                }
 
                public string UrlPathEncode (string s)
@@ -226,10 +386,10 @@ namespace System.Web {
                        if (s == null)
                                return null;
 
-                       int idx = s.IndexOf ("?");
+                       int idx = s.IndexOf ('?');
                        string s2 = null;
                        if (idx != -1) {
-                               s2 = s.Substring (0, idx-1);
+                               s2 = s.Substring (0, idx);
                                s2 = HttpUtility.UrlEncode (s2) + s.Substring (idx);
                        } else {
                                s2 = HttpUtility.UrlEncode (s);
@@ -249,7 +409,7 @@ namespace System.Web {
                public int ScriptTimeout {
                        get { return (int) context.ConfigTimeout.TotalSeconds; }
                        [AspNetHostingPermission (SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium)]
-                       set { context.ConfigTimeout = new TimeSpan (0, 0, value); }
+                       set { context.ConfigTimeout = TimeSpan.FromSeconds (value); }
                }
        }
 }