2010-11-15 03:31:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2010-12-06 06:34:46 +00:00
|
|
|
"camli/httputil"
|
2010-12-09 03:24:48 +00:00
|
|
|
"camli/jsonsign"
|
2010-11-15 05:51:52 +00:00
|
|
|
"fmt"
|
2010-11-15 03:31:47 +00:00
|
|
|
"http"
|
|
|
|
)
|
|
|
|
|
2010-12-08 05:55:24 +00:00
|
|
|
const kMaxJsonLength = 1024 * 1024
|
|
|
|
|
2010-11-15 03:31:47 +00:00
|
|
|
func handleSign(conn http.ResponseWriter, req *http.Request) {
|
|
|
|
if !(req.Method == "POST" && req.URL.Path == "/camli/sig/sign") {
|
2010-12-06 06:34:46 +00:00
|
|
|
httputil.BadRequestError(conn, "Inconfigured handler.")
|
2010-11-15 03:31:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.ParseForm()
|
|
|
|
|
2010-12-08 05:55:24 +00:00
|
|
|
jsonStr := req.FormValue("json")
|
|
|
|
if jsonStr == "" {
|
|
|
|
httputil.BadRequestError(conn, "Missing json parameter")
|
2010-11-15 03:31:47 +00:00
|
|
|
return
|
|
|
|
}
|
2010-12-08 05:55:24 +00:00
|
|
|
if len(jsonStr) > kMaxJsonLength {
|
|
|
|
httputil.BadRequestError(conn, "json parameter too large")
|
2010-11-15 03:31:47 +00:00
|
|
|
return
|
|
|
|
}
|
2010-11-15 05:51:52 +00:00
|
|
|
|
2010-12-09 03:24:48 +00:00
|
|
|
sreq := &jsonsign.SignRequest{UnsignedJson: jsonStr, Fetcher: blobFetcher}
|
|
|
|
signedJson, err := sreq.Sign()
|
2010-11-15 05:51:52 +00:00
|
|
|
if err != nil {
|
2010-12-09 03:24:48 +00:00
|
|
|
// TODO: some aren't really a "bad request"
|
|
|
|
httputil.BadRequestError(conn, fmt.Sprintf("%v", err))
|
2010-11-15 05:51:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
conn.Write([]byte(signedJson))
|
2010-11-15 03:31:47 +00:00
|
|
|
}
|