// for more details.
//
// Each handler parses the JSON list of block management requests
- // in the message body, and delivers them to the pull queue or
- // trash queue, respectively.
+ // in the message body, and replaces any existing pull queue or
+ // trash queue with their contentes.
+ //
rest.HandleFunc(`/pull`, PullHandler).Methods("PUT")
rest.HandleFunc(`/trash`, TrashHandler).Methods("PUT")
func GetBlockHandler(resp http.ResponseWriter, req *http.Request) {
hash := mux.Vars(req)["hash"]
- log.Printf("%s %s", req.Method, hash)
-
hints := mux.Vars(req)["hints"]
// Parse the locator string and hints from the request.
if err != nil {
// This type assertion is safe because the only errors
// GetBlock can return are DiskHashError or NotFoundError.
- if err == NotFoundError {
- log.Printf("%s: not found, giving up\n", hash)
- }
http.Error(resp, err.Error(), err.(*KeepError).HTTPCode)
return
}
resp.Header().Set("X-Block-Size", fmt.Sprintf("%d", len(block)))
_, err = resp.Write(block)
- if err != nil {
- log.Printf("GetBlockHandler: writing response: %s", err)
- }
return
}
hash := mux.Vars(req)["hash"]
- log.Printf("%s %s", req.Method, hash)
-
// Read the block data to be stored.
// If the request exceeds BLOCKSIZE bytes, issue a HTTP 500 error.
//
// A HandleFunc to address /index and /index/{prefix} requests.
//
func IndexHandler(resp http.ResponseWriter, req *http.Request) {
- prefix := mux.Vars(req)["prefix"]
-
- // Only the data manager may issue /index requests,
- // and only if enforce_permissions is enabled.
- // All other requests return 403 Forbidden.
- api_token := GetApiToken(req)
- if !enforce_permissions ||
- api_token == "" ||
- data_manager_token != api_token {
- http.Error(resp, PermissionError.Error(), PermissionError.HTTPCode)
+ // Reject unauthorized requests.
+ if !IsDataManagerToken(GetApiToken(req)) {
+ http.Error(resp, UnauthorizedError.Error(), UnauthorizedError.HTTPCode)
return
}
+
+ prefix := mux.Vars(req)["prefix"]
+
var index string
for _, vol := range KeepVM.Volumes() {
index = index + vol.Index(prefix)
//
func DeleteHandler(resp http.ResponseWriter, req *http.Request) {
hash := mux.Vars(req)["hash"]
- log.Printf("%s %s", req.Method, hash)
// Confirm that this user is an admin and has a token with unlimited scope.
var tok = GetApiToken(req)
func PullHandler(resp http.ResponseWriter, req *http.Request) {
// Reject unauthorized requests.
- if api_token := GetApiToken(req); !IsDataManagerToken(api_token) {
+ if !IsDataManagerToken(GetApiToken(req)) {
http.Error(resp, UnauthorizedError.Error(), UnauthorizedError.HTTPCode)
- log.Printf("%s %s: %s\n", req.Method, req.URL, UnauthorizedError.Error())
return
}
r := json.NewDecoder(req.Body)
if err := r.Decode(&pr); err != nil {
http.Error(resp, BadRequestError.Error(), BadRequestError.HTTPCode)
- log.Printf("%s %s: %s\n", req.Method, req.URL, err.Error())
return
}
// We have a properly formatted pull list sent from the data
// manager. Report success and send the list to the pull list
// manager for further handling.
- log.Printf("%s %s: received %v\n", req.Method, req.URL, pr)
resp.WriteHeader(http.StatusOK)
resp.Write([]byte(
fmt.Sprintf("Received %d pull requests\n", len(pr))))
func TrashHandler(resp http.ResponseWriter, req *http.Request) {
// Reject unauthorized requests.
- if api_token := GetApiToken(req); !IsDataManagerToken(api_token) {
+ if !IsDataManagerToken(GetApiToken(req)) {
http.Error(resp, UnauthorizedError.Error(), UnauthorizedError.HTTPCode)
- log.Printf("%s %s: %s\n", req.Method, req.URL, UnauthorizedError.Error())
return
}
r := json.NewDecoder(req.Body)
if err := r.Decode(&trash); err != nil {
http.Error(resp, BadRequestError.Error(), BadRequestError.HTTPCode)
- log.Printf("%s %s: %s\n", req.Method, req.URL, err.Error())
return
}
// We have a properly formatted trash list sent from the data
// manager. Report success and send the list to the trash work
// queue for further handling.
- log.Printf("%s %s: received %v\n", req.Method, req.URL, trash)
resp.WriteHeader(http.StatusOK)
resp.Write([]byte(
fmt.Sprintf("Received %d trash requests\n", len(trash))))
func IsDataManagerToken(api_token string) bool {
return data_manager_token != "" && api_token == data_manager_token
}
+
+type LoggingResponseWriter struct {
+ status int
+ data []byte
+ http.ResponseWriter
+}
+
+func (loggingWriter *LoggingResponseWriter) WriteHeader(code int) {
+ loggingWriter.status = code
+ loggingWriter.ResponseWriter.WriteHeader(code)
+}
+
+func (loggingWriter *LoggingResponseWriter) Write(data []byte) (int, error){
+ loggingWriter.data = data
+ return loggingWriter.ResponseWriter.Write(data)
+}
+
+type WrapRESTRouter struct {
+ router *mux.Router
+}
+
+func (wrapper *WrapRESTRouter) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
+ loggingWriter := LoggingResponseWriter{200, nil, resp}
+ wrapper.router.ServeHTTP(&loggingWriter, req)
+ if loggingWriter.data != nil && loggingWriter.status == 200 {
+ data_len := len(loggingWriter.data)
+ if data_len > 200 { // this could be a block, so just print the size
+ log.Printf("[%s] %s %s %d %d", req.RemoteAddr, req.Method, req.URL.Path[1:], loggingWriter.status, data_len)
+ } else { // this could be a hash or status or a small block etc
+ log.Printf("[%s] %s %s %d %s", req.RemoteAddr, req.Method, req.URL.Path[1:], loggingWriter.status, loggingWriter.data)
+ }
+ } else {
+ log.Printf("[%s] %s %s %d", req.RemoteAddr, req.Method, req.URL.Path[1:], loggingWriter.status)
+ }
+}