bugmagnet
bugmagnet

Reputation: 7769

IIS Url Rewrite: How to handle picture source srcset?

I have a site I'm reverse proxying using IIS UrlRewrite. So far I've had a fairly easy time of it thanks to Paul Cociuba's three articles on Microsoft Tech Community.

My current challenge is with a group of urls in a <picture><source srcset which are, heavily abbreviated,

<source srcset="/a/b/c/d/e/en/g/h/thumb%20classes%202020_SX_MX.component.crop-3x2-5.ts=1592419103694.jpg/a/c/d/oceania/au/en/g/jcr:a/root/z_1_col/z_4_col/image_1558468008 1400w,
/a/b/c/d/e/en/g/h/thumb%20classes%202020_SX_MX.component.crop-3x2-6.ts=1592419103694.jpg/a/c/d/oceania/au/en/g/jcr:a/root/z_1_col/z_4_col/image_1558468008 2000w,
/a/b/c/d/e/en/g/h/thumb%20classes%202020_SX_MX.component.crop-3x2-7.ts=1592419103694.jpg/a/c/d/oceania/au/en/g/jcr:a/root/z_1_col/z_4_col/image_1558468008 2800w,
/a/b/c/d/e/en/g/h/thumb%20classes%202020_SX_MX.component.crop-3x2-1.ts=1592419103694.jpg/a/c/d/oceania/au/en/g/jcr:a/root/z_1_col/z_4_col/image_1558468008 350w,
/a/b/c/d/e/en/g/h/thumb%20classes%202020_SX_MX.component.crop-3x2-2.ts=1592419103694.jpg/a/c/d/oceania/au/en/g/jcr:a/root/z_1_col/z_4_col/image_1558468008 460w,
/a/b/c/d/e/en/g/h/thumb%20classes%202020_SX_MX.component.crop-3x2-3.ts=1592419103694.jpg/a/c/d/oceania/au/en/g/jcr:a/root/z_1_col/z_4_col/image_1558468008 700w,
/a/b/c/d/e/en/g/h/thumb%20classes%202020_SX_MX.component.crop-3x2-4.ts=1592419103694.jpg/a/c/d/oceania/au/en/g/jcr:a/root/z_1_col/z_4_col/image_1558468008 920w" 
media="(max-width: 768px)" 
sizes="(max-width: 768px) 100vw">

How do I convert all of the paths that begin with /a/b/c to https://othersite/a/b/c? At worst I could perhaps write a regular expression that captures a fixed number of paths (say 7) and then rewrites them with {R:1} through {R:7} but I'm not sure at this point whether the number of items in each srcset is fixed.

Also, do I need to create a custom tag so as to match the <source srcset=?

Upvotes: 1

Views: 175

Answers (1)

bugmagnet
bugmagnet

Reputation: 7769

This worked and I'm rather surprised. I'm still not getting the response I'm expecting from the page (some of the images still aren't drawing), but at least the HTML appears to be correct.

First up, I've declared a custom tag

<customTags>
    <tags name="sourceSrcset">
        <tag name="source" attribute="srcset" />
    </tags>
</customTags>

Then I've written an outbound rule to use it

<rule name="source srcset" preCondition="ResponseIsTextHtml">
    <match filterByTags="CustomTags" customTags="sourceSrcset" pattern=",?\/(a\/b\/\S+\s\d+w)" />
    <action type="Rewrite" value="https://othersite/{R:1}" />
</rule>

In case you're wondering, ResponseIsTextHtml has a brother called ResponseIsTextAnything and these are declared as

<preCondition name="ResponseIsTextHtml">
    <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
<preCondition name="ResponseIsTextAnything">
    <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/(.+)" />
</preCondition>

Upvotes: 3

Related Questions