aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go44
1 files changed, 21 insertions, 23 deletions
diff --git a/main.go b/main.go
index a1a7357..0c257cb 100644
--- a/main.go
+++ b/main.go
@@ -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
// 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 {
select {
case title := <-metaChan:
@@ -298,31 +310,17 @@ func proxy(w http.ResponseWriter, req *http.Request) {
if !ok {
return
}
-
- space := metaint - len(queuedChunk)
- if space > len(chunk) {
- space = len(chunk)
- }
-
- queuedChunk = append(queuedChunk, chunk[:space]...)
- if len(queuedChunk) < metaint {
- break
+ missing := metaint - len(queuedData)
+ if len(chunk) < missing {
+ queuedData = append(queuedData, chunk...)
+ continue
}
- if _, err := bufrw.Write(queuedChunk); err != nil {
+ queuedData = append(queuedData, chunk[:missing]...)
+ if _, err := bufrw.Write(queuedData); err != nil {
return
}
-
- queuedChunk = chunk[space:]
- 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 {
+ queuedData = chunk[missing:]
+ if writeMeta() != nil || bufrw.Flush() != nil {
return
}
}