/// <summary> /// Saves back-end error response data /// </summary> /// <param name="webResponse"></param> protected virtual void FinalizeUnauthorizedWebResponse(WebResponse webResponse) { // Restore returned cookies to client if (RequestInfo.AcceptCookies) { try { Systems.CookieManager.RestoreCookiesFromResponse(webResponse, RequestInfo.TempCookies); ResponseInfo.Cookies = new CookieCollection(); Systems.CookieManager.RestoreCookiesFromResponse(webResponse, ResponseInfo.Cookies); } catch (Exception ex) { if (Systems.LogSystem.ErrorLogEnabled) { Systems.LogSystem.LogError(ex, "WebData.FinalizeUnauthorizedWebResponse", webResponse.ResponseUri.ToString()); } LastStatus = LastStatus.ContinueWithError; LastErrorMessage = ex.Message; LastException = ex; } } if (webResponse is HttpWebResponse) { // Response charset encoding, only for HTTP ResponseInfo.ContentEncoding = GetResponseEncoding((HttpWebResponse)webResponse, DefaultContentEncoding); } // Set redirect location ResponseInfo.AutoRedirectLocation = Consts.FilesConsts.PageAuthorization; ResponseInfo.AutoRedirectionType = AutoRedirectType.ASProxyPages; ResponseInfo.AutoRedirect = true; if (_requestProtocol != InternetProtocols.FTP) { // Get attached file name if available ResponseInfo.ContentFilename = DetectContentDispositionFile(webResponse.Headers["Content-Disposition"]); // Get response content type ResponseInfo.ContentType = webResponse.ContentType; } // Get response url address ResponseInfo.ResponseUrl = webResponse.ResponseUri.OriginalString; // Get response url base path ResponseInfo.ResponseRootUrl = UrlProvider.GetRootPath(webResponse.ResponseUri); // Get response application path ResponseInfo.ContentLength = webResponse.ContentLength; // response protocol ResponseInfo.ResponseProtocol = DetectWebResponseProtocol(webResponse); }
/// <summary> /// Saves back-end response data /// </summary> /// <param name="webResponse"></param> protected virtual void FinalizeWebResponse(WebResponse webResponse) { if (webResponse is HttpWebResponse) { HttpWebResponse httpResponse = (webResponse as HttpWebResponse); // Saves additional headers SaveResponseHeaders(httpResponse); // Response charset encoding, only for HTTP ResponseInfo.ContentEncoding = GetResponseEncoding(httpResponse, DefaultContentEncoding); if (_requestProtocol != InternetProtocols.FTP) { // Get attached file name if available ResponseInfo.ContentFilename = DetectContentDispositionFile(webResponse.Headers["Content-Disposition"]); // Get response content type ResponseInfo.ContentType = webResponse.ContentType; } // Get response url address ResponseInfo.ResponseUrl = webResponse.ResponseUri.OriginalString; // Get response url base path ResponseInfo.ResponseRootUrl = UrlProvider.GetRootPath(webResponse.ResponseUri); // Get response application path ResponseInfo.ContentLength = webResponse.ContentLength; // Redirect Location // ADDED Since 4.0; Ftp fixed in 4.8 string redirectLocation = null; // only in HTTP redirectLocation = webResponse.Headers[HttpResponseHeader.Location]; if (!string.IsNullOrEmpty(redirectLocation)) { if (UrlProvider.IsVirtualUrl(redirectLocation)) { // Get redirection full url redirectLocation = ReplaceResponseRedirectionLocation(webResponse.ResponseUri, ResponseInfo.ResponseRootUrl, redirectLocation); ResponseInfo.AutoRedirectionType = AutoRedirectType.RequestInternal; } else { ResponseInfo.AutoRedirectionType = AutoRedirectType.External; } ResponseInfo.AutoRedirectLocation = redirectLocation; ResponseInfo.AutoRedirect = true; } else { ResponseInfo.AutoRedirect = false; } } else if (webResponse is FtpWebResponse) { FtpWebResponse ftpResponse = (webResponse as FtpWebResponse); // Get response url address ResponseInfo.ResponseUrl = ftpResponse.ResponseUri.OriginalString; // Get response url base path ResponseInfo.ResponseRootUrl = UrlProvider.GetRootPath(webResponse.ResponseUri); // Get response application path ResponseInfo.ContentLength = ftpResponse.ContentLength; } // response protocol ResponseInfo.ResponseProtocol = DetectWebResponseProtocol(webResponse); // Restore returned cookies to client if (RequestInfo.AcceptCookies) { try { Systems.CookieManager.RestoreCookiesFromResponse(webResponse, RequestInfo.TempCookies); ResponseInfo.Cookies = new CookieCollection(); Systems.CookieManager.RestoreCookiesFromResponse(webResponse, ResponseInfo.Cookies); } catch (Exception ex) { if (Systems.LogSystem.ErrorLogEnabled) { Systems.LogSystem.LogError(ex, webResponse.ResponseUri.ToString()); } LastErrorMessage = ex.Message; LastException = ex; LastStatus = LastStatus.ContinueWithError; } } // Response status ApplyResponseHttpStatus(webResponse); }
/// <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; } }