gateway: fix seeker can't seek on specific files

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera 2017-10-18 20:10:22 +02:00
parent 329eb1cd1d
commit e71dce5dfb
5 changed files with 37 additions and 4 deletions

View File

@ -268,7 +268,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
if !dir {
name := gopath.Base(urlPath)
http.ServeContent(w, r, name, modtime, dr)
i.serverFile(w, r, name, modtime, dr)
return
}
@ -372,6 +372,17 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
}
}
func (i *gatewayHandler) serverFile(w http.ResponseWriter, req *http.Request, name string, modtime time.Time, content io.ReadSeeker) {
http.ServeContent(w, req, name, modtime, content)
//TODO: check for errors in ServeContent.. somehow
// If http.ServeContent can't figure out content size it won't write it to the
// responseWriter, Content-Length not being set is a good indicator of this
if req.Method != "HEAD" && w.Header().Get("Content-Length") == "" {
io.Copy(w, content)
}
}
func (i *gatewayHandler) postHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
p, err := i.api.Unixfs().Add(ctx, r.Body)
if err != nil {

View File

@ -0,0 +1,2 @@
foo

Binary file not shown.

View File

@ -124,7 +124,7 @@ test_expect_success "HEAD 'index.html' has no content" '
# test ipfs readonly api
test_curl_gateway_api() {
curl -sfo actual "http://127.0.0.1:$port/api/v0/$1"
curl -sfo actual "http://127.0.0.1:$port/api/v0/$1"
}
test_expect_success "get IPFS directory file through readonly API succeeds" '
@ -140,7 +140,7 @@ test_expect_success "refs IPFS directory file through readonly API succeeds" '
'
test_expect_success "test gateway api is sanitized" '
for cmd in "add" "block/put" "bootstrap" "config" "dht" "diag" "dns" "get" "id" "mount" "name/publish" "object/put" "object/new" "object/patch" "pin" "ping" "refs/local" "repo" "resolve" "stats" "swarm" "file" "update" "version" "bitswap"; do
for cmd in "add" "block/put" "bootstrap" "config" "dht" "diag" "dns" "get" "id" "mount" "name/publish" "object/put" "object/new" "object/patch" "pin" "ping" "refs/local" "repo" "resolve" "stats" "swarm" "file" "update" "version" "bitswap"; do
test_curl_resp_http_code "http://127.0.0.1:$port/api/v0/$cmd" "HTTP/1.1 404 Not Found"
done
'
@ -155,6 +155,17 @@ test_expect_success "try fetching it from gateway" '
test_cmp rfile ffile
'
test_expect_success "Add compact blocks" '
ipfs block put ../t0110-gateway-data/foo.block &&
FOO2_HASH=$(ipfs block put ../t0110-gateway-data/foofoo.block) &&
printf "foofoo" > expected
'
test_expect_success "GET compact blocks succeeds" '
curl -o actual "http://127.0.0.1:$port/ipfs/$FOO2_HASH" &&
test_cmp expected actual
'
test_kill_ipfs_daemon
test_done

View File

@ -243,7 +243,16 @@ func (dr *pbDagReader) Seek(offset int64, whence int) (int64, error) {
return dr.Seek(noffset, io.SeekStart)
case io.SeekEnd:
noffset := int64(dr.pbdata.GetFilesize()) - offset
return dr.Seek(noffset, io.SeekStart)
n, err := dr.Seek(noffset, io.SeekStart)
// Return negative number if we can't figure out the file size. Using io.EOF
// for this seems to be good(-enough) solution as it's only returned by
// precalcNextBuf when we step out of file range.
// This is needed for gateway to function properly
if err == io.EOF && *dr.pbdata.Type == ftpb.Data_File {
return -1, nil
}
return n, err
default:
return 0, errors.New("invalid whence")
}