Debian Codegraph @

httpindexhandler.go

raw
1 package desync
2
3 import (
4 "bytes"
5 "crypto/subtle"
6 "fmt"
7 "net/http"
8 "os"
9 "path"
10 )
11
12 // HTTPIndexHandler is the HTTP handler for index stores.
13 type HTTPIndexHandler struct {
14 HTTPHandlerBase
15 s IndexStore
16 }
17
18 // NewHTTPIndexHandler initializes an HTTP index store handler
19 func NewHTTPIndexHandler(s IndexStore, writable bool, auth string) http.Handler {
20 return HTTPIndexHandler{HTTPHandlerBase{"index", writable, auth}, s}
21 }
22
23 func (h HTTPIndexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
24 if h.authorization != "" && subtle.ConstantTimeCompare([]byte(r.Header.Get("Authorization")), []byte(h.authorization)) != 1 {
25 http.Error(w, "Unauthorized", http.StatusUnauthorized)
26 return
27 }
28 indexName := path.Base(r.URL.Path)
29 if !validIndexName(indexName) {
30 http.Error(w, "invalid index name", http.StatusBadRequest)
31 return
32 }
33
34 switch r.Method {
35 case "GET":
36 h.get(indexName, w)
37 case "HEAD":
38 h.head(indexName, w)
39 case "PUT":
40 h.put(indexName, w, r)
41 default:
42 w.WriteHeader(http.StatusMethodNotAllowed)
43 w.Write([]byte("only GET, PUT and HEAD are supported"))
44 }
45 }
46
47 func (h HTTPIndexHandler) get(indexName string, w http.ResponseWriter) {
48 idx, err := h.s.GetIndex(indexName)
49 if err != nil {
50 if os.IsNotExist(err) {
51 http.Error(w, "index not found", http.StatusNotFound)
52 return
53 }
54 fmt.Fprintf(os.Stderr, "failed to retrieve index %s: %s\n", indexName, err)
55 http.Error(w, "failed to retrieve index", http.StatusBadRequest)
56 return
57 }
58 b := new(bytes.Buffer)
59 _, err = idx.WriteTo(b)
60 if err != nil {
61 fmt.Fprintf(os.Stderr, "failed to serialize index %s: %s\n", indexName, err)
62 http.Error(w, "internal server error", http.StatusInternalServerError)
63 return
64 }
65 h.HTTPHandlerBase.get(indexName, b.Bytes(), err, w)
66 }
67
68 func (h HTTPIndexHandler) head(indexName string, w http.ResponseWriter) {
69 rdr, err := h.s.GetIndexReader(indexName)
70 if err != nil {
71 if os.IsNotExist(err) {
72 w.WriteHeader(http.StatusNotFound)
73 return
74 }
75 fmt.Fprintf(os.Stderr, "failed to retrieve index %s: %s\n", indexName, err)
76 w.WriteHeader(http.StatusBadRequest)
77 return
78 }
79 defer rdr.Close()
80 w.WriteHeader(http.StatusOK)
81 }
82
83 func (h HTTPIndexHandler) put(indexName string, w http.ResponseWriter, r *http.Request) {
84 err := h.HTTPHandlerBase.validateWritable(h.s.String(), w, r)
85 if err != nil {
86 return
87 }
88
89 // The upstream store needs to support writing as well
90 s, ok := h.s.(IndexWriteStore)
91 if !ok {
92 fmt.Fprintf(os.Stderr, "upstream index store '%s' does not support writing\n", h.s)
93 http.Error(w, "upstream index store does not support writing", http.StatusBadRequest)
94 return
95 }
96
97 // Read the chunk into memory
98 idx, err := IndexFromReader(r.Body)
99 if err != nil {
100 http.Error(w, "invalid index: "+err.Error(), http.StatusUnsupportedMediaType)
101 return
102 }
103
104 // Store it upstream
105 if err := s.StoreIndex(indexName, idx); err != nil {
106 fmt.Fprintf(os.Stderr, "failed to store index %s: %s\n", indexName, err)
107 http.Error(w, "internal server error", http.StatusInternalServerError)
108 return
109 }
110 w.WriteHeader(http.StatusOK)
111 }