proxy: headers read and rewritten in the connection's buffer

The HTTP proxy read each request into a buffer of 64 KB of its own,
cleared it with memset, and read the answer's headers into it again;
the header lines were copied there one by one from clibuf or srvbuf.
Now the header block of a request is read whole into clibuf, and that
of an answer into srvbuf, and worked on where it is (hdrread()):

- the block is moved to one byte into the buffer, and its lines, as
  they are kept, are moved down to its start, so the line being worked
  on, and the 0 written after it, never reach the next one; headers
  taken out are left behind;
- HDRRESERVE is left free after it, for the headers this proxy adds,
  before whatever came after it, a body or another request, from
  where the connection is read on;
- the buffer is grown only for a block bigger than it, up to the
  limits there were (512 KB for a request, 64 KB for an answer);
- the headers are copied to a buffer of their own only when they need
  more room than that, when a filter hands back one, when a body is to
  be read whole into the same buffer for a data filter, and for an ftp
  listing, which is written where the headers were.

Whether buf is the headers' own buffer is kept with it (bufown), not
found from where it points: the side's buffer may be a bigger one by
then let go of. filterswap() leaves alone a buffer in either side's
buffer, which may be one grown by malloc, not only their fixed arrays.

What took buf for a buffer of LINESIZE does no longer: the request is
logged from a copy of its line, and the rest of a request refused
before its headers were all read is drained through a small buffer of
its own, keeping track of where its lines begin.

Tests: a POST whose body comes in the same packet as its headers,
which are rewritten, with a header added and one taken out; 45 KB of
request headers; two requests pipelined in one packet; empty lines
before a request; an answer with a 30 KB header; a 600 KB header block
refused, the proxy answering after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Vladimir DubrovinandClaude Opus 5.5 committed 2026-10-18 22:49:46 +03:00
1 parent d9b9d1b9b7
commit aee69bf679
3 files changed
+391 -73

No files matched your search

+10 -3
View File
@@ -456,11 +456,18 @@ FILTER_ACTION handleconnectflt(struct clientparam *cparam){
/* A buffer filter given a buffer too small for what it makes of it hands
back one of its own, allocated with malloc, and leaves the one it was given
alone: nothing refers to that one any more, so it is freed here, unless it
is one of the connection's own buffers. The caller owns whatever *buf_p
points to on return. */
is, or is in, one of the connection's own buffers: headers are worked on
in clibuf or srvbuf, which may have been grown to a bigger one of
malloc's. The caller owns whatever *buf_p points to on return. */
static int connbuf(struct clientparam *param, unsigned char *p){
return (p >= param->clibuf && p < param->clibuf + param->clibufsize) ||
(p >= param->srvbuf && p < param->srvbuf + param->srvbufsize) ||
p == param->clibufdata || p == param->srvbufdata || p == param->tmpbuf;
}
static void filterswap(struct clientparam *param, unsigned char **cur, unsigned char *now){
if(now != *cur){
if(*cur != param->clibufdata && *cur != param->srvbufdata && *cur != param->tmpbuf) free(*cur);
if(!connbuf(param, *cur)) free(*cur);
*cur = now;
}
}