Source

scatter-share / balisage09 / slide33.py

Full commit
import amara
from akara.services import simple_service, response

ECOUNTER_SERVICE_ID = 'http://purl.org/akara/services/demo/element_counter'

#Config info is pulled in at global scope as AKARA_MODULE_CONFIG

#Security demo: create a URI jail outside of which XML operations won't leak
URI_JAIL = AKARA_MODULE_CONFIG.get('uri_jail')

#Create the assertion rule for the URI jail
ALLOWED = [(lambda uri, base=baseuri: uri.startswith(URI_JAIL), True)]

#Create a URI resolver instance that enforces the jail
restricted_resolver = irihelpers.resolver(authorizations=ALLOWED)

@simple_service('GET', ECOUNTER_SERVICE_ID, 'ecounter', 'text/plain')
def ecounter(uri):
    #e.g.: curl http://localhost:8880/akara.xslt?http://hg.akara.info/testdoc.xml"
    uri = inputsource(uri[0], resolver=restricted_resolver)
    doc = amara.parse(uri)
    ecount = doc.xml_select(u'count(//*)')
    return str(ecount)