Skip to content

Instantly share code, notes, and snippets.

@altaurog
Last active June 22, 2020 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save altaurog/0471a50535662823b4b54f6e7e3abd7b to your computer and use it in GitHub Desktop.
Save altaurog/0471a50535662823b4b54f6e7e3abd7b to your computer and use it in GitHub Desktop.
starlette on docker x-forwarded-proto
from starlette.requests import Request
from starlette.responses import JSONResponse
async def app(scope, receive, send):
request = Request(scope, receive)
response = JSONResponse(
{"scheme": request.url.scheme, "headers": dict(request.headers),}
)
await response(scope, receive, send)
FROM debian:10-slim
RUN apt-get update && \
apt-get install -y --no-install-recommends \
gcc make python3 python3-dev python3-pip
RUN pip3 install setuptools wheel
RUN pip3 install uvicorn starlette
COPY app.py app.py
CMD ["uvicorn", "app:app", "--port", "8080", "--host", "0.0.0.0", "--proxy-headers"]
@altaurog
Copy link
Author

altaurog commented Jun 22, 2020

package versions:
httptools==0.1.1
starlette==0.13.4
uvicorn==0.11.5
uvloop==0.14.0

running locally with uvicorn app:app --port 8080 --proxy-headers:

$ http :8080/ X-forwarded-proto:https
HTTP/1.1 200 OK
content-length: 185
content-type: application/json
date: Mon, 22 Jun 2020 17:39:52 GMT
server: uvicorn

{
    "headers": {
        "accept": "*/*",
        "accept-encoding": "gzip, deflate",
        "connection": "keep-alive",
        "host": "localhost:8080",
        "user-agent": "HTTPie/2.1.0",
        "x-forwarded-proto": "https"
    },
    "scheme": "https"
}

running with docker run -p 8080:8080 --rm -it httpsmwe:latest:

$ http :8080/ X-forwarded-proto:https
HTTP/1.1 200 OK
content-length: 184
content-type: application/json
date: Mon, 22 Jun 2020 17:40:20 GMT
server: uvicorn

{
    "headers": {
        "accept": "*/*",
        "accept-encoding": "gzip, deflate",
        "connection": "keep-alive",
        "host": "localhost:8080",
        "user-agent": "HTTPie/2.1.0",
        "x-forwarded-proto": "https"
    },
    "scheme": "http"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment