Hosting a GWT Webapp in IIS - iis

I am currently trying to configure an ASP.net Web Application through Web.config, to host a GWT WebApp in a specific folder. I've managed to configure the mimeMap for the .manifest file extension in the system.Webserver/staticContent section however, i'm stuck with the clientCache. I want to add a caching rule so that files with ".nocache." are served with the following headers:
"Expires", "Sat, 21 Jan 2012 12:12:02 GMT" (today -1);
"Pragma", "no-cache"
"Cache-control", "no-cache, no-store, must-revalidate"
Anyone knows how to do this within IIS 7+ ?

The file time stamp is automatically checked in IIS and the browser always requests the server for updated file based on the timestamp, so the .nocache. files don't need anything special in IIS.
However if you wanted the browser to cache the .cache. files then the following HttpModule sets the cache expiration date to 30 days from now for files that end in .cache.js or .cache.html (or any extension). The browser won't even request for updated versions of these files.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CacheModulePlayground
{
public class CacheModule : IHttpModule
{
private HttpApplication _context;
public void Init(HttpApplication context)
{
_context = context;
context.PreSendRequestHeaders += context_PreSendRequestHeaders;
}
void context_PreSendRequestHeaders(object sender, EventArgs e)
{
if (_context.Response.StatusCode == 200 || _context.Response.StatusCode == 304)
{
var path = _context.Request.Path;
var dotPos = path.LastIndexOf('.');
if (dotPos > 5)
{
var preExt = path.Substring(dotPos - 6, 7);
if (preExt == ".cache.")
{
_context.Response.Cache.SetExpires(DateTime.UtcNow.Add(TimeSpan.FromDays(30)));
}
}
}
}
public void Dispose()
{
_context = null;
}
}
}
The web.config for this is:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<modules>
<add name="cacheExtension" type="CacheModulePlayground.CacheModule"/>
</modules>
</system.webServer>
</configuration>

I ended up creating a custom httphandler to handle all the requests to the path .nocache. using a solution similar to the one described in here:
Prevent scripts from being cached programmatically

Create an HTTP module class in GwtCacheHttpModuleImpl.cs file
using System;
using System.Web;
using System.Text.RegularExpressions;
namespace YourNamespace
{
/// <summary>
/// Classe GwtCacheHttpModuleImpl
///
/// Permet de mettre en cache pour un an ou pas du tout les fichiers générés par GWT
/// </summary>
public class GwtCacheHttpModuleImpl : IHttpModule
{
private HttpApplication _context;
private static String GWT_FILE_EXTENSIONS_REGEX_STRING = "\\.(js|html|png|bmp|jpg|gif|htm|css|ttf|svg|woff|txt)$";
private static Regex GWT_CACHE_OR_NO_CACHE_FILE_REGEX = new Regex(".*\\.(no|)cache" + GWT_FILE_EXTENSIONS_REGEX_STRING);
private static Regex GWT_CACHE_FILE_REGEX = new Regex(".*\\.cache" + GWT_FILE_EXTENSIONS_REGEX_STRING);
#region IHttpModule Membres
public void Dispose()
{
_context = null;
}
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders += context_PreSendRequestHeaders;
_context = context;
}
#endregion
private void context_PreSendRequestHeaders(object sender, EventArgs e)
{
int responseStatusCode = _context.Response.StatusCode;
switch (responseStatusCode)
{
case 200:
case 304:
// Réponse gérée
break;
default:
// Réponse non gérée
return;
} /* end..switch */
String requestPath = _context.Request.Path;
if (!GWT_CACHE_OR_NO_CACHE_FILE_REGEX.IsMatch(requestPath))
{
// Fichier non géré
return;
}
HttpCachePolicy cachePolicy = _context.Response.Cache;
if (GWT_CACHE_FILE_REGEX.IsMatch(requestPath))
{
// Fichier à mettre en cache
cachePolicy.SetExpires(DateTime.UtcNow.Add(TimeSpan.FromDays(365))); /* now plus 1 year */
}
else
{
// Fichier à ne pas mettre en cache
cachePolicy.SetExpires(DateTime.UtcNow); /* ExpiresDefault "now" */
cachePolicy.SetMaxAge(TimeSpan.Zero); /* max-age=0 */
cachePolicy.SetCacheability(HttpCacheability.Public); /* Cache-Control public */
cachePolicy.SetRevalidation(HttpCacheRevalidation.AllCaches); /* must-revalidate */
}
}
}
}
Reference your HTTP module in Web.Config file :
Handle GWT file extention via ISAPI module
You should configure your application via IIS UI ( IIS 5.x and .NET 3.5 in my case ).
You could add other GWT file extensions like png, css, ...
a) Handle .js extension
Executable : c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll
Extension : .js
Limit to : GET,HEAD
b) Handle .html extension
Executable : c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll
Extension : .html
Limit to : GET,HEAD
Reference : GWT Perfect Caching for Apache server

Related

How to insert code in the static files like .html, .htm, .asp on IIS for non-asp project

