locked
Disabling bandwidth leeching RRS feed

  • Question

  • User-1046515647 posted
    I'm confused about when I should use a HttpHandler or factory... I want to disable bandwidth leeching of JPG or GIF files from my site. I added the application mappings to the asp.net DLL, and then simply added the following to global.asax file: protected void Application_BeginRequest(Object sender, EventArgs e) { // Check for bandwidth leeching and disable it if (Request.RequestType == "GET") { Uri referrer = Request.UrlReferrer; string reqhost = Request.Url.Host; if (referrer == null || reqhost != referrer.Host) { string ext = Path.GetExtension(Server.MapPath(Request.FilePath)); switch (ext) { case ".jpg": case ".gif": Response.Redirect("~/Admin/AccessDenied.aspx"); break; } } } } What's the advantage of using an HttpModule over a simple solution like this? Sure, I know this should do response.write, but hey, they shouldn't be doing this in the first place so I kind of prefer redirecting the request even if it isn't returning an image/jpeg Dan
    Thursday, November 7, 2002 3:16 PM

All replies

  • User-1293453231 posted
    I use this, to send an alternate image (nudies ;)) if it's a leeched image. Keep in mind that you have to make IIS handle the .jpg files with the aspnet_wp.dll. Also, you'll need this in your web.config. Don't be confused by all of the "jpg" in there. The first one is the name of the class, the second is the name of the dll (this assumes you've compiled the class to a file called jpg.dll): <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <httpHandlers> <add verb="*" path="*.jpg" type="jpg, jpg"/> </httpHandlers> </system.web> </configuration> Here's the actual class: using System; using System.Web; public class jpg : IHttpHandler { public void ProcessRequest(HttpContext context) { string FileName = context.Server.MapPath(context.Request.FilePath); if (context.Request.ServerVariables["HTTP_REFERER"] == null) { context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("/no.jpg"); } else { if (context.Request.ServerVariables["HTTP_REFERER"].IndexOf("mydomain.com") > 0) { context.Response.ContentType = "image/JPEG"; context.Response.WriteFile(FileName); } else { context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("/no.jpg"); } } } public bool IsReusable { get { return true; } } }
    Monday, December 2, 2002 12:51 PM
  • User881756249 posted
    Jeff, Could this be used in a web application as well, or does it have to be a seperate .DLL installed like an ISAPI filter or something?
    Thursday, September 25, 2003 10:56 PM
  • User-1293453231 posted
    No, you can put it in with the rest of your code, it doesn't have to be in its own assembly. What makes it "go" is the combination of the mapping of .jpg to .Net in IIS and then the line I indicated in web.config. The mapping at the IIS level isn't any different from the way .aspx requests are mapped to .Net. While in this case we're mapping a specific extention in web.config, all of the normal ASP .Net file extensions (.aspx, .ascx, .config, .asax, etc.) are mapped in machine.config to particular classes. This is exactly the same process.
    Sunday, September 28, 2003 7:45 PM
  • User857297241 posted
    Doesn't this put a large strain on IIS?
    Thursday, March 25, 2004 3:30 PM
  • User-1293453231 posted
    No, not in my experience. IIS really has little to do with it aside from excepting the request and handing it off. With a thousand users at any given time, even on an image-heavy site, I've had no problems.
    Friday, March 26, 2004 1:46 PM