2010-06-12 21:45:58 +00:00
|
|
|
// Copyright 2010 Brad Fitzpatrick <brad@danga.com>
|
|
|
|
//
|
|
|
|
// See LICENSE.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2010-07-11 04:18:16 +00:00
|
|
|
import (
|
2010-11-29 04:06:22 +00:00
|
|
|
"camli/auth"
|
2010-12-06 06:34:46 +00:00
|
|
|
"camli/httputil"
|
2010-12-06 06:29:11 +00:00
|
|
|
"camli/webserver"
|
2011-02-03 23:45:35 +00:00
|
|
|
"camli/blobserver"
|
2011-02-04 22:31:23 +00:00
|
|
|
"camli/blobserver/localdisk"
|
2011-02-03 23:56:02 +00:00
|
|
|
"camli/blobserver/handlers"
|
2011-03-05 22:25:08 +00:00
|
|
|
"camli/mysqlindexer" // TODO: temporary for testing; wrong place kinda
|
2010-07-11 04:18:16 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"http"
|
2011-01-26 05:33:19 +00:00
|
|
|
"log"
|
2011-02-03 06:42:31 +00:00
|
|
|
"strings"
|
2010-07-11 04:18:16 +00:00
|
|
|
"os"
|
|
|
|
)
|
2010-07-07 04:57:53 +00:00
|
|
|
|
2010-07-26 05:18:21 +00:00
|
|
|
var flagStorageRoot *string = flag.String("root", "/tmp/camliroot", "Root directory to store files")
|
2011-01-26 05:33:19 +00:00
|
|
|
var flagRequestLog *bool = flag.Bool("reqlog", false, "Log incoming requests")
|
2010-06-12 21:45:58 +00:00
|
|
|
|
2011-02-03 23:45:35 +00:00
|
|
|
var storage blobserver.Storage
|
2011-03-05 22:25:08 +00:00
|
|
|
var indexerStorage blobserver.Storage
|
2011-02-03 06:42:31 +00:00
|
|
|
|
|
|
|
const camliPrefix = "/camli/"
|
|
|
|
const partitionPrefix = "/partition-"
|
|
|
|
|
|
|
|
var InvalidCamliPath = os.NewError("Invalid Camlistore request path")
|
|
|
|
|
2011-02-04 01:08:04 +00:00
|
|
|
func parseCamliPath(path string) (partition blobserver.Partition, action string, err os.Error) {
|
2011-02-03 06:42:31 +00:00
|
|
|
camIdx := strings.Index(path, camliPrefix)
|
|
|
|
if camIdx == -1 {
|
|
|
|
err = InvalidCamliPath
|
|
|
|
return
|
|
|
|
}
|
|
|
|
action = path[camIdx+len(camliPrefix):]
|
|
|
|
if camIdx == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(path, partitionPrefix) {
|
|
|
|
err = InvalidCamliPath
|
|
|
|
return
|
|
|
|
}
|
2011-02-04 01:08:04 +00:00
|
|
|
name := path[len(partitionPrefix):camIdx]
|
|
|
|
if !isValidPartitionName(name) {
|
2011-02-03 06:42:31 +00:00
|
|
|
err = InvalidCamliPath
|
|
|
|
return
|
|
|
|
}
|
2011-02-04 01:08:04 +00:00
|
|
|
partition = blobserver.Partition(name)
|
2011-02-03 06:42:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func pickPartitionHandlerMaybe(req *http.Request) (handler http.HandlerFunc, intercept bool) {
|
|
|
|
if !strings.HasPrefix(req.URL.Path, partitionPrefix) {
|
|
|
|
intercept = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(handleCamli), true
|
|
|
|
}
|
|
|
|
|
|
|
|
func unsupportedHandler(conn http.ResponseWriter, req *http.Request) {
|
|
|
|
httputil.BadRequestError(conn, "Unsupported camlistore path or method.")
|
|
|
|
}
|
2010-12-14 02:20:31 +00:00
|
|
|
|
2010-10-04 15:28:14 +00:00
|
|
|
func handleCamli(conn http.ResponseWriter, req *http.Request) {
|
2011-02-03 06:42:31 +00:00
|
|
|
partition, action, err := parseCamliPath(req.URL.Path)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Invalid request for method %q, path %q",
|
|
|
|
req.Method, req.URL.Path)
|
|
|
|
unsupportedHandler(conn, req)
|
|
|
|
return
|
2010-07-18 18:08:45 +00:00
|
|
|
}
|
2011-03-05 22:25:08 +00:00
|
|
|
handleCamliUsingStorage(conn, req, action, partition, storage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleIndexRequest(conn http.ResponseWriter, req *http.Request) {
|
|
|
|
const prefix = "/indexer"
|
|
|
|
if !strings.HasPrefix(req.URL.Path, prefix) {
|
|
|
|
panic("bogus request")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
path := req.URL.Path[len(prefix):]
|
|
|
|
partition, action, err := parseCamliPath(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Invalid request for method %q, path %q",
|
|
|
|
req.Method, req.URL.Path)
|
|
|
|
unsupportedHandler(conn, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if partition != "" {
|
|
|
|
conn.WriteHeader(http.StatusBadRequest)
|
|
|
|
fmt.Fprintf(conn, "Indexer doesn't support partitions.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
handleCamliUsingStorage(conn, req, action, partition, indexerStorage)
|
|
|
|
}
|
2011-02-03 06:42:31 +00:00
|
|
|
|
2011-03-05 22:25:08 +00:00
|
|
|
func handleCamliUsingStorage(conn http.ResponseWriter, req *http.Request,
|
|
|
|
action string, partition blobserver.Partition, storage blobserver.Storage) {
|
2011-02-03 06:42:31 +00:00
|
|
|
handler := unsupportedHandler
|
2011-01-26 05:33:19 +00:00
|
|
|
if *flagRequestLog {
|
2011-02-03 06:42:31 +00:00
|
|
|
log.Printf("method %q; partition %q; action %q", req.Method, partition, action)
|
2011-01-26 05:33:19 +00:00
|
|
|
}
|
2010-07-11 04:18:16 +00:00
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
2011-02-03 06:42:31 +00:00
|
|
|
switch action {
|
|
|
|
case "enumerate-blobs":
|
2011-02-04 01:08:04 +00:00
|
|
|
handler = auth.RequireAuth(handlers.CreateEnumerateHandler(storage, partition))
|
2011-02-08 16:24:16 +00:00
|
|
|
case "stat":
|
|
|
|
handler = auth.RequireAuth(handlers.CreateStatHandler(storage, partition))
|
2010-07-26 05:18:21 +00:00
|
|
|
default:
|
2011-02-04 01:28:05 +00:00
|
|
|
handler = handlers.CreateGetHandler(storage)
|
2010-07-26 05:18:21 +00:00
|
|
|
}
|
2010-07-11 04:18:16 +00:00
|
|
|
case "POST":
|
2011-02-03 06:42:31 +00:00
|
|
|
switch action {
|
2011-02-08 16:24:16 +00:00
|
|
|
case "stat":
|
|
|
|
handler = auth.RequireAuth(handlers.CreateStatHandler(storage, partition))
|
2011-02-03 06:42:31 +00:00
|
|
|
case "upload":
|
2011-02-04 22:31:23 +00:00
|
|
|
handler = auth.RequireAuth(handlers.CreateUploadHandler(storage))
|
2011-02-03 06:42:31 +00:00
|
|
|
case "remove":
|
|
|
|
// Currently only allows removing from a non-main partition.
|
2011-02-03 23:56:02 +00:00
|
|
|
handler = auth.RequireAuth(handlers.CreateRemoveHandler(storage, partition))
|
2010-07-11 04:18:16 +00:00
|
|
|
}
|
|
|
|
case "PUT": // no longer part of spec
|
2011-02-04 22:31:23 +00:00
|
|
|
handler = auth.RequireAuth(handlers.CreateNonStandardPutHandler(storage))
|
2010-06-13 00:15:49 +00:00
|
|
|
}
|
2010-07-18 18:08:45 +00:00
|
|
|
handler(conn, req)
|
2010-06-12 21:45:58 +00:00
|
|
|
}
|
|
|
|
|
2010-10-04 15:28:14 +00:00
|
|
|
func handleRoot(conn http.ResponseWriter, req *http.Request) {
|
2011-02-04 22:31:23 +00:00
|
|
|
fmt.Fprintf(conn, "<html><body>This is camlistored, a <a href='http://camlistore.org'>Camlistore</a> storage daemon.</body></html>\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func exitFailure(pattern string, args ...interface{}) {
|
|
|
|
if !strings.HasSuffix(pattern, "\n") {
|
|
|
|
pattern = pattern + "\n"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, pattern, args...)
|
|
|
|
os.Exit(1)
|
2010-06-12 21:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2010-11-15 03:52:52 +00:00
|
|
|
auth.AccessPassword = os.Getenv("CAMLI_PASSWORD")
|
|
|
|
if len(auth.AccessPassword) == 0 {
|
2011-02-04 22:31:23 +00:00
|
|
|
exitFailure("No CAMLI_PASSWORD environment variable set.")
|
2010-06-12 21:45:58 +00:00
|
|
|
}
|
|
|
|
|
2011-02-04 22:31:23 +00:00
|
|
|
rootPrefix := func(s string) bool {
|
|
|
|
return strings.HasPrefix(*flagStorageRoot, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case *flagStorageRoot == "":
|
|
|
|
exitFailure("No storage root specified in --root")
|
|
|
|
case rootPrefix("s3:"):
|
|
|
|
// TODO: support Amazon, etc.
|
|
|
|
default:
|
|
|
|
var err os.Error
|
|
|
|
storage, err = localdisk.New(*flagStorageRoot)
|
|
|
|
if err != nil {
|
|
|
|
exitFailure("Error for --root of %q: %v", *flagStorageRoot, err)
|
2010-06-12 21:45:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-04 22:31:23 +00:00
|
|
|
if storage == nil {
|
|
|
|
exitFailure("Unsupported storage root type %q", *flagStorageRoot)
|
|
|
|
}
|
2010-12-14 02:20:31 +00:00
|
|
|
|
2010-12-06 06:29:11 +00:00
|
|
|
ws := webserver.New()
|
2011-02-03 06:42:31 +00:00
|
|
|
ws.RegisterPreMux(webserver.HandlerPicker(pickPartitionHandlerMaybe))
|
2010-12-06 06:29:11 +00:00
|
|
|
ws.HandleFunc("/", handleRoot)
|
|
|
|
ws.HandleFunc("/camli/", handleCamli)
|
2011-03-05 22:25:08 +00:00
|
|
|
|
|
|
|
// TODO: temporary
|
|
|
|
if true {
|
|
|
|
indexerStorage = &mysqlindexer.Indexer{}
|
|
|
|
ws.HandleFunc("/indexer/", handleIndexRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-12-06 06:29:11 +00:00
|
|
|
ws.Handle("/js/", http.FileServer("../../clients/js", "/js/"))
|
|
|
|
ws.Serve()
|
2010-06-12 21:45:58 +00:00
|
|
|
}
|