Rewriting Images served by httphandler doesn't work 100% of the time

Topics: User Forum
Dec 15, 2009 at 10:48 AM

I'm getting some odd behaviour I can't pinpoint with the rewriter.

I'm serving images via a httphandler in the format (blogengine.net's):

/image.axd?picture=/2009/12/15/picture-1.jpg 

I'm rewriting it via:

/img/2009/12/15/picture-1.jpg

Images are delivered in a blog entry with 5 images/entry on average. The problem is it doesnt work 100% of the time.
It works most of the time, but for some will just return good ole' 404.

The handler is set to display a specific 404 error if the file doesn't exist or if theres an exception so it appears the rewriter is the problem here.
Only by forcing browser refresh on that particular image will it reload.

Any clues as to why this is happening and how to fix it?

The rewrite rule is very simple:

RewriteRule ^/blog/img/(.+)\.jpg$ /blog/image.axd?picture=$1.jpg [L]

Dec 15, 2009 at 1:39 PM

What happens if you change yoru rewrite rule to:

RewriteRule ^/blog/img/(.+)$ /blog/image.axd?picture=$1 [L]

I've got a feeling that the greediness of the (.+) is causing it to fail, so either try the above, or try:

RewriteRule ^/blog/img/(.+?).jpg$ /blog/image.axd?picture=$1.jpg [L]

This should make the .+ ungreedy.

Hope this helps.

shonk

Dec 15, 2009 at 1:43 PM

Oh, might be simpler than that, it looks like your forward slashes are a bit out of sync...

Try:

RewriteRule ^/blog/img/(.+)\.jpg$ /blog/image.axd?picture=/$1.jpg [L]

or:

RewriteRule ^/blog/img(.+)\.jpg$ /blog/image.axd?picture=$1.jpg [L]

You were matching against the forward slash in the match (left) side, but not rewriting it...

Using your examples,

/img/2009/12/15/picture-1.jpg

would rewrite as

/image.axd?picture=2009/12/15/picture-1.jpg (note the lack of forward slash before the 2009)
Dec 16, 2009 at 11:04 AM

Thanks for the suggestions shonk.

Actually, I made a mistake in the original handler format. My bad, it's actually without the forward slash:

/image.axd?picture=2009/12/15/picture-1.jpg

That would eliminate your 2nd suggestion. Unfortunately your earlier suggestions don't work either.

I enabled logging and I stumbled upon an instance of the problem. IIRF rewrote all the images and skipped one for no reason.
There was no attempt at parsing the image at all. A bug perhaps?

Coordinator
Dec 17, 2009 at 2:00 AM
Edited Dec 17, 2009 at 2:00 AM

It very well could be a bug.  What can you tell me about the URL for the particular image?  Can you show an IIRF logfile excerpt that shows a "normal" rewrite for an image and also an excerpt that shows one that was skipped?   Or better, the log for one complete page load, with 4 or 5 images, including at least one that gets skipped?

Is it always the same image that is "skipped" ?  

Also, what version of Windows, IIS, and IIRF are you using?

Dec 17, 2009 at 4:49 AM
Edited Dec 17, 2009 at 4:53 AM

I'm afraid I can't get a log of a complete uncached page load as the occurance is random.

But here is an excerpt of the log. This example is a cached page where only the new un-cached images are parsed and rewritten by IIRF.

Thu Dec 17 13:09:36 - 26492 - DoRewrites: Url (no decoding): '/Current_Affairs/post/2009/11/03/Dolce-Gabbana-SpringSummer-2010.aspx'
Thu Dec 17 13:09:36 - 26492 - DoRewrites: No Rewrite
Thu Dec 17 13:09:38 - 26492 - DoRewrites: Url (no decoding): '/Current_Affairs/img/2009/11/1109-DolceGabbana-SS-420-3.jpg'
Thu Dec 17 13:09:38 - 26492 - EvaluateRules: Last Rule
Thu Dec 17 13:09:38 - 26492 - DoRewrites: Rewrite Url to: '/Current_Affairs/image.axd?picture=2009/11/1109-DolceGabbana-SS-420-3.jpg'
Thu Dec 17 13:09:38 - 26492 - DoRewrites: Url (no decoding): '/Current_Affairs/img/2009/11/1109-DolceGabbana-SS-420-1.jpg'
Thu Dec 17 13:09:38 - 26492 - EvaluateRules: Last Rule
Thu Dec 17 13:09:38 - 26492 - DoRewrites: Rewrite Url to: '/Current_Affairs/image.axd?picture=2009/11/1109-DolceGabbana-SS-420-1.jpg'
Thu Dec 17 13:09:38 - 26492 - DoRewrites: Url (no decoding): '/Current_Affairs/img/2009/11/1109-DolceGabbana-SS-420-5.jpg'
Thu Dec 17 13:09:38 - 26492 - EvaluateRules: Last Rule
Thu Dec 17 13:09:38 - 26492 - DoRewrites: Rewrite Url to: '/Current_Affairs/image.axd?picture=2009/11/1109-DolceGabbana-SS-420-5.jpg'
Thu Dec 17 13:09:40 - 11524 - DoRewrites: Url (no decoding): '/Current_Affairs/img/2009/11/1109-DolceGabbana-SS-420-4.jpg'
Thu Dec 17 13:09:40 - 11524 - EvaluateRules: Last Rule
Thu Dec 17 13:09:40 - 11524 - DoRewrites: Rewrite Url to: '/Current_Affairs/image.axd?picture=2009/11/1109-DolceGabbana-SS-420-4.jpg'

