Skip to content

Commit

Permalink
indexer: HTTP correctness changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hdonnay committed Mar 5, 2020
1 parent 0017946 commit 741fc2c
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions indexer/httptransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ func NewHTTPTransport(service Service) (*HTTP, error) {
return h, nil
}

func unmodified(r *http.Request, v string) bool {
if vs, ok := r.Header["If-None-Match"]; ok {
sort.Strings(vs)
return sort.SearchStrings(vs, v) != -1
}
return false
}

func (h *HTTP) IndexReportHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
w.Header().Set("content-type", "application/json")
if r.Method != http.MethodGet {
resp := &je.Response{
Code: "method-not-allowed",
Expand All @@ -65,6 +74,11 @@ func (h *HTTP) IndexReportHandler(w http.ResponseWriter, r *http.Request) {
Message: "malformed path: " + err.Error(),
}
je.Error(w, resp, http.StatusBadRequest)
}

validator := fmt.Sprintf(`"%s|%s"`, h.serv.State(), manifest.String())
if unmodified(r, validator) {
w.WriteHeader(http.StatusNotModified)
return
}

Expand All @@ -86,6 +100,7 @@ func (h *HTTP) IndexReportHandler(w http.ResponseWriter, r *http.Request) {
return
}

w.Header().Add("etag", validator)
err = json.NewEncoder(w).Encode(report)
if err != nil {
resp := &je.Response{
Expand All @@ -98,6 +113,7 @@ func (h *HTTP) IndexReportHandler(w http.ResponseWriter, r *http.Request) {
}

func (h *HTTP) IndexHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
if r.Method != http.MethodPost {
resp := &je.Response{
Code: "method-not-allowed",
Expand Down Expand Up @@ -132,8 +148,9 @@ func (h *HTTP) IndexHandler(w http.ResponseWriter, r *http.Request) {
return
}

next := path.Join(IndexReportAPIPath, m.Hash.String())
w.Header().Set("location", next)
w.WriteHeader(http.StatusCreated)
w.Header().Set("location", path.Join(IndexReportAPIPath, m.Hash.String()))
err = json.NewEncoder(w).Encode(report)
if err != nil {
resp := &je.Response{
Expand All @@ -150,16 +167,14 @@ func (h *HTTP) StateHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Header().Set("content-type", "application/json")
s := h.serv.State()
tag := `"` + s + `"`
w.Header().Add("etag", tag)

es, ok := r.Header["If-None-Match"]
if ok {
if sort.Strings(es); sort.SearchStrings(es, tag) != -1 {
w.WriteHeader(http.StatusNotModified)
return
}
if unmodified(r, tag) {
w.WriteHeader(http.StatusNotModified)
return
}
w.Header().Set("content-type", "application/json")

Expand Down

0 comments on commit 741fc2c

Please sign in to comment.