Skip to content

Commit

Permalink
Case insensitive query parameters (#321)
Browse files Browse the repository at this point in the history
* GetCapabilities returns a 404

* make only query params lowercase

* changed middleware to LowerCaseQueryStringMiddleware and made it optional with API setting.

* changed middleware to LowerCaseQueryStringMiddleware and made it optional with API setting. Only lowercase of query keys.

* changed middleware to LowerCaseQueryStringMiddleware and made it optional with API setting. Hack to lowercase only query keys.

* add test case

* linting

* update changelog

Co-authored-by: vincentsarago <vincent.sarago@gmail.com>
  • Loading branch information
lorenzori and vincentsarago authored Jun 11, 2021
1 parent 00a57be commit adbcd13
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

* update `tilejson` and `WMTSCapabilities.xml` endpoints to allow list querystrings (as done previously in https://github.com/developmentseed/titiler/issues/319)

### titiler.application

* add `titiler.application.middleware.LowerCaseQueryStringMiddleware` to cast all query string parameter to lowercase (author @lorenzori, https://github.com/developmentseed/titiler/pull/321)

## 0.3.2 (2021-05-26)

### titiler.core
Expand Down
28 changes: 28 additions & 0 deletions titiler/application/tests/test_case_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Test titiler.application.middleware.LowerCaseQueryStringMiddleware."""


from titiler.application.middleware import LowerCaseQueryStringMiddleware

from fastapi import FastAPI, Query

from starlette.testclient import TestClient


def test_lowercase_middleware():
"""Make sure upper and lower case QS are accepted."""
app = FastAPI()

@app.get("/route1")
async def route1(value: str = Query(...)):
"""route1."""
return {"value": value}

app.add_middleware(LowerCaseQueryStringMiddleware)

client = TestClient(app)

response = client.get("/route1?value=lorenzori")
assert response.json() == {"value": "lorenzori"}

response = client.get("/route1?VALUE=lorenzori")
assert response.json() == {"value": "lorenzori"}
4 changes: 4 additions & 0 deletions titiler/application/titiler/application/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from titiler.application.middleware import (
CacheControlMiddleware,
LoggerMiddleware,
LowerCaseQueryStringMiddleware,
TotalTimeMiddleware,
)
from titiler.application.routers import cog, mosaic, stac, tms
Expand Down Expand Up @@ -71,6 +72,9 @@
app.add_middleware(LoggerMiddleware, headers=True, querystrings=True)
app.add_middleware(TotalTimeMiddleware)

if api_settings.lower_case_query_parameters:
app.add_middleware(LowerCaseQueryStringMiddleware)


@app.get("/healthz", description="Health Check", tags=["Health Check"])
def ping():
Expand Down
21 changes: 21 additions & 0 deletions titiler/application/titiler/application/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,24 @@ async def dispatch(self, request: Request, call_next):

response = await call_next(request)
return response


class LowerCaseQueryStringMiddleware(BaseHTTPMiddleware):
"""Middleware to make URL parameters case-insensitive.
taken from: https://github.com/tiangolo/fastapi/issues/826
"""

async def dispatch(self, request: Request, call_next):
"""dispatch request."""

self.DECODE_FORMAT = "latin-1"

query_string = ""
for k in request.query_params:
query_string += k.lower() + "=" + request.query_params[k] + "&"

query_string = query_string[:-1]
request.scope["query_string"] = query_string.encode(self.DECODE_FORMAT)

response = await call_next(request)
return response
2 changes: 2 additions & 0 deletions titiler/application/titiler/application/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class ApiSettings(pydantic.BaseSettings):
disable_stac: bool = False
disable_mosaic: bool = False

lower_case_query_parameters: bool = False

@pydantic.validator("cors_origins")
def parse_cors_origin(cls, v):
"""Parse CORS origins."""
Expand Down

0 comments on commit adbcd13

Please sign in to comment.