1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3// Package httprange selects the byte range of a download from the Range and
4// If-Range headers of a request. The HTTP adapters that serve release assets
5// share it, so they answer the same way.
6package httprange
7
8import (
9 "errors"
10 "net/http"
11 "strconv"
12 "strings"
13)
14
15// ErrUnsatisfiable means the request asked for a range the content cannot give.
16// The caller answers 416 with "Content-Range: bytes */<size>".
17var ErrUnsatisfiable = errors.New("httprange: unsatisfiable byte range")
18
19// Select returns the part of a size-byte body to send.
20//
21// Without a usable Range it returns the whole body (0, size, false, nil):
22// - the method is not GET (HEAD answers with the headers of the full body);
23// - there is no Range header, its unit is not "bytes", or it lists several
24// ranges (ADR 0034: multipart ranges are not served);
25// - If-Range is present and is not exactly etag. The comparison is strong:
26// a weak validator or an HTTP date never matches.
27//
28// A single "bytes=a-b", "bytes=a-" or "bytes=-n" gives (offset, length, true,
29// nil); an end past the content is cut to it. A malformed range, a start at or
30// past size, a suffix of zero, or any range of an empty body returns
31// ErrUnsatisfiable.
32func Select(r *http.Request, size int64, etag string) (offset, length int64, partial bool, err error) {
33 whole := func() (int64, int64, bool, error) { return 0, size, false, nil }
34 if r.Method != http.MethodGet {
35 return whole()
36 }
37 value := strings.Join(r.Header.Values("Range"), ",")
38 if value == "" || strings.Contains(value, ",") || !strings.HasPrefix(value, "bytes=") {
39 return whole()
40 }
41 if ifRange := r.Header.Get("If-Range"); ifRange != "" && (etag == "" || ifRange != etag || strings.HasPrefix(ifRange, "W/")) {
42 return whole()
43 }
44 a, b, ok := strings.Cut(strings.TrimSpace(strings.TrimPrefix(value, "bytes=")), "-")
45 if !ok || size <= 0 {
46 return 0, 0, false, ErrUnsatisfiable
47 }
48 if a == "" {
49 n, e := strconv.ParseInt(b, 10, 64)
50 if e != nil || n <= 0 {
51 return 0, 0, false, ErrUnsatisfiable
52 }
53 n = min(n, size)
54 return size - n, n, true, nil
55 }
56 start, e := strconv.ParseInt(a, 10, 64)
57 if e != nil || start < 0 || start >= size {
58 return 0, 0, false, ErrUnsatisfiable
59 }
60 end := size - 1
61 if b != "" {
62 end, e = strconv.ParseInt(b, 10, 64)
63 if e != nil || end < start {
64 return 0, 0, false, ErrUnsatisfiable
65 }
66 end = min(end, size-1)
67 }
68 return start, end - start + 1, true, nil
69}
70
71// ContentRange is the Content-Range value of a partial answer.
72func ContentRange(offset, length, size int64) string {
73 return "bytes " + strconv.FormatInt(offset, 10) + "-" + strconv.FormatInt(offset+length-1, 10) + "/" + strconv.FormatInt(size, 10)
74}
75
76// Unsatisfied is the Content-Range value of a 416 answer.
77func Unsatisfied(size int64) string { return "bytes */" + strconv.FormatInt(size, 10) }