Home > front end >  Can the server respond with content-length less than requested range if proper headers are used
Can the server respond with content-length less than requested range if proper headers are used

Time:01-26

I am developing a Node-JS server with some custom behavior for video streaming.

Server is exposing a file as stream on the root path localhost:3000/ and listens for range requests. On the frontend there is a html5 video player which I have noticed by logging, request from in range values current-byte to total-bytes when you scrub on the timeline.

For now I am just piping the file-stream into the response.

My scenario requires me to query another process about the file in question and only pipe down the part of file that it allows me to. Now my question is if the player requests for example from byte 0 to 1000 and I respond with 0 to 100 and I set the headers like below, would the player understand that it didnt get the full data it wanted and make any subsequent requests.

 const range = fromOtherProcess(); // the other process told me this range is allowed
 response.statusCode = 206; // parital content
 response.setHeader('Content-Length', range.end - range.start   1);
 response.setHeader('Content-Range', 'bytes '   range.start   '-'   range.end   '/'   size);

CodePudding user response:

If you can't satisfy the requested range, you want to respond with 416, and include a Content-Range header telling the client what the satisfiable range actually is. See

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/416

If you're really going to handle range requests yourself, you probably want to read the RFC. You need to gracefully handle badly formatted requests, unknown units, multiple ranges, etc. The page I linked above has a link to the current RFC.

  •  Tags:  
  • Related