Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Prerender.io/PrerenderConfigSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ public String ExtensionsToIgnoreString
{
this["extensionsToIgnore"] = value;
}
}

[ConfigurationProperty("ignoreAllSubdomains")]
public bool IgnoreAllSubdomains
{
get
{
return (bool)this["ignoreAllSubdomains"];
}
set
{
this["ignoreAllSubdomains"] = value;
}
}

public IEnumerable<String> ExtensionsToIgnore
Expand Down
67 changes: 53 additions & 14 deletions Prerender.io/PrerenderModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ private void DoPrerender(HttpApplication context)
{
response.Headers.Add(header, value);
}
}
}
response.Write(result.ResponseBody);
response.Flush();
context.CompleteRequest();
Expand All @@ -94,7 +94,7 @@ private ResponseResult GetPrerenderedPageResponse(HttpRequest request)
try
{
// Get the web response and read content etc. if successful
var webResponse = (HttpWebResponse) webRequest.GetResponse();
var webResponse = (HttpWebResponse)webRequest.GetResponse();
var reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
return new ResponseResult(webResponse.StatusCode, reader.ReadToEnd(), webResponse.Headers);
}
Expand Down Expand Up @@ -129,15 +129,15 @@ private String GetApiUrl(HttpRequest request)
if (string.Equals(request.Headers["X-Forwarded-Proto"], "https", StringComparison.InvariantCultureIgnoreCase))
{
url = url.Replace("http://", "https://");
}

// Remove the application from the URL
if (_prerenderConfig.StripApplicationNameFromRequestUrl && !string.IsNullOrEmpty(request.ApplicationPath) && request.ApplicationPath != "/")
{
// http://test.com/MyApp/?_escape_=/somewhere
url = url.Replace(request.ApplicationPath, string.Empty);
}
}
// Remove the application from the URL
if (_prerenderConfig.StripApplicationNameFromRequestUrl && !string.IsNullOrEmpty(request.ApplicationPath) && request.ApplicationPath != "/")
{
// http://test.com/MyApp/?_escape_=/somewhere
url = url.Replace(request.ApplicationPath, string.Empty);
}
var prerenderServiceUrl = _prerenderConfig.PrerenderServiceUrl;
return prerenderServiceUrl.EndsWith("/")
? (prerenderServiceUrl + url)
Expand All @@ -149,7 +149,12 @@ private bool ShouldShowPrerenderedPage(HttpRequest request)
{
var useAgent = request.UserAgent;
var url = request.Url;
var referer = request.UrlReferrer == null ? string.Empty : request.UrlReferrer.AbsoluteUri;
var referer = request.UrlReferrer == null ? string.Empty : request.UrlReferrer.AbsoluteUri;

if (IgnoreAllSubdomains(url))
{
return false;
}

if (HasEscapedFragment(request))
{
Expand All @@ -166,7 +171,6 @@ private bool ShouldShowPrerenderedPage(HttpRequest request)
return false;
}


if (IsInResources(url))
{
return false;
Expand Down Expand Up @@ -220,6 +224,41 @@ private IEnumerable<String> GetExtensionsToIgnore()
}
return extensionsToIgnore;
}
private bool IgnoreAllSubdomains(Uri url)
{
if (_prerenderConfig.IgnoreAllSubdomains)
{
string subDomain = GetSubDomain(url);

if (!String.IsNullOrEmpty(subDomain))
{
if (subDomain.ToLower() == "www")
{
return false;
}
return true;
}
}
return false;
}

private string GetSubDomain(Uri url)
{

if (url.HostNameType == UriHostNameType.Dns)
{
string host = url.Host;

if (host.Split('.').Length > 2)
{
int lastIndex = host.LastIndexOf(".");
int index = host.LastIndexOf(".", lastIndex - 1);
return host.Substring(0, index);
}
}

return null;
}

private bool IsInSearchUserAgent(string useAgent)
{
Expand Down