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
Expose message length as a property of TextReceived
#86
Comments
How important do you think it is to measure this limit exactly? Notice that you're not going to get the actual on-the-wire size from this, since it doesn't include headers. And you don't really care about on-the-wire size here either, I don't think – the goal is to avoid spending unbounded amounts of memory on buffering, right? |
Yes, that's the goal. Since we don't know how much memory will be used until we decode the string (I think that's why you asked if exactness matters), I was thinking that it would be far easier to enforce maximum message size by comparing to the "payload len" field in the header. If the payload length exceeds the maximum, then the library doesn't even need to decode the payload. This is how aaugstin's websockets appears to do it. Their maximum is enforced per frame, not per message. (Enforcing per message makes more sense to me.) It also won't decompress more than the maximum amount, to prevent a zlib bomb, I suppose. |
wsproto intentionally doesn't enforce any maximum on individual frames, because it can handle arbitrarily large frames in bounded memory (by streaming out pieces of the frame as fragments), and it wants to support users who can handle streaming too. (I still think it'd be nice to support this someday in trio-websocket, e.g. with a low-level The issue with zlib bombs is interesting though... if that's a concern then it can't be neatly handled at a higher level. The low-level frame decoding code actually needs to know about it. Some options:
|
Yes, but isn't the fragmentation of WebSocket frames controlled by the peer that is sending them? I don't see where wsproto can break up large frames and stream them to the caller. Am I confused? |
Ah, that's the confusion! wsproto's decoder does actually re-fragments WebSocket frames on the fly to reduce buffering. If you look at
If you look in And then in And then eventually these end up as the Lines 77 to 90 in deecce1
This is really convoluted and confusing, sorry! But at the end of the day, what it means as a user is:
|
I want to enforce a maximum message size in trio-websocket. For a
BytesReceived
event, it is easy to checklen(event.data)
. But for aTextReceived
event, the string is already decoded andlen(event.data)
returns something else. (The number of characters? The number of code points?) I can encode the string and check its length, but this is a bit of extra work, and I'm unsure that decoding and re-encoding will yield the same bytes.There are two possible ways to address this in
wsproto
.None
.data_size
property toTextReceived
that stores the the message length in bytes. This requires the caller to enforce maximum message sizes. It's a bit awkward to implement because the message size is a field on the header object, not the frame.Is there interest in this? I can make a PR.
The text was updated successfully, but these errors were encountered: