http icon indicating copy to clipboard operation
http copied to clipboard

Implement a correct header multimap

Open ducdetronquito opened this issue 5 years ago • 1 comments

The http core types must define a correct header multimap.

Some good work has already been done by @daurnimator and other contributors in the standard library, and I feel like it would be great to embed it within the Zig HTTP core types library.

Pros:

  • Do not rewrite something from scratch

Cons:

  • Does not have yet a non-allocating API (reported in https://github.com/ziglang/zig/issues/5875)
  • Some implementation details feels unecessary to me (cf: never-indexed fields), but I my understanding of the HTTP protocol is very limited to be really confident here :/

ducdetronquito avatar Sep 22 '20 20:09 ducdetronquito

First rework step made with the PR https://github.com/ducdetronquito/http/pull/7.

The idea is to avoid the complexity of a proper multimap implementation by providing a simple list of headers.

Advantages:

  • Very simple implementation
  • Preserve headers order (cool for proxies)

Drawbacks:

  • Probably a performance penalty, but I haven't benchmark it. I imagine it will only have an impact compared to a hashmap for high number of items.
  • No API to remove a header: I have no use-case for it yet

Remarks:

As of yet, lookup and addition of headers are case-insensitive for standard headers, but case sensitive for custom headers.

var headers = Headers.init(std.testing.allocator);
defer headers.deinit();

try headers.add("Content-Length", "10");
try headers.add("Gotta-Go", "Fast");


headers.get("Content-Lenght"); // --> OK
headers.get("content-length"); // --> OK

headers.get("Gotta-Go"); // --> OK
headers.get("gotta-go"); // --> KO

ducdetronquito avatar Oct 30 '20 12:10 ducdetronquito