Handled possible short writes to httpResponseWriter (#1804)

* XL: Handled possible short writes to httpResponseWriter

* Added tests for Range Header combinations
This commit is contained in:
Krishnan Parthasarathi 2016-05-30 23:57:15 +05:30 committed by Harshavardhana
parent b466f27705
commit 967c2b2940
3 changed files with 30 additions and 20 deletions

View File

@ -19,7 +19,7 @@ package main
import "errors"
// ReadFile - decoded erasure coded file.
func (e erasure) ReadFile(volume, path string, bufferOffset int64, startOffset int64, buffer []byte) (int64, error) {
func (e erasure) ReadFile(volume, path string, startOffset int64, buffer []byte) (int64, error) {
// Calculate the current encoded block size.
curEncBlockSize := getEncodedBlockLen(int64(len(buffer)), e.DataBlocks)
offsetEncOffset := getEncodedBlockLen(startOffset, e.DataBlocks)
@ -89,7 +89,7 @@ func (e erasure) ReadFile(volume, path string, bufferOffset int64, startOffset i
}
// Copy data blocks.
copy(buffer, dataBlocks[bufferOffset:])
copy(buffer, dataBlocks)
// Relenquish memory.
dataBlocks = nil

View File

@ -920,9 +920,18 @@ func (s *MyAPIXLSuite) TestPartialContent(c *C) {
c.Assert(response.StatusCode, Equals, http.StatusOK)
// Prepare request
var table = []struct {
byteRange string
expectedString string
}{
{"6-7", "Wo"},
{"6-", "World"},
{"-7", "o World"},
}
for _, t := range table {
request, err = s.newRequest("GET", testAPIXLServer.URL+"/partial-content/bar", 0, nil)
c.Assert(err, IsNil)
request.Header.Add("Range", "bytes=6-7")
request.Header.Add("Range", "bytes="+t.byteRange)
client = http.Client{}
response, err = client.Do(request)
@ -930,8 +939,8 @@ func (s *MyAPIXLSuite) TestPartialContent(c *C) {
c.Assert(response.StatusCode, Equals, http.StatusPartialContent)
partialObject, err := ioutil.ReadAll(response.Body)
c.Assert(err, IsNil)
c.Assert(string(partialObject), Equals, "Wo")
c.Assert(string(partialObject), Equals, t.expectedString)
}
}
func (s *MyAPIXLSuite) TestListObjectsHandlerErrors(c *C) {

View File

@ -1,6 +1,7 @@
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"io"
@ -63,27 +64,27 @@ func (xl xlObjects) GetObject(bucket, object string, startOffset int64, length i
}
var buffer = make([]byte, curBlockSize)
var n int64
n, err = erasure.ReadFile(bucket, pathJoin(object, part.Name), partOffset, beginOffset, buffer)
n, err = erasure.ReadFile(bucket, pathJoin(object, part.Name), beginOffset, buffer)
if err != nil {
return err
}
if length > int64(len(buffer)) {
var m int
m, err = writer.Write(buffer)
var m int64
m, err = io.Copy(writer, bytes.NewReader(buffer[partOffset:]))
if err != nil {
return err
}
length -= int64(m)
length -= m
} else {
_, err = writer.Write(buffer[:length])
_, err = io.CopyN(writer, bytes.NewReader(buffer[partOffset:]), length)
if err != nil {
return err
}
return nil
}
totalLeft -= partOffset + n
totalLeft -= n
beginOffset += n
// Reset part offset to 0 to read rest of the parts from the beginning.
// Reset part offset to 0 to read rest of the part from the beginning.
partOffset = 0
}
}