#do stuff depending on headers

1 messages Β· Page 1 of 1 (latest)

late cape
#

I want to do stuff depending on headers in a middleware wrapped_receive, so far I'm doing something like:

        async def wrapped_send(message: HTTPSendMessage) -> None:
            if message["type"] == "http.response.start":
                headers = dict(message["headers"])
                if headers[b"content-type"].decode().partition(";")[0] != "text/html":
                    do_something()
                if headers.get(b"transfer-encoding") == b"chunked":
                    do_another()

I wondered if there was a more "Litestar way" to do that, for instance I could probably replace by headers = dict(message["headers"]) by headers = dict(MutableScopeHeaders.from_message(message=message).headers but that's not that shorter or clearer,

stable emberBOT
#
Notes for do stuff depending on headers
At your assistance

@late cape

No Response?

If no response in a reasonable time, ping @Member.

Closing

To close, type !solve or byte solve.

MCVE

Please include an MCVE so that we can reproduce your issue locally.

orchid cipher
#

Hmm. I don't think so

#

There's not really any utilities for mucking around in the send callable

#

Does it have to be a middleware or could it also be a hook?

#

If it's a hook, you might be able to just access the response object

late cape
#

it has to be a middleware I wanna make sure that if compression is enabled I skip the hotreload script injection, or somehow I make sure that the order of middleware is correct

late cape
#

which makes me realize I dont get something : in a on_app_init plugin, if my app has declared the compression_config it has a compression middleware in app_config.compression_config.middleware_class, but it isnt available in the app_config.middleware list.

I'd like to make sure the middeware my plugin is going to create comes before the compression one, is there a way to do that ?

#

I'm asking this because by the time I'm doing app_config.middleware.append(HotReloadMiddleware()) the compression midleware kicked in and I get gzip stuff so I cant inject a js reload script...

#

I admit it's a little bit far-fetched but using my reload plugin so far works fine except this bit

#

once fix I'll pusblish it, or without this possibility ie no-reload if compression enabled

late cape
#

@orchid cipher sorry to bug you on this, but is there a way in a plugin to insert a middleware before the compression one ?

orchid cipher
#

Unfortunately not, no 😬

#

@rain thunder and I have been talking about a feature that would allow you to do this a few times by now, but so far no one's gotten around to implement it

#

But currently, there's no way to specify a specific order of middlewares

late cape
#

bummer πŸ™‚

#

ok that will be a known limitation for my reload thing, not a huge issue tbh, I dont know the code well enough to dig into that anyways,

#

feels somewhat weird though to have middlewares in app_config.middlewares and in other parts, there might be a good reason though

orchid cipher
#

This was just a bad design decision

#

This should be solved though by the new ASGIMiddleware pattern πŸ™‚
There's no need anymore for this config stuff

#

Ideally, anything can just be a plain middleware now, and if it's more complex, a plugin. Less special casing

late cape
#

So if I "upgrade" the compression thing into a plugin that might be a good first step ?

orchid cipher
#

However, even if it was e.g. a plugin, that might not solve your problem, because the plugin that might add a compression middleware could also be initialised after yours

late cape
#

Ok you answered me 😜

orchid cipher
#

πŸ˜›

#

IMO the middleware priority is something that we definitely need

#

We have quite a few hacks in place already to work around this

#

idk why this isn't standard in frameworks really 🀷

#

Should be able to do something like e.g. middleware=[WithPriority(MyMiddleware, before=SomeOtherMiddleware)] or middleware=[WithPriority(MyMiddleware, try_first=True)]

#

This would also make it so things like open telemetry and sentry stuff and things like caching doesn't have to be special cased

late cape
#

in fact all middewares that are not in app_config.middleware

#

all those have a compression_config, cors_config etc

orchid cipher
#

Well these are 2 separate problems

#

We've got this config stuff, which is special casing some things. Those should be simple ASGIMiddlewares or if needed a plugin

#

But then there's special casing amongst those special cases πŸ˜„

#

And that's where you'd need the priority

orchid cipher
orchid cipher
#

Feel free to start working on this, I probably won't be able to in the near future

late cape
#

ok got it I think, I'll try to get a feel for it 1st by turning the compression thing into an ASGIMiddleware and see where I feel the pain

#

interesting

orchid cipher
late cape
#

yep ofc, but it's a good 1st step you think or not worth spending time on it ?

orchid cipher
#

Absolutely worth it, we need to get rid of all these config things

#

That would then also make the middleware priorities easier