v0.35.2 80d5067

httprange.go RawHistoryFolder

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) }

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 2

Tags 45

↑↓ moveenter openesc close