2011-01-28 07:07:18 +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-02-04 22:31:23 +00:00
|
|
|
package handlers
|
2010-07-26 03:34:04 +00:00
|
|
|
|
|
|
|
import (
|
2010-12-03 15:34:24 +00:00
|
|
|
"camli/blobref"
|
2011-02-04 22:31:23 +00:00
|
|
|
"camli/blobserver"
|
2010-12-06 06:34:46 +00:00
|
|
|
"camli/httputil"
|
2010-07-26 03:34:04 +00:00
|
|
|
"fmt"
|
2010-11-15 03:52:52 +00:00
|
|
|
"http"
|
2011-02-04 22:31:23 +00:00
|
|
|
"log"
|
2010-07-26 03:34:04 +00:00
|
|
|
"os"
|
2011-02-09 18:57:30 +00:00
|
|
|
"strconv"
|
2010-07-26 03:34:04 +00:00
|
|
|
)
|
|
|
|
|
2011-05-09 16:11:18 +00:00
|
|
|
func CreateStatHandler(storage blobserver.Storage) func(http.ResponseWriter, *http.Request) {
|
2011-02-04 22:31:23 +00:00
|
|
|
return func(conn http.ResponseWriter, req *http.Request) {
|
2011-05-09 16:11:18 +00:00
|
|
|
handleStat(conn, req, storage)
|
2011-02-04 22:31:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-08 16:24:16 +00:00
|
|
|
const maxStatBlobs = 1000
|
2011-02-04 22:31:23 +00:00
|
|
|
|
2011-05-09 16:11:18 +00:00
|
|
|
func handleStat(conn http.ResponseWriter, req *http.Request, storage blobserver.BlobStatter) {
|
2011-02-04 22:31:23 +00:00
|
|
|
toStat := make([]*blobref.BlobRef, 0)
|
2011-02-08 16:24:16 +00:00
|
|
|
switch req.Method {
|
|
|
|
case "POST":
|
|
|
|
fallthrough
|
|
|
|
case "GET":
|
|
|
|
camliVersion := req.FormValue("camliversion")
|
|
|
|
if camliVersion == "" {
|
|
|
|
httputil.BadRequestError(conn, "No camliversion")
|
2010-07-26 03:34:04 +00:00
|
|
|
return
|
|
|
|
}
|
2011-02-08 16:24:16 +00:00
|
|
|
n := 0
|
|
|
|
for {
|
|
|
|
n++
|
|
|
|
key := fmt.Sprintf("blob%v", n)
|
|
|
|
value := req.FormValue(key)
|
|
|
|
if value == "" {
|
|
|
|
n--
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if n > maxStatBlobs {
|
|
|
|
httputil.BadRequestError(conn, "Too many stat blob checks")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ref := blobref.Parse(value)
|
|
|
|
if ref == nil {
|
|
|
|
httputil.BadRequestError(conn, "Bogus blobref for key "+key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
toStat = append(toStat, ref)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
httputil.BadRequestError(conn, "Invalid method.");
|
|
|
|
return
|
|
|
|
|
2011-02-04 22:31:23 +00:00
|
|
|
}
|
2010-07-26 03:34:04 +00:00
|
|
|
|
2011-02-09 18:57:30 +00:00
|
|
|
waitSeconds := 0
|
|
|
|
if waitStr := req.FormValue("maxwaitsec"); waitStr != "" {
|
|
|
|
waitSeconds, _ = strconv.Atoi(waitStr)
|
|
|
|
switch {
|
|
|
|
case waitSeconds < 0:
|
|
|
|
waitSeconds = 0
|
|
|
|
case waitSeconds > 30:
|
2011-02-26 22:03:10 +00:00
|
|
|
// TODO: don't hard-code 30. push this up into a blobserver interface
|
|
|
|
// for getting the configuration of the server (ultimately a flag in
|
|
|
|
// in the binary)
|
2011-02-09 18:57:30 +00:00
|
|
|
waitSeconds = 30
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-08 16:24:16 +00:00
|
|
|
statRes := make([]map[string]interface{}, 0)
|
|
|
|
if len(toStat) > 0 {
|
2011-02-04 22:31:23 +00:00
|
|
|
blobch := make(chan *blobref.SizedBlobRef)
|
|
|
|
resultch := make(chan os.Error, 1)
|
2010-07-26 03:34:04 +00:00
|
|
|
go func() {
|
2011-05-09 16:11:18 +00:00
|
|
|
err := storage.Stat(blobch, toStat, waitSeconds)
|
2011-02-04 22:31:23 +00:00
|
|
|
close(blobch)
|
|
|
|
resultch <- err
|
2010-07-26 03:34:04 +00:00
|
|
|
}()
|
|
|
|
|
2011-02-04 22:31:23 +00:00
|
|
|
for sb := range blobch {
|
|
|
|
ah := make(map[string]interface{})
|
|
|
|
ah["blobRef"] = sb.BlobRef.String()
|
|
|
|
ah["size"] = sb.Size
|
2011-02-08 16:24:16 +00:00
|
|
|
statRes = append(statRes, ah)
|
2011-02-04 22:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := <-resultch
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Stat error: %v", err)
|
|
|
|
conn.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
2010-07-26 03:34:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-09 16:11:18 +00:00
|
|
|
configer, _ := storage.(blobserver.Configer)
|
|
|
|
ret := commonUploadResponse(configer, req)
|
2011-02-08 16:24:16 +00:00
|
|
|
ret["stat"] = statRes
|
2011-02-09 18:57:30 +00:00
|
|
|
ret["canLongPoll"] = true
|
2010-12-06 06:34:46 +00:00
|
|
|
httputil.ReturnJson(conn, ret)
|
2010-07-26 03:34:04 +00:00
|
|
|
}
|