def application()

in modules/wsgi/composite.py [0:0]


def application(e, r):
    m = requestMethod(e)
    fpath = requestPath(e)

    # Serve static files
    if m == "GET":
        if fpath.endswith(".html"):
            return fileresult(e, r, "text/html", fpath)
        if fpath.endswith(".css"):
            return fileresult(e, r, "text/css", fpath)
        if fpath.endswith(".js"):
            return fileresult(e, r, "application/x-javascript", fpath)
        if fpath.endswith(".png"):
            return fileresult(e, r, "image/png", fpath)
        if fpath == "/":
            return result(e, r, 301, (("Location", "/index.html"),))

        # Debug hook
        if fpath == "/debug":
            return result(e, r, 200, (("Content-type", "text/plain"),), ("Debug",))

        # Sign in and out
        if fpath == "/login":
            redir = signin("/")
            if redir:
                return result(e, r, 301, (("Location", redir),))
        if fpath == "/logout":
            redir = signout(signin("/"))
            if redir:
                return result(e, r, 301, (("Location", redir),))

    # Find the requested component
    path = tokens(fpath)
    uc = uriToComponent(path, comps)
    uri = car(uc)
    if uri == None:
        return failure(e, r, 404)
    comp = cadr(uc)

    # Call the requested component function
    id = path[len(uri):]
    if m == "GET":
        v = comp("get", id)
        
        # Write a simple value as a JSON value
        if not isList(v):
            return result(e, r, 200, (("Content-type", "application/json"),), writeJSON(valuesToElements((("'value", v),))))

        # Write an empty list as a JSON empty value
        if not isList(v):
            return result(e, r, 200, (("Content-type", "application/json"),), writeJSON(()))

        # Write content-type / content-list pair
        if isString(car(v)) and not isNull(cdr(v)) and isList(cadr(v)):
            return result(e, r, 200, (("Content-type", car(v)),), cadr(v))
        
        # Convert list of values to element values
        ve = valuesToElements(v)

        # Write an assoc result as a JSON value
        if isList(car(ve)) and not isNull(car(ve)):
            el = car(ve)
            if isSymbol(car(el)) and car(el) == element and not isNull(cdr(el)) and isSymbol(cadr(el)):
                if cadr(el) == "'feed":
                    return result(e, r, 200, (("Content-type", "application/atom+xml"),), writeATOMFeed(ve))
                if cadr(el) == "'entry":
                    return result(e, r, 200, (("Content-type", "application/atom+xml"),), writeATOMEntry(ve))

        # Write a JSON value
        return result(e, r, 200, (("Content-type", "application/json"),), writeJSON(ve))

    if m == "POST":
        ct = requestContentType(e)

        # Handle a JSON-RPC function call
        if contains(ct, "application/json-rpc") or contains(ct, "text/plain") or contains(ct, "application/x-www-form-urlencoded"):
            print >> stderr, "Handling JSON-RPC request"
            json = elementsToValues(readJSON(requestBody(e)))
            args = postArgs(json)
            jid = cadr(assoc("'id", args))
            func = funcName(cadr(assoc("'method", args)))
            params = cadr(assoc("'params", args))
            v = comp(func, *params)
            return result(e, r, 200, (("Content-type", "application/json-rpc"),), jsonResult(jid, v))

        # Handle an ATOM entry POST
        if contains(ct, "application/atom+xml"):
            ae = elementsToValues(readATOMEntry(requestBody(e)))
            v = comp("post", id, ae)
            if isNull(v):
                return failure(e, r, 500)
            return result(e, r, 201, (("Location", request_uri(e) + "/" + "/".join(v)),))
        return failure(e, r, 500)
    
    if m == "PUT":
        # Handle an ATOM entry PUT
        ae = elementsToValues(readATOMEntry(requestBody(e)))
        v = comp("put", id, ae)
        if v == False:
            return failure(e, r, 404)
        return result(e, r, 200)
    
    if m == "PATCH":
        # Handle an ATOM entry PATCH
        ae = elementsToValues(readATOMEntry(requestBody(e)))
        v = comp("patch", id, ae)
        if v == False:
            return failure(e, r, 404)
        return result(e, r, 200)
    
    if m == "DELETE":
        v = comp("delete", id)
        if v == False:
            return failure(e, r, 404)
        return result(e, r, 200)
    
    return failure(e, r, 500)