shared-brotli-fetch-spec.txt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. [DRAFT]
  2. Introduction:
  3. This document is a draft proposal for Shard Brotli dictionaries in the fetch spec
  4. (https://fetch.spec.whatwg.org/).
  5. The goal is to add support for custom dictionaries for Brotli. A dictionary is used
  6. to improve compression. A client can download a dictionary from a server and then
  7. use it to decompress resources compressed with this dictionary.
  8. This document specifies how the client and server negotiate the dictionary over HTTP.
  9. A high level overview is as follows: The server adds an HTTP header to the response
  10. with a URL of the dictionary. The browser downloads the dictionary from the URL and
  11. then caches it so it can be reused. The server also adds a checksum to an HTTP header
  12. which the client uses to verify the dictionary. Caching, CORS, and other existing
  13. mechanisms are used. A dictionary can be a pre-made static dictionary, but does not
  14. have to be, for example a previous page loaded from this server, or an old version
  15. of a page, can be used as well.
  16. Below are changes and additions to add Shared Brotli dictionaries to the fetch spec
  17. at https://fetch.spec.whatwg.org/:
  18. Additions to `4.5. HTTP-network-or-cache fetch`
  19. Add to point `15. Modify httpRequest’s header list per HTTP.`:
  20. If the recursive-sbr flag is enabled, `Accept-Encoding` may not contain `sbr`
  21. [NOTE-BOX] When sbr can be used, it is possible to add a header Available-Dict
  22. with the URL and hash code of a cached resource. The server may then use it as
  23. shared dictionary.
  24. Additions to `4.6. HTTP-network fetch`
  25. Add after point `10. Run these steps, but abort if the ongoing fetch is terminated`:
  26. 11. Let codings be the result of extracting header list values given
  27. `Content-Encoding` and response’s header list.
  28. 12. If codings contains `sbr`
  29. 1. If the header list does not contain `Sbr-Dict`, return a network error
  30. 2. Let dictionaryId be the result of extracting header list values given
  31. `Sbr-Dict` and response’s header list.
  32. To point `12. Run these substeps in parallel:`, add new first sub-point:
  33. 1. If codings contains `sbr`, run these subsubsteps:
  34. 1. Let dictionaryResponse be the result of performing a
  35. Shared-Brotli-dictionary fetch given dictionaryId and request.
  36. 2. If dictionaryResponse is a network error, return a network error.
  37. Change point `12.4. Set bytes to the result of handling content codings given codings and bytes.` to:
  38. 4. Set bytes to the result of handling content codings given codings, bytes
  39. and, if codings contains `sbr`, also dictionaryResponse's body.
  40. [NOTE-BOX] If the dictionary is still being fetched, which happens in
  41. parallel, enqueue bytes in a compressed buffer and handle content coding
  42. once the dictionary is fetched
  43. Additions to `2.2.4. Bodies`
  44. Change last section `To handle content codings ...` to:
  45. To handle content codings given codings, bytes and optionally a dictionary, run these substeps:
  46. 1. If codings are not supported, return bytes.
  47. 2. If the codings has `sbr`, run these subsubsteps:
  48. a. Return the result of decoding bytes and dictionary with the Shared
  49. Brotli decoder.
  50. [Shared Brotli Spec] [IANA Brotli](https://www.iana.org/assignments/http-parameters/http-parameters.xhtml)
  51. 3. Else:
  52. a. Return the result of decoding bytes with the given codings, as
  53. explained in HTTP. [HTTP] [HTTP-SEMANTICS] [HTTP-COND] [HTTP-CACHING]
  54. [HTTP-AUTH]
  55. New section `4.10. Shared-Brotli-dictionary fetch`
  56. To perform a Shared-Brotli-dictionary fetch using dictionaryId, and parentRequest, perform these steps:
  57. 1. Let dictionaryURL be the URL extracted from dictionaryId
  58. 2. Let dictionaryHash be the hash id extracted from dictionaryId
  59. 3. Let dictionaryRequest be a new request whose method is `GET`, url is
  60. dictionaryURL, mode is "cors", and client is parentRequest's client.
  61. 4. Let dictionaryResponse be the result of performing an
  62. [HTTP-network-or-cache](https://fetch.spec.whatwg.org/#concept-http-network-or-cache-fetch)
  63. fetch using dictionaryRequest with the recursive-sbr flag set to true.
  64. [NOTE-BOX] For compression benefits, the dictionary should be reused to
  65. decode multiple different responses. We rely on caching to achieve this.
  66. It is suggested for servers to not add any "no-cache" or short "max-age"
  67. Cache-Control directives, and it is suggested for the client to effectively
  68. support caching it.
  69. [NOTE-BOX] Since the same dictionary can be identified by a hash code, a
  70. browser can avoid fetching a dictionary if it already has one with the same
  71. hashed cached from a different source URL.
  72. [NOTE-BOX] It is suggested that a server does not reuse the same URL
  73. to host an updated or different dictionary. Instead the same dictionary URL
  74. should contain a dictionary with the same content and same hash.
  75. 5. If dictionaryResponse is a network error, return a network error.
  76. 6. If dictionaryResponse's status is not an ok status, return a network error.
  77. 7. Let tokens be the result of
  78. [parsing metadata](https://w3c.github.io/webappsec-subresource-integrity/#parse-metadata)
  79. given dictionaryHash.
  80. [Subresource Integrity](https://w3c.github.io/webappsec-subresource-integrity/)
  81. 8. If tokens is no metadata or the length of tokens is not 1, return a network
  82. error
  83. 9. Let algorithm be the alg component of tokens[0]. If alg is 'hw3', set
  84. algorithm to 256-bit HighwayHash
  85. 10. Let digest be the val component of tokens[1].
  86. 11. Let hashValue be the result of base64 decoding digest
  87. [base64](https://tools.ietf.org/html/rfc4648)
  88. 12. If hashValue is not a valid base64 encoding, return a network error
  89. [NOTE-BOX] All of the supported hashing algorithms are cryptographically
  90. secure.
  91. 13. Compute the hash code of dictionaryResponse's body using algorithm and
  92. compare this checksum for equality with hashValue. If the computed
  93. checksum does not match hashValue, return a network error.
  94. 14. Return dictionaryResponse.