I'm new to TypeScript/JavaScript and I'm trying to do multipart range requests (RFC 7233) for different parts of a binary file, using TypeScript and XMLHttpRequest 2.
According to RFC 7233, a multipart/byteranges response has the following:
HTTP/1.1 206 Partial Content
Date: Wed, 15 Nov 1995 06:25:24 GMT
Last-Modified: Wed, 15 Nov 1995 04:58:08 GMT
Content-Length: 1741
Content-Type: multipart/byteranges; boundary=THIS_STRING_SEPARATES
--THIS_STRING_SEPARATES
Content-Type: application/pdf
Content-Range: bytes 500-999/8000
...the first range...
--THIS_STRING_SEPARATES
Content-Type: application/pdf
Content-Range: bytes 7000-7999/8000
...the second range
--THIS_STRING_SEPARATES--
I see two options:
- Treat the body of the response as binary data array (set
XMLHttpRequest.responseType = "arraybuffer"
), convert the boundary string into binary, search in the binary data array for each body part delimited by the boundary-string-converted-to-binary, extract the headers for each payload and converted them to string? or, - Similar to above, but treat the body as a string (set
XMLHttpRequest.responseType = "text"
), identify the body parts delimited by the boundary-string, and convert the payloads to binary data arrays?
What is the proper way to handle/parse such responses in javascript/typescript, since the response contains multiple body parts, each with its own headers (string) and payload (binary)?
Is there a simpler way?
Any suggestions are welcomed. Thanks!
Content-Length
is1741
in this case. As I understand it, it is the number of bytes of the whole body, but it does not come up to1741
. Maybe because it is an example?