rest-server/handlers.go

293 lines
6.5 KiB
Go
Raw Normal View History

2015-09-18 17:13:38 +02:00
package main
import (
"encoding/json"
"fmt"
"io"
2015-09-18 17:13:38 +02:00
"io/ioutil"
"log"
2015-09-18 17:13:38 +02:00
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
2016-11-06 18:59:19 +01:00
// Context contains repository metadata.
2015-09-18 17:13:38 +02:00
type Context struct {
path string
}
2016-11-11 01:29:55 +01:00
func isHashed(dir string) bool {
return dir == "data"
}
func createDirectories(c *Context) {
log.Println("Creating repository directories")
dirs := []string{
"data",
"index",
"keys",
"locks",
"snapshots",
"tmp",
}
for _, d := range dirs {
if err := os.MkdirAll(filepath.Join(c.path, d), 0700); err != nil {
log.Fatal(err)
}
}
for i := 0; i < 256; i++ {
if err := os.MkdirAll(filepath.Join(c.path, "data", fmt.Sprintf("%02x", i)), 0700); err != nil {
log.Fatal(err)
}
}
}
2016-11-06 11:15:33 +01:00
// AuthHandler wraps h with a http.HandlerFunc that performs basic authentication against the user/passwords pairs
// stored in f and returns the http.HandlerFunc.
2015-09-19 14:52:50 +02:00
func AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2016-11-06 18:59:19 +01:00
if username, password, ok := r.BasicAuth(); !ok || !f.Validate(username, password) {
2015-09-19 14:52:50 +02:00
http.Error(w, "401 unauthorized", 401)
return
}
2015-09-19 14:52:50 +02:00
h.ServeHTTP(w, r)
}
}
2016-11-06 11:15:33 +01:00
// CheckConfig returns a http.HandlerFunc that checks whether a configuration exists.
2015-09-18 17:13:38 +02:00
func CheckConfig(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2016-12-27 12:26:41 +01:00
if *debug {
log.Println("CheckConfig()")
}
2015-09-18 17:13:38 +02:00
config := filepath.Join(c.path, "config")
st, err := os.Stat(config)
if err != nil {
2015-09-18 17:13:38 +02:00
http.Error(w, "404 not found", 404)
return
}
w.Header().Add("Content-Length", fmt.Sprint(st.Size()))
2015-09-18 17:13:38 +02:00
}
}
2016-11-06 11:15:33 +01:00
// GetConfig returns a http.HandlerFunc that allows for a config to be retrieved.
2015-09-18 17:13:38 +02:00
func GetConfig(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2016-12-27 12:26:41 +01:00
if *debug {
log.Println("GetConfig()")
}
2015-09-18 17:13:38 +02:00
config := filepath.Join(c.path, "config")
bytes, err := ioutil.ReadFile(config)
if err != nil {
http.Error(w, "404 not found", 404)
return
}
2015-09-18 17:13:38 +02:00
w.Write(bytes)
}
}
2016-11-06 11:15:33 +01:00
// SaveConfig returns a http.HandlerFunc that allows for a config to be saved.
2015-09-18 17:13:38 +02:00
func SaveConfig(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2016-12-27 12:26:41 +01:00
if *debug {
log.Println("SaveConfig()")
}
2015-09-18 17:13:38 +02:00
config := filepath.Join(c.path, "config")
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "400 bad request", 400)
return
}
2016-11-06 18:59:19 +01:00
if err := ioutil.WriteFile(config, bytes, 0600); err != nil {
2015-09-18 17:13:38 +02:00
http.Error(w, "500 internal server error", 500)
return
}
2015-09-18 17:13:38 +02:00
w.Write([]byte("200 ok"))
}
}
2016-11-06 11:15:33 +01:00
// ListBlobs returns a http.HandlerFunc that lists all blobs of a given type in an arbitrary order.
2015-09-18 17:13:38 +02:00
func ListBlobs(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2016-12-27 12:26:41 +01:00
if *debug {
log.Println("ListBlobs()")
}
2015-09-18 17:13:38 +02:00
vars := strings.Split(r.RequestURI, "/")
dir := vars[1]
path := filepath.Join(c.path, dir)
items, err := ioutil.ReadDir(path)
2015-09-18 17:13:38 +02:00
if err != nil {
http.Error(w, "404 not found", 404)
return
}
var names []string
for _, i := range items {
2016-11-11 01:29:55 +01:00
if isHashed(dir) {
subpath := filepath.Join(path, i.Name())
subitems, err := ioutil.ReadDir(subpath)
if err != nil {
http.Error(w, "404 not found", 404)
return
}
for _, f := range subitems {
names = append(names, f.Name())
}
} else {
names = append(names, i.Name())
}
2015-09-18 17:13:38 +02:00
}
2015-09-18 17:13:38 +02:00
data, err := json.Marshal(names)
if err != nil {
http.Error(w, "500 internal server error", 500)
return
}
2015-09-18 17:13:38 +02:00
w.Write(data)
}
}
2016-11-06 11:15:33 +01:00
// CheckBlob returns a http.HandlerFunc that tests whether a blob exists and returns 200, if it does, or 404 otherwise.
2015-09-18 17:13:38 +02:00
func CheckBlob(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2016-12-27 12:26:41 +01:00
if *debug {
log.Println("CheckBlob()")
}
2015-09-18 17:13:38 +02:00
vars := strings.Split(r.RequestURI, "/")
dir := vars[1]
name := vars[2]
2016-11-11 01:29:55 +01:00
if isHashed(dir) {
name = filepath.Join(name[:2], name)
}
2015-09-18 17:13:38 +02:00
path := filepath.Join(c.path, dir, name)
st, err := os.Stat(path)
2015-09-18 17:13:38 +02:00
if err != nil {
http.Error(w, "404 not found", 404)
return
}
w.Header().Add("Content-Length", fmt.Sprint(st.Size()))
2015-09-18 17:13:38 +02:00
}
}
2016-11-06 11:15:33 +01:00
// GetBlob returns a http.HandlerFunc that retrieves a blob from the repository.
2015-09-18 17:13:38 +02:00
func GetBlob(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2016-12-27 12:26:41 +01:00
if *debug {
log.Println("GetBlob()")
}
2015-09-18 17:13:38 +02:00
vars := strings.Split(r.RequestURI, "/")
dir := vars[1]
name := vars[2]
2016-11-11 01:29:55 +01:00
if isHashed(dir) {
name = filepath.Join(name[:2], name)
}
2015-09-18 17:13:38 +02:00
path := filepath.Join(c.path, dir, name)
file, err := os.Open(path)
2015-09-18 17:13:38 +02:00
if err != nil {
http.Error(w, "404 not found", 404)
return
}
2015-09-18 17:13:38 +02:00
http.ServeContent(w, r, "", time.Unix(0, 0), file)
file.Close()
2015-09-18 17:13:38 +02:00
}
}
// SaveBlob returns a http.HandlerFunc that saves a blob to the repository.
2015-09-18 17:13:38 +02:00
func SaveBlob(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2016-12-27 12:26:41 +01:00
if *debug {
log.Println("SaveBlob()")
}
2015-09-18 17:13:38 +02:00
vars := strings.Split(r.RequestURI, "/")
dir := vars[1]
name := vars[2]
if dir == "keys" {
if _, err := os.Stat("keys"); err != nil && os.IsNotExist(err) {
createDirectories(c)
}
}
tmp := filepath.Join(c.path, "tmp", name)
tf, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY, 0600)
2015-09-18 17:13:38 +02:00
if err != nil {
http.Error(w, "500 internal server error", 500)
return
}
if _, err := io.Copy(tf, r.Body); err != nil {
tf.Close()
os.Remove(tmp)
2016-11-06 18:59:19 +01:00
http.Error(w, "400 bad request", 400)
2015-09-18 17:13:38 +02:00
return
}
if err := tf.Sync(); err != nil {
tf.Close()
os.Remove(tmp)
2016-11-06 18:59:19 +01:00
http.Error(w, "500 internal server error", 500)
return
}
if err := tf.Close(); err != nil {
os.Remove(tmp)
2016-11-06 18:59:19 +01:00
http.Error(w, "500 internal server error", 500)
return
}
2016-11-11 01:29:55 +01:00
if isHashed(dir) {
name = filepath.Join(name[:2], name)
}
path := filepath.Join(c.path, dir, name)
if err := os.Rename(tmp, path); err != nil {
os.Remove(tmp)
2016-11-06 18:59:19 +01:00
os.Remove(path)
http.Error(w, "500 internal server error", 500)
2015-09-18 17:13:38 +02:00
return
}
2015-09-18 17:13:38 +02:00
w.Write([]byte("200 ok"))
}
}
2016-11-06 11:15:33 +01:00
// DeleteBlob returns a http.HandlerFunc that deletes a blob from the repository.
2015-09-18 17:13:38 +02:00
func DeleteBlob(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2016-12-27 12:26:41 +01:00
if *debug {
log.Println("DeleteBlob()")
}
2015-09-18 17:13:38 +02:00
vars := strings.Split(r.RequestURI, "/")
dir := vars[1]
name := vars[2]
2016-11-11 01:29:55 +01:00
if isHashed(dir) {
name = filepath.Join(name[:2], name)
}
2015-09-18 17:13:38 +02:00
path := filepath.Join(c.path, dir, name)
2016-11-06 18:59:19 +01:00
if err := os.Remove(path); err != nil {
2015-09-18 17:13:38 +02:00
http.Error(w, "500 internal server error", 500)
return
}
2015-09-18 17:13:38 +02:00
w.Write([]byte("200 ok"))
}
}