Solution: AS3 Security Error #2122 with 300 redirects
The Flash player has an issue where it will not load a crossdomain.xml file on a redirect. What this means is that setting the load policy has no effect, since it will only load the crossdomain.xml from the first domain, not the redirected one.
A consequence of this is when loading images from CDNs such as Edgecast, Akamai, etc., you will get Security Error #2122 when you attempt to get the width/height of the image. Luckily, there is a simple and straightforward solution (via @jesterxl).
All you do is reload the image using the redirected url, which is available in the LoaderInfo.url in the onComplete event listener of the first load. The benefit of doing this is that the second load comes from the cache, so it's basically instantaneous (or however long it takes to load the crossdomain.xml from the redirected url). Here's some sample code on how to pull this off.
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
function load(url:String):void
{
var sprite:Sprite = new Sprite();
var loader:Loader = new Loader();
sprite.addChild(loader);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
var lc:LoaderContext = new LoaderContext(true);
lc.checkPolicyFile = true;
loader.load(new URLRequest(url), lc);
}
function onComplete(event:Event):void
{
// the redirected url
var path:String = LoaderInfo(event.target).url;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onReallyComplete);
// swap out the old loader with the new one into the same container
LoaderInfo(event.target).loader.parent.addChild(loader);
LoaderInfo(event.target).loader.parent.removeChild(LoaderInfo(event.target).loader);
var lc:LoaderContext = new LoaderContext(true);
lc.checkPolicyFile = true;
loader.load(new URLRequest(path), lc);
}
function onReallyComplete(event:Event):void
{
var w:int = LoaderInfo(event.target).content.width;
var h:int = LoaderInfo(event.target).content.height;
}
Posted in AS3, Bugs, Tips/Tricks
December 24th, 2008 at 9:48 am
I recently ran into this issue on a project I was working on, and I think the way I solved it was by loading the CDN's policy file first and then loading my original policy file and doing the loading that way. I am not looking at the code right now but if memory serves me correctly that seemed to work.
December 30th, 2008 at 1:12 pm
@matt The issue is that you have no idea where the CDN's policy file is because the CDN is doing a redirect to a completely different server. Loading the policy file on the CDN's main server has no effect because the file is coming from a different domain, which is dynamic based on where the user is geographically.
January 8th, 2009 at 3:17 pm
Steve, could you show us how checkPolicyFile can be set on a page asset in Gaia? I just ran into this issue (only with Flash Player 10 though) and this is my first experience using your framework.
thanks!
January 9th, 2009 at 6:29 am
[...] > Solution: AS3 Security Error #2122 with 300 redirects | steven sacks [...]
January 12th, 2009 at 10:15 pm
Thank you so much! Worked like a charm for CastFire Thumbnails
March 21st, 2009 at 5:31 pm
[...] had forgotten Steven Sacks and I had this exact same problem loading images from Cast Fire 3 months ago. If you load a JPEG from Cast Fire from example, [...]
February 5th, 2010 at 8:29 am
Thank you!!! You save me some precious time. Keep working on!!!!!