How to navigate to local file with ChromeDriver using a file path? - c#

I am building a set of tests for an automation framework, but am running into an issue navigating to a local HTML page I have created.
Here is where I'm creating the ChromeDriver instance.
if (AllowFileAccessAcrossFiles)
{
ChromeOptions options = new ChromeOptions();
// Have tried, none, individually, as well as both.
options.AddArgument("--allow-file-access-from-files");
options.AddArgument("--enable-local-file-accesses ");
driver = new ChromeDriver(options);
}
This ChromeDriver instance is later passed into the NgWebDriver class so that I am able to use Protractor-net in my tests as well as abstract the test tools.
internal TestWebDriver(RemoteWebDriver driver, TestConfiguration configuration)
{
// ...
_driver = new NgWebDriver(driver);
// ...
}
When the framework calls into the driver to navigate to the page it passes the correct file path ("file:/// ..."), but it never makes it into the browser URL and is not navigated to. (i.e. the URL says data;)
How do I navigate to a local HTML page with a file path with ChromeDriver?

It turns out that this resolution to this issue is rooted in NgWebDriver. NgWebDriver defers to the drivers for IE, Edge, PhantomJS, Firefox, and Safari to navigate to the URL, but if it is anything else then it just runs this: this.ExecuteScript("window.name += '" + AngularDeferBootstrap + "'; window.location.href = '" + value + "';");
The JavaScript method that is being called doesn't handle just passing in a local path, it needs an http(s) string to navigate. So whether or not we can pass in a local path is up to the specific driver's implementation of the set method for the Url property.
Below is the relevant Protractor-net property.
public class NgWebDriver : IWebDriver, IWrapsDriver, IJavaScriptExecutor
{
private const string AngularDeferBootstrap = "NG_DEFER_BOOTSTRAP!";
private IWebDriver driver;
private IJavaScriptExecutor jsExecutor;
private string rootElement;
private IList<NgModule> mockModules;
// constructors and stuff
/// <summary>
/// Gets or sets the URL the browser is currently displaying.
/// </summary>
public string Url
{
get
{
this.WaitForAngular();
return this.driver.Url;
}
set
{
// Reset URL
this.driver.Url = "about:blank";
// TODO: test Android
IHasCapabilities hcDriver = this.driver as IHasCapabilities;
if (hcDriver != null &&
(hcDriver.Capabilities.BrowserName == "internet explorer" ||
hcDriver.Capabilities.BrowserName == "MicrosoftEdge" ||
hcDriver.Capabilities.BrowserName == "phantomjs" ||
hcDriver.Capabilities.BrowserName == "firefox" ||
hcDriver.Capabilities.BrowserName.ToLower() == "safari"))
{
this.ExecuteScript("window.name += '" + AngularDeferBootstrap + "';");
this.driver.Url = value;
}
else
{
this.ExecuteScript("window.name += '" + AngularDeferBootstrap + "'; window.location.href = '" + value + "';");
}
if (!this.IgnoreSynchronization)
{
try
{
// Make sure the page is an Angular page.
long? angularVersion = this.ExecuteAsyncScript(ClientSideScripts.TestForAngular) as long?;
if (angularVersion.HasValue)
{
if (angularVersion.Value == 1)
{
// At this point, Angular will pause for us, until angular.resumeBootstrap is called.
// Add default module for Angular v1
this.mockModules.Add(new Ng1BaseModule());
// Register extra modules
foreach (NgModule ngModule in this.mockModules)
{
this.ExecuteScript(ngModule.Script);
}
// Resume Angular bootstrap
this.ExecuteScript(ClientSideScripts.ResumeAngularBootstrap,
String.Join(",", this.mockModules.Select(m => m.Name).ToArray()));
}
else if (angularVersion.Value == 2)
{
if (this.mockModules.Count > 0)
{
throw new NotSupportedException("Mock modules are not supported in Angular 2");
}
}
}
}
catch (WebDriverTimeoutException wdte)
{
throw new InvalidOperationException(
String.Format("Angular could not be found on the page '{0}'", value), wdte);
}
}
}
}
Since this property assumes that an application is using Angular, when navigating with Navigate().GoToUrl() you must include, again, whether the application is using Angular via a bool.
In our case, we were not using Angular and passing that into the GoToUrl() method calls directly into the wrapped IWebDriver via INavigation. This wrapped driver handles local files correctly.
Below is the navigation class in Protractor-net:
public class NgNavigation : INavigation
{
private NgWebDriver ngDriver;
private INavigation navigation;
// irrelevant constructors and such
/// <summary>
/// Load a new web page in the current browser window.
/// </summary>
/// <param name="url">The URL to load. It is best to use a fully qualified URL</param>
/// <param name="ensureAngularApp">Ensure the page is an Angular page by throwing an exception.</param>
public void GoToUrl(string url, bool ensureAngularApp)
{
if (ensureAngularApp)
{
this.ngDriver.Url = url;
}
else
{
this.navigation.GoToUrl(url);
}
}

Related

CefSharp Search Engine Implamentation

I am working on a cefsharp based browser and i am trying to implement a search engine into the browser, but the code I have tried docent work, it doesn't really have any errors but when i star the project and type something i the text field nothing happens and it dosent load the search engine i entered into the code, the only time the textbox loads anything is when a url is typed.
This is the code used in the browser that docent work
private void LoadUrl(string url)
{
if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
{
WebUI.Load(url);
}
else
{
var searchUrl = "https://www.google.com/search?q=" + WebUtility.HtmlEncode(url);
WebUI.Load(searchUrl);
}
}
i have also tried
void LoadURl(String url)
{
if (url.StartsWith("http"))
{
WebUI.Load(url);
}
else
{
WebUI.Load(url);
}
}
i was also suggested to try
private void LoadUrl(string url)
{
if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
{
WebUI.LoadUrl(url);
}
else
{
var searchUrl = "https://www.google.com/search?q=" + Uri.EscapeDataString(url);
WebUI.LoadUrl(searchUrl);
}
}
We have here really few Information on how your code works. But what I notice is that you use WebUtility.HtmlEncode for the search query. WebUtility has also a WebUtility.UrlEncode Method, that how I understand your question makes more sense it the context. This is the documentation for the method: https://learn.microsoft.com/de-de/dotnet/api/system.net.webutility.urlencode
The Url you are generating is invalid. You need to use Uri.EscapeDataString to convert the url param into a string that can be appended to a url.
// For this example we check if a well formed absolute Uri was provided
// and load that Url, all others will be loaded using the search engine
// e.g. https://github.com will load directly, attempting to load
// github.com will load the search engine with github.com as the query.
//
if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
{
chromiumWebBrowser.LoadUrl(url);
}
else
{
var searchUrl = "https://www.google.com/search?q=" + Uri.EscapeDataString(url);
chromiumWebBrowser.LoadUrl(searchUrl);
}
nothing happens and it dosent load the search engine
You need to subscribe to the LoadError event to get actual error messages. It's up to you to display errors to the user. The following is a basic example:
chromiumWebBrowser.LoadError += OnChromiumWebBrowserLoadError;
private void OnChromiumWebBrowserLoadError(object sender, LoadErrorEventArgs e)
{
//Actions that trigger a download will raise an aborted error.
//Aborted is generally safe to ignore
if (e.ErrorCode == CefErrorCode.Aborted)
{
return;
}
var errorHtml = string.Format("<html><body><h2>Failed to load URL {0} with error {1} ({2}).</h2></body></html>",
e.FailedUrl, e.ErrorText, e.ErrorCode);
_ = e.Browser.SetMainFrameDocumentContentAsync(errorHtml);
}
For testing purposes you can also copy and paste the searchUrl string you've generated and try loading it in Chrome to see what happens, you should also get an error.

How to avoid Reflected_xss_all_clients vulnerabilities in Winforms c#

Currently, I am working for a Winforms project.
When I am scanning my Winforms application through CheckMarx then I am getting multiple Reflected_xss_all_clients vulnerabilities.
I know there is no scripting in Winforms. XSS is a web threat but may be there would be some way to remediate these threats during scanning.
Here is the error code section 1:
private void UpdatePreviewValue()
{
try
{
// Set the preview value
if (txtFieldValue.Text != string.Empty)
{
// Show the preview value
lblPreview.Text = "(" + txtFieldValue.Text + ")";
}
else
{
// Show that there is no field value
lblPreview.Text = Properties.Resources.Std_Txt_Fld_NoFieldValue;
}
}
catch (Exception ex)
{
frmErrorHandler.ShowDataError(Properties.ErrorStrings.ErrorTitle_SrcFldCtlInteger_UpdatePreviewValue, DataErrorImageConstants.Exclamation, ex);
}
}
in above code section, the line lblPreview.Text = "(" + txtFieldValue.Text + ")";is throwing Reflected_xss_all_clients vulnerabilities.
Here is the error code section 2:
/// <summary>
/// Method to copy an existing node for moving inside a grid
/// </summary>
/// <param name="rowToCopy">GridRow to copy</param>
/// <returns>GridRow</returns>
private GridRow CopyGridRow(GridRow rowToCopy)
{
GridRow newRow = gridCategories.NewRow();
newRow.Tag = rowToCopy.Tag;
newRow.Cells[0].Text = rowToCopy.Cells[0].Text;
newRow.Cells[0].Image = rowToCopy.Cells[0].Image;
newRow.Cells[1].Text = rowToCopy.Cells[1].Text;
if (rowToCopy.HasRows)
{
foreach (GridRow nestedRow in rowToCopy.NestedRows)
{
newRow.NestedRows.Add(CopyGridRow(nestedRow));
}
}
return newRow;
}
in above code section, the line newRow.Cells[0].Text = rowToCopy.Cells[0].Text; and newRow.Cells[1].Text = rowToCopy.Cells[1].Text;are throwing Reflected_xss_all_clientsvulnerabilities.
Here is the error code section 3:
/// <summary>
/// Method used to add a new discrete value to the listview
/// </summary>
private void AddDiscreteValue()
{
// check we have an entry to add
if (txtDiscreteValue.Text != "")
{
SetDiscreteValue(txtDiscreteValue.Text, true, null, false);
}
}
In above code section, the line SetDiscreteValue(txtDiscreteValue.Text, true, null, false); is throwing Reflected_xss_all_clients vulnerabilities for txtDiscreteValue.Text
Please suggest any way to remediate it, if possible.
Checkmarx will follow the string from input to use. Sometimes it identifies a variable which is not fillterd transited to the frontend as a XSS.
As for me, I always ignore the XSS reported from Checkmarx. Maybe you can use a fillter function before use the string variable. Like this
txtFieldValue.Text=cleanXSS(txtFieldValue.Text)
As for cleanXSS(), you can find many examples after google.

Script Notify for ms-appdata

I want to notify my web view from button in html file and trigger the javascript:
function notify(str) {
window.external.notify(str);
}
The event captured using wv_ScriptNotify(..., ...):
void wv_ScriptNotify(object sender, NotifyEventArgs e)
{
Color c=Colors.Red;
if (e.CallingUri.Scheme =="ms-appx-web" || e.CallingUri.Scheme == "ms-appdata")
{
if (e.Value.ToLower() == "blue") c = Colors.Blue;
else if (e.Value.ToLower() == "green") c = Colors.Green;
}
appendLog(string.Format("Response from script at '{0}': '{1}'", e.CallingUri, e.Value), c);
}
I set the html file on ms-appx-web and it running well, and I realize that the html file must be store into local folder. So I change the ms-appx-web:///.../index.html to ms-appdata:///local/.../index.html.
Already search in microsoft forum and get this. On that thread there is a solution using resolver, but I'm still confusing, how can it notify from javascript like using window.external.notify? And what kind of event in C# side that will capture the "notify" from javascript other than "ScriptNotify"?
Update
There is a solution from here, example using the resolver and it said to use ms-local-stream:// rather than using ms-appdata://local so I can still use the ScriptNotify event. But unfortunately the example using the ms-appx that means using the InstalledLocation not the LocalFolder.
Trying to googling and search in msdn site for the documentation for ms-local-stream but the only documentation is just the format of ms-local-stream without any example like this ms-local-stream://appname_KEY/folder/file.
Based from that documentation, I made some sample to try it:
public sealed class StreamUriWinRTResolver : IUriToStreamResolver
{
/// <summary>
/// The entry point for resolving a Uri to a stream.
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
{
if (uri == null)
{
throw new Exception();
}
string path = uri.AbsolutePath;
// Because of the signature of this method, it can't use await, so we
// call into a separate helper method that can use the C# await pattern.
return getContent(path).AsAsyncOperation();
}
/// <summary>
/// Helper that maps the path to package content and resolves the Uri
/// Uses the C# await pattern to coordinate async operations
/// </summary>
private async Task<IInputStream> getContent(string path)
{
// We use a package folder as the source, but the same principle should apply
// when supplying content from other locations
try
{
// My package name is "WebViewResolver"
// The KEY is "MyTag"
string scheme = "ms-local-stream:///WebViewResolver_MyTag/local/MyFolderOnLocal" + path; // Invalid path
// string scheme = "ms-local-stream:///WebViewResolver_MyTag/MyFolderOnLocal" + path; // Invalid path
Uri localUri = new Uri(scheme);
StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
return stream.GetInputStreamAt(0);
}
catch (Exception) { throw new Exception("Invalid path"); }
}
}
And inside my MainPage.xaml.cs:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// The 'Host' part of the URI for the ms-local-stream protocol needs to be a combination of the package name
// and an application-defined key, which identifies the specific resolver, in this case 'MyTag'.
Uri url = wv.BuildLocalStreamUri("MyTag", "index.html");
StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver();
// Pass the resolver object to the navigate call.
wv.NavigateToLocalStreamUri(url, myResolver);
}
It always get the exception when it reach the StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri); line.
If anybody ever got this problem and already solved it, please advise.
After debugging it, I found something interesting, the BuildLocalStreamUri part is already make the ms-local-stream automatically.
I made some changes on the getContent method inside StreamUriWinRTResolver class:
public sealed class StreamUriWinRTResolver : IUriToStreamResolver
{
/// <summary>
/// The entry point for resolving a Uri to a stream.
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
{
if (uri == null)
{
throw new Exception();
}
string path = uri.AbsolutePath;
// Because of the signature of this method, it can't use await, so we
// call into a separate helper method that can use the C# await pattern.
return getContent(path).AsAsyncOperation();
}
/// <summary>
/// Helper that maps the path to package content and resolves the Uri
/// Uses the C# await pattern to coordinate async operations
/// </summary>
private async Task<IInputStream> getContent(string path)
{
// We use a package folder as the source, but the same principle should apply
// when supplying content from other locations
try
{
// Don't use "ms-appdata:///" on the scheme string, because inside the path
// will contain "/local/MyFolderOnLocal/index.html"
string scheme = "ms-appdata://" + path;
Uri localUri = new Uri(scheme);
StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
return stream.GetInputStreamAt(0);
}
catch (Exception) { throw new Exception("Invalid path"); }
}
}
Change the file path on the MainPage.xaml.cs:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// The 'Host' part of the URI for the ms-local-stream protocol needs to be a combination of the package name
// and an application-defined key, which identifies the specific resolver, in this case 'MyTag'.
Uri url = wv.BuildLocalStreamUri("MyTag", "/local/MyFolderOnLocal/index.html");
StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver();
// Pass the resolver object to the navigate call.
wv.NavigateToLocalStreamUri(url, myResolver);
wv.ScriptNotify += wv_ScriptNotify;
}
protected override void wv_ScriptNotify(object sender, NavigationEventArgs e)
{
if (e.CallingUri.Scheme == "ms-local-stream")
{
// Do your work here...
}
}

