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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2011-05-26 04:56:48 +00:00
|
|
|
"bytes"
|
2011-04-16 22:44:22 +00:00
|
|
|
"fmt"
|
|
|
|
"http"
|
2011-05-26 04:56:48 +00:00
|
|
|
"io"
|
2011-04-16 23:18:31 +00:00
|
|
|
"json"
|
2011-05-26 04:56:48 +00:00
|
|
|
"log"
|
2011-04-16 22:44:22 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
|
2011-05-30 04:39:23 +00:00
|
|
|
"camli/blobref"
|
2011-05-09 21:20:19 +00:00
|
|
|
"camli/blobserver"
|
2011-04-16 22:44:22 +00:00
|
|
|
"camli/jsonconfig"
|
2011-05-29 17:50:17 +00:00
|
|
|
"camli/schema"
|
2011-04-16 22:44:22 +00:00
|
|
|
)
|
|
|
|
|
2011-05-26 04:56:48 +00:00
|
|
|
var _ = log.Printf
|
|
|
|
|
2011-05-30 02:05:21 +00:00
|
|
|
var staticFilePattern = regexp.MustCompile(`^([a-zA-Z0-9\-\_]+\.(html|js|css|png|jpg|gif))$`)
|
2011-04-17 23:01:41 +00:00
|
|
|
var identPattern = regexp.MustCompile(`^[a-zA-Z\_]+$`)
|
2011-04-16 22:44:22 +00:00
|
|
|
|
|
|
|
// UIHandler handles serving the UI and discovery JSON.
|
|
|
|
type UIHandler struct {
|
|
|
|
// URL prefixes (path or full URL) to the primary blob and
|
|
|
|
// search root. Only used by the UI and thus necessary if UI
|
|
|
|
// is true.
|
2011-04-17 23:01:41 +00:00
|
|
|
BlobRoot string
|
|
|
|
SearchRoot string
|
|
|
|
JSONSignRoot string
|
2011-04-16 22:44:22 +00:00
|
|
|
|
|
|
|
FilesDir string
|
2011-05-29 17:50:17 +00:00
|
|
|
|
|
|
|
Storage blobserver.Storage // of BlobRoot
|
2011-04-16 22:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultFilesDir() string {
|
|
|
|
dir, _ := filepath.Split(os.Args[0])
|
|
|
|
return filepath.Join(dir, "ui")
|
|
|
|
}
|
|
|
|
|
2011-05-26 14:34:39 +00:00
|
|
|
func init() {
|
|
|
|
blobserver.RegisterHandlerConstructor("ui", newUiFromConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUiFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (h http.Handler, err os.Error) {
|
2011-04-16 22:44:22 +00:00
|
|
|
ui := &UIHandler{}
|
2011-04-17 23:01:41 +00:00
|
|
|
ui.BlobRoot = conf.OptionalString("blobRoot", "")
|
|
|
|
ui.SearchRoot = conf.OptionalString("searchRoot", "")
|
|
|
|
ui.JSONSignRoot = conf.OptionalString("jsonSignRoot", "")
|
2011-04-16 22:44:22 +00:00
|
|
|
ui.FilesDir = conf.OptionalString("staticFiles", defaultFilesDir())
|
|
|
|
if err = conf.Validate(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
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 {
|
|
|
|
err = fmt.Errorf("UI 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
|
|
|
|
}
|
|
|
|
|
|
|
|
if ui.BlobRoot != "" {
|
2011-05-29 17:50:17 +00:00
|
|
|
bs, err := ld.GetStorage(ui.BlobRoot)
|
2011-05-26 14:34:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("UI handler's blobRoot of %q error: %v", ui.BlobRoot, err)
|
2011-05-09 21:20:19 +00:00
|
|
|
}
|
2011-05-29 17:50:17 +00:00
|
|
|
ui.Storage = bs
|
2011-05-09 21:20:19 +00:00
|
|
|
}
|
|
|
|
|
2011-04-16 22:44:22 +00:00
|
|
|
fi, sterr := os.Stat(ui.FilesDir)
|
|
|
|
if sterr != nil || !fi.IsDirectory() {
|
|
|
|
err = fmt.Errorf("UI handler's \"staticFiles\" of %q is invalid", ui.FilesDir)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return ui, nil
|
|
|
|
}
|
|
|
|
|
2011-05-26 04:56:48 +00:00
|
|
|
func camliMode(req *http.Request) string {
|
|
|
|
// TODO-GO: this is too hard to get at the GET Query args on a
|
|
|
|
// POST request.
|
|
|
|
m, err := http.ParseQuery(req.URL.RawQuery)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if mode, ok := m["camli.mode"]; ok && len(mode) > 0 {
|
|
|
|
return mode[0]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-04-16 22:44:22 +00:00
|
|
|
func (ui *UIHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2011-05-30 02:05:21 +00:00
|
|
|
base := req.Header.Get("X-PrefixHandler-PathBase")
|
|
|
|
suffix := req.Header.Get("X-PrefixHandler-PathSuffix")
|
|
|
|
|
2011-04-16 23:18:31 +00:00
|
|
|
rw.Header().Set("Vary", "Accept")
|
|
|
|
switch {
|
|
|
|
case wantsDiscovery(req):
|
2011-04-17 23:01:41 +00:00
|
|
|
ui.serveDiscovery(rw, req)
|
2011-05-26 04:56:48 +00:00
|
|
|
case wantsUploadHelper(req):
|
|
|
|
ui.serveUploadHelper(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 04:39:23 +00:00
|
|
|
} else if wantsPermanode(req) {
|
|
|
|
file = "permanode.html"
|
2011-05-30 02:05:21 +00:00
|
|
|
} else if req.URL.Path == base {
|
2011-04-16 23:18:31 +00:00
|
|
|
file = "index.html"
|
2011-05-30 02:05:21 +00:00
|
|
|
} else {
|
|
|
|
http.Error(rw, "Illegal URL.", 404)
|
|
|
|
return
|
2011-04-16 23:18:31 +00:00
|
|
|
}
|
|
|
|
http.ServeFile(rw, req, filepath.Join(ui.FilesDir, file))
|
2011-04-16 22:44:22 +00:00
|
|
|
}
|
2011-04-16 23:18:31 +00:00
|
|
|
}
|
|
|
|
|
2011-04-17 23:01:41 +00:00
|
|
|
func (ui *UIHandler) serveDiscovery(rw http.ResponseWriter, req *http.Request) {
|
2011-04-16 23:18:31 +00:00
|
|
|
rw.Header().Set("Content-Type", "text/javascript")
|
2011-04-17 23:01:41 +00:00
|
|
|
inCb := false
|
|
|
|
if cb := req.FormValue("cb"); identPattern.MatchString(cb) {
|
|
|
|
fmt.Fprintf(rw, "%s(", cb)
|
|
|
|
inCb = true
|
|
|
|
}
|
|
|
|
bytes, _ := json.Marshal(map[string]interface{}{
|
2011-05-29 17:50:17 +00:00
|
|
|
"blobRoot": ui.BlobRoot,
|
|
|
|
"searchRoot": ui.SearchRoot,
|
|
|
|
"jsonSignRoot": ui.JSONSignRoot,
|
|
|
|
"uploadHelper": "?camli.mode=uploadhelper", // hack; remove with better javascript
|
|
|
|
})
|
2011-04-17 23:01:41 +00:00
|
|
|
rw.Write(bytes)
|
|
|
|
if inCb {
|
|
|
|
rw.Write([]byte{')'})
|
|
|
|
}
|
2011-04-16 22:44:22 +00:00
|
|
|
}
|
2011-05-26 04:56:48 +00:00
|
|
|
|
|
|
|
func (ui *UIHandler) serveUploadHelper(rw http.ResponseWriter, req *http.Request) {
|
2011-05-29 17:50:17 +00:00
|
|
|
if ui.Storage == nil {
|
|
|
|
http.Error(rw, "No BlobRoot configured", 500)
|
|
|
|
return
|
|
|
|
}
|
2011-05-26 04:56:48 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
defer io.Copy(rw, &buf)
|
|
|
|
|
|
|
|
fmt.Fprintf(&buf, "<pre>\n")
|
|
|
|
|
|
|
|
mr, err := req.MultipartReader()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(&buf, "multipart reader: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
part, err := mr.NextPart()
|
|
|
|
if err == os.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
buf.Reset()
|
2011-05-29 17:50:17 +00:00
|
|
|
http.Error(rw, "Multipart error: "+err.String(), 500)
|
2011-05-26 04:56:48 +00:00
|
|
|
break
|
|
|
|
}
|
2011-05-29 17:50:17 +00:00
|
|
|
br, err := schema.WriteFileFromReader(ui.Storage, part.FileName(), part)
|
|
|
|
|
|
|
|
fmt.Fprintf(&buf, "filename=%q, formname=%q, br=%s, err=%v\n", part.FileName(), part.FormName(), br, err)
|
|
|
|
|
2011-05-26 04:56:48 +00:00
|
|
|
}
|
|
|
|
}
|