Skip to content

buffering changes to allow header rules #596

Closed

Description

@Geal

right now, sozu is built to handle very large requests: it only cares about the path and the Host header for routing (and, in some cases, the sticky session cookie). Once it has seen them, it starts forwarding data to the backend.
While this has been useful to handle some weird request (like very large cookies), it is limiting what we can do in routing. I'd like to add rules depending on the presence or content of some headers.
There are only 2 ways to do that:

  • allocate buffers to store the header content
  • put a hard limit on the request size: the request line or status line and all the headers should be smaller than the buffer size (the content can be streamed once we have parsed the request and done the routing)

This would make routing slightly slower in some cases (when we get the routing info but not all headers have come, which is rare), but would make parsing and routing easier, and allow more features

Activity

  1. Geal commented on Jun 21, 2019

    @Geal
    MemberAuthor

    Plan:

    • in lib/src/protocol/http/mod.rs, wait for all the headers to be parsed to start proxying
    • define a structure to represent a type of header and its position
      • issue: currently, buffering is defined around the BufferQueue struct that indicates parsing and streaming state: position of currently valid data, end of currently parsed data, position at which we start parsing again (in case of chunking, etc). Headers could point to data that is marked as parsed and ready to be shipped to the backend
    • is the current buffer structure still adapted?
      • we could use bytes::Bytes, which would make it easier to pass data around
      • do we adapt the BufferQueue to do a bit more?
        • the parser could generate a list of structures referring to the buffer, ie request line from here to there, Host header from here to there, etc. We could then apply filters to this list (which would make headers rewriting a bit easier)
    • define how header rules work. Can they be stacked, do we use them for routing? Do we use a list of filters instead of testing rules one by one?
  2. added this to the 0.12 milestone on Jan 10, 2020
  3. Geal commented on Jul 29, 2021

    @Geal
    MemberAuthor

    note: those header changes are necessary for:
    -HTTP/2 support ( #540 ) to separate the client and server HTTP 1 implementations and transmit data between HTTP1 and HTTP/2 parts

    • io_uring: with a clearer separation of the buffers, we will be able to lend them to the kernel to execute tasks list with io_uring
    • wasm plugins WebAssembly plugins #685: we should give access to the headers to wasm plugins, without having them parse again the headers
  4. self-assigned this
    on Jul 30, 2021
  5. modified the milestones: v0.14.0, v0.17.0 on Feb 18, 2022
  6. FlorentinDUBOIS commented on May 2, 2026

    @FlorentinDUBOIS
    Collaborator

    Status update on the original plan, against current main:

    1. Wait for all headers before proxying — done. The legacy lib/src/protocol/http/ path is gone; H1 flows through kawa (lib/src/protocol/kawa_h1/) and the unified Mux session layer (lib/src/protocol/mux/). Router::connect runs route_from_request against a fully populated HttpContext before any backend connect, on both H1 (MuxClear) and H2 (MuxTls, shipped in feat(h2): HTTP/2 support via unified Mux session layer #1209). HTTPS listeners advertising h2 enforce buffer_size >= 16393 so the entire header block lands in one buffer.

    2. Structured header representation — done. Headers are now kawa::Block::Header(kawa::Pair { key, val }) referencing the parser buffer, with structured access via HttpContext. Owned snapshots (HeaderEditSnapshot { key, val }) are taken at the routing edit boundary so downstream emission paths can mutate without aliasing the parser slab.

    3. Buffer structure modernised — done. BufferQueue is removed. The block-list-over-Checkout shape from kawa replaced it. Not the bytes::Bytes originally speculated, but the same property: the parser yields a list of structures referring back into the buffer, with header positions usable for filtering.

    4. Header rules — shipped as mutation in feat: HTTP answer templates, URL rewrite, redirect, custom headers, basic auth #1231 (merged 2026-04-28). RequestHttpFrontend.headers: repeated Header with HeaderPosition { REQUEST, RESPONSE, BOTH }, set/replace/delete semantics (empty val deletes by name, HAProxy del-header parity), and $HOST[n] / $PATH[n] capture substitution in rewrite templates. Applied at the wire emission boundary in mux/router.rs::apply_request_rewrites_and_headers (request side) and mux/shared.rs::apply_response_header_edits (response side, just before kawa.prepare).

    Residual gap: matching frontends on a header value or presence is still not implemented — the router keys on (hostname, path, method). The closest active design vehicle is #1239 (programmable Rust hooks). If header-predicate routing matters as a separate concern, opening a fresh issue scoped narrowly to that — or contributing the predicate shape to #1239 — is the cleaner path forward.

    Closing as completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions