Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make HEAD responses emtpy #600

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion bases/renku_data_services/data_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import sentry_sdk
import uvloop
from sanic import Sanic
from sanic import Request, Sanic
from sanic.log import logger
from sanic.response import BaseHTTPResponse
from sanic.worker.loader import AppLoader
from sentry_sdk.integrations.asyncio import AsyncioIntegration
from sentry_sdk.integrations.grpc import GRPCIntegration
Expand Down Expand Up @@ -124,6 +125,12 @@ async def setup_sentry(_: Sanic) -> None:

app.register_middleware(validate_null_byte, "request")

@app.middleware("response")
async def handle_head(request: Request, response: BaseHTTPResponse) -> None:
"""Make sure HEAD requests return an empty body."""
if request.method == "HEAD":
response.body = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean, the response has already been created and is now discarded before being rendered?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. the response needs to be created in some cases as we return the etag in the headers (which gets calculated based on DB state) and a client getting the etag without having to receive the whole entity is one of the main use-cases for head requests in our case.

So it doesn't save on server processing, just unnecessary network traffic.

There's not really a better way to solve this with Sanic from what I could tell, other than implementing individual "HEAD" handlers for every endpoint we have.


@app.main_process_start
async def do_migrations(_: Sanic) -> None:
logger.info("running migrations")
Expand Down
11 changes: 11 additions & 0 deletions test/bases/renku_data_services/data_api/test_data_connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ async def test_get_one_data_connector(sanic_client: SanicASGITestClient, create_
assert data_connector.get("slug") == "a-new-data-connector"


@pytest.mark.asyncio
async def test_head_one_data_connector(sanic_client: SanicASGITestClient, create_data_connector, user_headers) -> None:
data_connector = await create_data_connector("A new data connector")
data_connector_id = data_connector["id"]

_, response = await sanic_client.head(f"/api/data/data_connectors/{data_connector_id}", headers=user_headers)

assert response.status_code == 200, response.text
assert response.json is None


@pytest.mark.asyncio
async def test_get_one_by_slug_data_connector(
sanic_client: SanicASGITestClient, create_data_connector, user_headers
Expand Down
Loading