2011-04-16 22:44:22 +00:00
|
|
|
/*
|
2018-01-04 00:52:49 +00:00
|
|
|
Copyright 2011 The Perkeep Authors
|
2011-04-16 22:44:22 +00:00
|
|
|
|
|
|
|
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"
|
2018-05-02 20:35:23 +00:00
|
|
|
"html"
|
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
|
|
|
|
2018-01-03 05:03:30 +00:00
|
|
|
"perkeep.org/internal/httputil"
|
|
|
|
"perkeep.org/internal/images"
|
|
|
|
"perkeep.org/internal/osutil"
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
"perkeep.org/pkg/auth"
|
|
|
|
"perkeep.org/pkg/blobserver"
|
|
|
|
"perkeep.org/pkg/buildinfo"
|
|
|
|
"perkeep.org/pkg/jsonsign/signhandler"
|
|
|
|
"perkeep.org/pkg/search"
|
|
|
|
"perkeep.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
|
2016-11-09 23:14:32 +00:00
|
|
|
shareRoot string // share handler's prefix, if any.
|
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
|
2018-01-13 21:40:52 +00:00
|
|
|
hasLegacySHA1 bool // whether the index has SHA1 blobs. requires searchHandler.
|
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 == "" {
|
2017-09-10 13:28:34 +00:00
|
|
|
err = fmt.Errorf("root handler's %q references non-existent %q", key, v)
|
2015-05-02 12:26:33 +00:00
|
|
|
} 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", "")
|
2016-11-09 23:14:32 +00:00
|
|
|
root.shareRoot = conf.OptionalString("shareRoot", "")
|
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)
|
2018-01-13 21:40:52 +00:00
|
|
|
// the result from root.searchHandler.HasLegacySHA1() is determined on index
|
|
|
|
// startup, and never changes during the server's lifetime, so we might as well
|
|
|
|
// cache it here too.
|
|
|
|
root.hasLegacySHA1 = root.searchHandler.HasLegacySHA1()
|
2013-01-11 18:35:19 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2016-11-09 23:14:32 +00:00
|
|
|
func (rh *RootHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if wantsDiscovery(r) {
|
|
|
|
if auth.Allowed(r, auth.OpDiscovery) {
|
|
|
|
rh.serveDiscovery(w, r)
|
2012-11-07 17:04:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !rh.Stealth {
|
2016-11-09 23:14:32 +00:00
|
|
|
auth.SendUnauthorized(w, r)
|
2012-11-07 17:04:40 +00:00
|
|
|
}
|
2012-05-13 19:16:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-04-16 22:44:22 +00:00
|
|
|
if rh.Stealth {
|
|
|
|
return
|
|
|
|
}
|
2016-11-09 23:14:32 +00:00
|
|
|
if r.RequestURI == "/" && rh.ui != nil {
|
|
|
|
http.Redirect(w, r, "/ui/", http.StatusMovedPermanently)
|
2015-05-07 15:39:29 +00:00
|
|
|
return
|
|
|
|
}
|
2018-04-28 00:22:13 +00:00
|
|
|
switch r.URL.Path {
|
|
|
|
case "/favicon.ico":
|
2021-07-29 19:24:05 +00:00
|
|
|
ServeStaticFile(w, r, &Files, "favicon.ico")
|
2013-01-13 05:25:55 +00:00
|
|
|
return
|
2018-04-28 00:22:13 +00:00
|
|
|
case "/mobile-setup":
|
|
|
|
http.Redirect(w, r, "/ui/mobile.html", http.StatusFound)
|
|
|
|
return
|
|
|
|
case "/":
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
2013-01-13 05:25:55 +00:00
|
|
|
}
|
2018-04-28 00:22:13 +00:00
|
|
|
|
2014-03-17 03:13:47 +00:00
|
|
|
f := func(p string, a ...interface{}) {
|
2016-11-09 23:14:32 +00:00
|
|
|
fmt.Fprintf(w, p, a...)
|
2014-03-17 03:13:47 +00:00
|
|
|
}
|
2018-04-21 18:04:53 +00:00
|
|
|
f("<html><body><p>This is perkeepd (%s), a "+
|
2018-05-02 20:35:23 +00:00
|
|
|
"<a href='http://perkeep.org'>Perkeep</a> server.</p>",
|
|
|
|
html.EscapeString(buildinfo.Summary()))
|
2014-03-17 03:13:47 +00:00
|
|
|
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
|
|
|
}
|
2016-11-09 23:14:32 +00:00
|
|
|
fmt.Fprintf(w, "</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,
|
2016-11-09 23:14:32 +00:00
|
|
|
ShareRoot: rh.shareRoot,
|
2015-05-02 12:26:33 +00:00
|
|
|
StatusRoot: rh.statusRoot,
|
|
|
|
OwnerName: rh.OwnerName,
|
|
|
|
UserName: rh.Username,
|
2016-06-22 14:47:33 +00:00
|
|
|
AuthToken: auth.DiscoveryToken(),
|
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
|
|
|
}
|
2018-01-13 21:40:52 +00:00
|
|
|
d.HasLegacySHA1Index = rh.hasLegacySHA1
|
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)
|
|
|
|
}
|