return err for processRangeRequest()

This commit is contained in:
chrislu 2022-09-07 20:14:09 -07:00
parent 695e3a909c
commit 7e52b1e195
2 changed files with 14 additions and 14 deletions

View file

@ -277,7 +277,7 @@ func adjustHeaderContentDisposition(w http.ResponseWriter, r *http.Request, file
} }
} }
func processRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64, mimeType string, writeFn func(writer io.Writer, offset int64, size int64) error) { func processRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64, mimeType string, writeFn func(writer io.Writer, offset int64, size int64) error) error {
rangeReq := r.Header.Get("Range") rangeReq := r.Header.Get("Range")
bufferedWriter := bufio.NewWriterSize(w, 128*1024) bufferedWriter := bufio.NewWriterSize(w, 128*1024)
defer bufferedWriter.Flush() defer bufferedWriter.Flush()
@ -285,11 +285,11 @@ func processRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64
if rangeReq == "" { if rangeReq == "" {
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10)) w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))
if err := writeFn(bufferedWriter, 0, totalSize); err != nil { if err := writeFn(bufferedWriter, 0, totalSize); err != nil {
glog.Errorf("processRangeRequest headers: %+v err: %v", w.Header(), err) glog.Errorf("processRangeRequest: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return fmt.Errorf("processRangeRequest: %v", err)
} }
return return nil
} }
//the rest is dealing with partial content request //the rest is dealing with partial content request
@ -298,17 +298,17 @@ func processRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64
if err != nil { if err != nil {
glog.Errorf("processRangeRequest headers: %+v err: %v", w.Header(), err) glog.Errorf("processRangeRequest headers: %+v err: %v", w.Header(), err)
http.Error(w, err.Error(), http.StatusRequestedRangeNotSatisfiable) http.Error(w, err.Error(), http.StatusRequestedRangeNotSatisfiable)
return return fmt.Errorf("processRangeRequest header: %v", err)
} }
if sumRangesSize(ranges) > totalSize { if sumRangesSize(ranges) > totalSize {
// The total number of bytes in all the ranges // The total number of bytes in all the ranges
// is larger than the size of the file by // is larger than the size of the file by
// itself, so this is probably an attack, or a // itself, so this is probably an attack, or a
// dumb client. Ignore the range request. // dumb client. Ignore the range request.
return return nil
} }
if len(ranges) == 0 { if len(ranges) == 0 {
return return nil
} }
if len(ranges) == 1 { if len(ranges) == 1 {
// RFC 2616, Section 14.16: // RFC 2616, Section 14.16:
@ -329,18 +329,18 @@ func processRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64
w.WriteHeader(http.StatusPartialContent) w.WriteHeader(http.StatusPartialContent)
err = writeFn(bufferedWriter, ra.start, ra.length) err = writeFn(bufferedWriter, ra.start, ra.length)
if err != nil { if err != nil {
glog.Errorf("processRangeRequest headers: %+v err: %v", w.Header(), err) glog.Errorf("processRangeRequest range[0]: %+v err: %v", w.Header(), err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return fmt.Errorf("processRangeRequest range[0]: %v", err)
} }
return return nil
} }
// process multiple ranges // process multiple ranges
for _, ra := range ranges { for _, ra := range ranges {
if ra.start > totalSize { if ra.start > totalSize {
http.Error(w, "Out of Range", http.StatusRequestedRangeNotSatisfiable) http.Error(w, "Out of Range", http.StatusRequestedRangeNotSatisfiable)
return return fmt.Errorf("out of range: %v", err)
} }
} }
sendSize := rangesMIMESize(ranges, mimeType, totalSize) sendSize := rangesMIMESize(ranges, mimeType, totalSize)
@ -371,6 +371,7 @@ func processRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64
if _, err := io.CopyN(bufferedWriter, sendContent, sendSize); err != nil { if _, err := io.CopyN(bufferedWriter, sendContent, sendSize); err != nil {
glog.Errorf("processRangeRequest err: %v", err) glog.Errorf("processRangeRequest err: %v", err)
http.Error(w, "Internal Error", http.StatusInternalServerError) http.Error(w, "Internal Error", http.StatusInternalServerError)
return return fmt.Errorf("processRangeRequest err: %v", err)
} }
return nil
} }

View file

@ -328,14 +328,13 @@ func writeResponseContent(filename, mimeType string, rs io.ReadSeeker, w http.Re
return nil return nil
} }
processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error { return processRangeRequest(r, w, totalSize, mimeType, func(writer io.Writer, offset int64, size int64) error {
if _, e = rs.Seek(offset, 0); e != nil { if _, e = rs.Seek(offset, 0); e != nil {
return e return e
} }
_, e = io.CopyN(writer, rs, size) _, e = io.CopyN(writer, rs, size)
return e return e
}) })
return nil
} }
func (vs *VolumeServer) streamWriteResponseContent(filename string, mimeType string, volumeId needle.VolumeId, n *needle.Needle, w http.ResponseWriter, r *http.Request, readOption *storage.ReadOption) { func (vs *VolumeServer) streamWriteResponseContent(filename string, mimeType string, volumeId needle.VolumeId, n *needle.Needle, w http.ResponseWriter, r *http.Request, readOption *storage.ReadOption) {