From 00d7a29b2d59e3adc4e7787916314b6ff241ffee Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Fri, 27 Oct 2017 12:25:17 +1100 Subject: [PATCH] Add basic support for /.well-known/host-meta(.json)?, not much info in it so far but it works --- Pipfile | 1 + Pipfile.lock | 15 +++++++++- wellknowns/urls.py | 2 ++ wellknowns/views/__init__.py | 2 ++ wellknowns/views/host_meta.py | 35 ++++++++++++++++++++++++ wellknowns/{views.py => views/static.py} | 0 6 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 wellknowns/views/__init__.py create mode 100644 wellknowns/views/host_meta.py rename wellknowns/{views.py => views/static.py} (100%) diff --git a/Pipfile b/Pipfile index a177362..29076bb 100644 --- a/Pipfile +++ b/Pipfile @@ -25,6 +25,7 @@ python-slugify = "*" markdown = "*" bleach = "*" django-debug-toolbar = "*" +xrd = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index e454bb2..a4a8456 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "89e62295608d784115e1097fc46d5aa458ad26971e7668a1835717f9409fddac" + "sha256": "dc39eb988fd0df9d07f81c689d5ee326958f557b423bd98e6d52d0610e5691f5" }, "host-environment-markers": { "implementation_name": "cpython", @@ -164,6 +164,13 @@ ], "version": "==2.6" }, + "isodate": { + "hashes": [ + "sha256:aa4d33c06640f5352aca96e4b81afd8ab3b47337cc12089822d6f322ac772c81", + "sha256:2e364a3d5759479cdb2d37cce6b9376ea504db2ff90252a2e5b7cc89cc9ff2d8" + ], + "version": "==0.6.0" + }, "lxml": { "hashes": [ "sha256:7a8715539adb41c78129983ba69d852e0102a3f51d559eeb91dce1f6290c4ad0", @@ -397,6 +404,12 @@ "sha256:b36a1c245f2d304965eb4e0a82848379241dc04b865afcc4aab16748587e1923" ], "version": "==0.5.1" + }, + "xrd": { + "hashes": [ + "sha256:51d01f732b5b5b7983c5179ffaed864408d95a667b3a6630fe27aa7528274089" + ], + "version": "==0.1" } }, "develop": {} diff --git a/wellknowns/urls.py b/wellknowns/urls.py index f33e902..3c98532 100644 --- a/wellknowns/urls.py +++ b/wellknowns/urls.py @@ -5,4 +5,6 @@ from . import views app_name = 'wellknowns' urlpatterns = [ url(r'^keybase.txt$', views.keybase, name='keybase'), + url(r'^host-meta$', views.host_meta_xml, name='host-meta'), + url(r'^host-meta.json$', views.host_meta_json, name='host-meta.json'), ] diff --git a/wellknowns/views/__init__.py b/wellknowns/views/__init__.py new file mode 100644 index 0000000..b1d80d9 --- /dev/null +++ b/wellknowns/views/__init__.py @@ -0,0 +1,2 @@ +from .static import keybase +from .host_meta import host_meta_xml, host_meta_json diff --git a/wellknowns/views/host_meta.py b/wellknowns/views/host_meta.py new file mode 100644 index 0000000..7c4d4d0 --- /dev/null +++ b/wellknowns/views/host_meta.py @@ -0,0 +1,35 @@ +from django.http import HttpResponse +from django.conf import settings +from lemoncurry.templatetags.lemoncurry_tags import get_package_json +from xrd import XRD, Attribute, Element, Link + + +def add_links(dest): + package = get_package_json() + links = ( + Link(rel='license', href='https://creativecommons.org/licenses/by-sa/4.0/'), + Link(rel='code-repository', href=package['repository']), + ) + dest.extend(links) + + +def host_meta(request): + h = XRD() + h.attributes.append(Attribute('xmlns:hm', 'http://host-meta.net/ns/1.0')) + h.elements.append(Element('hm:Host', request.META['HTTP_HOST'])) + add_links(h.links) + return h + + +def host_meta_xml(request): + return HttpResponse( + host_meta(request).to_xml().toprettyxml(indent=' ', encoding='utf-8'), + content_type='application/xrd+xml', + ) + + +def host_meta_json(request): + return HttpResponse( + host_meta(request).to_json(), + content_type='application/json' + ) diff --git a/wellknowns/views.py b/wellknowns/views/static.py similarity index 100% rename from wellknowns/views.py rename to wellknowns/views/static.py