/// <summary> /// Generates redirect location by specified parameters /// </summary> protected virtual string GetRedirectedRequestUrl(string asproxyPage, string destination, string referrer, string webMethod, bool encodeUrl) { // Encode redirect page if needed if (encodeUrl) { destination = UrlProvider.EncodeUrl(destination); if (!string.IsNullOrEmpty(referrer)) { referrer = UrlProvider.EncodeUrl(referrer); } } else { // just make it url safe destination = UrlProvider.EscapeUrlQuery(destination); if (!string.IsNullOrEmpty(referrer)) { referrer = UrlProvider.EscapeUrlQuery(referrer); } } // If address exists in current page address it will automatically replaced asproxyPage = UrlBuilder.AddUrlQuery(asproxyPage, Consts.Query.UrlAddress, destination); // Apply decode option asproxyPage = UrlBuilder.AddUrlQuery(asproxyPage, Consts.Query.Decode, Convert.ToByte(encodeUrl).ToString()); // Apply current page as referrer url for redirect url if (!string.IsNullOrEmpty(referrer)) { asproxyPage = UrlBuilder.AddUrlQueryToEnd(asproxyPage, Consts.Query.Redirect, referrer); } // If page is marked as posted back, remove the remark if (string.IsNullOrEmpty(webMethod)) { // The default web method will be used asproxyPage = UrlBuilder.RemoveQuery(asproxyPage, Consts.Query.WebMethod); } else { // changing the web method asproxyPage = UrlBuilder.ReplaceUrlQuery(asproxyPage, Consts.Query.WebMethod, webMethod); } return(asproxyPage); }
public static void ReplaceFormsSources(ref string htmlCodes, string pageUrlNoQuery, string newPageFormat, string pagePath, string siteRootUrl, bool encodeUrl, bool changeMethod, string extraAttributeFormat) { TextRange methodResult; // = new TextRange(); TextRange actionResult; // = new TextRange(); int cursorPos = 0; string newAttribute = ""; string formMethod = ""; string tmp, actionSrc = ""; string orgValue = ""; bool addNewAttribute = false; bool hasNewAttribute = false; if (!string.IsNullOrEmpty(extraAttributeFormat)) { addNewAttribute = true; hasNewAttribute = true; } do { addNewAttribute = hasNewAttribute; if (changeMethod) { methodResult = HtmlParser.GetTagAttributeValuePos(ref htmlCodes, "<form", "method", cursorPos); if (methodResult.Start > -1 && methodResult.End > -1) { // get the method formMethod = htmlCodes.Substring(methodResult.Start, 2); // validate the method formMethod = WebMethods.DetectMethod(formMethod, WebMethods.DefaultMethods.GET); htmlCodes = htmlCodes.Remove(methodResult.Start, methodResult.End - methodResult.Start); htmlCodes = htmlCodes.Insert(methodResult.Start, "POST"); } else { int formPos = StringCompare.IndexOfIgnoreCase(ref htmlCodes, "<form", cursorPos); int tagEnd; if (formPos != -1) { tagEnd = StringCompare.IndexOfMatchCase(ref htmlCodes, '>', formPos); if (tagEnd != -1) { htmlCodes = htmlCodes.Insert(tagEnd, " method=POST "); } } formMethod = WebMethods.GET; } } actionResult = HtmlParser.GetTagAttributeValuePos(ref htmlCodes, "<form", "action", cursorPos); if (actionResult.Start == -1) { break; } if (actionResult.Start > -1 && actionResult.End > -1) { cursorPos = actionResult.Start; //====== Correct value position according to quotes existence======= // actionResult = ASProxyFunctions.CorrectValueIfQuoteExists(ref pageHtml, actionResult); // Get the value actionSrc = htmlCodes.Substring(actionResult.Start, actionResult.End - actionResult.Start); // BUG fixed in v5 beta 2 // now supports forms with javascript if (UrlProvider.IsClientSitdeUrl(actionSrc) == false) { //====== Convert virtual url to absolute ====== actionSrc = UrlProvider.JoinUrl(actionSrc, pageUrlNoQuery, pagePath, siteRootUrl); //====== Delete invalid character such as tab and line feed ====== actionSrc = UrlProvider.IgnoreInvalidUrlCharctersInHtml(actionSrc); orgValue = actionSrc; //===== If another site url, has bookmark===== if (actionSrc.IndexOf('#') != -1) { actionSrc = UrlBuilder.RemoveUrlBookmark(actionSrc, out tmp); } //=====Get desired address======= actionSrc = HttpUtility.HtmlDecode(actionSrc); //====== Encode url to make unknown it ====== if (encodeUrl) { actionSrc = UrlProvider.EncodeUrl(actionSrc); } else { // just url safe actionSrc = UrlProvider.EscapeUrlQuery(actionSrc); } //====== Add it to our url ====== actionSrc = string.Format(newPageFormat, actionSrc); if (changeMethod) { //actionSrc = UrlBuilder.AddUrlQuery(actionSrc, Consts.qIsPostForm, ((int)method).ToString()); actionSrc = UrlBuilder.AddUrlQueryToEnd(actionSrc, Consts.Query.WebMethod, formMethod); } // Make it html safe actionSrc = HttpUtility.HtmlEncode(actionSrc); //====== Replace it with old url ====== htmlCodes = htmlCodes.Remove(actionResult.Start, actionResult.End - actionResult.Start); htmlCodes = htmlCodes.Insert(actionResult.Start, actionSrc); } else { // this is client side url addNewAttribute = false; } if (addNewAttribute) { // Apply orginal value and encoded value to format newAttribute = string.Format(extraAttributeFormat, orgValue, actionSrc, "POST"); // Locate end of tag cursorPos = StringCompare.IndexOfMatchCase(ref htmlCodes, '>', actionResult.Start); if (htmlCodes[cursorPos - 1] == '/') { cursorPos--; } // Insert to it htmlCodes = htmlCodes.Insert(cursorPos, newAttribute); } } else { if (actionResult.Start != -1) { cursorPos = actionResult.Start; } cursorPos = StringCompare.IndexOfMatchCase(ref htmlCodes, ">", cursorPos); } }while (actionResult.Start != -1); }