X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/34227825eec21d3a393d4467b7ed768b52ac32b1..ac2426ef6ba7c2c722da0f7b0add2c5040529bd3:/services/keepproxy/keepproxy.go diff --git a/services/keepproxy/keepproxy.go b/services/keepproxy/keepproxy.go index 865212d747..7900096caf 100644 --- a/services/keepproxy/keepproxy.go +++ b/services/keepproxy/keepproxy.go @@ -16,7 +16,6 @@ import ( "os/signal" "reflect" "regexp" - "strings" "sync" "syscall" "time" @@ -494,7 +493,7 @@ func (this PutBlockHandler) ServeHTTP(resp http.ResponseWriter, req *http.Reques } } -// ServeHTTP implemenation for IndexHandler +// ServeHTTP implementation for IndexHandler // Supports only GET requests for /index/{prefix:[0-9a-f]{0,32}} // For each keep server found in LocalRoots: // Invokes GetIndex using keepclient @@ -516,59 +515,40 @@ func (handler IndexHandler) ServeHTTP(resp http.ResponseWriter, req *http.Reques kc := *handler.KeepClient - var pass bool - var tok string - if pass, tok = CheckAuthorizationHeader(kc, handler.ApiTokenCache, req); !pass { + ok, token := CheckAuthorizationHeader(kc, handler.ApiTokenCache, req) + if !ok { status, err = http.StatusForbidden, BadAuthorizationHeader return } // Copy ArvadosClient struct and use the client's API token arvclient := *kc.Arvados - arvclient.ApiToken = tok + arvclient.ApiToken = token kc.Arvados = &arvclient - var indexResp []byte - var reader io.Reader - - switch req.Method { - case "GET": - for uuid := range kc.LocalRoots() { - reader, err = kc.GetIndex(uuid, prefix) - if err != nil { - break - } - - var readBytes []byte - readBytes, err = ioutil.ReadAll(reader) - if err != nil { - break - } - - // Got index; verify that it is complete - // The response should be "\n" if no locators matched the prefix - // Else, it should be a list of locators followed by a blank line - if (!strings.HasSuffix(string(readBytes), "\n\n")) && (string(readBytes) != "\n") { - err = errors.New("Got incomplete index") - } - - // Trim the extra empty new line found in response from each server - indexResp = append(indexResp, (readBytes[0 : len(readBytes)-1])...) - } - - // Append empty line at the end of concatenation of all server responses - indexResp = append(indexResp, ([]byte("\n"))...) - default: + // Only GET method is supported + if req.Method != "GET" { status, err = http.StatusNotImplemented, MethodNotSupported return } - switch err { - case nil: - status = http.StatusOK - resp.Header().Set("Content-Length", fmt.Sprint(len(indexResp))) - _, err = resp.Write(indexResp) - default: - status = http.StatusBadGateway + // Get index from all LocalRoots and write to resp + var reader io.Reader + for uuid := range kc.LocalRoots() { + reader, err = kc.GetIndex(uuid, prefix) + if err != nil { + status = http.StatusBadGateway + return + } + + _, err = io.Copy(resp, reader) + if err != nil { + status = http.StatusBadGateway + return + } } + + // Got index from all the keep servers and wrote to resp + status = http.StatusOK + resp.Write([]byte("\n")) }