2011-07-03 17:53:02 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-10-18 18:12:01 +00:00
|
|
|
package server
|
2011-07-03 17:53:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-02-05 05:07:59 +00:00
|
|
|
"log"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"net/http"
|
2015-02-05 05:07:59 +00:00
|
|
|
"os"
|
2015-04-13 17:45:54 +00:00
|
|
|
"strings"
|
2013-01-25 23:18:32 +00:00
|
|
|
"time"
|
2011-07-03 17:53:02 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/blobserver"
|
|
|
|
"camlistore.org/pkg/magic"
|
|
|
|
"camlistore.org/pkg/schema"
|
2015-02-05 05:07:59 +00:00
|
|
|
"camlistore.org/pkg/search"
|
2016-02-05 14:56:16 +00:00
|
|
|
"go4.org/readerutil"
|
2016-04-22 04:34:24 +00:00
|
|
|
"golang.org/x/net/context"
|
2011-07-03 17:53:02 +00:00
|
|
|
)
|
|
|
|
|
2013-01-25 23:18:32 +00:00
|
|
|
const oneYear = 365 * 86400 * time.Second
|
|
|
|
|
2015-04-13 17:45:54 +00:00
|
|
|
var debugPack = strings.Contains(os.Getenv("CAMLI_DEBUG_X"), "packserve")
|
|
|
|
|
2011-07-03 17:53:02 +00:00
|
|
|
type DownloadHandler struct {
|
2015-02-05 05:07:59 +00:00
|
|
|
Fetcher blob.Fetcher
|
|
|
|
Cache blobserver.Storage
|
|
|
|
|
|
|
|
// Search is optional. If present, it's used to map a fileref
|
|
|
|
// to a wholeref, if the Fetcher is of a type that knows how
|
|
|
|
// to get at a wholeref more efficiently. (e.g. blobpacked)
|
|
|
|
Search *search.Handler
|
|
|
|
|
2015-02-02 09:41:35 +00:00
|
|
|
ForceMIME string // optional
|
2011-07-03 17:53:02 +00:00
|
|
|
}
|
|
|
|
|
2014-03-14 19:11:08 +00:00
|
|
|
func (dh *DownloadHandler) blobSource() blob.Fetcher {
|
|
|
|
return dh.Fetcher // TODO: use dh.Cache
|
2011-07-03 17:53:02 +00:00
|
|
|
}
|
|
|
|
|
2015-02-02 09:41:35 +00:00
|
|
|
type fileInfo struct {
|
2015-04-13 17:45:54 +00:00
|
|
|
mime string
|
|
|
|
name string
|
|
|
|
size int64
|
|
|
|
rs io.ReadSeeker
|
|
|
|
close func() error // release the rs
|
|
|
|
whyNot string // for testing, why fileInfoPacked failed.
|
2015-02-02 09:41:35 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 19:02:41 +00:00
|
|
|
func (dh *DownloadHandler) fileInfo(req *http.Request, file blob.Ref) (fi fileInfo, packed bool, err error) {
|
2016-04-22 04:34:24 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
|
2015-02-05 05:07:59 +00:00
|
|
|
// Fast path for blobpacked.
|
2016-04-22 04:34:24 +00:00
|
|
|
fi, ok := fileInfoPacked(ctx, dh.Search, dh.Fetcher, req, file)
|
2015-04-13 17:45:54 +00:00
|
|
|
if debugPack {
|
|
|
|
log.Printf("download.go: fileInfoPacked: ok=%v, %+v", ok, fi)
|
|
|
|
}
|
2015-02-05 05:07:59 +00:00
|
|
|
if ok {
|
2015-04-13 19:02:41 +00:00
|
|
|
return fi, true, nil
|
2015-02-05 05:07:59 +00:00
|
|
|
}
|
2015-02-02 09:41:35 +00:00
|
|
|
fr, err := schema.NewFileReader(dh.blobSource(), file)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mime := dh.ForceMIME
|
|
|
|
if mime == "" {
|
|
|
|
mime = magic.MIMETypeFromReaderAt(fr)
|
|
|
|
}
|
|
|
|
if mime == "" {
|
|
|
|
mime = "application/octet-stream"
|
|
|
|
}
|
|
|
|
return fileInfo{
|
|
|
|
mime: mime,
|
|
|
|
name: fr.FileName(),
|
|
|
|
size: fr.Size(),
|
|
|
|
rs: fr,
|
|
|
|
close: fr.Close,
|
2015-04-13 19:02:41 +00:00
|
|
|
}, false, nil
|
2015-02-02 09:41:35 +00:00
|
|
|
}
|
|
|
|
|
2015-02-05 05:07:59 +00:00
|
|
|
// Fast path for blobpacked.
|
2016-04-22 04:34:24 +00:00
|
|
|
func fileInfoPacked(ctx context.Context, sh *search.Handler, src blob.Fetcher, req *http.Request, file blob.Ref) (packFileInfo fileInfo, ok bool) {
|
2015-04-13 17:45:54 +00:00
|
|
|
if sh == nil {
|
|
|
|
return fileInfo{whyNot: "no search"}, false
|
2015-02-05 05:07:59 +00:00
|
|
|
}
|
2015-04-13 17:45:54 +00:00
|
|
|
wf, ok := src.(blobserver.WholeRefFetcher)
|
2015-02-05 05:07:59 +00:00
|
|
|
if !ok {
|
2015-04-13 17:45:54 +00:00
|
|
|
return fileInfo{whyNot: "fetcher type"}, false
|
2015-02-05 05:07:59 +00:00
|
|
|
}
|
2015-04-13 17:45:54 +00:00
|
|
|
if req != nil && req.Header.Get("Range") != "" {
|
2015-02-05 05:07:59 +00:00
|
|
|
// TODO: not handled yet. Maybe not even important,
|
|
|
|
// considering rarity.
|
2015-04-13 17:45:54 +00:00
|
|
|
return fileInfo{whyNot: "range header"}, false
|
2015-02-05 05:07:59 +00:00
|
|
|
}
|
2016-04-22 04:34:24 +00:00
|
|
|
des, err := sh.Describe(ctx, &search.DescribeRequest{BlobRef: file})
|
2015-02-05 05:07:59 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("ui: fileInfoPacked: skipping fast path due to error from search: %v", err)
|
2015-04-13 17:45:54 +00:00
|
|
|
return fileInfo{whyNot: "search error"}, false
|
2015-02-05 05:07:59 +00:00
|
|
|
}
|
|
|
|
db, ok := des.Meta[file.String()]
|
|
|
|
if !ok || db.File == nil {
|
2015-04-13 17:45:54 +00:00
|
|
|
return fileInfo{whyNot: "search index doesn't know file"}, false
|
2015-02-05 05:07:59 +00:00
|
|
|
}
|
|
|
|
fi := db.File
|
|
|
|
if !fi.WholeRef.Valid() {
|
2015-04-13 17:45:54 +00:00
|
|
|
return fileInfo{whyNot: "no wholeref from search index"}, false
|
2015-02-05 05:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
offset := int64(0)
|
|
|
|
rc, wholeSize, err := wf.OpenWholeRef(fi.WholeRef, offset)
|
|
|
|
if err == os.ErrNotExist {
|
2015-04-13 17:45:54 +00:00
|
|
|
return fileInfo{whyNot: "WholeRefFetcher returned ErrNotexist"}, false
|
2015-02-05 05:07:59 +00:00
|
|
|
}
|
|
|
|
if wholeSize != fi.Size {
|
|
|
|
log.Printf("ui: fileInfoPacked: OpenWholeRef size %d != index size %d; ignoring fast path", wholeSize, fi.Size)
|
2015-04-13 17:45:54 +00:00
|
|
|
return fileInfo{whyNot: "WholeRefFetcher and index don't agree"}, false
|
2015-02-05 05:07:59 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2015-04-13 17:45:54 +00:00
|
|
|
log.Printf("ui: fileInfoPacked: skipping fast path due to error from WholeRefFetcher (%T): %v", src, err)
|
|
|
|
return fileInfo{whyNot: "WholeRefFetcher error"}, false
|
2015-02-05 05:07:59 +00:00
|
|
|
}
|
|
|
|
return fileInfo{
|
|
|
|
mime: fi.MIMEType,
|
|
|
|
name: fi.FileName,
|
|
|
|
size: fi.Size,
|
2016-02-05 14:56:16 +00:00
|
|
|
rs: readerutil.NewFakeSeeker(rc, fi.Size-offset),
|
2015-02-05 05:07:59 +00:00
|
|
|
close: rc.Close,
|
|
|
|
}, true
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (dh *DownloadHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request, file blob.Ref) {
|
2011-07-03 17:53:02 +00:00
|
|
|
if req.Method != "GET" && req.Method != "HEAD" {
|
2015-02-02 09:41:35 +00:00
|
|
|
http.Error(rw, "Invalid download method", http.StatusBadRequest)
|
2011-07-03 17:53:02 +00:00
|
|
|
return
|
|
|
|
}
|
2013-01-25 23:18:32 +00:00
|
|
|
if req.Header.Get("If-Modified-Since") != "" {
|
|
|
|
// Immutable, so any copy's a good copy.
|
|
|
|
rw.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
2011-07-03 17:53:02 +00:00
|
|
|
|
2015-04-13 19:02:41 +00:00
|
|
|
fi, packed, err := dh.fileInfo(req, file)
|
2011-07-03 17:53:02 +00:00
|
|
|
if err != nil {
|
2015-02-02 09:41:35 +00:00
|
|
|
http.Error(rw, "Can't serve file: "+err.Error(), http.StatusInternalServerError)
|
2011-07-03 17:53:02 +00:00
|
|
|
return
|
|
|
|
}
|
2015-02-02 09:41:35 +00:00
|
|
|
defer fi.close()
|
2011-07-03 17:53:02 +00:00
|
|
|
|
2013-01-25 23:18:32 +00:00
|
|
|
h := rw.Header()
|
2015-02-02 09:41:35 +00:00
|
|
|
h.Set("Content-Length", fmt.Sprint(fi.size))
|
2013-01-25 23:18:32 +00:00
|
|
|
h.Set("Expires", time.Now().Add(oneYear).Format(http.TimeFormat))
|
2015-02-02 09:41:35 +00:00
|
|
|
h.Set("Content-Type", fi.mime)
|
2015-04-13 19:02:41 +00:00
|
|
|
if packed {
|
|
|
|
h.Set("X-Camlistore-Packed", "1")
|
|
|
|
}
|
2011-07-03 17:53:02 +00:00
|
|
|
|
2015-02-02 09:41:35 +00:00
|
|
|
if fi.mime == "application/octet-stream" {
|
2011-12-11 01:52:35 +00:00
|
|
|
// Chrome seems to silently do nothing on
|
|
|
|
// application/octet-stream unless this is set.
|
|
|
|
// Maybe it's confused by lack of URL it recognizes
|
|
|
|
// along with lack of mime type?
|
2015-02-02 09:41:35 +00:00
|
|
|
fileName := fi.name
|
2014-11-06 22:40:25 +00:00
|
|
|
if fileName == "" {
|
|
|
|
fileName = "file-" + file.String() + ".dat"
|
|
|
|
}
|
|
|
|
rw.Header().Set("Content-Disposition", "attachment; filename="+fileName)
|
2011-12-11 01:52:35 +00:00
|
|
|
}
|
2011-07-03 19:28:39 +00:00
|
|
|
|
2013-01-25 23:18:32 +00:00
|
|
|
if req.Method == "HEAD" && req.FormValue("verifycontents") != "" {
|
2013-08-04 02:54:30 +00:00
|
|
|
vbr, ok := blob.Parse(req.FormValue("verifycontents"))
|
|
|
|
if !ok {
|
2011-07-03 17:53:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
hash := vbr.Hash()
|
|
|
|
if hash == nil {
|
|
|
|
return
|
|
|
|
}
|
2015-02-02 09:41:35 +00:00
|
|
|
io.Copy(hash, fi.rs) // ignore errors, caught later
|
2011-07-03 17:53:02 +00:00
|
|
|
if vbr.HashMatches(hash) {
|
|
|
|
rw.Header().Set("X-Camli-Contents", vbr.String())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-02 09:41:35 +00:00
|
|
|
http.ServeContent(rw, req, "", time.Now(), fi.rs)
|
2011-07-03 17:53:02 +00:00
|
|
|
}
|