-
Notifications
You must be signed in to change notification settings - Fork 17
/
http.go
146 lines (134 loc) · 3.49 KB
/
http.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/boltdb/bolt"
"io"
"log"
"net/http"
"os"
"path/filepath"
)
type deleteObj struct {
Filename string
DistroName string
Arch string
Section string
}
func uploadHandler(config conf, db *bolt.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
if r.Method != "POST" {
http.Error(w, "method not supported", http.StatusMethodNotAllowed)
return
}
if config.EnableAPIKeys {
apiKey := r.URL.Query().Get("key")
if apiKey == "" {
http.Error(w, "api key not present", http.StatusUnauthorized)
return
}
if !validateAPIkey(db, apiKey) {
http.Error(w, "api key not valid", http.StatusUnauthorized)
return
}
}
archType := r.URL.Query().Get("arch")
if archType == "" {
archType = "all"
}
distroName := r.URL.Query().Get("distro")
if distroName == "" {
distroName = "stable"
}
section := r.URL.Query().Get("section")
if section == "" {
section = "main"
}
reader, err := r.MultipartReader()
if err != nil {
httpErrorf(w, "error creating multipart reader: %s", err)
return
}
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
if part.FileName() == "" {
continue
}
path := filepath.Join(config.ArchPath(distroName, section, archType), part.FileName())
dst, err := os.Create(path)
if *verbose {
log.Printf("Deb package %s has been uploaded to %s %s %s", part.FileName(), distroName, section, archType)
}
if err != nil {
httpErrorf(w, "error creating deb file: %s", err)
return
}
if _, err := io.Copy(dst, part); err != nil {
httpErrorf(w, "error writing deb file: %s", err)
return
}
dst.Close()
if !parsedconfig.EnableDirectoryWatching {
rebuildRepoMetadata(path)
if *verbose {
log.Printf("Repository %s %s %s has been rebuilt", distroName, section, archType)
}
}
}
w.WriteHeader(http.StatusOK)
})
}
func deleteHandler(config conf, db *bolt.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
if r.Method != "DELETE" {
http.Error(w, "method not supported", http.StatusMethodNotAllowed)
return
}
if config.EnableAPIKeys {
apiKey := r.URL.Query().Get("key")
if apiKey == "" {
http.Error(w, "api key not present", http.StatusUnauthorized)
return
}
if !validateAPIkey(db, apiKey) {
http.Error(w, "api key not valid", http.StatusUnauthorized)
return
}
}
var toDelete deleteObj
if err := json.NewDecoder(r.Body).Decode(&toDelete); err != nil {
httpErrorf(w, "failed to decode json: %s", err)
return
}
debPath := filepath.Join(config.ArchPath(toDelete.DistroName, toDelete.Section, toDelete.Arch), toDelete.Filename)
if err := os.Remove(debPath); err != nil {
httpErrorf(w, "failed to delete: %s", err)
return
}
if *verbose {
log.Printf("Deb package %s has been deleted", toDelete.Filename)
}
})
}
func validateAPIkey(db *bolt.DB, key string) bool {
err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("APIkeys"))
v := b.Get([]byte(key))
if len(v) == 0 {
return errors.New("key not found")
}
return nil
})
return err == nil
}
func httpErrorf(w http.ResponseWriter, format string, a ...interface{}) {
err := fmt.Errorf(format, a...)
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}