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

Automatic escape of header fields #40

Open
elukey opened this issue May 11, 2023 · 2 comments
Open

Automatic escape of header fields #40

elukey opened this issue May 11, 2023 · 2 comments

Comments

@elukey
Copy link

elukey commented May 11, 2023

Hi!

I am trying this library with KServe (see kserve/kserve#2782) and I noticed that headers like User-Agent are not automatically escaped. Could it be a feature to add? For example, if I set something like:

'{"remote_address": "%(h)s", "user_name": "%(u)s", "date": "%(t)s", "status": "%(s)s", "method": "%(m)s", "url_path": "%(U)s", "query_string": "%(q)s", "protocol": "%(H)s", "response_length": "%(B)s", "referer": "%(f)s", "user_agent": "%(a)s", "request_time_seconds": "%(L)s"}'

The above leads to valid JSON only if the HTTP headers don't carry extra quotes. I am not sure where the escape would fit best, but added to this library (maybe with a flag?) could be a nice addition. I can work on a PR if the idea is acceptable :)

@Kludex
Copy link
Owner

Kludex commented May 11, 2023

Sure

@elukey
Copy link
Author

elukey commented May 19, 2023

@Kludex If you have time I'd need an advice about the best road to take. Ideally having a JSON representation of the Uvicorn access logs would be nice, so I tried multiple roads:

  1. As described above, hacked the format string to print a JSON-like output, but it is not very resilient when we have special chars.
  2. Run uvicorn with a python json logger in its access log logging config (like python-json-logger) but when I override it with asgi-logger I loose also the JSON formatting.

Not sure if you have suggestions about 1), otherwise another solution could be to add a "json" flag to asgi-logger to use a json formatter in AccessLoggerMiddleware. No idea what road is best, I am all ears for suggestions :)

Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants