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-12-23 20:39:15 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2012-11-07 17:57:43 +00:00
|
|
|
"errors"
|
2011-04-16 22:44:22 +00:00
|
|
|
"fmt"
|
2012-12-23 20:39:15 +00:00
|
|
|
"io"
|
2011-05-26 04:56:48 +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"
|
2012-12-23 20:39:15 +00:00
|
|
|
"os"
|
2012-05-13 20:02:20 +00:00
|
|
|
"path"
|
2012-12-03 22:46:08 +00:00
|
|
|
"path/filepath"
|
2011-04-16 22:44:22 +00:00
|
|
|
"regexp"
|
2011-06-15 08:42:18 +00:00
|
|
|
"strconv"
|
2011-05-31 17:20:28 +00:00
|
|
|
"strings"
|
2012-12-23 20:39:15 +00:00
|
|
|
"sync"
|
2012-12-12 02:29:58 +00:00
|
|
|
"time"
|
2011-04-16 22:44:22 +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/blobref"
|
|
|
|
"camlistore.org/pkg/blobserver"
|
|
|
|
"camlistore.org/pkg/httputil"
|
|
|
|
"camlistore.org/pkg/jsonconfig"
|
2012-12-29 14:51:42 +00:00
|
|
|
"camlistore.org/pkg/jsonsign/signhandler"
|
2012-12-12 17:04:05 +00:00
|
|
|
"camlistore.org/pkg/osutil"
|
2013-06-11 09:50:56 +00:00
|
|
|
newuistatic "camlistore.org/server/camlistored/ui"
|
2011-04-16 22:44:22 +00:00
|
|
|
)
|
|
|
|
|
2011-05-26 04:56:48 +00:00
|
|
|
var _ = log.Printf
|
|
|
|
|
2012-05-13 19:14:31 +00:00
|
|
|
var (
|
2013-06-11 09:50:56 +00:00
|
|
|
staticFilePattern = regexp.MustCompile(`^([a-zA-Z0-9\-\_]+\.(html|js|css|png|jpg|gif))$`)
|
|
|
|
identOrDotPattern = regexp.MustCompile(`^[a-zA-Z\_]+(\.[a-zA-Z\_]+)*$`)
|
2012-05-13 19:14:31 +00:00
|
|
|
|
|
|
|
// Download URL suffix:
|
|
|
|
// $1: blobref (checked in download handler)
|
|
|
|
// $2: optional "/filename" to be sent as recommended download name,
|
|
|
|
// if sane looking
|
2013-06-11 09:50:56 +00:00
|
|
|
downloadPattern = regexp.MustCompile(`^download/([^/]+)(/.*)?$`)
|
|
|
|
thumbnailPattern = regexp.MustCompile(`^thumbnail/([^/]+)(/.*)?$`)
|
|
|
|
treePattern = regexp.MustCompile(`^tree/([^/]+)(/.*)?$`)
|
|
|
|
closurePattern = regexp.MustCompile(`^closure/(([^/]+)(/.*)?)$`)
|
2012-05-13 19:14:31 +00:00
|
|
|
)
|
2011-04-16 22:44:22 +00:00
|
|
|
|
2012-12-03 22:46:08 +00:00
|
|
|
var newuiFiles = newuistatic.Files
|
2011-06-17 21:13:01 +00:00
|
|
|
|
2011-04-16 22:44:22 +00:00
|
|
|
// UIHandler handles serving the UI and discovery JSON.
|
|
|
|
type UIHandler struct {
|
2012-11-07 17:57:43 +00:00
|
|
|
// JSONSignRoot is the optional path or full URL to the JSON
|
|
|
|
// Signing helper. Only used by the UI and thus necessary if
|
|
|
|
// UI is true.
|
|
|
|
// TODO(bradfitz): also move this up to the root handler,
|
|
|
|
// if we start having clients (like phones) that we want to upload
|
|
|
|
// but don't trust to have private signing keys?
|
2011-04-17 23:01:41 +00:00
|
|
|
JSONSignRoot string
|
2011-04-16 22:44:22 +00:00
|
|
|
|
2011-06-19 20:09:43 +00:00
|
|
|
PublishRoots map[string]*PublishHandler
|
|
|
|
|
2012-11-07 17:57:43 +00:00
|
|
|
prefix string // of the UI handler itself
|
|
|
|
root *RootHandler
|
2012-12-29 14:51:42 +00:00
|
|
|
sigh *signhandler.Handler // or nil
|
2012-11-07 17:57:43 +00:00
|
|
|
|
|
|
|
Cache blobserver.Storage // or nil
|
|
|
|
sc ScaledImage // cache for scaled images, optional
|
2011-07-05 19:10:35 +00:00
|
|
|
|
2013-06-12 09:05:01 +00:00
|
|
|
// closureHandler serves the Closure JS files.
|
|
|
|
closureHandler http.Handler
|
2011-04-16 22:44:22 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 14:34:39 +00:00
|
|
|
func init() {
|
2012-05-13 19:53:17 +00:00
|
|
|
blobserver.RegisterHandlerConstructor("ui", newUIFromConfig)
|
2011-05-26 14:34:39 +00:00
|
|
|
}
|
|
|
|
|
2012-05-13 19:53:17 +00:00
|
|
|
func newUIFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (h http.Handler, err error) {
|
2012-05-13 19:20:18 +00:00
|
|
|
ui := &UIHandler{
|
2012-05-13 20:02:20 +00:00
|
|
|
prefix: ld.MyPrefix(),
|
2012-05-13 19:20:18 +00:00
|
|
|
JSONSignRoot: conf.OptionalString("jsonSignRoot", ""),
|
|
|
|
}
|
2011-06-19 20:09:43 +00:00
|
|
|
pubRoots := conf.OptionalList("publishRoots")
|
2011-06-04 16:58:50 +00:00
|
|
|
cachePrefix := conf.OptionalString("cache", "")
|
2011-10-25 20:30:02 +00:00
|
|
|
scType := conf.OptionalString("scaledImage", "")
|
2011-04-16 22:44:22 +00:00
|
|
|
if err = conf.Validate(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2011-05-09 21:20:19 +00:00
|
|
|
|
2012-12-24 00:58:26 +00:00
|
|
|
if ui.JSONSignRoot != "" {
|
|
|
|
h, _ := ld.GetHandler(ui.JSONSignRoot)
|
2012-12-29 14:51:42 +00:00
|
|
|
if sigh, ok := h.(*signhandler.Handler); ok {
|
2012-12-24 00:58:26 +00:00
|
|
|
ui.sigh = sigh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-19 20:09:43 +00:00
|
|
|
ui.PublishRoots = make(map[string]*PublishHandler)
|
|
|
|
for _, pubRoot := range pubRoots {
|
|
|
|
h, err := ld.GetHandler(pubRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("UI handler's publishRoots references invalid %q", pubRoot)
|
|
|
|
}
|
|
|
|
pubh, ok := h.(*PublishHandler)
|
|
|
|
if !ok {
|
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, fmt.Errorf("UI handler's publishRoots references invalid %q; not a PublishHandler", pubRoot)
|
2011-06-19 20:09:43 +00:00
|
|
|
}
|
2011-06-19 21:12:56 +00:00
|
|
|
ui.PublishRoots[pubRoot] = pubh
|
2011-06-19 20:09:43 +00:00
|
|
|
}
|
|
|
|
|
2011-05-09 21:20:19 +00:00
|
|
|
checkType := func(key string, htype string) {
|
|
|
|
v := conf.OptionalString(key, "")
|
|
|
|
if v == "" {
|
|
|
|
return
|
|
|
|
}
|
2011-05-26 14:34:39 +00:00
|
|
|
ct := ld.GetHandlerType(v)
|
2011-05-09 21:20:19 +00:00
|
|
|
if ct == "" {
|
|
|
|
err = fmt.Errorf("UI handler's %q references non-existant %q", key, v)
|
|
|
|
} else if ct != htype {
|
2013-01-11 03:29:10 +00:00
|
|
|
err = fmt.Errorf("UI handler's %q references %q of type %q; expected type %q", key, v, ct, htype)
|
2011-05-09 21:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
checkType("searchRoot", "search")
|
|
|
|
checkType("jsonSignRoot", "jsonsign")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-06-04 16:58:50 +00:00
|
|
|
if cachePrefix != "" {
|
|
|
|
bs, err := ld.GetStorage(cachePrefix)
|
|
|
|
if err != nil {
|
2011-06-17 03:45:47 +00:00
|
|
|
return nil, fmt.Errorf("UI handler's cache of %q error: %v", cachePrefix, err)
|
2011-06-04 16:58:50 +00:00
|
|
|
}
|
|
|
|
ui.Cache = bs
|
2011-10-25 20:30:02 +00:00
|
|
|
switch scType {
|
|
|
|
case "lrucache":
|
2013-06-12 09:05:01 +00:00
|
|
|
ui.sc = NewScaledImageLRU()
|
2011-10-25 20:30:02 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported ui handler's scType: %q ", scType)
|
|
|
|
}
|
2011-06-04 16:58:50 +00:00
|
|
|
}
|
|
|
|
|
2013-06-12 09:05:01 +00:00
|
|
|
// In development mode, serve the Closure files from disk directly.
|
|
|
|
isDevServer := os.Getenv("CAMLI_DEV_UI_FILES") != "" // and not in --staticres mode
|
|
|
|
if isDevServer {
|
|
|
|
camliRootPath, err := osutil.GoPackagePath("camlistore.org")
|
|
|
|
if err == nil {
|
|
|
|
closureDir := filepath.Join(camliRootPath, "tmp", "closure-lib", "closure")
|
|
|
|
fi, err := os.Stat(closureDir)
|
|
|
|
if err == nil && fi.IsDir() {
|
|
|
|
ui.closureHandler = http.FileServer(http.Dir(closureDir))
|
|
|
|
}
|
2013-06-11 09:50:56 +00:00
|
|
|
} else {
|
2013-06-12 09:05:01 +00:00
|
|
|
log.Printf("Package camlistore.org not found in $GOPATH (or $GOPATH not defined)." +
|
|
|
|
" Online closure from " + closureBaseURL + " will be used.")
|
2013-06-11 09:50:56 +00:00
|
|
|
}
|
2013-06-12 09:05:01 +00:00
|
|
|
}
|
|
|
|
if ui.closureHandler == nil {
|
|
|
|
ui.closureHandler = closureRedirector{}
|
2012-12-12 17:04:05 +00:00
|
|
|
}
|
2011-07-05 19:10:35 +00:00
|
|
|
|
2012-11-07 17:57:43 +00:00
|
|
|
rootPrefix, _, err := ld.FindHandlerByType("root")
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("No root handler configured, which is necessary for the ui handler")
|
|
|
|
}
|
|
|
|
if h, err := ld.GetHandler(rootPrefix); err == nil {
|
|
|
|
ui.root = h.(*RootHandler)
|
|
|
|
ui.root.registerUIHandler(ui)
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("failed to find the 'root' handler")
|
|
|
|
}
|
|
|
|
|
2011-04-16 22:44:22 +00:00
|
|
|
return ui, nil
|
|
|
|
}
|
|
|
|
|
2013-06-11 09:50:56 +00:00
|
|
|
const closureBaseURL = "https://closure-library.googlecode.com/git"
|
|
|
|
|
2013-06-12 09:05:01 +00:00
|
|
|
// closureRedirector is a hack to redirect requests for Closure's million *.js files
|
|
|
|
// to https://closure-library.googlecode.com/git.
|
|
|
|
// TODO: this doesn't work when offline. We need to run genjsdeps over all of the Camlistore
|
|
|
|
// UI to figure out which Closure *.js files to fileembed and generate zembed. Then this
|
|
|
|
// type can be deleted.
|
2013-06-11 12:00:53 +00:00
|
|
|
type closureRedirector struct{}
|
2013-06-11 09:50:56 +00:00
|
|
|
|
2013-06-12 09:05:01 +00:00
|
|
|
func (closureRedirector) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2013-06-12 09:17:30 +00:00
|
|
|
newURL := closureBaseURL + "/" + path.Clean(httputil.PathSuffix(req))
|
2013-06-11 09:50:56 +00:00
|
|
|
http.Redirect(rw, req, newURL, http.StatusTemporaryRedirect)
|
|
|
|
}
|
|
|
|
|
2011-05-26 04:56:48 +00:00
|
|
|
func camliMode(req *http.Request) string {
|
2012-05-13 19:13:58 +00:00
|
|
|
return req.URL.Query().Get("camli.mode")
|
2011-05-26 04:56:48 +00:00
|
|
|
}
|
|
|
|
|
2011-04-16 23:18:31 +00:00
|
|
|
func wantsDiscovery(req *http.Request) bool {
|
|
|
|
return req.Method == "GET" &&
|
|
|
|
(req.Header.Get("Accept") == "text/x-camli-configuration" ||
|
2011-05-29 17:50:17 +00:00
|
|
|
camliMode(req) == "config")
|
2011-05-26 04:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func wantsUploadHelper(req *http.Request) bool {
|
|
|
|
return req.Method == "POST" && camliMode(req) == "uploadhelper"
|
2011-04-16 23:18:31 +00:00
|
|
|
}
|
|
|
|
|
2012-11-09 18:35:59 +00:00
|
|
|
func wantsRecentPermanodes(req *http.Request) bool {
|
|
|
|
return req.Method == "GET" && req.FormValue("mode") == "thumbnails"
|
|
|
|
}
|
|
|
|
|
2011-05-30 04:39:23 +00:00
|
|
|
func wantsPermanode(req *http.Request) bool {
|
|
|
|
return req.Method == "GET" && blobref.Parse(req.FormValue("p")) != nil
|
|
|
|
}
|
|
|
|
|
2011-05-30 23:41:56 +00:00
|
|
|
func wantsBlobInfo(req *http.Request) bool {
|
|
|
|
return req.Method == "GET" && blobref.Parse(req.FormValue("b")) != nil
|
|
|
|
}
|
|
|
|
|
2011-07-17 15:50:55 +00:00
|
|
|
func wantsFileTreePage(req *http.Request) bool {
|
|
|
|
return req.Method == "GET" && blobref.Parse(req.FormValue("d")) != nil
|
|
|
|
}
|
|
|
|
|
2013-06-11 09:50:56 +00:00
|
|
|
func wantsClosure(req *http.Request) bool {
|
2012-12-13 18:20:45 +00:00
|
|
|
if req.Method == "GET" {
|
2013-06-12 09:17:30 +00:00
|
|
|
suffix := httputil.PathSuffix(req)
|
2013-06-11 09:50:56 +00:00
|
|
|
return closurePattern.MatchString(suffix)
|
2012-12-12 17:04:05 +00:00
|
|
|
}
|
2012-12-13 18:20:45 +00:00
|
|
|
return false
|
2012-12-12 17:04:05 +00:00
|
|
|
}
|
|
|
|
|
2011-04-16 22:44:22 +00:00
|
|
|
func (ui *UIHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2013-06-12 09:17:30 +00:00
|
|
|
suffix := httputil.PathSuffix(req)
|
2011-05-30 02:05:21 +00:00
|
|
|
|
2011-04-16 23:18:31 +00:00
|
|
|
rw.Header().Set("Vary", "Accept")
|
|
|
|
switch {
|
|
|
|
case wantsDiscovery(req):
|
2012-11-07 17:57:43 +00:00
|
|
|
ui.root.serveDiscovery(rw, req)
|
2011-05-26 04:56:48 +00:00
|
|
|
case wantsUploadHelper(req):
|
|
|
|
ui.serveUploadHelper(rw, req)
|
2011-05-31 17:20:28 +00:00
|
|
|
case strings.HasPrefix(suffix, "download/"):
|
|
|
|
ui.serveDownload(rw, req)
|
2011-06-15 08:42:18 +00:00
|
|
|
case strings.HasPrefix(suffix, "thumbnail/"):
|
|
|
|
ui.serveThumbnail(rw, req)
|
2011-07-17 15:50:55 +00:00
|
|
|
case strings.HasPrefix(suffix, "tree/"):
|
|
|
|
ui.serveFileTree(rw, req)
|
2013-06-11 09:50:56 +00:00
|
|
|
case wantsClosure(req):
|
2013-06-12 09:05:01 +00:00
|
|
|
ui.serveClosure(rw, req)
|
2011-04-16 23:18:31 +00:00
|
|
|
default:
|
2011-04-17 23:01:41 +00:00
|
|
|
file := ""
|
2011-05-30 02:05:21 +00:00
|
|
|
if m := staticFilePattern.FindStringSubmatch(suffix); m != nil {
|
2011-04-17 23:01:41 +00:00
|
|
|
file = m[1]
|
2011-05-30 02:05:21 +00:00
|
|
|
} else {
|
2011-05-30 23:41:56 +00:00
|
|
|
switch {
|
|
|
|
case wantsPermanode(req):
|
|
|
|
file = "permanode.html"
|
|
|
|
case wantsBlobInfo(req):
|
|
|
|
file = "blobinfo.html"
|
2011-07-17 15:50:55 +00:00
|
|
|
case wantsFileTreePage(req):
|
|
|
|
file = "filetree.html"
|
2013-06-12 09:17:30 +00:00
|
|
|
case req.URL.Path == httputil.PathBase(req):
|
2012-12-12 02:29:58 +00:00
|
|
|
file = "index.html"
|
2011-05-30 23:41:56 +00:00
|
|
|
default:
|
2013-06-11 09:50:56 +00:00
|
|
|
http.Error(rw, "Illegal URL.", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if file == "deps.js" {
|
|
|
|
envVar := newuiFiles.OverrideEnv
|
|
|
|
if envVar != "" && os.Getenv(envVar) != "" {
|
|
|
|
serveDepsJS(rw, req)
|
2011-05-30 23:41:56 +00:00
|
|
|
return
|
|
|
|
}
|
2011-04-16 23:18:31 +00:00
|
|
|
}
|
2013-06-11 09:50:56 +00:00
|
|
|
serveStaticFile(rw, req, newuiFiles, file)
|
2011-04-16 22:44:22 +00:00
|
|
|
}
|
2011-04-16 23:18:31 +00:00
|
|
|
}
|
|
|
|
|
2012-12-12 02:29:58 +00:00
|
|
|
func serveStaticFile(rw http.ResponseWriter, req *http.Request, root http.FileSystem, file string) {
|
|
|
|
f, err := root.Open("/" + file)
|
|
|
|
if err != nil {
|
|
|
|
http.NotFound(rw, req)
|
2013-06-11 09:50:56 +00:00
|
|
|
log.Printf("Failed to open file %q from newuiFiles: %v", file, err)
|
2012-12-12 02:29:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
var modTime time.Time
|
|
|
|
if fi, err := f.Stat(); err == nil {
|
|
|
|
modTime = fi.ModTime()
|
|
|
|
}
|
|
|
|
http.ServeContent(rw, req, file, modTime, f)
|
|
|
|
}
|
|
|
|
|
2012-11-07 17:57:43 +00:00
|
|
|
func (ui *UIHandler) populateDiscoveryMap(m map[string]interface{}) {
|
2011-06-19 21:12:56 +00:00
|
|
|
pubRoots := map[string]interface{}{}
|
|
|
|
for key, pubh := range ui.PublishRoots {
|
2011-07-01 21:31:41 +00:00
|
|
|
m := map[string]interface{}{
|
2011-07-02 16:09:50 +00:00
|
|
|
"name": pubh.RootName,
|
2011-06-24 17:34:10 +00:00
|
|
|
"prefix": []string{key},
|
2011-06-19 21:12:56 +00:00
|
|
|
// TODO: include gpg key id
|
|
|
|
}
|
2013-01-11 18:35:19 +00:00
|
|
|
if sh, ok := ui.root.SearchHandler(); ok {
|
|
|
|
pn, err := sh.Index().PermanodeOfSignerAttrValue(sh.Owner(), "camliRoot", pubh.RootName)
|
2011-07-01 21:31:41 +00:00
|
|
|
if err == nil {
|
|
|
|
m["currentPermanode"] = pn.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pubRoots[pubh.RootName] = m
|
2011-06-19 21:12:56 +00:00
|
|
|
}
|
|
|
|
|
2012-11-07 17:57:43 +00:00
|
|
|
uiDisco := map[string]interface{}{
|
2011-08-25 15:14:47 +00:00
|
|
|
"jsonSignRoot": ui.JSONSignRoot,
|
2012-05-13 20:02:20 +00:00
|
|
|
"uploadHelper": ui.prefix + "?camli.mode=uploadhelper", // hack; remove with better javascript
|
|
|
|
"downloadHelper": path.Join(ui.prefix, "download") + "/",
|
|
|
|
"directoryHelper": path.Join(ui.prefix, "tree") + "/",
|
2011-08-25 15:14:47 +00:00
|
|
|
"publishRoots": pubRoots,
|
2012-11-07 17:57:43 +00:00
|
|
|
}
|
2012-12-24 00:58:26 +00:00
|
|
|
if ui.sigh != nil {
|
2012-12-29 14:51:42 +00:00
|
|
|
uiDisco["signing"] = ui.sigh.DiscoveryMap(ui.JSONSignRoot)
|
2012-12-24 00:58:26 +00:00
|
|
|
}
|
2012-11-07 17:57:43 +00:00
|
|
|
for k, v := range uiDisco {
|
|
|
|
if _, ok := m[k]; ok {
|
|
|
|
log.Fatalf("Duplicate discovery key %q", k)
|
|
|
|
}
|
|
|
|
m[k] = v
|
|
|
|
}
|
2011-04-16 22:44:22 +00:00
|
|
|
}
|
2011-05-26 04:56:48 +00:00
|
|
|
|
2011-05-31 17:20:28 +00:00
|
|
|
func (ui *UIHandler) serveDownload(rw http.ResponseWriter, req *http.Request) {
|
2012-11-07 17:57:43 +00:00
|
|
|
if ui.root.Storage == nil {
|
2011-05-31 17:20:28 +00:00
|
|
|
http.Error(rw, "No BlobRoot configured", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-12 09:17:30 +00:00
|
|
|
suffix := httputil.PathSuffix(req)
|
2011-05-31 17:20:28 +00:00
|
|
|
m := downloadPattern.FindStringSubmatch(suffix)
|
|
|
|
if m == nil {
|
|
|
|
httputil.ErrorRouting(rw, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-11 09:50:56 +00:00
|
|
|
fbr := blobref.Parse(m[1])
|
2011-06-10 01:33:26 +00:00
|
|
|
if fbr == nil {
|
2011-05-31 17:20:28 +00:00
|
|
|
http.Error(rw, "Invalid blobref", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-07-03 17:53:02 +00:00
|
|
|
dh := &DownloadHandler{
|
2012-11-07 17:57:43 +00:00
|
|
|
Fetcher: ui.root.Storage,
|
2011-07-03 17:53:02 +00:00
|
|
|
Cache: ui.Cache,
|
2011-06-06 16:07:07 +00:00
|
|
|
}
|
2011-07-03 17:53:02 +00:00
|
|
|
dh.ServeHTTP(rw, req, fbr)
|
2011-05-31 17:20:28 +00:00
|
|
|
}
|
2011-06-15 08:42:18 +00:00
|
|
|
|
|
|
|
func (ui *UIHandler) serveThumbnail(rw http.ResponseWriter, req *http.Request) {
|
2012-11-07 17:57:43 +00:00
|
|
|
if ui.root.Storage == nil {
|
2011-06-15 08:42:18 +00:00
|
|
|
http.Error(rw, "No BlobRoot configured", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-12 09:17:30 +00:00
|
|
|
suffix := httputil.PathSuffix(req)
|
2011-06-15 08:42:18 +00:00
|
|
|
m := thumbnailPattern.FindStringSubmatch(suffix)
|
|
|
|
if m == nil {
|
|
|
|
httputil.ErrorRouting(rw, req)
|
|
|
|
return
|
|
|
|
}
|
2011-06-19 20:09:43 +00:00
|
|
|
|
2011-06-15 08:42:18 +00:00
|
|
|
query := req.URL.Query()
|
|
|
|
width, err := strconv.Atoi(query.Get("mw"))
|
|
|
|
if err != nil {
|
2012-11-04 13:08:11 +00:00
|
|
|
http.Error(rw, "Invalid specified max width 'mw'", 500)
|
2011-06-15 08:42:18 +00:00
|
|
|
return
|
|
|
|
}
|
2011-06-17 02:31:36 +00:00
|
|
|
height, err := strconv.Atoi(query.Get("mh"))
|
2011-06-15 08:42:18 +00:00
|
|
|
if err != nil {
|
2012-11-04 13:08:11 +00:00
|
|
|
http.Error(rw, "Invalid specified height 'mh'", 500)
|
2011-06-15 08:42:18 +00:00
|
|
|
return
|
2011-06-19 20:09:43 +00:00
|
|
|
}
|
2011-06-15 08:42:18 +00:00
|
|
|
|
2013-06-11 09:50:56 +00:00
|
|
|
blobref := blobref.Parse(m[1])
|
2011-06-15 08:42:18 +00:00
|
|
|
if blobref == nil {
|
|
|
|
http.Error(rw, "Invalid blobref", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-07-03 20:33:41 +00:00
|
|
|
th := &ImageHandler{
|
2012-11-07 17:57:43 +00:00
|
|
|
Fetcher: ui.root.Storage,
|
2011-07-03 20:33:41 +00:00
|
|
|
Cache: ui.Cache,
|
|
|
|
MaxWidth: width,
|
|
|
|
MaxHeight: height,
|
2011-10-25 20:30:02 +00:00
|
|
|
sc: ui.sc,
|
2011-06-15 08:42:18 +00:00
|
|
|
}
|
2011-07-03 20:33:41 +00:00
|
|
|
th.ServeHTTP(rw, req, blobref)
|
2011-06-15 08:42:18 +00:00
|
|
|
}
|
2011-07-17 15:50:55 +00:00
|
|
|
|
|
|
|
func (ui *UIHandler) serveFileTree(rw http.ResponseWriter, req *http.Request) {
|
2012-11-07 17:57:43 +00:00
|
|
|
if ui.root.Storage == nil {
|
2011-07-17 15:50:55 +00:00
|
|
|
http.Error(rw, "No BlobRoot configured", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-12 09:17:30 +00:00
|
|
|
suffix := httputil.PathSuffix(req)
|
2011-07-17 15:50:55 +00:00
|
|
|
m := treePattern.FindStringSubmatch(suffix)
|
|
|
|
if m == nil {
|
|
|
|
httputil.ErrorRouting(rw, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-11 09:50:56 +00:00
|
|
|
blobref := blobref.Parse(m[1])
|
2011-07-17 15:50:55 +00:00
|
|
|
if blobref == nil {
|
|
|
|
http.Error(rw, "Invalid blobref", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fth := &FileTreeHandler{
|
2012-11-07 17:57:43 +00:00
|
|
|
Fetcher: ui.root.Storage,
|
2011-08-25 15:14:47 +00:00
|
|
|
file: blobref,
|
2011-07-17 15:50:55 +00:00
|
|
|
}
|
|
|
|
fth.ServeHTTP(rw, req)
|
|
|
|
}
|
2012-12-13 18:20:45 +00:00
|
|
|
|
2013-06-12 09:05:01 +00:00
|
|
|
func (ui *UIHandler) serveClosure(rw http.ResponseWriter, req *http.Request) {
|
2013-06-12 09:17:30 +00:00
|
|
|
suffix := httputil.PathSuffix(req)
|
2012-12-23 19:01:19 +00:00
|
|
|
if ui.closureHandler == nil {
|
2013-06-11 09:50:56 +00:00
|
|
|
log.Printf("%v not served: closure handler is nil", suffix)
|
2012-12-13 18:20:45 +00:00
|
|
|
http.NotFound(rw, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m := closurePattern.FindStringSubmatch(suffix)
|
2013-06-11 09:50:56 +00:00
|
|
|
if m == nil {
|
|
|
|
httputil.ErrorRouting(rw, req)
|
2012-12-23 20:39:15 +00:00
|
|
|
return
|
|
|
|
}
|
2013-06-11 09:50:56 +00:00
|
|
|
req.URL.Path = "/" + m[1]
|
|
|
|
ui.closureHandler.ServeHTTP(rw, req)
|
2012-12-13 18:20:45 +00:00
|
|
|
}
|
2012-12-23 20:39:15 +00:00
|
|
|
|
|
|
|
// serveDepsJS serves an auto-generated Closure deps.js file.
|
2013-05-24 21:19:29 +00:00
|
|
|
func serveDepsJS(rw http.ResponseWriter, req *http.Request) {
|
2012-12-23 20:39:15 +00:00
|
|
|
envVar := newuiFiles.OverrideEnv
|
|
|
|
if envVar == "" {
|
|
|
|
log.Printf("No newuiFiles.OverrideEnv set; can't generate deps.js")
|
|
|
|
http.NotFound(rw, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dir := os.Getenv(envVar)
|
|
|
|
if dir == "" {
|
|
|
|
log.Printf("The %q environment variable is not set; can't generate deps.js", envVar)
|
|
|
|
http.NotFound(rw, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fi, err := os.Stat(dir)
|
|
|
|
if err != nil || !fi.IsDir() {
|
|
|
|
log.Printf("The %q environment variable points to non-directory %s; can't generate deps.js", envVar, dir)
|
|
|
|
http.NotFound(rw, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(path, ".js") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
suffix := path[len(dir)+1:]
|
|
|
|
prov, req, err := parseProvidesRequires(info, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(prov) > 0 {
|
|
|
|
fmt.Fprintf(&buf, "goog.addDependency(%q, %v, %v);\n", "../../"+suffix, jsList(prov), jsList(req))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error walking %d generating deps.js: %v", dir, err)
|
|
|
|
http.Error(rw, "Server error", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rw.Header().Set("Content-Type", "text/javascript; charset=utf-8")
|
|
|
|
io.Copy(rw, &buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
var provReqRx = regexp.MustCompile(`^goog\.(provide|require)\(['"]([\w\.]+)['"]\)`)
|
|
|
|
|
|
|
|
type depCacheItem struct {
|
|
|
|
modTime time.Time
|
|
|
|
provides, requires []string
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
depCacheMu sync.Mutex
|
|
|
|
depCache = map[string]depCacheItem{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func parseProvidesRequires(fi os.FileInfo, path string) (provides, requires []string, err error) {
|
|
|
|
mt := fi.ModTime()
|
|
|
|
depCacheMu.Lock()
|
|
|
|
defer depCacheMu.Unlock()
|
|
|
|
if ci := depCache[path]; ci.modTime.Equal(mt) {
|
|
|
|
return ci.provides, ci.requires, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
br := bufio.NewReader(f)
|
|
|
|
for {
|
|
|
|
l, err := br.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(l, "goog.") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m := provReqRx.FindStringSubmatch(l)
|
|
|
|
if m != nil {
|
|
|
|
if m[1] == "provide" {
|
|
|
|
provides = append(provides, m[2])
|
|
|
|
} else {
|
|
|
|
requires = append(requires, m[2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
depCache[path] = depCacheItem{provides: provides, requires: requires, modTime: mt}
|
|
|
|
return provides, requires, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// jsList prints a list of strings as JavaScript list.
|
|
|
|
type jsList []string
|
|
|
|
|
|
|
|
func (s jsList) String() string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteByte('[')
|
|
|
|
for i, v := range s {
|
|
|
|
if i > 0 {
|
|
|
|
buf.WriteString(", ")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&buf, "%q", v)
|
|
|
|
}
|
|
|
|
buf.WriteByte(']')
|
|
|
|
return buf.String()
|
|
|
|
}
|