koajs / koa-range

[MAINTAINERS WANTED] range request implementation for koa, see http://tools.ietf.org/html/rfc7233
50 stars 12 forks source link

If the starting point of the range request is greater than the maximum length of the content, the response status 416 shall be returned. #19

Open masx200 opened 3 years ago

masx200 commented 3 years ago

If the starting point of the range request is greater than the maximum length of the content, the response status 416 shall be returned.

GET / HTTP/1.1
Host: localhost:4000
Connection: keep-alive
Cache-Control: no-cache
sec-ch-ua: " Not;A Brand";v="99", "Microsoft Edge";v="91", "Chromium";v="91"
DNT: 1
accept-language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.54
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
upgrade-insecure-requests: 1
if-none-match: "253b-oDUrS8IQyJRM9EiG2CmtiW6ahlM"
range: bytes=9999999-
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: http://localhost:4000/
Accept-Encoding: identity
HTTP/1.1 206 Partial Content
Access-Control-Allow-Origin: *
Accept-Ranges: bytes
Vary: Origin, Accept-Encoding
Content-Type: text/html; charset=utf-8
Content-Length: -9990468
ETag: "253b-pRYhHemHVMhhBMOOkvWAjABIxIE"
Content-Range: bytes 9999999-9530/9531
Date: Tue, 22 Jun 2021 05:59:36 GMT
Connection: keep-alive
Keep-Alive: timeout=5

I found that some video player clients will send out requests for video files beyond the scope. E.g. PotPlayer

masx200 commented 3 years ago

https://github.com/koajs/koa-range/blob/3d18e9f18e450a48e6464f4497e8b619e7310e14/index.js#L80

 if (len !== '*') {
    ctx.length = end - start + 1;
  }

Just make the following modifications.

    if (len !== "*") {
        let ctxlength = end - start + 1;
        if (ctxlength <= 0) {
            ctx.status = 416;
        }

        ctx.length = Math.max(0, ctxlength);
    }
masx200 commented 3 years ago

https://github.com/masx200/koa-range/blob/efa08c6951a53cce9df70fc21690cbe5beb147dd/index.js#L87

masx200 commented 3 years ago

https://github.com/koajs/koa-range/pull/23