2011-11-07 16:17:04 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package index
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2011-11-07 16:17:04 +00:00
|
|
|
|
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/magic"
|
|
|
|
"camlistore.org/pkg/schema"
|
2011-11-07 16:17:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// maxSniffSize is how much of a blob to buffer in memory for both
|
|
|
|
// MIME sniffing (in which case 1MB is way overkill) and also for
|
|
|
|
// holding a schema blob in memory for analysis in later steps (where
|
|
|
|
// 1MB is about the max size a claim can be, with about 1023K (of
|
|
|
|
// slack space)
|
|
|
|
const maxSniffSize = 1024 * 1024
|
|
|
|
|
|
|
|
type BlobSniffer struct {
|
|
|
|
header []byte
|
|
|
|
written int64
|
|
|
|
camli *schema.Superset
|
|
|
|
mimeType *string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sn *BlobSniffer) Superset() (*schema.Superset, bool) {
|
|
|
|
return sn.camli, sn.camli != nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (sn *BlobSniffer) Write(d []byte) (int, error) {
|
2011-11-07 16:17:04 +00:00
|
|
|
sn.written += int64(len(d))
|
|
|
|
if len(sn.header) < maxSniffSize {
|
|
|
|
n := maxSniffSize - len(sn.header)
|
|
|
|
if len(d) < n {
|
|
|
|
n = len(d)
|
|
|
|
}
|
|
|
|
sn.header = append(sn.header, d[:n]...)
|
|
|
|
}
|
|
|
|
return len(d), nil
|
|
|
|
}
|
|
|
|
|
2011-11-10 01:15:58 +00:00
|
|
|
func (sn *BlobSniffer) Size() int64 {
|
|
|
|
return sn.written
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:17:04 +00:00
|
|
|
func (sn *BlobSniffer) IsTruncated() bool {
|
|
|
|
return sn.written > maxSniffSize
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (sn *BlobSniffer) Body() ([]byte, error) {
|
2011-11-07 16:17:04 +00:00
|
|
|
if sn.IsTruncated() {
|
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
|
|
|
return nil, errors.New("was truncated")
|
2011-11-07 16:17:04 +00:00
|
|
|
}
|
2012-01-02 04:21:13 +00:00
|
|
|
return sn.header, nil
|
2011-11-07 16:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns content type or empty string if unknown
|
|
|
|
func (sn *BlobSniffer) MimeType() string {
|
|
|
|
if sn.mimeType != nil {
|
|
|
|
return *sn.mimeType
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sn *BlobSniffer) Parse() {
|
|
|
|
// Try to parse it as JSON
|
|
|
|
// TODO: move this into the magic library? Is the magic library Camli-specific
|
|
|
|
// or to be upstreamed elsewhere?
|
|
|
|
if sn.bufferIsCamliJson() {
|
|
|
|
str := "application/json; camliType=" + sn.camli.Type
|
|
|
|
sn.mimeType = &str
|
|
|
|
}
|
|
|
|
|
|
|
|
if mime := magic.MimeType(sn.header); mime != "" {
|
|
|
|
sn.mimeType = &mime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sn *BlobSniffer) bufferIsCamliJson() bool {
|
|
|
|
buf := sn.header
|
|
|
|
if len(buf) < 2 || buf[0] != '{' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
camli := new(schema.Superset)
|
|
|
|
err := json.Unmarshal(buf, camli)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
sn.camli = camli
|
|
|
|
return true
|
|
|
|
}
|