Browse Source

Register wellknown URLs

master
Sam Black 8 years ago
parent
commit
28ca686428
  1. 65
      flask_wellknown/wellknown.py
  2. 5
      wellknown_demo.py

65
flask_wellknown/wellknown.py

@ -19,8 +19,9 @@
"""
import re
import flask
from flask import Blueprint
from flask import current_app
from flask import request
from flask import send_from_directory
from xrd import Element
@ -28,7 +29,7 @@ from xrd import XRD
from werkzeug.contrib.cache import SimpleCache
wellknown = Blueprint('wellknown', __name__, static_folder="static")
cache = SimpleCache()
cache = {}
# HostMeta is mostly from django-wellknown
@ -36,7 +37,7 @@ class HostMeta(XRD):
"""
Host-meta information
"""
def __init__(self):
def __init__(self, app):
"""
Host-meta information
"""
@ -44,9 +45,9 @@ class HostMeta(XRD):
self.attributes.append(('xmlns:hm', 'http://host-meta.net/ns/1.0'))
hosts = current_app.config.get("WELLKNOWN_HOSTMETA_HOSTS", [])
hosts = app.config.get("WELLKNOWN_HOSTMETA_HOSTS", [])
if not hosts:
hosts.append(current_app.config.get('SERVER_NAME', 'example.com'))
hosts.append(app.config.get('SERVER_NAME', 'example.com'))
for host in hosts:
self.elements.append(Element('hm:Host', host))
@ -60,6 +61,51 @@ class HostMeta(XRD):
return expr.sub(r'>\g<1></', xml)
def register(path, handler, content_type):
"""
Register well-known handlers.
:param path: URL path for well-known
:type path: string
:param handler: function to handle well-known call
:type handler: function
:param content_type: set content type for returned data
:type content_type: string
:raises: KeyError
"""
if cache.get(path):
raise KeyError("Path already registered")
cache[path] = (handler, content_type)
def resource(path):
"""
Return resource handler for specific path.
:param path: URL path from well-known
:type path: string
:returns: output from handler
"""
if path not in cache:
flask.abort(404)
handler, content_type = cache.get(path, (None, None))
if not handler or not content_type:
flask.abort(404)
if callable(handler):
output = handler()
else:
output = handler
response = make_response(output)
response['content-type'] = content_type
return response
@wellknown.route('/.well-known/')
@wellknown.route('/.well-known/<metainfo>')
def base(metainfo=None):
@ -67,7 +113,7 @@ def base(metainfo=None):
Base wellknown URL scheme
"""
if metainfo:
return "Hello, {}!".format(metainfo)
return resource(metainfo)
else:
return "Hello, World!"
@ -94,3 +140,10 @@ def crossdomain():
return crossdomain_handler(request)
else:
return send_from_directory(wellknown.static_folder, 'crossdomain.xml')
def register_all(app):
"""
Register all well-known URLs
"""
register('host-meta', HostMeta(app), 'application/xrd+xml')

5
wellknown_demo.py

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import flask_wellknown.wellknown as wellknown
from flask import Flask
from flask_wellknown.wellknown import wellknown
class CONFIG(object):
@ -12,6 +12,7 @@ class CONFIG(object):
app = Flask(__name__)
app.config.from_object(CONFIG)
app.register_blueprint(wellknown)
app.register_blueprint(wellknown.wellknown)
wellknown.register_all(app)
app.run(host='127.0.0.1', port=8080)

Loading…
Cancel
Save