I want to add a script on my IIS Server.
So that it will be applied on all the websites that are upload will have that script in their request response.
Anyone who knows how to do it?
I had implemented the IHttpModule and IHttpHandler, it works fine for the asp.net projects.
but if the website contains only html, css, and js files in the folder, this solution doesn't work.
Here the HttpModule and HttpHandler
public class MyCustomHttpModuleClass : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PostRequestHandlerExecute += OnPostRequestHandlerExecute;
}
public void OnPostRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpContext context = application.Context;
context.Response.Write("<h1>alert('HELLO')</h1>");
}
}
public class MyHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.Write("<h1>alert('HELLO')</h1>");
}
}
I'm not sure if you have learnt how to add Custom Module and Handler in IIS. After tested your module and handler with static website, it works fine.
I will just give you a sample of adding them to IIS.
1.Create a project "class library .netframework". I name the project"ClassLibrary1"
2.Add class "MyCustomHttpModuleClass" and "MyHandler" to the project
3.Build this solution and find "ClassLibrary1.dll" in the "project/bin/debug" folder.
4.Copy "ClassLibrary1.dll" to the website root "BIN" folder.
5.Add managed module and handler by choose your dll.(should in the list after you copied)Just mention that your custom handler only work on the file extension you set up.
Now they work.

Can't load HttpModule in GAC

I write a HttpModule in .Net 3.5 and trying to install on the server root of IIS 10 server. I use gacutil.exe v7.0A successfully install it and I use /l command confirm that my module exists. I add configuration below to C:\Windows\System32\inetsrv\config\applicationHost.config:
<add name="ModuleModEF35" type="ModuleModEF35.MyModule, ModuleModEF35" preCondition="managedHandler" />
And I got FileNotFoundException when i browsed the child website. Due to some reasons, I can't place my module in the individual \bin folder though I know it definitely works.
namespace ModuleModEF35
{
class MyModule : System.Web.IHttpModule
{
void IHttpModule.Dispose()
{
}
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += Context_BeginRequest;
}
private void Context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
if (context.Request.RawUrl.Contains("%EF"))
{
context.Response.Redirect("https://http.cat/403");
context.Response.End();
}
}
}
}

Accessing BeginRequest/EndRequest events in HTTPModule for IIS 7.5 with ApplicationPool in Managed Pipeline Integrated mode

Environment: IIS 7.5 using an AppPool set to Managed Pipeline Integrated mode
I'm trying to log the certain state of the HTTP Request before it enters the pipeline and the HTTP response as it exists, specifically a few form values and the cookie collection.
I'm doing this in an HTTPModule
public class MyLoggingModule : IHttpModule
{
private static readonly log4net.ILog _logger =
log4net.LogManager.GetLogger(typeof(MyLoggingModule));
public void Init(HttpApplication context)
{
context.BeginRequest += LogRequestState;
context.EndRequest += LogResponseState;
}
}
private void LogRequestState(object sender, EventArgs e)
{
//Invokes...
HttpContext.Current.Server.UrlDecode
HttpContext.Current.Request.Url
HttpContext.Current.Request.Form.AllKeys
HttpContext.Current.Request.Cookies
HttpContext.Current.Response.Cookies.AllKeys
_logger.Debug("...");
}
private void LogResponseState(object sender, EventArgs e)
{
// Invokes ...
FederatedAuthentication.SessionAuthenticationModule.CookieHandler.Name
HttpContext.Current.Response.Cookies.AllKeys
_logger.Debug("...");
}
Web.Config settings
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="false">
<add name="MyLoggingModule" type="MyApp.Api.HttpModules.MyLoggingModule, MyApp.Api"/>
</modules>
<urlCompression doStaticCompression="true" doDynamicCompression="true"/>
</system.webServer>
I will get a runtime error only available in the Application Logs (try/catch does not catch this exception):
Exception information:
Exception type: NullReferenceException
Exception message: Object reference not set to an instance of an object.
at System.Web.HttpApplication.PipelineStepManager.ResumeSteps(Exception error)
at System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb)
at System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context)
It seems to be very similar to the issue raised here: HttpModule.Init - safely add HttpApplication.BeginRequest handler in IIS7 integrated mode
That issue has a solution along the lines of
public class MyLoggingModule : IHttpModule
{
public override void Init()
{
base.Init();
lock (_initialisationLockObject)
{
context.BeginRequest -= LogRequestState;
context.BeginRequest += LogRequestState;
context.EndRequest -= LogResponseState;
context.EndRequest += LogResponseState;
}
}
}
Given that the post is over 8 years old and the solution was not accepted and criticized on other posts is there a way to achieve this now?

How to make FluentFTP to log to log4net log?

