Thursday, December 1, 2011

Google App Engine Easy crossdomain.xml Pattern

Been struggling to figure out how to put a crossdomain.xml file in a GAE app? Static files dirs and all that getting in the way?

Fear not; the requirement for a crossdomain.xml file is only that it be available as a text/xml response type at the root address of your application.

It does NOT have to actually be a physical file (that's the trick).

So, in your GAE app (assuming you are using Python and that you know the basics of setting one up...)

Add this class to your main.py script (this is the script spec'd in your yaml file as the main execution script).


class SendCrossDomain ( webapp.RequestHandler ):

def get ( self ):

crossdomain = '<?xml version="1.0"?>'
crossdomain = crossdomain + '<!DOCTYPE cross-domain-policy SYSTEM "/xml/dtds/cross-domain-policy.dtd">'
crossdomain = crossdomain + '<cross-domain-policy>'
crossdomain = crossdomain + '<site-control permitted-cross-domain-policies="all"/>'
crossdomain = crossdomain + '<allow-access-from domain="*" to-ports="*" secure="false"/>'
crossdomain = crossdomain + '<allow-http-request-headers-from domain="*" headers="*" secure="false"/>'
crossdomain = crossdomain + '</cross-domain-policy>'

self.response.headers [ 'Content-Type' ] = 'text/xml'
self.response.out.write ( crossdomain )


Now, later in your main.py file, in your "main" method typically, where you spec url endpoint handlers, make sure you have an entry for crossdomain.xml.


application_paths = [ ( '/crossdomain.xml', SendCrossDomain ) ]


Now when a user hits the url /yourappid.appengine.com/crossdomain.xml, the SendCrossDomain class will respond to the GET request with XML data sent as text/xml, which satisfies the requirement for the Flash player.

As always, thanks for visiting.