Re: [whatwg/fetch] Allow used body replacement in Request constructor (#675)

> I wonder if we should move step 7.1 down to the other body checks (and I also wonder if we could simplify those somewhat).

Agreed about moving the check down with the others. I'll find a nice spot for it and amend the commit.

I'm not sure there's a better factorization of the body setup steps (38-44). :/ It seems largely complicated by the fact that nothing after the *input* Request object body stealing (setting up *rs* and piping to it) is allowed to throw (to enforce the strong exception safety guarantee, I suppose). Maybe the best thing to do would be to defer the piping action until after all the checks are complete?

1. Let *action* be null.
2. Let *body* be null.
3. *[Step 41]* If *init*'s `body` member is present, run these substeps:
   1. Let *Content-Type* be null.
   2. If *init*'s `keepalive` member is present and is true, then set *body*
      and *Content-Type* to the result of extracting *init*'s `body` member,
      with *keepalive flag* set.
   3. Otherwise, set *body* and *Content-Type* to the result of extracting
      *init*'s `body` member.
   4. If *Content-Type* is non-null and *r*'s headers's header list does not
      contain '`Content-Type`', then append '`Content-Type`'/*Content-Type* to
      *r*'s headers.
4. Otherwise, if *input* is a `Request` object, run these substeps:
   1. *[Step 7.1]* If *input* is disturbed or locked, throw a `TypeError`.
   2. Set *body* to *input*'s request's body.
   3. Set *action* to an action that runs the following substeps:
      1. *[Step 43.1]* Let *rs* be a `ReadableStream` object from which one can read the exact
         same data as one could read from *body*'s stream.
      2. *[Step 43.2]* Set *body* to a new body whose stream is *rs*, whose source is *body*'s
         source and whose total bytes is *body*'s total bytes.
5. *[Step 39]* If *body* is non-null and *request*'s method is `'GET'` or `'HEAD'`, then
   throw a `TypeError`.
6. *[Step 42]* If *body* is non-null and *body*'s source is null, then run these substeps:
   1. If *r*'s request's mode is neither "`same-origin`" nor "`cors`", then
      throw a `TypeError`.
   2. Set *r*'s request's use-CORS-preflight flag.
7. If *action* is non-null, run *action* **immediately**.
8. *[Step 44]* Set *r*'s request's body to *body*.

I'm not really sure that's "better". :) @yutakahirano?

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/pull/675#issuecomment-369741664

Received on Thursday, 1 March 2018 21:46:18 UTC