mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-09-20 14:27:42 +00:00
Re-add translation of QRZ/HamQTH query params to header params, since it turns out someone was using that
This commit is contained in:
@@ -3,27 +3,39 @@ from tornado.httpclient import AsyncHTTPClient
|
||||
from tornado.httputil import HTTPHeaders
|
||||
|
||||
|
||||
_LEGACY_PARAM_TO_HEADER_MAP = {
|
||||
"qrz_username": "X-QRZ-Username",
|
||||
"qrz_password": "X-QRZ-Password",
|
||||
"qrz_session_key": "X-QRZ-Session-Key",
|
||||
"hamqth_username": "X-HamQTH-Username",
|
||||
"hamqth_password": "X-HamQTH-Password",
|
||||
"hamqth_session_id": "X-HamQTH-Session-ID",
|
||||
}
|
||||
|
||||
class V1RedirectHandler(tornado.web.RequestHandler):
|
||||
"""Transparently proxies requests from the old API to the new one,
|
||||
returning whatever the v2 endpoint returns, for endpoints with no breaking changes."""
|
||||
|
||||
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 += f"?{self.request.query}"
|
||||
|
||||
# Copy the incoming headers so we can add translated legacy credentials without changing the original
|
||||
# request.
|
||||
headers = HTTPHeaders(self.request.headers)
|
||||
for param, header in _LEGACY_PARAM_TO_HEADER_MAP.items():
|
||||
value = self.get_query_argument(param, default=None)
|
||||
if value:
|
||||
headers[header] = value
|
||||
|
||||
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""),
|
||||
body=None if self.request.method == "GET" else (self.request.body or b""),
|
||||
raise_error=False,
|
||||
follow_redirects=False,
|
||||
request_timeout=10.0,
|
||||
@@ -48,13 +60,4 @@ class V1RedirectHandler(tornado.web.RequestHandler):
|
||||
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)
|
||||
await self._proxy(path)
|
||||
@@ -3,11 +3,33 @@ import re
|
||||
from server.handlers.api.spots import APISpotsHandler, APISpotsStreamHandler
|
||||
|
||||
_GRID_SOURCE_RE = re.compile(r'"dx_location_source":\s*"GRID"')
|
||||
_LEGACY_PARAM_TO_HEADER_MAP = {
|
||||
"qrz_username": "X-QRZ-Username",
|
||||
"qrz_password": "X-QRZ-Password",
|
||||
"qrz_session_key": "X-QRZ-Session-Key",
|
||||
"hamqth_username": "X-HamQTH-Username",
|
||||
"hamqth_password": "X-HamQTH-Password",
|
||||
"hamqth_session_id": "X-HamQTH-Session-ID",
|
||||
}
|
||||
|
||||
|
||||
def _handle_legacy_params(handler):
|
||||
"""Copy v1 query-string QRZ/HamQTH credentials into the v2 headers, so the v2 handler can see them"""
|
||||
|
||||
for param, header in _LEGACY_PARAM_TO_HEADER_MAP.items():
|
||||
if header in handler.request.headers:
|
||||
continue
|
||||
value = handler.get_query_argument(param, default=None)
|
||||
if value:
|
||||
handler.request.headers[header] = value
|
||||
|
||||
class V1APISpotsHandler(APISpotsHandler):
|
||||
"""API request handler for /api/v1/spots (GET). Included in early Spothole v2 for backwards compatibility."""
|
||||
|
||||
def prepare(self):
|
||||
_handle_legacy_params(self)
|
||||
super().prepare()
|
||||
|
||||
def write(self, chunk):
|
||||
if isinstance(chunk, str):
|
||||
chunk = _GRID_SOURCE_RE.sub('"dx_location_source": "SPOT"', chunk)
|
||||
@@ -17,6 +39,10 @@ class V1APISpotsHandler(APISpotsHandler):
|
||||
class V1APISpotsStreamHandler(APISpotsStreamHandler):
|
||||
"""API request handler for /api/v1/spots/stream (SSE). Included in early Spothole v2 for backwards compatibility."""
|
||||
|
||||
def prepare(self):
|
||||
_handle_legacy_params(self)
|
||||
super().prepare()
|
||||
|
||||
def write_message(self, *args, **kwargs):
|
||||
args = list(args)
|
||||
for i, a in enumerate(args):
|
||||
|
||||
Reference in New Issue
Block a user