Some cleanup

The proxy function still keeps bothering me.
This commit is contained in:
Přemysl Eric Janouch 2018-10-11 17:23:08 +02:00
parent dddbc5556e
commit 61e3c68cc8
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 21 additions and 23 deletions

44
main.go
View File

@ -289,7 +289,19 @@ func proxy(w http.ResponseWriter, req *http.Request) {
// dataProc may return less data near the end of a subfile, so we give it // dataProc may return less data near the end of a subfile, so we give it
// a maximum count of bytes to return at once and do our own buffering. // a maximum count of bytes to return at once and do our own buffering.
var queuedMetaUpdate, queuedChunk []byte var queuedMetaUpdate, queuedData []byte
writeMeta := func() error {
if !wantMeta {
return nil
}
var meta [1 + 16*255]byte
meta[0] = byte((copy(meta[1:], queuedMetaUpdate) + 15) / 16)
queuedMetaUpdate = nil
_, err := bufrw.Write(meta[:1+int(meta[0])*16])
return err
}
for { for {
select { select {
case title := <-metaChan: case title := <-metaChan:
@ -298,31 +310,17 @@ func proxy(w http.ResponseWriter, req *http.Request) {
if !ok { if !ok {
return return
} }
missing := metaint - len(queuedData)
space := metaint - len(queuedChunk) if len(chunk) < missing {
if space > len(chunk) { queuedData = append(queuedData, chunk...)
space = len(chunk) continue
} }
queuedData = append(queuedData, chunk[:missing]...)
queuedChunk = append(queuedChunk, chunk[:space]...) if _, err := bufrw.Write(queuedData); err != nil {
if len(queuedChunk) < metaint {
break
}
if _, err := bufrw.Write(queuedChunk); err != nil {
return return
} }
queuedData = chunk[missing:]
queuedChunk = chunk[space:] if writeMeta() != nil || bufrw.Flush() != nil {
if wantMeta {
var meta [1 + 16*255]byte
meta[0] = byte((copy(meta[1:], queuedMetaUpdate) + 15) / 16)
queuedMetaUpdate = nil
if _, err := bufrw.Write(meta[:1+int(meta[0])*16]); err != nil {
return
}
}
if err := bufrw.Flush(); err != nil {
return return
} }
} }