In the naming convention for all images is the same(mmyy-image-title-x.jpg) except for the index number at the end of the filename.

In this case, DolceGabbana-SS-420-2.jpg and -6.jpg, which are supposed to appear after -4.jpg was skipped entirely.

The behaviour is generally consistent across browsers; if the image doesn't load in IE, it won't load in Firefox or Chrome. There are cases where that particular image will load in Firefox but not in IE but the general behavior is the former. This happens once in about 4 pages.

Setting a higher LogLevel (3+) forces the image to load on page refresh (cause looping is slower, hence lesser chance of miss?) This makes higher level logging quite impossible.

I'm running IIRF 2.0.1.1013 on IIS6/W2K3.
Another thing of note is that, the folder /Current_Affairs is an app within the full site (/).
Root and /Current_Affairs each has its on iirf.ini cause just having one in root doesn't rewrite those for /Current_Affairs. Is that normal? cause I thought youd be able to control all rewrites from root.'

Hope that helps! Thanks Cheeso for looking into this.

ThuDec 17 13:09:38 - 26492 - DoRewrites: Url (no decoding): '/Current_Affairs/img/2009/11/1109-DolceGabbana-SS-420-3.jpg'
Thu Dec 17 13:09:38 - 26492 - EvaluateRules: Last Rule
Thu Dec 17 13:09:38 - 26492 - DoRewrites: Rewrite Url to: '/Current_Affairs/image.axd?picture=2009/11/1109-DolceGabbana-SS-420-3.jpg'
Thu Dec 17 13:09:38 - 26492 - DoRewrites: Url (no decoding): '/Current_Affairs/img/2009/11/1109-DolceGabbana-SS-420-1.jpg'
Thu Dec 17 13:09:38 - 26492 - EvaluateRules: Last Rule
Thu Dec 17 13:09:38 - 26492 - DoRewrites: Rewrite Url to: '/Current_Affairs/image.axd?picture=2009/11/1109-DolceGabbana-SS-420-1.jpg'
Thu Dec 17 13:09:38 - 26492 - DoRewrites: Url (no decoding): '/Current_Affairs/img/2009/11/1109-DolceGabbana-SS-420-5.jpg'
Thu Dec 17 13:09:38 - 26492 - EvaluateRules: Last Rule
Thu Dec 17 13:09:38 - 26492 - DoRewrites: Rewrite Url to: '/Current_Affairs/image.axd?picture=2009/11/1109-DolceGabbana-SS-420-5.jpg'
Thu Dec 17 13:09:40 - 11524 - DoRewrites: Url (no decoding): '/Current_Affairs/img/2009/11/1109-DolceGabbana-SS-420-4.jpg'
Thu Dec 17 13:09:40 - 11524 - EvaluateRules: Last Rule
Thu Dec 17 13:09:40 - 11524 - DoRewrites: Rewrite Url to: '/Current_Affairs/image.axd?picture=2009/11/1109-DolceGabbana-SS-420-4.jpg'
Coordinator
Dec 18, 2009 at 6:13 AM

Hello Mr Z-man,

I don't see any indication that DolceGabbana-SS-420-2.jpg and -6.jpg were ever received by IIRF.   This means, IIRF doesn't think it received a request for those pictures.

Why not?  I don't know.   but it looks like IIRF isn't getting the request for the files you see as being "skipped". 

Now, it could be that IIRF is getting those requests, and it just happens that the log excerpt you posted doesn't show it.  I can't really tell from the log.  But you told me that there's nothing more interesting in the log. If that's the case, then IIRF isn't getting the requests. 

Next step would be to trace the requests going from browser to server.  If you're using jQuery or AJAX, it's possible that there's a program logic error or timing error in the javascript code, that is causing the request to never be sent out.   Trace the requests with something like Fiddler

I don't understand what you're talking about, with setting the log level higher.  Are you saying that setting the loglevel higher in IIRF changes the behavior?  And why do you say "this makes higher level logging quite impossible" ? I don't  understand that at all. 

 

 

Dec 21, 2009 at 10:12 AM

Hey Cheeso,

Its seems really weird that IIRF would just ignore some images. Guess I'll have to dig deeper.

As for the images, they are just plain html, no jquery or ajax in the middle.

What I meant with the higher level logging being impossible was that if I set the log to a higher level (e.g 3 or 4), the skipped images start showing both in log and the browser.
This makes is hard to narrow down the problem because if some images on a page were skipped on loglevel2, changing the loglevel to 3 and refreshing the page will force the images to load and the problem to disappear.

Perhaps when IIRF loops through URLs at a slower rate (due to loglevel3), it doesn't miss an image compared to no logging or loglevel2?

This is of course just conjecture. I''ll dig deeper and post any updates here.

Thanks!

Coordinator
Dec 21, 2009 at 12:19 PM
Edited Dec 21, 2009 at 5:20 PM

Puzzling.  I would try a Fiddler trace, to see if the "missing images" are really being requested, and what the response is.  IIRF is telling you the requests didn't arrive.  See what the browser thinks.