-
Notifications
You must be signed in to change notification settings - Fork 2
/
fs_http.go
123 lines (112 loc) · 2.57 KB
/
fs_http.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"sync"
)
type HTTP struct {
ctx context.Context
}
func (g *HTTP) ensure() bool {
if g.ctx == nil {
g.ctx = context.Background()
}
return true
}
func (g *HTTP) List(dir string) (file []Info, err error) {
u := uri(dir)
size, err := httpsize(dir)
return []Info{{URL: &u, Size: size}}, err
//curl -v -H 'Range: bytes=0-0' http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4
}
var cache = sync.Map{}
func newHTTPRequest(verb, path string, body io.Reader) (*http.Request, error) {
r, err := http.NewRequest(verb, path, body)
if err != nil {
return nil, err
}
if *header != "" {
k, v, _ := strings.Cut(*header, ":")
r.Header.Add(k, v)
}
if *agent != "" {
r.Header.Add("User-Agent", *agent)
}
return r, nil
}
func httpsize(dir string) (size int, err error) {
v, _ := cache.Load(dir)
if v, _ := v.(int); v != 0 {
return v, nil
}
defer func() {
if err == nil {
cache.Store(dir, size)
}
}()
r, err := newHTTPRequest("GET", dir, nil)
if err != nil {
return 0, err
}
r.Header.Add("Range", "bytes=0-0")
resp, err := http.DefaultClient.Do(r)
if err != nil || resp.StatusCode/100 > 3 {
if err == nil && resp.StatusCode == 416 {
return 0, nil
}
if err == nil {
return 0, fmt.Errorf("http: %s", resp.Status)
}
return 0, err
}
go func() {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}()
a, b, size := 0, 0, 0
_, err = fmt.Sscanf(resp.Header.Get("Content-Range"), "bytes %d-%d/%d", &a, &b, &size)
return size, err
}
func (f HTTP) Open(file string) (io.ReadCloser, error) {
if *slow {
return f.open(file)
}
r, err := f.fastopen(file)
if err != nil {
return f.open(file)
}
return r, err
}
func (f HTTP) open(file string) (io.ReadCloser, error) {
f.ensure()
req, err := newHTTPRequest("GET", file, nil)
if err != nil {
return nil, err
}
req = req.WithContext(f.ctx)
if *seek != 0 || *count != 0 {
if *count == 0 {
req.Header.Add("Range", fmt.Sprintf("bytes=%d-", *seek))
} else {
req.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", *seek, *seek+*count-1))
}
}
resp, err := http.DefaultClient.Do(req)
if err != nil || resp.StatusCode >= 400 {
if err == nil {
err = fmt.Errorf("status: %v", resp.StatusCode)
}
// NOTE(as): bug here with connection reuse
// if the body isn't read+closed by callee
return nil, err
}
return resp.Body, err
}
func (f HTTP) Create(file string) (io.WriteCloser, error) {
return nil, fmt.Errorf("http: create: not implemented yet")
}
func (f HTTP) Close() error { return nil }