I couldn't find any information on how to do it. Basically FluentFTP is using System.Diagnostics to log their messages.
FluentFtp expose the following static method:
FtpTrace.AddListener(TraceListener listener);
However I don't know if there is any way to implement (or use existing implementation, which?) TraceListener in the way it relays everything to log4net engine.
Any hints or ideas?
Thanks, Radek
You can attach a listener to the OnLogEvent method that FluentFTP exposes.
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void UploadFTP(FileInfo localFile, string remoteFileLocation, string remoteServer, NetworkCredential credentials)
{
FtpClient client = new FtpClient(remoteServer, credentials);
client.RetryAttempts = 3;
client.OnLogEvent = OnFTPLogEvent;
client.Connect();
if (!client.UploadFile(localFile.FullName, remoteFileLocation, FtpExists.Overwrite, false, FtpVerify.Retry | FtpVerify.Throw))
{
throw new Exception($"Could not Upload File {localFile.Name}. See Logs for more information");
}
}
private static void OnFTPLogEvent(FtpTraceLevel ftpTraceLevel, string logMessage)
{
switch (ftpTraceLevel)
{
case FtpTraceLevel.Error:
Log.Error(logMessage);
break;
case FtpTraceLevel.Verbose:
Log.Debug(logMessage);
break;
case FtpTraceLevel.Warn:
Log.Warn(logMessage);
break;
case FtpTraceLevel.Info:
default:
Log.Info(logMessage);
break;
}
}
The method OnFTPLogEvent will be called every-time the OnLogEvent action will be called allowing you to extend any logging you have already built into your application.
Basically FluentFTP is using System.Diagnostics.TraceListener so in order to make it logging to your log4net log you need to write your own simple class that would redirect logs to log4net logger. Like the following:
using System.Diagnostics;
using log4net;
namespace YourApp.Logging
{
public class Log4NetTraceListener : TraceListener
{
private readonly ILog _log;
public Log4NetTraceListener(string provider)
{
_log = LogManager.GetLogger(provider);
}
public override void Write(string message)
{
if(_log == null)
return;
if(!string.IsNullOrWhiteSpace(message))
_log.Info(message);
}
public override void WriteLine(string message)
{
if(_log == null)
return;
if (!string.IsNullOrWhiteSpace(message))
_log.Info(message);
}
}
}
Then, in your app.config file add the following entry:
<system.diagnostics>
<trace autoflush="true"></trace>
<sources>
<source name="FluentFTP">
<listeners>
<clear />
<add name="FluentLog" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="FluentLog" type="YourApp.Logging.Log4NetTraceListener, YourApp" initializeData="FluentLog" />
</sharedListeners>
</system.diagnostics>
That should enable FluentFtp logs and merge it with your application log4net log.

Impossible to show Custom Error page with Nancy on OWIN

I have a website using Nancy which is hosted using OWIN.
In my Startup.cs file I define the PassThroughOptions as follows:
public void Configuration(IAppBuilder app)
{
app.UseNancy(o => {
o.PassThroughWhenStatusCodesAre(
HttpStatusCode.NotFound,
HttpStatusCode.InternalServerError
);
o.Bootstrapper = new Bootstrapper();
});
app.UseStageMarker(PipelineStage.MapHandler);
}
I need to pass-through the NotFound requests, so that things like my bundled .less files or miniprofiler-results or static files in the root of my site (robots.txt or sitemap.xml) work.
I also have a custom StatusCodeHandler for the 404 code, which also checks a custom header to distinguish between static files (or .less bundles/miniprofiler) and actual stuff that is not found in my modules' methods.
public void Handle(HttpStatusCode statusCode, NancyContext context)
{
Log.Warn("Not found: " + context.Request.Url);
base.Handle(statusCode, context, "Errors/NotFound");
}
This handler then should actually show the error page.
protected void Handle(HttpStatusCode statusCode, NancyContext context, string view)
{
var response = new Negotiator(context)
.WithModel(GetErrorModel(context))
.WithStatusCode(statusCode)
.WithView(view);
context.Response = responseNegotiator.NegotiateResponse(response, context);
}
But the error page is never shown. The request is processed three times and eventually the default IIS error page is shown (using errorMode="Custom" for httpErrors) or simply a white page (using existingResponse="PassThrough" for httpErrors).
Is there any way to display something so simple as a custom error page when hosting a Nancy website on OWIN?
What you've got there looks good, it looks like you've be using the Hosting Nancy with Owin docs.
Here's what works for me:
The Startup.cs (required for Owin): (We've both coded the configuration function differently, you're just using the extension helper while I'm not. Same result. This is in my App.Web project.)
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseNancy(options =>
{
options.Bootstrapper = new BootStrapper();
options.PerformPassThrough = context => context.Response.StatusCode == HttpStatusCode.NotFound;
});
app.UseStageMarker(PipelineStage.MapHandler);
}
}
404 handler: (As per the docs, doesn't matter where this is in the project, by implementing IStatusCodeHandler it'll be automatically picked up by Nancy This is in my App.WebApi project with other module classes.)
public class StatusCode404Handler : IStatusCodeHandler
{
public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context)
{
return statusCode == HttpStatusCode.NotFound;
}
public void Handle(HttpStatusCode statusCode, NancyContext context)
{
var response = new GenericFileResponse("statuspages/404.html", "text/html")
{
StatusCode = statusCode
};
context.Response = response;
}
}
The 'statuspages' folder in my App.Web project:
Check this SO post for a comparison of using GenericFileReponse or ViewRenderer (How to display my 404 page in Nancy?).

Resources