Skip navigation.

Sample Question From 70-315 ExamAll recent postsBandwidth Leeching Stops Now: Take Two

Bandwidth Leeching Stops Here and Now

I can’t stand it when people steal bandwidth. It doesn’t matter if they steal it from me or somebody else. I think there’s an issue with ethics there. I’ve been somewhat ignoring it when people were "hotlinking" images from my site. Until now. Only once I wrote to owners of a Chinese site and asked them to simply copy images to their own site instead of linking straight to mine, but they ignored it. As traffic here, at AspNetResources.com, has grown considerably over the past 12 months (thank you guys!) leeching has become a nuisance.

I sat down and wrote an HttpHandler which serves a "custom" image every time somebody hotlinks one from my site:

Stop stealing my bandwidth

The first iteration didn’t go that smooth (thank you, Gordon!) and I realized I wasn’t checking referrers correctly. Most code samples out there compare the referrer’s host to your own host. The problem is if you click a link in a newsreader or email and it takes you straight to the site, there will be no referrer at all.

Also, comparing hosts to each other isn’t accurate. For example, Url.Host may return aspnetresources.com or www.aspnetresources.com. Comparison of these two fails. This is something you don’t catch when debugging from http://localhost. Eventually, I settled for a simple regex to parse out domain names.

How to Roll Your Own Handler

Feel free to grab source code of my image handler. Just remember to change the namespace. I’m sure yours will be different.

Next, you’ll need to instruct ASP.NET to take over images by delegating their streaming to this HttpHandler. For this you’ll need to add a couple of entries to the <httpHandlers> section of your web.config. Again, the namespace and assembly names will be different for each one of you.

Last, but not least, email your ISP and ask their tech support to configure .jpg, .gif and .png extension mappings to point to the ASP.NET ISAPI. They’ll know what to do.

Comments

Comment permalink 1 Frank Zehelein |
While it is generally a good idea it has some drawbacks. I have written myself a custom startpage, which collects different newsfeeds which i read daily (yours e.g.) and put it on my localhost webpage. Right now (well for about one or two days) i get these images and not the ones i want to see :( This technique really does work against New Readers. Well i could fake my referer if i get your block but i don't think i will...

Generally i would log all referrers to my images and work with a blacklist.
Comment permalink 2 yaron |
Also, you may want to consider allowing the more well known on-line news aggregators... I often read feeds from bloglines, for example, and since you do provide full feeds (which is good), this "are you stealing..." image is annoying.
I don't think it should really matter if I use a local aggregator or an on-line one...
Comment permalink 3 Milan Negovan |
Frank and Yaron, excellent point! I forgot that online aggregators pretty much "steal" content, so to say. I'll go with a blacklist instead to quit pissing off my Bloglines, etc, readers. :)
Comment permalink 4 Web Hosting |
I really like that image! Would you mind if I used it? Borrowed of course not linked 2 your server!
Comment permalink 5 Milan Negovan |
You mean this masterpiece of artwork I spent a whole month crafting to perfection? Hmm.... Sure thing, if you keep my URL on the bottom. :)

Emails and Notifications

Would you like to be notified when somebody responds to this post?  Would you like to have these comments emailed to you?

TrackBacks

Sorry, TrackBacks are not allowed.

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):