Call mobile detection class method in ASP.NET app_code folder

Summary
I have an ASP.NET 3.5 website and a mobile detection method inside a C# class file inside my app_code folder. I want to call this method which sets a cookie, then switch my master page file if it's a mobile device.
I'm using a method i got from the comment section down in this article: http://www.codeproject.com/Articles/34422/Detecting-a-mobile-browser-in-ASP-NET
This just seemed simpler than using the 51degrees method of detection since i didn't really need a high level of detection, and i didn't want to send them to a different URL, but rather just flip to a different masterpage, and the NuGet package which makes a nice easy install doesn't work for ASP.NET 3.5.
The problem i'm at currently is with calling the method.
Here's the Code
External app_code class
public static class fooBar // test method
{
public static bool ean()
{
return true;
}
}
public static class HttpRequestExt
{
#region Private Fields
// These regular expressions retrieved from http://detectmobilebrowser.com/ "Open source mobile phone detection".
private static Regex MobileBrowsers = new Regex(#"android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\\/|plucker|pocket|psp|symbian|treo|up\\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino", RegexOptions.IgnoreCase | RegexOptions.Multiline);
private static Regex MobileApps = new Regex(#"1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\\-(n|u)|c55\\/|capi|ccwa|cdm\\-|cell|chtm|cldc|cmd\\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\\-s|devi|dica|dmob|do(c|p)o|ds(12|\\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\\-|_)|g1 u|g560|gene|gf\\-5|g\\-mo|go(\\.w|od)|gr(ad|un)|haie|hcit|hd\\-(m|p|t)|hei\\-|hi(pt|ta)|hp( i|ip)|hs\\-c|ht(c(\\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\\-(20|go|ma)|i230|iac( |\\-|\\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\\/)|klon|kpt |kwc\\-|kyo(c|k)|le(no|xi)|lg( g|\\/(k|l|u)|50|54|e\\-|e\\/|\\-[a-w])|libw|lynx|m1\\-w|m3ga|m50\\/|ma(te|ui|xo)|mc(01|21|ca)|m\\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\\-2|po(ck|rt|se)|prox|psio|pt\\-g|qa\\-a|qc(07|12|21|32|60|\\-[2-7]|i\\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\\-|oo|p\\-)|sdk\\/|se(c(\\-|0|1)|47|mc|nd|ri)|sgh\\-|shar|sie(\\-|m)|sk\\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\\-|v\\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\\-|tdg\\-|tel(i|m)|tim\\-|t\\-mo|to(pl|sh)|ts(70|m\\-|m3|m5)|tx\\-9|up(\\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\\-|2|g)|yas\\-|your|zeto|zte\\-", RegexOptions.IgnoreCase | RegexOptions.Multiline);
#endregion
public const string ViewMobileSiteCookieName = "ViewMobile";
/// <summary>
/// Determines if the request emanated from a mobile-device client;
/// and stores the result in a cookie on the response.
/// </summary>
/// <param name="request"></param>
/// <param name="Response"></param>
/// <returns></returns>
///
public static bool IsMobileClient(this System.Web.HttpRequest request, System.Web.HttpRequest Response)
{
bool isMobile = false;
bool isCookieSet = false;
var viewMobileCookie = request.Cookies[ViewMobileSiteCookieName];
if (viewMobileCookie != null && bool.TryParse(viewMobileCookie.Value, out isMobile))
{
isCookieSet = true;
}
else if (request.Browser.IsMobileDevice)
{
isMobile = true;
}
else if (request.ServerVariables["HTTP_X_WAP_PROFILE"].IsNotEmpty())
{
isMobile = true;
}
else if
(
request.ServerVariables["HTTP_ACCEPT"].IsNotEmpty()
&&
(
request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wap")
|| request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wml+xml")
)
)
{
isMobile = true;
}
else if (request.ServerVariables["HTTP_USER_AGENT"].IsNotEmpty())
{
string userAgent = request.ServerVariables["HTTP_USER_AGENT"];
isMobile = ((MobileBrowsers.IsMatch(userAgent) || MobileApps.IsMatch(userAgent.Substring(0, 4))));
}
// Store the result as a cookie.
if (!isCookieSet)
Response.Cookies.Add(new HttpCookie(ViewMobileSiteCookieName, isMobile.ToString()));
return isMobile;
}
public static bool IsNotEmpty(this string instance)
{
return instance != null && instance.Length > 0;
}
}
My call to it)
Right now im doing it on the page, but i figure i'll do this in global.asax on session start?
sectionTitle.InnerHtml = fooBar.ean().ToString(); // test works
sectionTitle.InnerHtml = HttpRequestExt.IsMobileClient.ToString(); // compile error
Compile Error:
CS0119: 'SWIC.HttpRequestExt.IsMobileClient(System.Web.HttpRequest, System.Web.HttpRequest)' is a 'method', which is not valid in the given context
Do i have to somehow cast this to the current instance? Should i just be doing this differently all together?
You're trying to call a method like a property. Methods need parentheses:
HttpRequestExt.IsMobileClient().ToString()
^^
You'll also have to call it on the current request, not just statically, as it is an extension method (which takes a parameter). E.g.:
sectionTitle.InnerHtml = Page.Request.IsMobileClient(Page.Response).ToString();

ASP.Net Combres and Combres MVC with Cloudfront

Currently I am using Amazon Cloudfront to service static objects on my ASP.Net MVC3 C# site. So all static resources have http://cdn.domainname.com/ appended before the resource.
At the same time I am using combres and combred mvc to compress and combine my CSS and Javascript.
The tag to output the minimized combined files are as follows.
#Html.Raw(WebExtensions.CombresLink("siteCss"))
#Html.Raw(WebExtensions.CombresLink("siteJs"))
This produces links on my site to
<link rel="stylesheet" type="text/css" href="/combres.axd/siteCss/-63135510/"/>
<script type="text/javascript" src="/combres.axd/siteJs/-561397631/"></script>
As you can see my cloudfront cdn isn't in front of it so I am not getting the benefits of cloudfront with these to files.
Is there anyone out there who knows how to insert my cdn without changing the source code of the actuall combress dll file?
I'm not familiar with Cloudfront, but with Combres (latest release) you can change the host name (which gets appended as prefix before the /combres.axd... by setting the host attribute in . For example:
<resourceSets url="~/combres.axd"
host="static.mysite.com"
defaultDuration="365"
defaultVersion="auto"
defaultDebugEnabled="false"
defaultIgnorePipelineWhenDebug="true"
localChangeMonitorInterval="30"
remoteChangeMonitorInterval="60"
>
Please let me know if this approach works with CloudFront?
I ran in the same issue a few months ago and just ran across this post. I was able to get around it by making my own Combres filter (FixUrlsInCSSFilter) that will read a "Base Url" value from the web.config or a database setting and apply it to all combres image urls.
Hope it helps someone out there...
combres.xml:
<combres xmlns='urn:combres'>
<filters>
<filter type="MySite.Filters.FixUrlsInCssFilter, MySite" />
</filters>
FixUrlsInCssFilter - most of this was copied from the original reflected file
public sealed class FixUrlsInCssFilter : ISingleContentFilter
{
/// <inheritdoc cref="IContentFilter.CanApplyTo" />
public bool CanApplyTo(ResourceType resourceType)
{
return resourceType == ResourceType.CSS;
}
/// <inheritdoc cref="ISingleContentFilter.TransformContent" />
public string TransformContent(ResourceSet resourceSet, Resource resource, string content)
{
string baseUrl = AppSettings.GetImageBaseUrl();
return Regex.Replace(content, #"url\((?<url>.*?)\)", match => FixUrl(resource, match, baseUrl),
RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
}
private static string FixUrl(Resource resource, Match match, string baseUrl)
{
try
{
const string template = "url(\"{0}\")";
var url = match.Groups["url"].Value.Trim('\"', '\'');
while (url.StartsWith("../", StringComparison.Ordinal))
{
url = url.Substring(3); // skip one '../'
}
if (!baseUrl.EndsWith("/"))
baseUrl += "/";
if (baseUrl.StartsWith("http"))
{
return string.Format(CultureInfo.InvariantCulture, template, baseUrl + url);
}
else
return string.Format(CultureInfo.InvariantCulture, template, (baseUrl + url).ResolveUrl());
}
catch (Exception ex)
{
// Be lenient here, only log. After all, this is just an image in the CSS file
// and it should't be the reason to stop loading that CSS file.
EventManager.RaiseExceptionEvent("Cannot fix url " + match.Value, ex);
return match.Value;
}
}
}
#region Required to override FixUrlsInCssFilter for Combres
public static class CombresExtensionMethods
{
/// <summary>
/// Returns the relative HTTP path from a partial path starting out with a ~ character or the original URL if it's an absolute or relative URL that doesn't start with ~.
/// </summary>
public static string ResolveUrl(this string originalUrl)
{
if (string.IsNullOrEmpty(originalUrl) || IsAbsoluteUrl(originalUrl) || !originalUrl.StartsWith("~", StringComparison.Ordinal))
return originalUrl;
/*
* Fix up path for ~ root app dir directory
* VirtualPathUtility blows up if there is a
* query string, so we have to account for this.
*/
var queryStringStartIndex = originalUrl.IndexOf('?');
string result;
if (queryStringStartIndex != -1)
{
var baseUrl = originalUrl.Substring(0, queryStringStartIndex);
var queryString = originalUrl.Substring(queryStringStartIndex);
result = string.Concat(VirtualPathUtility.ToAbsolute(baseUrl), queryString);
}
else
{
result = VirtualPathUtility.ToAbsolute(originalUrl);
}
return result.StartsWith("/", StringComparison.Ordinal) ? result : "/" + result;
}
private static bool IsAbsoluteUrl(string url)
{
int indexOfSlashes = url.IndexOf("://", StringComparison.Ordinal);
int indexOfQuestionMarks = url.IndexOf("?", StringComparison.Ordinal);
/*
* This has :// but still NOT an absolute path:
* ~/path/to/page.aspx?returnurl=http://www.my.page
*/
return indexOfSlashes > -1 && (indexOfQuestionMarks < 0 || indexOfQuestionMarks > indexOfSlashes);
}
}
#endregion
AppSettings class - to retrieve the value from the web.config. I also use this to build the path for images not handled by combres...
public class AppSettings
{
/// <summary>
/// Retrieves the value for "ImageBaseUrl" if the key exists
/// </summary>
/// <returns></returns>
public static string GetImageBaseUrl()
{
string baseUrl = "";
if (ConfigurationManager.AppSettings["ImageBaseUrl"] != null)
baseUrl = ConfigurationManager.AppSettings["ImageBaseUrl"];
return baseUrl;
}
}
web.config values
<appSettings>
<add key="ImageBaseUrl" value="~/Content/Images/" /> <!--For Development-->
<add key="ImageBaseUrl" value="https://d209523005EXAMPLE.cloudfront.net/Content/Images/" /> <!--For Production-->
</appSettings>

Categories

Resources