forked from htcat/htcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
228 lines (197 loc) · 4.55 KB
/
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package htcat
import (
"bufio"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"sync"
)
const (
_ = iota
kB int64 = 1 << (10 * iota)
mB
gB
tB
pB
eB
)
type HtCat struct {
io.WriterTo
d defrag
u *url.URL
cl *http.Client
// Protect httpFragGen with a Mutex.
httpFragGenMu sync.Mutex
hfg httpFragGen
}
type HttpStatusError struct {
error
Status string
}
func (cat *HtCat) startup(parallelism int) {
req := http.Request{
Method: "GET",
URL: cat.u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: nil,
Host: cat.u.Host,
}
resp, err := cat.cl.Do(&req)
if err != nil {
go cat.d.cancel(err)
return
}
// Check for non-200 OK response codes from the startup-GET.
if resp.StatusCode != 200 {
err = HttpStatusError{
error: fmt.Errorf(
"Expected HTTP Status 200, received: %q",
resp.Status),
Status: resp.Status}
go cat.d.cancel(err)
return
}
l := resp.Header.Get("Content-Length")
// Some kinds of small or indeterminate-length files will
// receive no parallelism. This procedure helps prepare the
// HtCat value for a one-HTTP-Request GET.
noParallel := func(wtc writerToCloser) {
f := cat.d.nextFragment()
cat.d.setLast(cat.d.lastAllocated())
f.contents = wtc
cat.d.register(f)
}
if l == "" {
// No Content-Length, stream without parallelism nor
// assumptions about the length of the stream.
go noParallel(struct {
io.WriterTo
io.Closer
}{
WriterTo: bufio.NewReader(resp.Body),
Closer: resp.Body,
})
return
}
length, err := strconv.ParseInt(l, 10, 64)
if err != nil {
// Invalid integer for Content-Length, defer reporting
// the error until a WriteTo call is made.
go cat.d.cancel(err)
return
}
// Set up httpFrag generator state.
cat.hfg.totalSize = length
cat.hfg.targetFragSize = 1 + ((length - 1) / int64(parallelism))
if cat.hfg.targetFragSize > 20*mB {
cat.hfg.targetFragSize = 20 * mB
}
// Very small fragments are probably not worthwhile to start
// up new requests for, but it in this case it was possible to
// ascertain the size, so take advantage of that to start
// reading in the background as eagerly as possible.
if cat.hfg.targetFragSize < 1*mB {
cat.hfg.curPos = cat.hfg.totalSize
er := newEagerReader(resp.Body, cat.hfg.totalSize)
go noParallel(er)
go er.WaitClosed()
return
}
// None of the other special short-circuit cases have been
// triggered, so begin preparation for full-blown parallel
// GET. One GET worker is started here to take advantage of
// the already pending response (which has no determinate
// length, so it must be limited).
hf := cat.nextFragment()
go func() {
er := newEagerReader(
struct {
io.Reader
io.Closer
}{
Reader: io.LimitReader(resp.Body, hf.size),
Closer: resp.Body,
},
hf.size)
hf.fragment.contents = er
cat.d.register(hf.fragment)
er.WaitClosed()
// Chain into being a regular worker, having finished
// the special start-up segment.
cat.get()
}()
}
func New(client *http.Client, u *url.URL, parallelism int) *HtCat {
cat := HtCat{
u: u,
cl: client,
}
cat.d.initDefrag()
cat.WriterTo = &cat.d
cat.startup(parallelism)
if cat.hfg.curPos == cat.hfg.totalSize {
return &cat
}
// Start background workers.
//
// "startup" starts one worker that is specially constructed
// to deal with the first request, so back off by one to
// prevent performing with too much parallelism.
for i := 1; i < parallelism; i += 1 {
go cat.get()
}
return &cat
}
func (cat *HtCat) nextFragment() *httpFrag {
cat.httpFragGenMu.Lock()
defer cat.httpFragGenMu.Unlock()
var hf *httpFrag
if cat.hfg.hasNext() {
f := cat.d.nextFragment()
hf = cat.hfg.nextFragment(f)
} else {
cat.d.setLast(cat.d.lastAllocated())
}
return hf
}
func (cat *HtCat) get() {
for {
hf := cat.nextFragment()
if hf == nil {
return
}
req := http.Request{
Method: "GET",
URL: cat.u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: hf.header,
Body: nil,
Host: cat.u.Host,
}
resp, err := cat.cl.Do(&req)
if err != nil {
cat.d.cancel(err)
return
}
// Check for an acceptable HTTP status code.
if !(resp.StatusCode == 206 || resp.StatusCode == 200) {
err = HttpStatusError{
error: fmt.Errorf("Expected HTTP Status "+
"206 or 200, received: %q",
resp.Status),
Status: resp.Status}
go cat.d.cancel(err)
return
}
er := newEagerReader(resp.Body, hf.size)
hf.fragment.contents = er
cat.d.register(hf.fragment)
er.WaitClosed()
}
}