2011-04-16 22:44:22 +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-04-16 22:44:22 +00:00
|
|
|
|
|
|
|
import (
|
2012-11-07 17:57:43 +00:00
|
|
|
"encoding/json"
|
2011-04-16 22:44:22 +00:00
|
|
|
"fmt"
|
2013-01-11 18:35:19 +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"
|
2013-01-11 22:21:56 +00:00
|
|
|
"sort"
|
2013-01-11 18:35:19 +00:00
|
|
|
"sync"
|
2011-04-16 22:44:22 +00:00
|
|
|
|
2012-04-15 18:09:51 +00:00
|
|
|
"camlistore.org/pkg/auth"
|
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"
|
2014-03-17 03:13:47 +00:00
|
|
|
"camlistore.org/pkg/buildinfo"
|
2015-05-02 12:26:33 +00:00
|
|
|
"camlistore.org/pkg/httputil"
|
2013-12-17 04:27:49 +00:00
|
|
|
"camlistore.org/pkg/images"
|
2015-05-02 12:26:33 +00:00
|
|
|
"camlistore.org/pkg/jsonsign/signhandler"
|
2014-02-11 17:39:32 +00:00
|
|
|
"camlistore.org/pkg/osutil"
|
2012-11-07 17:57:43 +00:00
|
|
|
"camlistore.org/pkg/search"
|
2015-05-02 12:26:33 +00:00
|
|
|
"camlistore.org/pkg/types/camtypes"
|
2016-02-05 14:56:16 +00:00
|
|
|
|
2015-12-01 16:19:49 +00:00
|
|
|
"go4.org/jsonconfig"
|
2016-02-05 14:56:16 +00:00
|
|
|
"go4.org/types"
|
2011-04-16 22:44:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RootHandler handles serving the about/splash page.
|
|
|
|
type RootHandler struct {
|
2012-11-07 17:57:43 +00:00
|
|
|
// Stealth determines whether we hide from non-authenticated
|
|
|
|
// clients.
|
2011-07-02 16:09:50 +00:00
|
|
|
Stealth bool
|
2012-05-13 19:16:59 +00:00
|
|
|
|
2014-02-11 17:39:32 +00:00
|
|
|
OwnerName string // for display purposes only.
|
|
|
|
Username string // default user for mobile setup.
|
2012-12-12 02:41:23 +00:00
|
|
|
|
2012-11-07 17:57:43 +00:00
|
|
|
// URL prefixes (path or full URL) to the primary blob and
|
|
|
|
// search root.
|
2014-08-30 13:41:19 +00:00
|
|
|
BlobRoot string
|
|
|
|
SearchRoot string
|
2015-02-21 12:22:11 +00:00
|
|
|
helpRoot string
|
2014-08-30 13:41:19 +00:00
|
|
|
importerRoot string
|
|
|
|
statusRoot string
|
|
|
|
Prefix string // root handler's prefix
|
2012-11-07 17:57:43 +00:00
|
|
|
|
2015-05-02 12:26:33 +00:00
|
|
|
// JSONSignRoot is the optional path or full URL to the JSON
|
|
|
|
// Signing helper.
|
|
|
|
JSONSignRoot string
|
|
|
|
|
2012-11-07 17:57:43 +00:00
|
|
|
Storage blobserver.Storage // of BlobRoot, or nil
|
2013-01-11 18:35:19 +00:00
|
|
|
|
|
|
|
searchInitOnce sync.Once // runs searchInit, which populates searchHandler
|
|
|
|
searchInit func()
|
|
|
|
searchHandler *search.Handler // of SearchRoot, or nil
|
2012-11-07 17:57:43 +00:00
|
|
|
|
2015-05-02 12:26:33 +00:00
|
|
|
ui *UIHandler // or nil, if none configured
|
|
|
|
sigh *signhandler.Handler // or nil, if none configured
|
|
|
|
sync []*SyncHandler // list of configured sync handlers, for discovery.
|
2011-04-16 22:44:22 +00:00
|
|
|
}
|
|
|
|
|
2013-01-11 18:35:19 +00:00
|
|
|
func (rh *RootHandler) SearchHandler() (h *search.Handler, ok bool) {
|
|
|
|
rh.searchInitOnce.Do(rh.searchInit)
|
|
|
|
return rh.searchHandler, rh.searchHandler != nil
|
|
|
|
}
|
|
|
|
|
2011-05-26 14:34:39 +00:00
|
|
|
func init() {
|
|
|
|
blobserver.RegisterHandlerConstructor("root", newRootFromConfig)
|
|
|
|
}
|
|
|
|
|
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 newRootFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (h http.Handler, err error) {
|
2015-05-02 12:26:33 +00:00
|
|
|
checkType := func(key string, htype string) {
|
|
|
|
v := conf.OptionalString(key, "")
|
|
|
|
if v == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ct := ld.GetHandlerType(v)
|
|
|
|
if ct == "" {
|
|
|
|
err = fmt.Errorf("root handler's %q references non-existant %q", key, v)
|
|
|
|
} else if ct != htype {
|
|
|
|
err = fmt.Errorf("root handler's %q references %q of type %q; expected type %q", key, v, ct, htype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
checkType("searchRoot", "search")
|
|
|
|
checkType("jsonSignRoot", "jsonsign")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-08-16 01:24:23 +00:00
|
|
|
username, _ := getUserName()
|
2012-11-07 17:57:43 +00:00
|
|
|
root := &RootHandler{
|
2015-05-02 12:26:33 +00:00
|
|
|
BlobRoot: conf.OptionalString("blobRoot", ""),
|
|
|
|
SearchRoot: conf.OptionalString("searchRoot", ""),
|
|
|
|
JSONSignRoot: conf.OptionalString("jsonSignRoot", ""),
|
|
|
|
OwnerName: conf.OptionalString("ownerName", username),
|
|
|
|
Username: osutil.Username(),
|
|
|
|
Prefix: ld.MyPrefix(),
|
2012-11-07 17:57:43 +00:00
|
|
|
}
|
2011-04-16 22:44:22 +00:00
|
|
|
root.Stealth = conf.OptionalBool("stealth", false)
|
2013-06-05 17:18:27 +00:00
|
|
|
root.statusRoot = conf.OptionalString("statusRoot", "")
|
2015-02-21 12:22:11 +00:00
|
|
|
root.helpRoot = conf.OptionalString("helpRoot", "")
|
2011-04-16 22:44:22 +00:00
|
|
|
if err = conf.Validate(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-04-15 18:09:51 +00:00
|
|
|
|
2012-11-07 17:57:43 +00:00
|
|
|
if root.BlobRoot != "" {
|
|
|
|
bs, err := ld.GetStorage(root.BlobRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Root handler's blobRoot of %q error: %v", root.BlobRoot, err)
|
|
|
|
}
|
|
|
|
root.Storage = bs
|
|
|
|
}
|
|
|
|
|
2015-05-02 12:26:33 +00:00
|
|
|
if root.JSONSignRoot != "" {
|
|
|
|
h, _ := ld.GetHandler(root.JSONSignRoot)
|
|
|
|
if sigh, ok := h.(*signhandler.Handler); ok {
|
|
|
|
root.sigh = sigh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-11 18:35:19 +00:00
|
|
|
root.searchInit = func() {}
|
2012-11-07 17:57:43 +00:00
|
|
|
if root.SearchRoot != "" {
|
2013-01-11 18:35:19 +00:00
|
|
|
prefix := root.SearchRoot
|
|
|
|
if t := ld.GetHandlerType(prefix); t != "search" {
|
|
|
|
if t == "" {
|
|
|
|
return nil, fmt.Errorf("root handler's searchRoot of %q is invalid and doesn't refer to a declared handler", prefix)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("root handler's searchRoot of %q is of type %q, not %q", prefix, t, "search")
|
|
|
|
}
|
|
|
|
root.searchInit = func() {
|
|
|
|
h, err := ld.GetHandler(prefix)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error fetching SearchRoot at %q: %v", prefix, err)
|
|
|
|
}
|
|
|
|
root.searchHandler = h.(*search.Handler)
|
|
|
|
root.searchInit = nil
|
|
|
|
}
|
2012-05-13 19:16:59 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 13:41:19 +00:00
|
|
|
if pfx, _, _ := ld.FindHandlerByType("importer"); err == nil {
|
|
|
|
root.importerRoot = pfx
|
|
|
|
}
|
|
|
|
|
2011-04-16 22:44:22 +00:00
|
|
|
return root, nil
|
|
|
|
}
|
|
|
|
|
2012-11-07 17:57:43 +00:00
|
|
|
func (rh *RootHandler) registerUIHandler(h *UIHandler) {
|
|
|
|
rh.ui = h
|
|
|
|
}
|
|
|
|
|
2013-01-11 22:21:56 +00:00
|
|
|
func (rh *RootHandler) registerSyncHandler(h *SyncHandler) {
|
|
|
|
rh.sync = append(rh.sync, h)
|
|
|
|
sort.Sort(byFromTo(rh.sync))
|
|
|
|
}
|
|
|
|
|
2012-11-07 17:04:40 +00:00
|
|
|
func (rh *RootHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2012-11-07 17:57:43 +00:00
|
|
|
if wantsDiscovery(req) {
|
2013-01-15 14:01:57 +00:00
|
|
|
if auth.Allowed(req, auth.OpDiscovery) {
|
2012-11-07 17:57:43 +00:00
|
|
|
rh.serveDiscovery(rw, req)
|
2012-11-07 17:04:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !rh.Stealth {
|
|
|
|
http.Error(rw, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
}
|
2012-05-13 19:16:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-04-16 22:44:22 +00:00
|
|
|
if rh.Stealth {
|
|
|
|
return
|
|
|
|
}
|
2015-05-07 15:39:29 +00:00
|
|
|
if req.RequestURI == "/" && rh.ui != nil {
|
|
|
|
http.Redirect(rw, req, "/ui/", http.StatusMovedPermanently)
|
|
|
|
return
|
|
|
|
}
|
2013-01-13 05:25:55 +00:00
|
|
|
if req.URL.Path == "/favicon.ico" {
|
2014-06-14 20:14:34 +00:00
|
|
|
ServeStaticFile(rw, req, Files, "favicon.ico")
|
2013-01-13 05:25:55 +00:00
|
|
|
return
|
|
|
|
}
|
2014-03-17 03:13:47 +00:00
|
|
|
f := func(p string, a ...interface{}) {
|
|
|
|
fmt.Fprintf(rw, p, a...)
|
|
|
|
}
|
|
|
|
f("<html><body><p>This is camlistored (%s), a "+
|
|
|
|
"<a href='http://camlistore.org'>Camlistore</a> server.</p>", buildinfo.Version())
|
|
|
|
if rh.ui != nil {
|
|
|
|
f("<p>To manage your content, access the <a href='%s'>%s</a>.</p>", rh.ui.prefix, rh.ui.prefix)
|
|
|
|
}
|
|
|
|
if rh.statusRoot != "" {
|
2015-02-21 12:22:11 +00:00
|
|
|
f("<p>To view status, see <a href='%s'>%s</a>.</p>", rh.statusRoot, rh.statusRoot)
|
|
|
|
}
|
|
|
|
if rh.helpRoot != "" {
|
|
|
|
f("<p>To view more information on accessing the server, see <a href='%s'>%s</a>.</p>", rh.helpRoot, rh.helpRoot)
|
2011-04-16 22:44:22 +00:00
|
|
|
}
|
2014-03-17 03:13:47 +00:00
|
|
|
fmt.Fprintf(rw, "</body></html>")
|
2011-04-16 22:44:22 +00:00
|
|
|
}
|
2012-11-07 17:57:43 +00:00
|
|
|
|
2013-01-11 22:21:56 +00:00
|
|
|
type byFromTo []*SyncHandler
|
|
|
|
|
|
|
|
func (b byFromTo) Len() int { return len(b) }
|
|
|
|
func (b byFromTo) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
|
|
|
func (b byFromTo) Less(i, j int) bool {
|
|
|
|
if b[i].fromName < b[j].fromName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return b[i].fromName == b[j].fromName && b[i].toName < b[j].toName
|
|
|
|
}
|
|
|
|
|
2012-11-07 17:57:43 +00:00
|
|
|
func (rh *RootHandler) serveDiscovery(rw http.ResponseWriter, req *http.Request) {
|
2015-05-02 12:26:33 +00:00
|
|
|
d := &camtypes.Discovery{
|
|
|
|
BlobRoot: rh.BlobRoot,
|
|
|
|
JSONSignRoot: rh.JSONSignRoot,
|
2015-02-21 12:22:11 +00:00
|
|
|
HelpRoot: rh.helpRoot,
|
2015-05-02 12:26:33 +00:00
|
|
|
ImporterRoot: rh.importerRoot,
|
|
|
|
SearchRoot: rh.SearchRoot,
|
|
|
|
StatusRoot: rh.statusRoot,
|
|
|
|
OwnerName: rh.OwnerName,
|
|
|
|
UserName: rh.Username,
|
2016-01-08 21:15:56 +00:00
|
|
|
AuthToken: auth.Token(),
|
2015-05-02 12:26:33 +00:00
|
|
|
ThumbVersion: images.ThumbnailVersion(),
|
2012-11-07 17:57:43 +00:00
|
|
|
}
|
2012-11-07 19:55:37 +00:00
|
|
|
if gener, ok := rh.Storage.(blobserver.Generationer); ok {
|
|
|
|
initTime, gen, err := gener.StorageGeneration()
|
|
|
|
if err != nil {
|
2015-05-02 12:26:33 +00:00
|
|
|
d.StorageGenerationError = err.Error()
|
2012-11-07 19:55:37 +00:00
|
|
|
} else {
|
2015-05-02 12:26:33 +00:00
|
|
|
d.StorageInitTime = types.Time3339(initTime)
|
|
|
|
d.StorageGeneration = gen
|
2012-11-07 19:55:37 +00:00
|
|
|
}
|
2013-12-13 07:24:34 +00:00
|
|
|
} else {
|
|
|
|
log.Printf("Storage type %T is not a blobserver.Generationer; not sending storageGeneration", rh.Storage)
|
2012-11-07 19:55:37 +00:00
|
|
|
}
|
2012-11-07 17:57:43 +00:00
|
|
|
if rh.ui != nil {
|
2015-05-02 12:26:33 +00:00
|
|
|
d.UIDiscovery = rh.ui.discovery()
|
|
|
|
}
|
|
|
|
if rh.sigh != nil {
|
|
|
|
d.Signing = rh.sigh.Discovery(rh.JSONSignRoot)
|
2012-11-07 17:57:43 +00:00
|
|
|
}
|
2013-01-11 22:21:56 +00:00
|
|
|
if len(rh.sync) > 0 {
|
2015-05-02 12:26:33 +00:00
|
|
|
syncHandlers := make([]camtypes.SyncHandlerDiscovery, 0, len(rh.sync))
|
2013-01-11 22:21:56 +00:00
|
|
|
for _, sh := range rh.sync {
|
2015-05-02 12:26:33 +00:00
|
|
|
syncHandlers = append(syncHandlers, sh.discovery())
|
2013-01-11 22:21:56 +00:00
|
|
|
}
|
2015-05-02 12:26:33 +00:00
|
|
|
d.SyncHandlers = syncHandlers
|
2013-01-11 22:21:56 +00:00
|
|
|
}
|
2015-05-02 12:26:33 +00:00
|
|
|
discoveryHelper(rw, req, d)
|
2012-11-07 17:57:43 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 12:26:33 +00:00
|
|
|
func discoveryHelper(rw http.ResponseWriter, req *http.Request, dr *camtypes.Discovery) {
|
2012-11-07 17:57:43 +00:00
|
|
|
rw.Header().Set("Content-Type", "text/javascript")
|
2013-01-20 20:46:50 +00:00
|
|
|
if cb := req.FormValue("cb"); identOrDotPattern.MatchString(cb) {
|
2012-11-07 17:57:43 +00:00
|
|
|
fmt.Fprintf(rw, "%s(", cb)
|
2012-12-23 22:17:14 +00:00
|
|
|
defer rw.Write([]byte(");\n"))
|
|
|
|
} else if v := req.FormValue("var"); identOrDotPattern.MatchString(v) {
|
2012-12-23 22:19:14 +00:00
|
|
|
fmt.Fprintf(rw, "%s = ", v)
|
2012-12-23 22:17:14 +00:00
|
|
|
defer rw.Write([]byte(";\n"))
|
2012-11-07 17:57:43 +00:00
|
|
|
}
|
2015-05-02 12:26:33 +00:00
|
|
|
bytes, err := json.MarshalIndent(dr, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
httputil.ServeJSONError(rw, httputil.ServerError("encoding discovery information: "+err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2012-11-07 17:57:43 +00:00
|
|
|
rw.Write(bytes)
|
|
|
|
}
|