rest-server/handlers.go

346 lines
7.6 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"
2016-12-28 00:57:25 +01:00
"strings"
2015-09-18 17:13:38 +02:00
"time"
2016-12-28 00:57:25 +01:00
"goji.io/middleware"
"goji.io/pat"
2015-09-18 17:13:38 +02:00
)
2016-11-11 01:29:55 +01:00
func isHashed(dir string) bool {
return dir == "data"
}
2016-12-28 00:57:25 +01:00
func getRepo(r *http.Request) string {
if strings.HasPrefix(fmt.Sprintf("%s", middleware.Pattern(r.Context())), "/:repo") {
2016-12-30 20:57:48 +01:00
return filepath.Join(config.path, pat.Param(r, "repo"))
2016-12-28 00:57:25 +01:00
}
2016-12-30 20:57:48 +01:00
return config.path
2016-12-28 00:57:25 +01:00
}
func createDirectories(path string) {
log.Printf("Creating repository directories in %s\n", path)
2016-12-27 16:15:54 +01:00
if err := os.MkdirAll(path, 0700); err != nil {
log.Fatal(err)
}
dirs := []string{
"data",
"index",
"keys",
"locks",
"snapshots",
"tmp",
}
for _, d := range dirs {
if err := os.MkdirAll(filepath.Join(path, d), 0700); err != nil {
log.Fatal(err)
}
}
for i := 0; i < 256; i++ {
if err := os.MkdirAll(filepath.Join(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) {
2016-12-28 19:51:25 +01:00
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
2015-09-19 14:52:50 +02:00
return
}
2015-09-19 14:52:50 +02:00
h.ServeHTTP(w, r)
}
}
// CheckConfig checks whether a configuration exists.
func CheckConfig(w http.ResponseWriter, r *http.Request) {
2016-12-30 20:57:48 +01:00
if config.debug {
log.Println("CheckConfig()")
}
2017-01-16 23:00:46 +01:00
cfg := filepath.Join(getRepo(r), "config")
st, err := os.Stat(cfg)
if err != nil {
2017-01-16 23:39:56 +01:00
if config.debug {
log.Print(err)
}
2016-12-28 19:51:25 +01:00
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
2015-09-18 17:13:38 +02:00
}
w.Header().Add("Content-Length", fmt.Sprint(st.Size()))
}
// GetConfig allows for a config to be retrieved.
func GetConfig(w http.ResponseWriter, r *http.Request) {
2016-12-30 20:57:48 +01:00
if config.debug {
log.Println("GetConfig()")
}
2017-01-16 23:00:46 +01:00
cfg := filepath.Join(getRepo(r), "config")
2017-05-01 20:02:27 +02:00
2017-01-16 23:00:46 +01:00
bytes, err := ioutil.ReadFile(cfg)
if err != nil {
2017-01-16 23:39:56 +01:00
if config.debug {
log.Print(err)
}
2016-12-28 19:51:25 +01:00
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
2015-09-18 17:13:38 +02:00
}
w.Write(bytes)
}
// SaveConfig allows for a config to be saved.
func SaveConfig(w http.ResponseWriter, r *http.Request) {
2016-12-30 20:57:48 +01:00
if config.debug {
log.Println("SaveConfig()")
}
2017-01-16 23:00:46 +01:00
cfg := filepath.Join(getRepo(r), "config")
2017-05-01 20:02:27 +02:00
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
2017-01-16 23:39:56 +01:00
if config.debug {
log.Print(err)
}
2016-12-28 19:51:25 +01:00
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
2017-05-01 20:01:52 +02:00
2017-01-16 23:00:46 +01:00
if err := ioutil.WriteFile(cfg, bytes, 0600); err != nil {
2017-01-16 23:39:56 +01:00
if config.debug {
log.Print(err)
}
2016-12-28 19:51:25 +01:00
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
2015-09-18 17:13:38 +02:00
}
}
// DeleteConfig removes a config.
func DeleteConfig(w http.ResponseWriter, r *http.Request) {
if config.debug {
log.Println("DeleteConfig()")
}
2017-05-01 20:01:52 +02:00
if err := os.Remove(filepath.Join(getRepo(r), "config")); err != nil {
if config.debug {
log.Print(err)
}
if os.IsNotExist(err) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
} else {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
return
}
}
// ListBlobs lists all blobs of a given type in an arbitrary order.
func ListBlobs(w http.ResponseWriter, r *http.Request) {
2016-12-30 20:57:48 +01:00
if config.debug {
log.Println("ListBlobs()")
}
dir := pat.Param(r, "type")
2016-12-28 00:57:25 +01:00
path := filepath.Join(getRepo(r), dir)
items, err := ioutil.ReadDir(path)
if err != nil {
2017-01-16 23:39:56 +01:00
if config.debug {
log.Print(err)
}
2016-12-28 19:51:25 +01:00
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
var names []string
for _, i := range items {
if isHashed(dir) {
subpath := filepath.Join(path, i.Name())
subitems, err := ioutil.ReadDir(subpath)
if err != nil {
2017-01-16 23:39:56 +01:00
if config.debug {
log.Print(err)
}
2016-12-28 19:51:25 +01:00
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
for _, f := range subitems {
names = append(names, f.Name())
}
} else {
names = append(names, i.Name())
2015-09-18 17:13:38 +02:00
}
}
data, err := json.Marshal(names)
if err != nil {
2017-01-16 23:39:56 +01:00
if config.debug {
log.Print(err)
}
2016-12-28 19:51:25 +01:00
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
2015-09-18 17:13:38 +02:00
}
w.Write(data)
}
2016-12-28 19:51:25 +01:00
// CheckBlob tests whether a blob exists.
func CheckBlob(w http.ResponseWriter, r *http.Request) {
2016-12-30 20:57:48 +01:00
if config.debug {
log.Println("CheckBlob()")
}
dir := pat.Param(r, "type")
name := pat.Param(r, "name")
if isHashed(dir) {
name = filepath.Join(name[:2], name)
}
2016-12-28 00:57:25 +01:00
path := filepath.Join(getRepo(r), dir, name)
st, err := os.Stat(path)
if err != nil {
2017-01-16 23:39:56 +01:00
if config.debug {
log.Print(err)
}
2016-12-28 19:51:25 +01:00
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
2015-09-18 17:13:38 +02:00
}
w.Header().Add("Content-Length", fmt.Sprint(st.Size()))
}
// GetBlob retrieves a blob from the repository.
func GetBlob(w http.ResponseWriter, r *http.Request) {
2016-12-30 20:57:48 +01:00
if config.debug {
log.Println("GetBlob()")
}
dir := pat.Param(r, "type")
name := pat.Param(r, "name")
if isHashed(dir) {
name = filepath.Join(name[:2], name)
}
2016-12-28 00:57:25 +01:00
path := filepath.Join(getRepo(r), dir, name)
file, err := os.Open(path)
if err != nil {
2017-01-16 23:39:56 +01:00
if config.debug {
log.Print(err)
}
2016-12-28 19:51:25 +01:00
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
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 saves a blob to the repository.
func SaveBlob(w http.ResponseWriter, r *http.Request) {
2016-12-30 20:57:48 +01:00
if config.debug {
log.Println("SaveBlob()")
}
2016-12-28 00:57:25 +01:00
repo := getRepo(r)
dir := pat.Param(r, "type")
name := pat.Param(r, "name")
if isHashed(dir) {
name = filepath.Join(name[:2], name)
}
path := filepath.Join(repo, dir, name)
tf, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0600)
if err != nil {
2017-01-16 23:39:56 +01:00
if config.debug {
log.Print(err)
}
2016-12-28 19:51:25 +01:00
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if _, err := io.Copy(tf, r.Body); err != nil {
tf.Close()
os.Remove(path)
2017-01-16 23:39:56 +01:00
if config.debug {
log.Print(err)
}
2016-12-28 19:51:25 +01:00
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
2017-05-01 20:01:52 +02:00
if err := tf.Sync(); err != nil {
tf.Close()
os.Remove(path)
2017-01-16 23:39:56 +01:00
if config.debug {
log.Print(err)
}
2016-12-28 19:51:25 +01:00
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
2017-05-01 20:01:52 +02:00
if err := tf.Close(); err != nil {
os.Remove(path)
2017-01-16 23:39:56 +01:00
if config.debug {
log.Print(err)
}
2016-12-28 19:51:25 +01:00
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
2015-09-18 17:13:38 +02:00
}
}
// DeleteBlob deletes a blob from the repository.
func DeleteBlob(w http.ResponseWriter, r *http.Request) {
2016-12-30 20:57:48 +01:00
if config.debug {
log.Println("DeleteBlob()")
}
dir := pat.Param(r, "type")
name := pat.Param(r, "name")
if isHashed(dir) {
name = filepath.Join(name[:2], name)
}
2016-12-28 00:57:25 +01:00
path := filepath.Join(getRepo(r), dir, name)
2017-05-01 20:01:52 +02:00
if err := os.Remove(path); err != nil {
if config.debug {
log.Print(err)
}
if os.IsNotExist(err) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
} else {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
return
}
2015-09-18 17:13:38 +02:00
}
// CreateRepo creates repository directories.
func CreateRepo(w http.ResponseWriter, r *http.Request) {
if config.debug {
log.Println("CreateRepo()")
}
if r.URL.Query().Get("create") != "true" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
createDirectories(getRepo(r))
}