fetch icon indicating copy to clipboard operation
fetch copied to clipboard

Support Structured Field Values in the Headers API

Open annevk opened this issue 6 years ago • 2 comments
trafficstars

In particular:

  • An algorithm that takes a header name name and a structured header type type and returns the corresponding header's value in some kind of useful object-representation for type (or an error).
  • An algorithm that takes a header name name and a structured header value value and replaces all corresponding headers with a new structured header (or errors if value cannot be coerced).

Considerations:

  • Should append() continue to function? It probably should (destroying the type in the process, except perhaps if it's a list or dictionary), otherwise converting existing headers to be structured headers would be difficult.
  • How should the getter operation operate on a structured header? Should it pretend to serialize first and then parse to retain maximum compatibility? E.g., being able to interpret something as a list and an item?

annevk avatar Aug 30 '19 09:08 annevk

This topic came up in the HTTP Workshop and it made me think about the requirements some more. The complicated piece here are the getters from an ergonomics perspective (as for the setter you can rely on the type of the value). There's three separate types and for the return values there's parameters. Returning the value without parameters is useful and often what you want. Taken together this might suggest something like:

typedef (boolean or long long or double or ByteString or Uint8Array) SFBareItem; // TODO: distinguish Token and String

interface Headers {
  SFBareItem getItem(ByteString name);
  SFItem getFullItem(ByteString name);
  …
};

(There's a question of course as to how much header processing happens in client-side JS to begin with. Perhaps this can be a library first.)

annevk avatar Nov 03 '22 06:11 annevk

@annevk ... I'd like to see if we can revive this a bit. Having structured header (https://datatracker.ietf.org/doc/html/rfc8941) support in Headers would be useful. I do think we might be able to keep it simple by not messing around the with the set/append/get methods and instead focus on introducing new statics on Headers that only deal with the serialization/deserialization.

h = new Headers();
h.set('abc', Headers.fromItem('a'));
h.set('xyz', Headers.fromList([1,2,3]);
h.set('foo', Headers.fromDictionary(...);

Headers.toDictionary(h.get('foo'));
// etc

The idea here would be to introduce six new static methods on Headers whose purpose to only to serialize/deserialize the structured header syntax:

  • Headers.fromItem(...)
  • Headers.fromList(...)
  • Headers.fromDict(...)
  • Headers.toItem(...)
  • Headers.toList(...)
  • Headers.toDict(...)

jasnell avatar Apr 09 '24 16:04 jasnell