| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/http" |
| 6 |
"os" |
| 7 |
|
| 8 |
"github.com/pkg/errors" |
| 9 |
) |
| 10 |
|
| 11 |
// HTTPHandlerBase is the base object for a HTTP chunk or index store. |
| 12 |
type HTTPHandlerBase struct { |
| 13 |
handlerType string |
| 14 |
writable bool |
| 15 |
authorization string |
| 16 |
} |
| 17 |
|
| 18 |
func (h HTTPHandlerBase) get(id string, b []byte, err error, w http.ResponseWriter) { |
| 19 |
switch err.(type) { |
| 20 |
case nil: |
| 21 |
w.WriteHeader(http.StatusOK) |
| 22 |
w.Write(b) |
| 23 |
case ChunkMissing, NoSuchObject: |
| 24 |
w.WriteHeader(http.StatusNotFound) |
| 25 |
fmt.Fprintf(w, "%s %s not found", h.handlerType, id) |
| 26 |
default: |
| 27 |
fmt.Fprintf(os.Stderr, "failed to retrieve %s %s: %s\n", h.handlerType, id, err) |
| 28 |
http.Error(w, "internal server error", http.StatusInternalServerError) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
func (h HTTPHandlerBase) validateWritable(storeName string, w http.ResponseWriter, r *http.Request) error { |
| 33 |
// Make sure writing was enabled for this server |
| 34 |
if !h.writable { |
| 35 |
msg := fmt.Sprintf("writing to upstream %s store is not enabled", h.handlerType) |
| 36 |
http.Error(w, msg, http.StatusBadRequest) |
| 37 |
return errors.Errorf("writing to upstream %s store '%s' is not enabled", h.handlerType, storeName) |
| 38 |
} |
| 39 |
return nil |
| 40 |
} |