Commit e837d788 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Read json instead of application/form-encoded

parent 155d1b6f
package builds package builds
import ( import (
"encoding/json"
"errors"
"net/http" "net/http"
"time" "time"
...@@ -10,10 +12,8 @@ import ( ...@@ -10,10 +12,8 @@ import (
) )
const ( const (
maxRegisterBodySize = 4 * 1024 maxRegisterBodySize = 4 * 1024
runnerBuildQueue = "runner:build_queue:" runnerBuildQueue = "runner:build_queue:"
runnerBuildQueueKey = "token"
runnerBuildQueueValue = "X-GitLab-Last-Update"
) )
var ( var (
...@@ -43,14 +43,9 @@ func init() { ...@@ -43,14 +43,9 @@ func init() {
) )
} }
func readRunnerToken(r *http.Request) (string, error) { type runnerRequest struct {
err := r.ParseForm() Token string `json:"token,omitempty"`
if err != nil { LastUpdate string `json:"last_update,omitempty"`
return "", err
}
token := r.FormValue(runnerBuildQueueKey)
return token, nil
} }
func readRunnerBody(w http.ResponseWriter, r *http.Request) ([]byte, error) { func readRunnerBody(w http.ResponseWriter, r *http.Request) ([]byte, error) {
...@@ -60,6 +55,21 @@ func readRunnerBody(w http.ResponseWriter, r *http.Request) ([]byte, error) { ...@@ -60,6 +55,21 @@ func readRunnerBody(w http.ResponseWriter, r *http.Request) ([]byte, error) {
return helper.ReadRequestBody(w, r, maxRegisterBodySize) return helper.ReadRequestBody(w, r, maxRegisterBodySize)
} }
func readRunnerRequest(r *http.Request, body []byte) (runnerRequest, error) {
var runnerRequest runnerRequest
if r.Header.Get("Content-Type") != "application/json" {
return runnerRequest, errors.New("invalid content-type received")
}
err := json.Unmarshal(body, &runnerRequest)
if err != nil {
return runnerRequest, err
}
return runnerRequest, nil
}
func proxyRegisterRequest(h http.Handler, w http.ResponseWriter, r *http.Request) { func proxyRegisterRequest(h http.Handler, w http.ResponseWriter, r *http.Request) {
registerHandlerOpen.WithLabelValues("proxying").Inc() registerHandlerOpen.WithLabelValues("proxying").Inc()
defer registerHandlerOpen.WithLabelValues("proxying").Dec() defer registerHandlerOpen.WithLabelValues("proxying").Dec()
...@@ -80,13 +90,6 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler ...@@ -80,13 +90,6 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
} }
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lastUpdate := r.Header.Get(runnerBuildQueueValue)
if lastUpdate == "" {
registerHandlerHits.WithLabelValues("missing-value").Inc()
proxyRegisterRequest(h, w, r)
return
}
requestBody, err := readRunnerBody(w, r) requestBody, err := readRunnerBody(w, r)
if err != nil { if err != nil {
registerHandlerHits.WithLabelValues("body-read-error").Inc() registerHandlerHits.WithLabelValues("body-read-error").Inc()
...@@ -94,14 +97,20 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler ...@@ -94,14 +97,20 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
return return
} }
runnerToken, err := readRunnerToken(helper.CloneRequestWithNewBody(r, requestBody)) runnerRequest, err := readRunnerRequest(r, requestBody)
if runnerToken == "" || err != nil { if err != nil {
registerHandlerHits.WithLabelValues("body-parse-error").Inc() registerHandlerHits.WithLabelValues("body-parse-error").Inc()
proxyRegisterRequest(h, w, r) proxyRegisterRequest(h, w, r)
return return
} }
result, err := watchForRunnerChange(runnerToken, lastUpdate, pollingDuration) if runnerRequest.Token == "" || runnerRequest.LastUpdate == "" {
registerHandlerHits.WithLabelValues("missing-values").Inc()
proxyRegisterRequest(h, w, r)
return
}
result, err := watchForRunnerChange(runnerRequest.Token, runnerRequest.LastUpdate, pollingDuration)
if err != nil { if err != nil {
registerHandlerHits.WithLabelValues("watch-error").Inc() registerHandlerHits.WithLabelValues("watch-error").Inc()
helper.Fail500(w, r, &watchError{err}) helper.Fail500(w, r, &watchError{err})
...@@ -122,18 +131,18 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler ...@@ -122,18 +131,18 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
// whether the connection is not dead // whether the connection is not dead
case redis.WatchKeyStatusSeenChange: case redis.WatchKeyStatusSeenChange:
registerHandlerHits.WithLabelValues("seen-change").Inc() registerHandlerHits.WithLabelValues("seen-change").Inc()
w.WriteHeader(204) w.WriteHeader(http.StatusNoContent)
// When we receive one of these statuses, it means that we detected no change, // When we receive one of these statuses, it means that we detected no change,
// so we return to runner 204, which means nothing got changed, // so we return to runner 204, which means nothing got changed,
// and there's no new builds to process // and there's no new builds to process
case redis.WatchKeyStatusTimeout: case redis.WatchKeyStatusTimeout:
registerHandlerHits.WithLabelValues("timeout").Inc() registerHandlerHits.WithLabelValues("timeout").Inc()
w.WriteHeader(204) w.WriteHeader(http.StatusNoContent)
case redis.WatchKeyStatusNoChange: case redis.WatchKeyStatusNoChange:
registerHandlerHits.WithLabelValues("no-change").Inc() registerHandlerHits.WithLabelValues("no-change").Inc()
w.WriteHeader(204) w.WriteHeader(http.StatusNoContent)
} }
}) })
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment