mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-08-13 16:07:30 +00:00
Transparent redirect handler instead of returning HTTP 308 to clients
This commit is contained in:
@@ -1,29 +1,54 @@
|
||||
import json
|
||||
|
||||
import tornado
|
||||
|
||||
from core.utils import safe_json_dumps
|
||||
|
||||
|
||||
class V1GoneHandler(tornado.web.RequestHandler):
|
||||
"""Returns 410 Gone with a message for any endpoints in the old API that have breaking changes in the new one or
|
||||
have been retired."""
|
||||
|
||||
def post(self):
|
||||
self.set_status(410)
|
||||
self.write(safe_json_dumps(
|
||||
"This API endpoint has a breaking change or has been removed in the current version of the Spothole API. Please see /apidocs for details of the current API version and the endpoints available."))
|
||||
self.set_header("Cache-Control", "no-store")
|
||||
self.set_header("Content-Type", "application/json")
|
||||
|
||||
from tornado.httpclient import AsyncHTTPClient
|
||||
|
||||
class V1RedirectHandler(tornado.web.RequestHandler):
|
||||
"""Returns 308 Permanent Redirect from any path in the old API to the new one, where there were no breaking changes."""
|
||||
"""Transparently proxies requests from the old API to the new one,
|
||||
returning whatever the v2 endpoint returns, for endpoints with no breaking changes."""
|
||||
|
||||
def get(self, path):
|
||||
new_url = "/api/v2/" + path
|
||||
SUPPORTED_METHODS = ("GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS")
|
||||
|
||||
# Methods where an HTTP body isn't valid
|
||||
_BODYLESS_METHODS = {"GET", "HEAD", "OPTIONS"}
|
||||
|
||||
async def _proxy(self, path):
|
||||
new_url = f"{self.request.protocol}://{self.request.host}/api/v2/{path}"
|
||||
if self.request.query:
|
||||
new_url += "?" + self.request.query
|
||||
self.set_status(308)
|
||||
self.set_header("Location", new_url)
|
||||
|
||||
client = AsyncHTTPClient()
|
||||
try:
|
||||
response = await client.fetch(
|
||||
new_url,
|
||||
method=self.request.method,
|
||||
headers=self.request.headers,
|
||||
body=None if self.request.method in self._BODYLESS_METHODS else (self.request.body or b""),
|
||||
raise_error=False,
|
||||
follow_redirects=False,
|
||||
request_timeout=10.0,
|
||||
)
|
||||
except Exception as e:
|
||||
raise tornado.web.HTTPError(502, reason=str(e))
|
||||
|
||||
self.set_status(response.code, response.reason)
|
||||
for name, value in response.headers.get_all():
|
||||
# Let Tornado recompute these for the outgoing response
|
||||
if name.lower() not in ("content-length", "transfer-encoding", "connection"):
|
||||
self.add_header(name, value)
|
||||
if response.body:
|
||||
self.write(response.body)
|
||||
self.finish()
|
||||
|
||||
async def get(self, path):
|
||||
await self._proxy(path)
|
||||
|
||||
async def post(self, path):
|
||||
await self._proxy(path)
|
||||
|
||||
async def put(self, path):
|
||||
await self._proxy(path)
|
||||
|
||||
async def delete(self, path):
|
||||
await self._proxy(path)
|
||||
|
||||
async def patch(self, path):
|
||||
await self._proxy(path)
|
||||
Reference in New Issue
Block a user