views:

473

answers:

1

I have a flash video player which requests a flv file from a central server. That server might redirect the request to a server from the user's country if possible, a lot like a CDN.

This video player also reports usage stats. One thing I'd like to report is the true server/location from which the player is streaming the video from. So basically, if it gets redirected I want to know about it.

It seems that you can't extract the url from a URLLoader, you can only keep a copy of the URLRequest that you constructed it with.

I notice that you can listen for HTTP status events, which would include a 302 or similar. But unfortunately, the HTTPStatusEvent object doesn't show the redirected location.

Any ideas about how to monitor for a redirect, and get the redirected location?

+2  A: 

I'm a bit surprised Flash allows you to redirect a video request at all. I did a bit of digging and it looks like you can get the info:

Handling Crossdomain.xml and 302 Redirects Using NetStream

His post specifically talks about the trouble of security issues that arise because of the fact some operations fail if data is from an untrusted server. Since he doesn't know where his video is coming from (302 redirect) the Flash Player doesn't trust it and prevents some operations on the loaded content.

How he gets the server the content was actually loaded from is to do an operation on the file that should not be allowed and he parses the domain information from the error message:

try
{
    var bit:BitmapData = new BitmapData(progressiveVideoPlayer.measuredWidth, progressiveVideoPlayer.measuredHeight, false, 0x000000);
    bit.draw(progressiveVideoPlayer);
}
catch(error:SecurityError)
{
    var list:Array = error.toString().split(" ");
    var swfURL:String = list[7] as String;
    var domain:String = list[10] as String;
    domain = domain.substring(0, domain.length - 1);
    var domainList:Array = domain.split("/");
    var protocol:String = domainList[0] as String;
    var address:String = domainList[2];
    var policyFileURL:String = protocol + "//" + address + "/crossdomain.xml";
    Security.loadPolicyFile(policyFileURL);
}

Notice he is doing it so that he can load the policy file (to allow the security restricted operations on the file). I'm not sure it will be helpful to you but at least read the article and have a think about it. You may contact the blog author directly too - he is pretty active in the general Flash community.

James Fassett
Hey hey! Thanks for your help, James. That's a very dirty but very effective way to get the server. I guess I can just try to draw on the video, and if it throws a SecurityError I can parse out the error message to get redirected location. Otherwise I can assume it wasn't redirected.
aaaidan
I admit it is *very* dirty and for that reason you have to make the decision whether or not it is really worth it. I can't see too much harm in it though - just be careful and give it some good testing. And pray that the behaviour and error message format doesn't change in a future Flash Player ...
James Fassett
Heh, yeah.Because the swf and the video are hosted in the same way on the same CDN network, I ended up using a LocalConnection to get the actual (final) domain, which ensures there are no cross domain requests.var realDomain:String = (new LocalConnection()).domain;
aaaidan