2011-01-28 07:07:18 +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-02-04 22:31:23 +00:00
|
|
|
package handlers
|
2010-07-26 03:34:04 +00:00
|
|
|
|
|
|
|
import (
|
2012-12-29 14:51:42 +00:00
|
|
|
"crypto/sha1"
|
2012-09-16 22:20:49 +00:00
|
|
|
"errors"
|
2010-07-26 03:34:04 +00:00
|
|
|
"fmt"
|
2012-09-16 22:20:49 +00:00
|
|
|
"io"
|
2010-12-02 06:34:08 +00:00
|
|
|
"log"
|
2010-12-21 02:51:13 +00:00
|
|
|
"mime"
|
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"
|
2011-02-02 06:48:12 +00:00
|
|
|
"strings"
|
2012-12-29 14:51:42 +00:00
|
|
|
"time"
|
2012-09-16 22:20:49 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
2012-09-16 22:20:49 +00:00
|
|
|
"camlistore.org/pkg/blobserver"
|
|
|
|
"camlistore.org/pkg/httputil"
|
2012-12-29 14:51:42 +00:00
|
|
|
"camlistore.org/pkg/jsonsign/signhandler"
|
2013-08-21 20:57:28 +00:00
|
|
|
"camlistore.org/pkg/readerutil"
|
2012-12-29 14:51:42 +00:00
|
|
|
"camlistore.org/pkg/schema"
|
2011-01-29 08:05:07 +00:00
|
|
|
)
|
2010-07-26 03:34:04 +00:00
|
|
|
|
2011-05-30 04:39:51 +00:00
|
|
|
// We used to require that multipart sections had a content type and
|
|
|
|
// filename to make App Engine happy. Now that App Engine supports up
|
|
|
|
// to 32 MB requests and programatic blob writing we can just do this
|
|
|
|
// ourselves and stop making compromises in the spec. Also because
|
|
|
|
// the JavaScript FormData spec (http://www.w3.org/TR/XMLHttpRequest2/)
|
|
|
|
// doesn't let you set those.
|
|
|
|
const oldAppEngineHappySpec = false
|
|
|
|
|
2013-07-08 04:12:18 +00:00
|
|
|
func CreateUploadHandler(storage blobserver.BlobReceiveConfiger) http.Handler {
|
|
|
|
return http.HandlerFunc(func(conn http.ResponseWriter, req *http.Request) {
|
2011-05-09 16:11:18 +00:00
|
|
|
handleMultiPartUpload(conn, req, storage)
|
2013-07-08 04:12:18 +00:00
|
|
|
})
|
2011-02-04 22:31:23 +00:00
|
|
|
}
|
2010-12-17 17:59:03 +00:00
|
|
|
|
2012-12-29 14:51:42 +00:00
|
|
|
// vivify verifies that all the chunks for the file described by fileblob are on the blobserver.
|
|
|
|
// It makes a planned permanode, signs it, and uploads it. It finally makes a camliContent claim
|
|
|
|
// on that permanode for fileblob, signs it, and uploads it to the blobserver.
|
2013-08-04 02:54:30 +00:00
|
|
|
func vivify(blobReceiver blobserver.BlobReceiveConfiger, fileblob blob.SizedRef) error {
|
|
|
|
sf, ok := blobReceiver.(blob.StreamingFetcher)
|
2012-12-29 14:51:42 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("BlobReceiver is not a StreamingFetcher")
|
|
|
|
}
|
2013-08-04 02:54:30 +00:00
|
|
|
fetcher := blob.SeekerFromStreamingFetcher(sf)
|
|
|
|
fr, err := schema.NewFileReader(fetcher, fileblob.Ref)
|
2012-12-29 14:51:42 +00:00
|
|
|
if err != nil {
|
2013-08-04 02:54:30 +00:00
|
|
|
return fmt.Errorf("Filereader error for blobref %v: %v", fileblob.Ref.String(), err)
|
2012-12-29 14:51:42 +00:00
|
|
|
}
|
|
|
|
defer fr.Close()
|
|
|
|
|
|
|
|
h := sha1.New()
|
|
|
|
n, err := io.Copy(h, fr)
|
|
|
|
if err != nil {
|
2013-08-04 02:54:30 +00:00
|
|
|
return fmt.Errorf("Could not read all file of blobref %v: %v", fileblob.Ref.String(), err)
|
2012-12-29 14:51:42 +00:00
|
|
|
}
|
|
|
|
if n != fr.Size() {
|
2013-08-04 02:54:30 +00:00
|
|
|
return fmt.Errorf("Could not read all file of blobref %v. Wanted %v, got %v", fileblob.Ref.String(), fr.Size(), n)
|
2012-12-29 14:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := blobReceiver.Config()
|
|
|
|
if config == nil {
|
|
|
|
return errors.New("blobReceiver has no config")
|
|
|
|
}
|
|
|
|
hf := config.HandlerFinder
|
|
|
|
if hf == nil {
|
|
|
|
return errors.New("blobReceiver config has no HandlerFinder")
|
|
|
|
}
|
|
|
|
JSONSignRoot, sh, err := hf.FindHandlerByType("jsonsign")
|
|
|
|
if err != nil || sh == nil {
|
|
|
|
return errors.New("jsonsign handler not found")
|
|
|
|
}
|
|
|
|
sigHelper, ok := sh.(*signhandler.Handler)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("handler is not a JSON signhandler")
|
|
|
|
}
|
|
|
|
discoMap := sigHelper.DiscoveryMap(JSONSignRoot)
|
|
|
|
publicKeyBlobRef, ok := discoMap["publicKeyBlobRef"].(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Discovery: json decoding error: %v", err)
|
|
|
|
}
|
|
|
|
|
2013-02-01 01:52:11 +00:00
|
|
|
// The file schema must have a modtime to vivify, as the modtime is used for all three of:
|
|
|
|
// 1) the permanode's signature
|
|
|
|
// 2) the camliContent attribute claim's "claimDate"
|
|
|
|
// 3) the signature time of 2)
|
|
|
|
claimDate, err := time.Parse(time.RFC3339, fr.FileSchema().UnixMtime)
|
2012-12-29 14:51:42 +00:00
|
|
|
if err != nil {
|
2013-02-01 01:52:11 +00:00
|
|
|
return fmt.Errorf("While parsing modtime for file %v: %v", fr.FileSchema().FileName, err)
|
2012-12-29 14:51:42 +00:00
|
|
|
}
|
2013-02-01 01:52:11 +00:00
|
|
|
|
|
|
|
permanodeBB := schema.NewHashPlannedPermanode(h)
|
2013-08-04 02:54:30 +00:00
|
|
|
permanodeBB.SetSigner(blob.MustParse(publicKeyBlobRef))
|
2013-02-01 01:52:11 +00:00
|
|
|
permanodeBB.SetClaimDate(claimDate)
|
|
|
|
permanodeSigned, err := sigHelper.Sign(permanodeBB)
|
2012-12-29 14:51:42 +00:00
|
|
|
if err != nil {
|
2013-02-01 01:52:11 +00:00
|
|
|
return fmt.Errorf("Signing permanode %v: %v", permanodeSigned, err)
|
2012-12-29 14:51:42 +00:00
|
|
|
}
|
2013-08-04 02:54:30 +00:00
|
|
|
permanodeRef := blob.SHA1FromString(permanodeSigned)
|
2013-09-16 14:57:14 +00:00
|
|
|
_, err = blobserver.ReceiveNoHash(blobReceiver, permanodeRef, strings.NewReader(permanodeSigned))
|
2013-01-07 19:15:18 +00:00
|
|
|
if err != nil {
|
2013-02-01 01:52:11 +00:00
|
|
|
return fmt.Errorf("While uploading signed permanode %v, %v: %v", permanodeRef, permanodeSigned, err)
|
2013-01-07 19:15:18 +00:00
|
|
|
}
|
2013-02-01 01:52:11 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
contentClaimBB := schema.NewSetAttributeClaim(permanodeRef, "camliContent", fileblob.Ref.String())
|
|
|
|
contentClaimBB.SetSigner(blob.MustParse(publicKeyBlobRef))
|
2013-02-01 01:52:11 +00:00
|
|
|
contentClaimBB.SetClaimDate(claimDate)
|
|
|
|
contentClaimSigned, err := sigHelper.Sign(contentClaimBB)
|
2012-12-29 14:51:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Signing camliContent claim: %v", err)
|
|
|
|
}
|
2013-08-04 02:54:30 +00:00
|
|
|
contentClaimRef := blob.SHA1FromString(contentClaimSigned)
|
2013-09-16 14:57:14 +00:00
|
|
|
_, err = blobserver.ReceiveNoHash(blobReceiver, contentClaimRef, strings.NewReader(contentClaimSigned))
|
2012-12-29 14:51:42 +00:00
|
|
|
if err != nil {
|
2013-02-01 01:52:11 +00:00
|
|
|
return fmt.Errorf("While uploading signed camliContent claim %v, %v: %v", contentClaimRef, contentClaimSigned, err)
|
2012-12-29 14:51:42 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-05-10 21:55:12 +00:00
|
|
|
func handleMultiPartUpload(conn http.ResponseWriter, req *http.Request, blobReceiver blobserver.BlobReceiveConfiger) {
|
2011-03-07 04:11:36 +00:00
|
|
|
if !(req.Method == "POST" && strings.Contains(req.URL.Path, "/camli/upload")) {
|
2011-05-10 21:55:12 +00:00
|
|
|
log.Printf("Inconfigured handler upload handler")
|
2010-12-06 06:34:46 +00:00
|
|
|
httputil.BadRequestError(conn, "Inconfigured handler.")
|
2010-07-26 03:34:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
receivedBlobs := make([]blob.SizedRef, 0, 10)
|
2010-11-05 06:27:29 +00:00
|
|
|
|
2010-07-26 03:34:04 +00:00
|
|
|
multipart, err := req.MultipartReader()
|
|
|
|
if multipart == nil {
|
2010-12-06 06:34:46 +00:00
|
|
|
httputil.BadRequestError(conn, fmt.Sprintf(
|
2010-07-26 03:34:04 +00:00
|
|
|
"Expected multipart/form-data POST request; %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-12-21 02:51:13 +00:00
|
|
|
var errText string
|
|
|
|
addError := func(s string) {
|
2011-05-11 11:59:45 +00:00
|
|
|
log.Printf("Client error: %s", s)
|
2010-12-21 02:51:13 +00:00
|
|
|
if errText == "" {
|
|
|
|
errText = s
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errText = errText + "\n" + s
|
|
|
|
}
|
|
|
|
|
2010-07-26 03:34:04 +00:00
|
|
|
for {
|
2011-03-07 04:11:36 +00:00
|
|
|
mimePart, err := multipart.NextPart()
|
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
|
|
|
if err == io.EOF {
|
2010-07-26 03:34:04 +00:00
|
|
|
break
|
|
|
|
}
|
2011-05-07 12:37:54 +00:00
|
|
|
if err != nil {
|
|
|
|
addError(fmt.Sprintf("Error reading multipart section: %v", err))
|
2010-07-26 03:34:04 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
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
|
|
|
contentDisposition, params, err := mime.ParseMediaType(mimePart.Header.Get("Content-Disposition"))
|
2013-01-01 01:21:00 +00:00
|
|
|
if err != nil {
|
2011-10-03 23:50:01 +00:00
|
|
|
addError("invalid Content-Disposition")
|
2011-08-25 15:14:47 +00:00
|
|
|
break
|
|
|
|
}
|
2011-10-03 23:50:01 +00:00
|
|
|
|
2010-12-21 02:51:13 +00:00
|
|
|
if contentDisposition != "form-data" {
|
|
|
|
addError(fmt.Sprintf("Expected Content-Disposition of \"form-data\"; got %q", contentDisposition))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
formName := params["name"]
|
2013-08-04 02:54:30 +00:00
|
|
|
ref, ok := blob.Parse(formName)
|
|
|
|
if !ok {
|
2010-12-21 02:51:13 +00:00
|
|
|
addError(fmt.Sprintf("Ignoring form key %q", formName))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2011-05-30 04:39:51 +00:00
|
|
|
if oldAppEngineHappySpec {
|
|
|
|
_, hasContentType := mimePart.Header["Content-Type"]
|
|
|
|
if !hasContentType {
|
|
|
|
addError(fmt.Sprintf("Expected Content-Type header for blobref %s; see spec", ref))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, hasFileName := params["filename"]
|
|
|
|
if !hasFileName {
|
|
|
|
addError(fmt.Sprintf("Expected 'filename' Content-Disposition parameter for blobref %s; see spec", ref))
|
|
|
|
continue
|
|
|
|
}
|
2010-07-26 03:34:04 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 20:57:28 +00:00
|
|
|
var tooBig int64 = blobserver.MaxBlobSize + 1
|
|
|
|
var readBytes int64
|
|
|
|
blobGot, err := blobserver.Receive(blobReceiver, ref, &readerutil.CountingReader{
|
|
|
|
io.LimitReader(mimePart, tooBig),
|
|
|
|
&readBytes,
|
|
|
|
})
|
|
|
|
if readBytes == tooBig {
|
|
|
|
err = fmt.Errorf("blob over the limit of %d bytes", blobserver.MaxBlobSize)
|
|
|
|
}
|
2010-11-05 06:27:29 +00:00
|
|
|
if err != nil {
|
2010-12-21 02:51:13 +00:00
|
|
|
addError(fmt.Sprintf("Error receiving blob %v: %v\n", ref, err))
|
2010-11-05 06:27:29 +00:00
|
|
|
break
|
2010-07-26 03:34:04 +00:00
|
|
|
}
|
2010-12-02 06:34:08 +00:00
|
|
|
log.Printf("Received blob %v\n", blobGot)
|
2010-11-05 06:27:29 +00:00
|
|
|
receivedBlobs = append(receivedBlobs, blobGot)
|
2010-07-26 03:34:04 +00:00
|
|
|
}
|
2010-11-05 06:27:29 +00:00
|
|
|
|
2012-09-16 22:20:49 +00:00
|
|
|
ret, err := commonUploadResponse(blobReceiver, req)
|
|
|
|
if err != nil {
|
2013-02-12 04:33:53 +00:00
|
|
|
httputil.ServeError(conn, req, err)
|
2012-09-16 22:20:49 +00:00
|
|
|
}
|
2010-12-02 06:34:08 +00:00
|
|
|
|
|
|
|
received := make([]map[string]interface{}, 0)
|
2010-11-05 06:27:29 +00:00
|
|
|
for _, got := range receivedBlobs {
|
2010-12-02 06:34:08 +00:00
|
|
|
blob := make(map[string]interface{})
|
2013-08-04 02:54:30 +00:00
|
|
|
blob["blobRef"] = got.Ref.String()
|
2011-02-04 22:31:23 +00:00
|
|
|
blob["size"] = got.Size
|
2010-12-02 06:34:08 +00:00
|
|
|
received = append(received, blob)
|
2010-11-05 06:27:29 +00:00
|
|
|
}
|
2010-12-02 06:34:08 +00:00
|
|
|
ret["received"] = received
|
2010-11-05 06:27:29 +00:00
|
|
|
|
2012-12-29 14:51:42 +00:00
|
|
|
if req.Header.Get("X-Camlistore-Vivify") == "1" {
|
|
|
|
for _, got := range receivedBlobs {
|
|
|
|
err := vivify(blobReceiver, got)
|
|
|
|
if err != nil {
|
2013-08-04 02:54:30 +00:00
|
|
|
addError(fmt.Sprintf("Error vivifying blob %v: %v\n", got.Ref.String(), err))
|
2012-12-29 14:51:42 +00:00
|
|
|
} else {
|
2013-08-04 02:54:30 +00:00
|
|
|
conn.Header().Add("X-Camlistore-Vivified", got.Ref.String())
|
2012-12-29 14:51:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-21 02:51:13 +00:00
|
|
|
if errText != "" {
|
|
|
|
ret["errorText"] = errText
|
|
|
|
}
|
|
|
|
|
2012-07-28 22:42:56 +00:00
|
|
|
httputil.ReturnJSON(conn, ret)
|
2010-11-05 06:27:29 +00:00
|
|
|
}
|
|
|
|
|
2012-09-16 22:20:49 +00:00
|
|
|
func commonUploadResponse(configer blobserver.Configer, req *http.Request) (map[string]interface{}, error) {
|
2010-11-05 06:27:29 +00:00
|
|
|
ret := make(map[string]interface{})
|
2013-01-01 01:36:28 +00:00
|
|
|
ret["maxUploadSize"] = blobserver.MaxBlobSize
|
2010-11-05 06:27:29 +00:00
|
|
|
ret["uploadUrlExpirationSeconds"] = 86400
|
2010-07-26 03:34:04 +00:00
|
|
|
|
2011-10-13 06:50:52 +00:00
|
|
|
if configer == nil {
|
2012-09-16 22:20:49 +00:00
|
|
|
err := errors.New("Cannot build uploadUrl: configer is nil")
|
|
|
|
log.Printf("%v", err)
|
|
|
|
return nil, err
|
2011-10-13 06:50:52 +00:00
|
|
|
} else if config := configer.Config(); config != nil {
|
2011-10-07 06:24:40 +00:00
|
|
|
// TODO: camli/upload isn't part of the spec. we should pick
|
|
|
|
// something different here just to make it obvious that this
|
|
|
|
// isn't a well-known URL and accidentally encourage lazy clients.
|
2012-09-16 22:20:49 +00:00
|
|
|
baseURL, err := httputil.BaseURL(config.URLBase, req)
|
|
|
|
if err != nil {
|
|
|
|
errStr := fmt.Sprintf("Cannot build uploadUrl: %v", err)
|
|
|
|
log.Printf(errStr)
|
|
|
|
return ret, fmt.Errorf(errStr)
|
|
|
|
}
|
|
|
|
ret["uploadUrl"] = baseURL + "/camli/upload"
|
2011-10-13 06:50:52 +00:00
|
|
|
} else {
|
2012-09-16 22:20:49 +00:00
|
|
|
err := errors.New("Cannot build uploadUrl: configer.Config is nil")
|
|
|
|
log.Printf("%v", err)
|
|
|
|
return nil, err
|
2011-10-07 06:24:40 +00:00
|
|
|
}
|
2012-09-16 22:20:49 +00:00
|
|
|
return ret, nil
|
2010-07-26 03:34:04 +00:00
|
|
|
}
|