File: http_fragment.go

package info (click to toggle)
golang-github-htcat-htcat 1.0.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 100 kB
  • sloc: makefile: 5
file content (47 lines) | stat: -rw-r--r-- 731 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package htcat

import (
	"fmt"
	"net/http"
)

type httpFragGen struct {
	curPos    int64
	totalSize int64

	targetFragSize int64
}

type httpFrag struct {
	*fragment
	header http.Header
	size   int64
}

func (fg *httpFragGen) hasNext() bool {
	return fg.curPos < fg.totalSize
}

func (fg *httpFragGen) nextFragment(f *fragment) *httpFrag {
	// Determine fragment size.
	var fragSize int64
	remaining := fg.totalSize - fg.curPos
	if remaining < fg.targetFragSize {
		fragSize = remaining
	} else {
		fragSize = fg.targetFragSize
	}

	hf := httpFrag{
		fragment: f,
		header: http.Header{
			"Range": {fmt.Sprintf("bytes=%d-%d",
				fg.curPos, fg.curPos+fragSize-1)},
		},
		size: fragSize,
	}

	fg.curPos += fragSize

	return &hf
}