Re: [hybi] WS framing alternative

Greg Wilkins <> Tue, 27 October 2009 10:08 UTC

Return-Path: <>
Received: from localhost (localhost []) by (Postfix) with ESMTP id BD06E3A6A06 for <>; Tue, 27 Oct 2009 03:08:36 -0700 (PDT)
X-Virus-Scanned: amavisd-new at
X-Spam-Flag: NO
X-Spam-Score: -2.194
X-Spam-Status: No, score=-2.194 tagged_above=-999 required=5 tests=[AWL=0.405, BAYES_00=-2.599]
Received: from ([]) by localhost ( []) (amavisd-new, port 10024) with ESMTP id grvPUKIC4Ghm for <>; Tue, 27 Oct 2009 03:08:35 -0700 (PDT)
Received: from ( []) by (Postfix) with ESMTP id C7D633A657C for <>; Tue, 27 Oct 2009 03:08:35 -0700 (PDT)
Received: by yxe30 with SMTP id 30so15847426yxe.29 for <>; Tue, 27 Oct 2009 03:08:46 -0700 (PDT)
Received: by with SMTP id c39mr26061231ybd.338.1256638126426; Tue, 27 Oct 2009 03:08:46 -0700 (PDT)
Received: from ? ( []) by with ESMTPS id 13sm2602480gxk.13.2009. (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 27 Oct 2009 03:08:45 -0700 (PDT)
Message-ID: <>
Date: Tue, 27 Oct 2009 21:08:40 +1100
From: Greg Wilkins <>
User-Agent: Thunderbird (X11/20090817)
MIME-Version: 1.0
To: Ian Hickson <>
References: <> <>
In-Reply-To: <>
X-Enigmail-Version: 0.95.7
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 7bit
Cc: "" <>, "Thomson, Martin" <>
Subject: Re: [hybi] WS framing alternative
X-Mailman-Version: 2.1.9
Precedence: list
List-Id: Server-Initiated HTTP <>
List-Unsubscribe: <>, <>
List-Archive: <>
List-Post: <>
List-Help: <>
List-Subscribe: <>, <>
X-List-Received-Date: Tue, 27 Oct 2009 10:08:36 -0000

Even though I said that discussion about protocol requirements
is more important, here is some more debate about protocol design:

Ian Hickson wrote:
> This would have some pretty major costs:
> - It requires length delimiting for text frames, which is more complicated 
> to implement (it's non-trivial to tell the difference between characters 
> and bytes).

But length delimited frames are in the protocol anyway, so they have to
be implemented anyway.

If there was only 1 framing type, then we have approximately half the
framing complexity.

> - It requires parsing using a presized buffer for variable-encoding text, 
> which risks character/byte mismatches and thus buffer overruns.

With sentinel encoding, sending data might be marginally simpler,
but receiving data is much harder.

You will still have buffers of fixed sizes when you receive bytes.  You
don't know how much data is coming, so you don't know how big to make
your buffer or when to start turning bytes into characters.

I can see implementations reading a buffer.... scanning for 0x00, not
finding it... allocating a larger buffer... copying the bytes ...
reading more bytes ... scanning again for 0x00.... still not finding
it.... allocating yet another larger buffer... copying the bytes ....

etc.  etc.  until either you get a denial of service or you find
0x00, when you can finally scan over all the bytes again to
convert to characters.

If the message is larger than you want to read, then you don't
know this until you have read all the bytes you can and scanned for
a 0x00.  Resource limits are always better applied before the resource
is consumed than after.

To avoid wastful copying, you have to allocate wasteful large buffers,
which can still fill up and might need to grow.

Existing UTF-8 to character conversions will error if they get a partial
multipart character, so you can start conversion to characters until
you have the entire message.   Efficient implementations will be forced
to reimplement utf-8 converters to scan for 0x00 while converting.
This will allow smaller fixed byte buffers, but will still need
growing and/or large character buffers.  This is probably also
what needs to be done to be able to use direct kernel buffers.

I don't think there is a clear winner between the two types
of framing.  Both have their pros and cons.    But implementing
both will be approximately twice as complex and it will mean that
you have to deal with both sets of cons.