Beispiel #1
0
        public override void Execute(ref string codes, string pageUrl, string pageUrlNoQuery, string pagePath, string rootUrl)
        {
            try
            {
                // 1- executing plugins
                if (Plugins.IsPluginAvailable(PluginHosts.IPluginCSSProcessor))
                {
                    Plugins.CallPluginMethod(PluginHosts.IPluginCSSProcessor,
                                             PluginMethods.IPluginCSSProcessor.BeforeExecute,
                                             this, (object)codes, pageUrl, pageUrlNoQuery, pagePath, rootUrl);
                }

                // ASProxy pages url formats generator
                ASProxyPagesFormat pages = new ASProxyPagesFormat(_UserOptions.EncodeUrl);

                // For @Import rule
                CSSReplacer.ReplaceCSSClassStyleUrl(ref codes,
                                                    pageUrlNoQuery,
                                                    pages.PageAnyType,
                                                    pagePath,
                                                    rootUrl,
                                                    _UserOptions.EncodeUrl,
                                                    true);

                // For backgrounds
                CSSReplacer.ReplaceCSSClassStyleUrl(ref codes,
                                                    pageUrlNoQuery,
                                                    pages.PageAnyType,
                                                    pagePath,
                                                    rootUrl,
                                                    _UserOptions.EncodeUrl,
                                                    false);


                // 2- executing plugins
                if (Plugins.IsPluginAvailable(PluginHosts.IPluginCSSProcessor))
                {
                    Plugins.CallPluginMethod(PluginHosts.IPluginCSSProcessor,
                                             PluginMethods.IPluginCSSProcessor.AfterExecute,
                                             this, (object)codes, pageUrl, pageUrlNoQuery, pagePath, rootUrl);
                }
            }
            catch (Exception ex)
            {
                // error logs
                if (Systems.LogSystem.ErrorLogEnabled)
                {
                    Systems.LogSystem.LogError(ex, pageUrl);
                }

                LastStatus       = LastStatus.ContinueWithError;
                LastException    = ex;
                LastErrorMessage = "ASProxy has some errors!";

                codes = "/* ASProxy has some errors! \n"
                        + ex.Message + " */"
                        + codes;
            }
        }
Beispiel #2
0
        public override void Execute(ref string codes, string pageUrl, string pageUrlNoQuery, string pagePath, string rootUrl)
        {
            try
            {
                // 1- executing plugins
                if (Plugins.IsPluginAvailable(PluginHosts.IPluginCSSProcessor))
                    Plugins.CallPluginMethod(PluginHosts.IPluginCSSProcessor,
                        PluginMethods.IPluginCSSProcessor.BeforeExecute,
                        this, (object)codes, pageUrl, pageUrlNoQuery, pagePath, rootUrl);

                // ASProxy pages url formats generator
                ASProxyPagesFormat pages = new ASProxyPagesFormat(_UserOptions.EncodeUrl);

                // For @Import rule
                CSSReplacer.ReplaceCSSClassStyleUrl(ref codes,
                    pageUrlNoQuery,
                    pages.PageAnyType,
                    pagePath,
                    rootUrl,
                    _UserOptions.EncodeUrl,
                    true);

                // For backgrounds
                CSSReplacer.ReplaceCSSClassStyleUrl(ref codes,
                    pageUrlNoQuery,
                    pages.PageAnyType,
                    pagePath,
                    rootUrl,
                    _UserOptions.EncodeUrl,
                    false);

                // 2- executing plugins
                if (Plugins.IsPluginAvailable(PluginHosts.IPluginCSSProcessor))
                    Plugins.CallPluginMethod(PluginHosts.IPluginCSSProcessor,
                        PluginMethods.IPluginCSSProcessor.AfterExecute,
                        this, (object)codes, pageUrl, pageUrlNoQuery, pagePath, rootUrl);

            }
            catch (Exception ex)
            {
                // error logs
                if (Systems.LogSystem.ErrorLogEnabled)
                    Systems.LogSystem.LogError(ex, pageUrl);

                LastStatus = LastStatus.ContinueWithError;
                LastException = ex;
                LastErrorMessage = "ASProxy has some errors!";

                codes = "/* ASProxy has some errors! \n"
                    + ex.Message + " */"
                    + codes;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Processes the html codes
        /// </summary>
        /// <param name="codes">Html codes</param>
        /// <param name="pageUrl">Page url. E.G. http://Site.com/users/profile.aspx?uid=90</param>
        /// <param name="rootUrl">Root path. E.G. http://Site.com/</param>
        public override void Execute(ref string codes, string pageUrl, string pageUrlNoQuery, string pagePath, string rootUrl)
        {
            try
            {
                // 1- executing plugins
                if (Plugins.IsPluginAvailable(PluginHosts.IPluginHtmlProcessor))
                    Plugins.CallPluginMethod(PluginHosts.IPluginHtmlProcessor,
                        PluginMethods.IPluginHtmlProcessor.BeforeExecute,
                        this, (object)codes, pageUrl, pageUrlNoQuery, pagePath, rootUrl);

                // ASProxy pages url formats generator
                ASProxyPagesFormat pages = new ASProxyPagesFormat(_UserOptions.EncodeUrl);

                // Original urls addistional codes option
                bool orginalUrlRequired = false;

                // Renames ASPDotNET standard ViewState name to a temporary name
                // This name will reset to default when the page posted back
                HtmlReplacer.ReplaceAspDotNETViewState(ref codes);

                // If remove scripts chosen, remove all the scripts.
                if (_UserOptions.RemoveScripts)
                    HtmlReplacer.RemoveScripts(ref codes);

                // If remove embeded objects is requested
                if (_UserOptions.RemoveObjects)
                {
                    // Removing <object> tag
                    HtmlParser.RemoveTagContent(ref codes, "object", true);

                    // Removing <embed> tag
                    HtmlParser.RemoveTagContent(ref codes, "embed", true);
                }

                // Applying the BASE tag to the URLs.
                string baseHref;
                if (HtmlReplacer.ReplaceBaseSources(ref codes, true, out baseHref))
                {
                    // changing page base path to specified Base in the document
                    pagePath = UrlProvider.AddSlashToEnd(baseHref);

                    // BUGFIX v4.6.1:: site root url should change also
                    rootUrl = UrlProvider.GetRootPath(rootUrl);
                }

                // processing style sheet links
                if (_UserOptions.Images)
                {
                    HtmlReplacer.ReplaceCssLinks(ref codes,
                        pageUrlNoQuery,
                        pages.PageAnyType,
                        pagePath,
                        rootUrl,
                        _UserOptions.EncodeUrl);

                    // TODO: CSSReplacer
                    // This function replaces "Import" rule and background images!
                    // So, this breaches to background option role. Since v4.0
                    CSSReplacer.ReplaceStyleTagStyleUrl(ref codes,
                        pageUrlNoQuery,
                        pages.PageAnyType,
                        pages.PageAnyType,
                        pagePath,
                        rootUrl,
                        _UserOptions.EncodeUrl);
                }

                // It seems with javascript encoding these methods are useless!!
                // The javascript may be disabled in browser so we have to this anyway
                if (_UserOptions.Links)
                {
                    string extraAttib = "";

                    // Add displaying orginal url address code
                    if (_UserOptions.OrginalUrl)
                    {
                        orginalUrlRequired = true;
                        extraAttib = Resources.STR_OrginalUrl_TagAttributeFormat;
                    }

                    // Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
                    extraAttib += Consts.ClientContent.attrAlreadyEncodedAttributeWithValue;

                    HtmlReplacer.ReplaceAnchors(ref codes,
                        pageUrlNoQuery,
                        pages.PageDefault,
                        pagePath,
                        rootUrl,
                        _UserOptions.EncodeUrl,
                        extraAttib);

                    HtmlReplacer.ReplaceHttpRefresh(ref codes,
                        pageUrlNoQuery,
                        pages.PageDefault,
                        pagePath,
                        rootUrl,
                        _UserOptions.EncodeUrl);
                }
                else
                {
                    string extraAttib;

                    // Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
                    extraAttib = Consts.ClientContent.attrAlreadyEncodedAttributeWithValue;

                    HtmlReplacer.ReplaceAnchors(ref codes,
                        pageUrlNoQuery,
                        "{0}",
                        pagePath,
                        rootUrl,
                        false,
                        extraAttib);

                    HtmlReplacer.ReplaceHttpRefresh(ref codes,
                        pageUrlNoQuery,
                        "{0}",
                        pagePath,
                        rootUrl,
                        _UserOptions.EncodeUrl);
                }

                if (_UserOptions.Frames)
                {
                    string extraAttrib = Resources.STR_IFrame_ExtraAttribute;

                    // Encode <iframe> tags
                    HtmlReplacer.ReplaceIFrames(ref codes,
                        pageUrlNoQuery,
                        pages.PageHtml,
                        pagePath,
                        rootUrl,
                        _UserOptions.EncodeUrl,
                        extraAttrib);

                    // Encode <framset> tags
                    HtmlReplacer.ReplaceFrameSets(ref codes,
                        pageUrlNoQuery,
                        pages.PageHtml,
                        pagePath,
                        rootUrl,
                        _UserOptions.EncodeUrl);
                }

                // Encode <img> tags
                if (_UserOptions.Images)
                {
                    string extraAttrib = "";

                    // Add code to display orginal url address
                    if (_UserOptions.OrginalUrl)
                    {
                        orginalUrlRequired = true;
                        extraAttrib = Resources.STR_OrginalUrl_TagAttributeFormat;
                    }

                    // Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
                    extraAttrib += Consts.ClientContent.attrAlreadyEncodedAttributeWithValue;

                    HtmlReplacer.ReplaceImages(ref codes,
                        pageUrlNoQuery,
                        pages.PageAnyType,
                        pagePath,
                        rootUrl,
                        _UserOptions.EncodeUrl,
                        extraAttrib);

                    // Encode background image of <body> , <table> and <td> tags
                    HtmlReplacer.ReplaceBackgrounds(ref codes,
                        pageUrlNoQuery,
                        pages.PageAnyType,
                        pagePath,
                        rootUrl,
                        _UserOptions.EncodeUrl);
                }
                else
                {
                    string extraAttrib;

                    // Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
                    extraAttrib = Consts.ClientContent.attrAlreadyEncodedAttributeWithValue;

                    HtmlReplacer.ReplaceImages(ref codes,
                        pageUrlNoQuery,
                        "{0}",
                        pagePath,
                        rootUrl,
                        false,
                        extraAttrib);
                }

                // Encodes script tags if RemoveScripts option is disabled
                if (_UserOptions.RemoveScripts == false)
                {
                    HtmlReplacer.ReplaceScripts(ref codes,
                        pageUrlNoQuery,
                        pages.PageAnyType,
                        pagePath,
                        rootUrl,
                        _UserOptions.EncodeUrl);

                    // TODO: JSReplacer
                    JSReplacer.ReplaceScriptTagCodes(ref codes);

                    // V5.2: Replaces tags events using RegEx
                    HtmlReplacer.ReplaceTagsEvents(ref codes,
                      pageUrlNoQuery,
                      pages.PageAnyType,
                      pagePath,
                      pageUrl,
                      rootUrl,
                      _UserOptions.EncodeUrl);
                }

                // Encode <embed> tags
                HtmlReplacer.ReplaceEmbeds(ref codes,
                    pageUrlNoQuery,
                    pages.PageAnyType,
                    pagePath,
                    rootUrl,
                    _UserOptions.EncodeUrl);

                // Encode <form> tags
                if (_UserOptions.SubmitForms)
                {
                    string extraAttrib;

                    // Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
                    extraAttrib =
                        Consts.ClientContent.attrAlreadyEncodedAttributeWithValue
                        + Resources.STR_SubmitForms_ExtraAttribute;

                    HtmlReplacer.ReplaceFormsSources(ref codes,
                        pageUrlNoQuery,
                        pages.PageDefault,
                        pagePath,
                        rootUrl,
                        _UserOptions.EncodeUrl,
                        _UserOptions.SubmitForms,
                        extraAttrib);
                }
                else
                {
                    string extraAttib;

                    // Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
                    extraAttib =
                        Consts.ClientContent.attrAlreadyEncodedAttributeWithValue
                        + Resources.STR_SubmitForms_ExtraAttribute;

                    HtmlReplacer.ReplaceFormsSources(ref codes,
                        pageUrlNoQuery,
                        "{0}",
                        pagePath,
                        pagePath,
                        false,
                        _UserOptions.SubmitForms,
                        extraAttib);
                }

                // Add dynamic encoding javascript codes
                string jsEncoderCodes = GenerateJsEncoderCodes(pageUrl,
                            pageUrlNoQuery,
                            pagePath,
                            rootUrl);

                // Add jsEncoder codes to page
                ExtraCodesForPage = jsEncoderCodes + ExtraCodesForPage;

                // OrginalUrl additional injection html codes
                if (orginalUrlRequired)
                {
                    // TODO: Check necessary
                    ExtraCodesForPage = Resources.STR_OrginalUrl_FloatBar + ExtraCodesForPage;

                    // Inject to html, right after the body element
                    ExtraCodesForBody = Resources.STR_OrginalUrl_Functions + ExtraCodesForBody;
                }

                // 2- executing plugins
                if (Plugins.IsPluginAvailable(PluginHosts.IPluginHtmlProcessor))
                    Plugins.CallPluginMethod(PluginHosts.IPluginHtmlProcessor,
                        PluginMethods.IPluginHtmlProcessor.AfterExecute,
                        this, (object)codes, pageUrl, pageUrlNoQuery, pagePath, rootUrl);

            }
            catch (Exception ex)
            {
                // error logs
                if (Systems.LogSystem.ErrorLogEnabled)
                    Systems.LogSystem.LogError(ex, pageUrl);

                codes = "<center><b>ASProxy has some errors! The delivered content may not work properly.</b></center>" + ex.Message + "<br />"
                    + codes;
            }
        }
		public override void Execute(ref string codes, string pageUrl, string pageUrlNoQuery, string pagePath, string rootUrl)
		{
			try
			{
				// 1- executing plugins
				if (Plugins.IsPluginAvailable(PluginHosts.IPluginHtmlProcessor))
					Plugins.CallPluginMethod(PluginHosts.IPluginHtmlProcessor,
						PluginMethods.IPluginHtmlProcessor.BeforeExecute,
						this, (object)codes, pageUrl, pageUrlNoQuery, pagePath, rootUrl);

				// ASProxy pages url formats generator
				ASProxyPagesFormat pages = new ASProxyPagesFormat(_UserOptions.EncodeUrl);

				// Original urls addistional codes option
				bool orginalUrlRequired = false;
				string regexPattarn;


				// Renames ASPDotNET standard ViewState name to a temporary name
				// This name will reset to default when the page posted back
				HtmlReplacer.ReplaceAspDotNETViewState(ref codes);


				// If remove scripts chosen, remove all the scripts.
				if (_UserOptions.RemoveScripts)
					HtmlReplacer.RemoveScripts(ref codes);

				// If remove embeded objects is requested
				if (_UserOptions.RemoveObjects)
				{
					// Removing <object> tag
					HtmlParser.RemoveTagContent(ref codes, "object", true);

					// Removing <embed> tag
					HtmlParser.RemoveTagContent(ref codes, "embed", true);
				}


				// Applying the BASE tag to the URLs.
				string baseHref;
				if (GetBaseTagSource(ref codes, out baseHref))
				{
					// changing page base path to specified Base in the document
					pagePath = UrlProvider.AddSlashToEnd(baseHref);

					// site root url should change also
					rootUrl = UrlProvider.GetRootPath(pagePath);
				}

				// splitting the code by scripts
				regexPattarn = @"<script\b[^>]*>(?:.*?)</script>";
				MatchCollection matchColl = Regex.Matches(codes,
												regexPattarn,
												RegexOptions.Compiled |
												RegexOptions.Singleline |
												RegexOptions.IgnoreCase);
				// check if is any script found
				if (matchColl.Count > 0)
				{
					// end of capture is end of html
					int endOfCapture = codes.Length;
					string codePart;
					int scriptEnd;
					bool processScripts = !_UserOptions.RemoveScripts;

					for (int i = matchColl.Count - 1; i >= 0; i--)
					{
						Match match = matchColl[i];
						scriptEnd = match.Index + match.Length;

						// grab the part
						codePart = codes.Substring(scriptEnd, endOfCapture - scriptEnd);

						// empty test
						if (codePart.Length > 0)
						{
							// Proccess the selected html part
							ProcessHtml(ref codePart, pageUrl, pageUrlNoQuery, pagePath, rootUrl, ref orginalUrlRequired, pages);

							// apply processed part
							codes = codes.Remove(scriptEnd, endOfCapture - scriptEnd);
							codes = codes.Insert(scriptEnd, codePart);
						}


						// removes scripts
						if (processScripts && match.Length > 0)
						{
							// grab the script part
							codePart = codes.Substring(match.Index, match.Length);

							// processes embeded scripts
							ReplaceEmbededScript(ref codePart,
									pageUrl,
									pageUrlNoQuery,
									pagePath,
									rootUrl);

							// Replaces scripts source 
							regexPattarn = @"(?><script)(?>\s+[^>\s]+)*?\s*(?>src\s*=(?!\\)\s*)(?>(['""])?)(?<URL>(?(1)(?(?<="")[^""]+|[^']+)|[^ >]+))(?(1)\1|)";
							ApplyToUrlSpecifed(ref codePart,
								regexPattarn,
								pageUrl,
								pageUrlNoQuery,
								pagePath,
								rootUrl,
								pages.PageJS,
								_UserOptions.EncodeUrl,
								null);

							// apply processed part
							codes = codes.Remove(match.Index, match.Length);
							codes = codes.Insert(match.Index, codePart);

						}


						// grab until this point
						endOfCapture = match.Index;
					}

					// and here we should process the reset of contents
					scriptEnd = 0;

					// grab the part
					codePart = codes.Substring(scriptEnd, endOfCapture - scriptEnd);

					// Proccess the selected html part
					ProcessHtml(ref codePart, pageUrl, pageUrlNoQuery, pagePath, rootUrl, ref orginalUrlRequired, pages);

					// apply processed part
					codes = codes.Remove(scriptEnd, endOfCapture - scriptEnd);
					codes = codes.Insert(scriptEnd, codePart);

					// releasing memory
					codePart = null;

				}
				else
				{
					// Proccess the html
					ProcessHtml(ref codes, pageUrl, pageUrlNoQuery, pagePath, rootUrl, ref orginalUrlRequired, pages);
				}

				// Adding dynamic encoding javascript codes
				string jsEncoderCodes = GenerateJsEncoderCodes(pageUrl,
							pageUrlNoQuery,
							pagePath,
							rootUrl);

				// Add jsEncoder codes to page
				ExtraCodesForPage = jsEncoderCodes + ExtraCodesForPage;

				// OrginalUrl additional injection html codes
				if (orginalUrlRequired)
				{
					// Inject to html, should run after body tag
					ExtraCodesForBody = Resources.STR_OrginalUrl_Functions + ExtraCodesForBody;

					// TODO: Check necessary
					ExtraCodesForPage = Resources.STR_OrginalUrl_FloatBar + ExtraCodesForPage;
				}

				// 2- executing plugins
				if (Plugins.IsPluginAvailable(PluginHosts.IPluginHtmlProcessor))
					Plugins.CallPluginMethod(PluginHosts.IPluginHtmlProcessor,
						PluginMethods.IPluginHtmlProcessor.AfterExecute,
						this, (object)codes, pageUrl, pageUrlNoQuery, pagePath, rootUrl);

			}
			catch (Exception ex)
			{
				// error logs
				if (Systems.LogSystem.ErrorLogEnabled)
					Systems.LogSystem.LogError(ex, pageUrl);

				LastStatus = LastStatus.ContinueWithError;
				LastException = ex;
				LastErrorMessage = "ASProxy has some errors! The delivered content may not work properly.";
			}
		}
		protected virtual void ApplyToUrlSpecifiedAutoDetect(
			ref string codes,
			string pattern,
			string pageUrl,
			string pageUrlNoQuery,
			string pagePath,
			string rootUrl,
			ASProxyPagesFormat pages,
			bool encodeUrl,
			string extraAttributeFormat)
		{
			try
			{

				bool addNewAttribute = false;
				bool hasNewAttribute = false;
				string newPageFormat;

				if (!string.IsNullOrEmpty(extraAttributeFormat))
				{
					addNewAttribute = true;
					hasNewAttribute = true;
				}

				MatchCollection matchColl = Regex.Matches(codes,
												pattern,
												RegexOptions.IgnoreCase |
												RegexOptions.Compiled);
				for (int i = matchColl.Count - 1; i >= 0; i--)
				{
					addNewAttribute = hasNewAttribute;

					Match match = matchColl[i];
					Group group = match.Groups["URL"];

					Group tagGroup = match.Groups["T"];

					// asproxy page, default is anytype
					newPageFormat = pages.PageAnyType;

					// if tag found
					if (tagGroup != null)
					{
						switch (tagGroup.Value.ToLower())
						{
							case "img":
								newPageFormat = pages.PageImage;
								break;
							case "script":
								newPageFormat = pages.PageJS;
								break;
							case "embed":
								newPageFormat = pages.PageAnyType;
								break;
						}
					}

					if (group != null)
					{
						string matchValue = group.Value;
						string orgValue;


						//===== If link is a bookmark don't change it=====
						if (matchValue.StartsWith("#"))
							continue;

						// Decode html code
						// some codes are in hex e.g. &#39; represents (')
						matchValue = HttpUtility.HtmlDecode(matchValue);

						// removes anu quotes from beginning and ending
						matchValue = HtmlTags.RemoveQuotesFromTagAttributeValue(matchValue);

						// if it is client side script
						if (UrlProvider.IsClientSitdeUrl(matchValue) == false)
						{
							string bookmarkPart = string.Empty;

							// Convert virtual url to absolute
							matchValue = UrlProvider.JoinUrl(matchValue, pageUrlNoQuery, pagePath, rootUrl);

							// Delete invalid character such as tab and line feed
							matchValue = UrlProvider.IgnoreInvalidUrlCharctersInHtml(matchValue);

							// Save orginal value
							orgValue = matchValue;

							//===== If another site url, has bookmark
							if (matchValue.IndexOf('#') != -1)
								matchValue = UrlBuilder.RemoveUrlBookmark(matchValue, out bookmarkPart);

							//====== Encode url to make it unknown ======
							if (encodeUrl)
								matchValue = UrlProvider.EncodeUrl(matchValue);
							else
								// just url safe
								matchValue = UrlProvider.EscapeUrlQuery(matchValue);

							//====== Add it to our url ======
							matchValue = string.Format(newPageFormat, matchValue);

							//===== Add bookmark to the end of url =====
							if (bookmarkPart.Length > 0)
							{
								matchValue += bookmarkPart;
								bookmarkPart = "";
							}
						}
						else
						{
							// parse the javascript url
							if (UrlProvider.IsJavascriptUrl(matchValue))
							{
								// execute
								Processors.JSProcessor.Execute(ref matchValue,
									pageUrl,
									pageUrlNoQuery,
									pagePath,
									rootUrl);
							}


							orgValue = matchValue;
							addNewAttribute = false;
						}

						//====== Make it safe
						matchValue = HttpUtility.HtmlEncode(matchValue);


						if (addNewAttribute)
						{
							// Apply original value and encoded value to format
							// BUG: Problem with format string that contain (') or (") characters
							// Bug Fixed since version 4.7
							string newAttribute = string.Format(extraAttributeFormat, orgValue, matchValue);

							Group extGroup = match.Groups["EXT"];
							if (extGroup != null)
							{
								codes = codes.Insert(extGroup.Index, newAttribute);
							}
						}

						// Replace the tag
						codes = codes.Remove(group.Index, group.Length);
						codes = codes.Insert(group.Index, matchValue);
					}
				}
			}
			catch (Exception ex)
			{
				// error logs
				if (Systems.LogSystem.ErrorLogEnabled)
					Systems.LogSystem.LogError(ex, pagePath);

				LastStatus = LastStatus.ContinueWithError;
				LastException = ex;
				LastErrorMessage = "Failed to process some contents.";
			}
		}
		private void ProcessHtml(ref string codes, string pageUrl, string pageUrlNoQuery, string pagePath, string rootUrl, ref bool orginalUrlRequired, ASProxyPagesFormat pages)
		{
			string regexPattarn;

			// HttpRefresh
			// replaces <meta http-equiv="refresh" content=1;url=HttpRefresh.htm>
			// BUGFIX v5.5b2: Mono has some issue with "(?(UQ)\2|)(?(Q)\1|)", it is a backtrack to detect (") and (') after the URL, specially when we have something like: content="0;url='refresh.html'"
			// here is the original one, regexPattarn = @"<meta(?>\s+[^>\s]+)*?\s*content\s*=\s*(?<Q>[""'])?[0-9]+\s*;\s*url=(?<UQ>['""]|['])?(?<URL>(?(?<="")[^""]+|(?(?<=')[^']+|[^'"" >]+)))(?(UQ)\2|)(?(Q)\1|)";
			regexPattarn = @"<meta(?>\s+[^>\s]+)*?\s*content\s*=\s*(?<Q>[""'])?[0-9]+\s*;\s*url=(?<UQ>['""]|['])?(?<URL>(?(?<="")[^""]+|(?(?<=')[^']+|[^'"" >]+)))";
			ApplyToUrlSpecifed(ref codes,
				regexPattarn,
				pageUrl,
				pageUrlNoQuery,
				pagePath,
				rootUrl,
				pages.PageDefault,
				_UserOptions.EncodeUrl,
				null);


			if (_UserOptions.Links)
			{
				string extraAttib;

				// Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
				extraAttib = Consts.ClientContent.attrAlreadyEncodedAttributeWithValue;

				// Add displaying orginal url address code
				if (_UserOptions.OrginalUrl)
				{
					orginalUrlRequired = true;
					extraAttib += Resources.STR_OrginalUrl_TagAttributeFormat;
				}

				// Anchors
				// Special replace, anchors should go to main page
				// the "EXT" in regex is required to inject extra attributes
				regexPattarn = @"(?><a)(?>\s+[^>\s]+)*?\s*(?>href\s*=(?!\\)\s*)(?>(['""])?)(?<URL>(?(1)(?(?<="")[^""]+|[^']+)|[^ >]+))(?(1)\1|)(?<EXT>)";
				ApplyToUrlSpecifed(ref codes,
					regexPattarn,
					pageUrl,
					pageUrlNoQuery,
					pagePath,
					rootUrl,
					pages.PageDefault,
					_UserOptions.EncodeUrl,
					extraAttib);
			}
			else
			{
				string extraAttib;

				// Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
				extraAttib = Consts.ClientContent.attrAlreadyEncodedAttributeWithValue;

				// Anchors
				// Special replace, anchors should go to main page
				// the "EXT" in regex is required to inject extra attributes
				regexPattarn = @"(?><a)(?>\s+[^>\s]+)*?\s*(?>href\s*=(?!\\)\s*)(?>(['""])?)(?<URL>(?(1)(?(?<="")[^""]+|[^']+)|[^ >]+))(?(1)\1|)(?<EXT>)";
				ApplyToUrlSpecifed(ref codes,
					regexPattarn,
					pageUrl,
					pageUrlNoQuery,
					pagePath,
					rootUrl,
					"{0}",
					_UserOptions.EncodeUrl,
					extraAttib);
			}

			if (_UserOptions.Images)
			{
				// Background attribute
				regexPattarn = @"(?><[A-Z][A-Z0-9]+)(?>\s+[^>\s]+)*?\s*(?>background\s*=(?!\\)\s*)(?>(['""])?)(?<URL>(?(1)(?(?<="")[^""]+|[^']+)|[^ >]+))(?(1)\1|)";
				ApplyToUrlSpecifed(ref codes,
					regexPattarn,
					pageUrl,
					pageUrlNoQuery,
					pagePath,
					rootUrl,
					pages.PageAnyType,
					_UserOptions.EncodeUrl,
					null);

				// Replace embedded styles
				ReplaceEmbededStyle(ref codes,
						pageUrl,
						pageUrlNoQuery,
						pagePath,
						rootUrl);

				// Replace inline styles
				ReplaceInlineStyle(ref codes,
						pageUrl,
						pageUrlNoQuery,
						pagePath,
						rootUrl);

				// Replace with IMG tag
				// Replaces Link|Embed|script  for  href|src|background
				// FOR NEXT ApplyToUrlSpecifed
				regexPattarn = @"(?><(?<T>img|link|embed|script))(?>\s+[^>\s]+)*?\s*(?>(?<A>href|src)\s*=(?!\\)\s*)(?>(['""])?)(?<URL>(?(1)(?(?<="")[^""]+|[^']+)|[^ >]+))(?(1)\1|)";
			}
			else
				// Replace without IMG tag
				// Replaces link|embed|script  for  href|src|background
				// FOR NEXT ApplyToUrlSpecifed
				regexPattarn = @"(?><(?<T>link|embed|script))(?>\s+[^>\s]+)*?\s*(?>(?<A>href|src)\s*=(?!\\)\s*)(?>(['""])?)(?<URL>(?(1)(?(?<="")[^""]+|[^']+)|[^ >]+))(?(1)\1|)";
			// Replaces link|embed|script  for  href|src|background
			ApplyToUrlSpecifiedAutoDetect(ref codes,
					regexPattarn,
					pageUrl,
					pageUrlNoQuery,
					pagePath,
					rootUrl,
					pages,
					_UserOptions.EncodeUrl,
					null);

			// Tags events
			// Replaces tags events using RegEx
			ReplaceTagsEvents(ref codes,
					pageUrl,
					pageUrlNoQuery,
					pagePath,
					rootUrl);

			if (_UserOptions.Frames)
			{
				string extraAttib;

				// Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
				extraAttib = Consts.ClientContent.attrAlreadyEncodedAttributeWithValue;

				// Adds onload event handler to detect the frame changes
				extraAttib += Resources.STR_IFrame_ExtraAttribute;

				// Iframe / Frameset
				// the "EXT" in regex is required to inject extra attributes
				regexPattarn = @"(?><(?<T>iframe|frame))(?>\s+[^>\s]+)*?\s*(?>src\s*=(?!\\)\s*)(?>(['""])?)(?<URL>(?(1)(?(?<="")[^""]+|[^']+)|[^ >]+))(?(1)\1|)(?<EXT>)";
				ApplyToUrlSpecifed(ref codes,
					regexPattarn,
					pageUrl,
					pageUrlNoQuery,
					pagePath,
					rootUrl,
					pages.PageHtml,
					_UserOptions.EncodeUrl,
					extraAttib);
			}

			// Object tags 
			// <param name="movie" value="before.swf">
			ReplaceObjectsMovie(ref codes,
				pageUrl,
				pageUrlNoQuery,
				pagePath,
				rootUrl,
				pages.PageAnyType,
				_UserOptions.EncodeUrl,
				null);


			// Encode <form> tags
			if (_UserOptions.SubmitForms)
			{
				string extraAttrib;

				// Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
				extraAttrib =
					Consts.ClientContent.attrAlreadyEncodedAttributeWithValue
					+ Resources.STR_SubmitForms_ExtraAttribute;

				HtmlReplacer.ReplaceFormsSources(ref codes,
					pageUrlNoQuery,
					pages.PageDefault,
					pagePath,
					rootUrl,
					_UserOptions.EncodeUrl,
					_UserOptions.SubmitForms,
					extraAttrib);
			}
			else
			{
				string extraAttib;

				// Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
				extraAttib =
					Consts.ClientContent.attrAlreadyEncodedAttributeWithValue
					+ Resources.STR_SubmitForms_ExtraAttribute;

				HtmlReplacer.ReplaceFormsSources(ref codes,
					pageUrlNoQuery,
					"{0}",
					pagePath,
					pagePath,
					false,
					_UserOptions.SubmitForms,
					extraAttib);
			}
		}
Beispiel #7
0
        /// <summary>
        /// Processes the html codes
        /// </summary>
        /// <param name="codes">Html codes</param>
        /// <param name="pageUrl">Page url. E.G. http://Site.com/users/profile.aspx?uid=90</param>
        /// <param name="rootUrl">Root path. E.G. http://Site.com/</param>
        public override void Execute(ref string codes, string pageUrl, string pageUrlNoQuery, string pagePath, string rootUrl)
        {
            try
            {
                // 1- executing plugins
                if (Plugins.IsPluginAvailable(PluginHosts.IPluginHtmlProcessor))
                {
                    Plugins.CallPluginMethod(PluginHosts.IPluginHtmlProcessor,
                                             PluginMethods.IPluginHtmlProcessor.BeforeExecute,
                                             this, (object)codes, pageUrl, pageUrlNoQuery, pagePath, rootUrl);
                }

                // ASProxy pages url formats generator
                ASProxyPagesFormat pages = new ASProxyPagesFormat(_UserOptions.EncodeUrl);

                // Original urls addistional codes option
                bool orginalUrlRequired = false;


                // Renames ASPDotNET standard ViewState name to a temporary name
                // This name will reset to default when the page posted back
                HtmlReplacer.ReplaceAspDotNETViewState(ref codes);


                // If remove scripts chosen, remove all the scripts.
                if (_UserOptions.RemoveScripts)
                {
                    HtmlReplacer.RemoveScripts(ref codes);
                }


                // If remove embeded objects is requested
                if (_UserOptions.RemoveObjects)
                {
                    // Removing <object> tag
                    HtmlParser.RemoveTagContent(ref codes, "object", true);

                    // Removing <embed> tag
                    HtmlParser.RemoveTagContent(ref codes, "embed", true);
                }

                // Applying the BASE tag to the URLs.
                string baseHref;
                if (HtmlReplacer.ReplaceBaseSources(ref codes, true, out baseHref))
                {
                    // changing page base path to specified Base in the document
                    pagePath = UrlProvider.AddSlashToEnd(baseHref);

                    // BUGFIX v4.6.1:: site root url should change also
                    rootUrl = UrlProvider.GetRootPath(rootUrl);
                }

                // processing style sheet links
                if (_UserOptions.Images)
                {
                    HtmlReplacer.ReplaceCssLinks(ref codes,
                                                 pageUrlNoQuery,
                                                 pages.PageAnyType,
                                                 pagePath,
                                                 rootUrl,
                                                 _UserOptions.EncodeUrl);

                    // TODO: CSSReplacer
                    // This function replaces "Import" rule and background images!
                    // So, this breaches to background option role. Since v4.0
                    CSSReplacer.ReplaceStyleTagStyleUrl(ref codes,
                                                        pageUrlNoQuery,
                                                        pages.PageAnyType,
                                                        pages.PageAnyType,
                                                        pagePath,
                                                        rootUrl,
                                                        _UserOptions.EncodeUrl);
                }

                // It seems with javascript encoding these methods are useless!!
                // The javascript may be disabled in browser so we have to this anyway
                if (_UserOptions.Links)
                {
                    string extraAttib = "";

                    // Add displaying orginal url address code
                    if (_UserOptions.OrginalUrl)
                    {
                        orginalUrlRequired = true;
                        extraAttib         = Resources.STR_OrginalUrl_TagAttributeFormat;
                    }

                    // Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
                    extraAttib += Consts.ClientContent.attrAlreadyEncodedAttributeWithValue;

                    HtmlReplacer.ReplaceAnchors(ref codes,
                                                pageUrlNoQuery,
                                                pages.PageDefault,
                                                pagePath,
                                                rootUrl,
                                                _UserOptions.EncodeUrl,
                                                extraAttib);

                    HtmlReplacer.ReplaceHttpRefresh(ref codes,
                                                    pageUrlNoQuery,
                                                    pages.PageDefault,
                                                    pagePath,
                                                    rootUrl,
                                                    _UserOptions.EncodeUrl);
                }
                else
                {
                    string extraAttib;

                    // Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
                    extraAttib = Consts.ClientContent.attrAlreadyEncodedAttributeWithValue;

                    HtmlReplacer.ReplaceAnchors(ref codes,
                                                pageUrlNoQuery,
                                                "{0}",
                                                pagePath,
                                                rootUrl,
                                                false,
                                                extraAttib);

                    HtmlReplacer.ReplaceHttpRefresh(ref codes,
                                                    pageUrlNoQuery,
                                                    "{0}",
                                                    pagePath,
                                                    rootUrl,
                                                    _UserOptions.EncodeUrl);
                }

                if (_UserOptions.Frames)
                {
                    string extraAttrib = Resources.STR_IFrame_ExtraAttribute;

                    // Encode <iframe> tags
                    HtmlReplacer.ReplaceIFrames(ref codes,
                                                pageUrlNoQuery,
                                                pages.PageHtml,
                                                pagePath,
                                                rootUrl,
                                                _UserOptions.EncodeUrl,
                                                extraAttrib);

                    // Encode <framset> tags
                    HtmlReplacer.ReplaceFrameSets(ref codes,
                                                  pageUrlNoQuery,
                                                  pages.PageHtml,
                                                  pagePath,
                                                  rootUrl,
                                                  _UserOptions.EncodeUrl);
                }

                // Encode <img> tags
                if (_UserOptions.Images)
                {
                    string extraAttrib = "";

                    // Add code to display orginal url address
                    if (_UserOptions.OrginalUrl)
                    {
                        orginalUrlRequired = true;
                        extraAttrib        = Resources.STR_OrginalUrl_TagAttributeFormat;
                    }

                    // Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
                    extraAttrib += Consts.ClientContent.attrAlreadyEncodedAttributeWithValue;

                    HtmlReplacer.ReplaceImages(ref codes,
                                               pageUrlNoQuery,
                                               pages.PageAnyType,
                                               pagePath,
                                               rootUrl,
                                               _UserOptions.EncodeUrl,
                                               extraAttrib);

                    // Encode background image of <body> , <table> and <td> tags
                    HtmlReplacer.ReplaceBackgrounds(ref codes,
                                                    pageUrlNoQuery,
                                                    pages.PageAnyType,
                                                    pagePath,
                                                    rootUrl,
                                                    _UserOptions.EncodeUrl);
                }
                else
                {
                    string extraAttrib;

                    // Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
                    extraAttrib = Consts.ClientContent.attrAlreadyEncodedAttributeWithValue;

                    HtmlReplacer.ReplaceImages(ref codes,
                                               pageUrlNoQuery,
                                               "{0}",
                                               pagePath,
                                               rootUrl,
                                               false,
                                               extraAttrib);
                }

                // Encodes script tags if RemoveScripts option is disabled
                if (_UserOptions.RemoveScripts == false)
                {
                    HtmlReplacer.ReplaceScripts(ref codes,
                                                pageUrlNoQuery,
                                                pages.PageAnyType,
                                                pagePath,
                                                rootUrl,
                                                _UserOptions.EncodeUrl);

                    // TODO: JSReplacer
                    JSReplacer.ReplaceScriptTagCodes(ref codes);

                    // V5.2: Replaces tags events using RegEx
                    HtmlReplacer.ReplaceTagsEvents(ref codes,
                                                   pageUrlNoQuery,
                                                   pages.PageAnyType,
                                                   pagePath,
                                                   pageUrl,
                                                   rootUrl,
                                                   _UserOptions.EncodeUrl);
                }

                // Encode <embed> tags
                HtmlReplacer.ReplaceEmbeds(ref codes,
                                           pageUrlNoQuery,
                                           pages.PageAnyType,
                                           pagePath,
                                           rootUrl,
                                           _UserOptions.EncodeUrl);

                // Encode <form> tags
                if (_UserOptions.SubmitForms)
                {
                    string extraAttrib;

                    // Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
                    extraAttrib =
                        Consts.ClientContent.attrAlreadyEncodedAttributeWithValue
                        + Resources.STR_SubmitForms_ExtraAttribute;

                    HtmlReplacer.ReplaceFormsSources(ref codes,
                                                     pageUrlNoQuery,
                                                     pages.PageDefault,
                                                     pagePath,
                                                     rootUrl,
                                                     _UserOptions.EncodeUrl,
                                                     _UserOptions.SubmitForms,
                                                     extraAttrib);
                }
                else
                {
                    string extraAttib;

                    // Add an attribute to determine that this tag already encoded and javascript encoder shouldn't encode it again.
                    extraAttib =
                        Consts.ClientContent.attrAlreadyEncodedAttributeWithValue
                        + Resources.STR_SubmitForms_ExtraAttribute;

                    HtmlReplacer.ReplaceFormsSources(ref codes,
                                                     pageUrlNoQuery,
                                                     "{0}",
                                                     pagePath,
                                                     pagePath,
                                                     false,
                                                     _UserOptions.SubmitForms,
                                                     extraAttib);
                }

                // Add dynamic encoding javascript codes
                string jsEncoderCodes = GenerateJsEncoderCodes(pageUrl,
                                                               pageUrlNoQuery,
                                                               pagePath,
                                                               rootUrl);


                // Add jsEncoder codes to page
                ExtraCodesForPage = jsEncoderCodes + ExtraCodesForPage;

                // OrginalUrl additional injection html codes
                if (orginalUrlRequired)
                {
                    // TODO: Check necessary
                    ExtraCodesForPage = Resources.STR_OrginalUrl_FloatBar + ExtraCodesForPage;

                    // Inject to html, right after the body element
                    ExtraCodesForBody = Resources.STR_OrginalUrl_Functions + ExtraCodesForBody;
                }


                // 2- executing plugins
                if (Plugins.IsPluginAvailable(PluginHosts.IPluginHtmlProcessor))
                {
                    Plugins.CallPluginMethod(PluginHosts.IPluginHtmlProcessor,
                                             PluginMethods.IPluginHtmlProcessor.AfterExecute,
                                             this, (object)codes, pageUrl, pageUrlNoQuery, pagePath, rootUrl);
                }
            }
            catch (Exception ex)
            {
                // error logs
                if (Systems.LogSystem.ErrorLogEnabled)
                {
                    Systems.LogSystem.LogError(ex, pageUrl);
                }

                codes = "<center><b>ASProxy has some errors! The delivered content may not work properly.</b></center>" + ex.Message + "<br />"
                        + codes;
            }
        }