Skip to content

Commit

Permalink
Clean up 'Accept' header handling to always handle '*/*' correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
rt121212121 committed Apr 11, 2022
1 parent 963b115 commit e059f4d
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions simple_indexer/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ async def get_endpoints_data(request: web.Request) -> web.Response:

async def indexer_get_indexer_settings(request: web.Request) -> web.Response:
app_state: ApplicationState = request.app['app_state']
accept_type = request.headers.get('Accept', 'application/json')
if accept_type not in ('*/*', 'application/json'):
accept_type = request.headers.get("Accept", "*/*")
if accept_type == "*/*":
accept_type = "application/json"
if accept_type != "application/json":
raise web.HTTPBadRequest(reason="invalid 'Accept', expected 'application/json', "
f"got '{accept_type}'")

Expand Down Expand Up @@ -136,7 +138,9 @@ async def indexer_post_indexer_settings(request: web.Request) -> web.Response:
async def get_restoration_matches(request: web.Request) -> web.StreamResponse:
"""This the main endpoint for the rapid restoration API"""
app_state: ApplicationState = request.app['app_state']
accept_type = request.headers.get('Accept')
accept_type = request.headers.get("Accept", "*/*")
if accept_type == "*/*":
accept_type = "application/json"

body = await request.content.read()
if body:
Expand Down Expand Up @@ -180,7 +184,9 @@ async def get_restoration_matches(request: web.Request) -> web.StreamResponse:

async def get_transaction(request: web.Request) -> web.Response:
app_state: ApplicationState = request.app['app_state']
accept_type = request.headers.get('Accept')
accept_type = request.headers.get("Accept", "*/*")
if accept_type == "*/*":
accept_type = "application/json"

tx_id = request.match_info['txid']
if not tx_id:
Expand Down Expand Up @@ -213,7 +219,9 @@ async def get_merkle_proof(request: web.Request) -> web.Response:
"""
# Todo - use the bitcoin node as much as possible (this is only for RegTest)
app_state: ApplicationState = request.app['app_state']
accept_type = request.headers.get('Accept')
accept_type = request.headers.get("Accept", "*/*")
if accept_type == "*/*":
accept_type = "application/json"

txid = request.match_info['txid']
if not txid:
Expand Down Expand Up @@ -291,8 +299,11 @@ async def post_output_spends(request: web.Request) -> web.Response:
"""
Return the metadata for each provided outpoint if they are spent.
"""
accept_type = request.headers.get('Accept')
accept_type = request.headers.get("Accept", "*/*")
if accept_type == "*/*":
accept_type = "application/json"
content_type = request.headers.get('Content-Type')

body = await request.content.read()
if not body:
raise web.HTTPBadRequest(reason="no body")
Expand Down Expand Up @@ -344,8 +355,8 @@ async def post_output_spend_notifications_register(request: web.Request) -> web.
has connected to the notification web socket before making this call, and can keep up
with the notifications.
"""
accept_type = request.headers.get("Accept")
if accept_type is None:
accept_type = request.headers.get("Accept", "*/*")
if accept_type == "*/*":
accept_type = "application/json"

content_type = request.headers.get("Content-Type")
Expand Down Expand Up @@ -457,8 +468,10 @@ async def indexer_post_transaction_filter(request: web.Request) -> web.Response:
if synchronizer is None:
raise web.HTTPServiceUnavailable(reason="error finding synchronizer")

accept_type = request.headers.get("Accept", "application/json")
if accept_type not in ("application/json", "*/*"):
accept_type = request.headers.get("Accept", "*/*")
if accept_type == "*/*":
accept_type = "application/json"
if accept_type != "application/json":
raise web.HTTPBadRequest(reason="only json response body supported")

content_type = request.headers.get("Content-Type")
Expand Down Expand Up @@ -539,7 +552,9 @@ async def indexer_post_transaction_filter_delete(request: web.Request) -> web.Re
if synchronizer is None:
raise web.HTTPServiceUnavailable(reason="error finding synchronizer")

accept_type = request.headers.get('Accept', "application/json")
accept_type = request.headers.get("Accept", "*/*")
if accept_type == "*/*":
accept_type = "application/octet-stream"
if accept_type != "application/octet-stream":
raise web.HTTPBadRequest(reason="only binary response body supported")

Expand Down

0 comments on commit e059f4d

Please sign in to comment.