forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hooks.go
89 lines (81 loc) · 2.4 KB
/
hooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package gitserver
import (
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
func hooksHandler(config *Config) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
segments := strings.Split(r.URL.Path[1:], "/")
for _, s := range segments {
if len(s) == 0 || s == "." || s == ".." {
http.NotFound(w, r)
return
}
}
if !config.AllowPush {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
switch len(segments) {
case 2:
path := filepath.Join(config.Home, segments[0], "hooks", segments[1])
if segments[0] == "hooks" {
path = filepath.Join(config.HookDirectory, segments[1])
}
switch r.Method {
// TODO: support HEAD or prevent GET for security
case "GET":
w.Header().Set("Content-Type", "text/plain")
http.ServeFile(w, r, path)
case "DELETE":
if err := os.Remove(path); err != nil {
log.Printf("error: attempted to remove %s: %v", path, err)
}
w.WriteHeader(http.StatusNoContent)
case "PUT":
if stat, err := os.Stat(path); err == nil {
if stat.IsDir() || (stat.Mode()&0111) == 0 {
http.Error(w, fmt.Errorf("only executable hooks can be changed: %v", stat).Error(), http.StatusInternalServerError)
return
}
// unsymlink and overwrite
if (stat.Mode() & os.ModeSymlink) != 0 {
os.Remove(path)
}
}
f, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0750)
if err != nil {
http.Error(w, fmt.Errorf("unable to open hook file: %v", err).Error(), http.StatusInternalServerError)
return
}
defer f.Close()
max := config.MaxHookBytes + 1
body := io.LimitReader(r.Body, max)
buf := make([]byte, max)
n, err := io.ReadFull(body, buf)
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
http.Error(w, fmt.Errorf("unable to read hook: %v", err).Error(), http.StatusInternalServerError)
return
}
if int64(n) == max {
http.Error(w, fmt.Errorf("hook was too long, truncated to %d bytes", config.MaxHookBytes).Error(), 422)
} else {
w.WriteHeader(http.StatusOK)
}
if _, err := f.Write(buf[:n]); err != nil {
http.Error(w, fmt.Errorf("unable to write hook: %v", err).Error(), http.StatusInternalServerError)
return
}
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
default:
http.NotFound(w, r)
}
})
}