kraken
77 строк · 2.5 Кб
1// Copyright (c) 2016-2019 Uber Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package blobserver15
16import (17"net/http"18"os"19"strconv"20"strings"21
22"github.com/uber/kraken/core"23"github.com/uber/kraken/lib/store"24"github.com/uber/kraken/utils/handler"25)
26
27// parseContentRange parses start / end integers from a Content-Range header.
28func parseContentRange(h http.Header) (start, end int64, err error) {29contentRange := h.Get("Content-Range")30if len(contentRange) == 0 {31return 0, 0, handler.Errorf("no Content-Range header").Status(http.StatusBadRequest)32}33parts := strings.Split(contentRange, "-")34if len(parts) != 2 {35return 0, 0, handler.Errorf(36"cannot parse Content-Range header %q: expected format \"start-end\"", contentRange).37Status(http.StatusBadRequest)38}39start, err = strconv.ParseInt(parts[0], 10, 64)40if err != nil {41return 0, 0, handler.Errorf(42"cannot parse start of range in Content-Range header %q: %s", contentRange, err).43Status(http.StatusBadRequest)44}45end, err = strconv.ParseInt(parts[1], 10, 64)46if err != nil {47return 0, 0, handler.Errorf(48"cannot parse end of range in Content-Range header %q: %s", contentRange, err).49Status(http.StatusBadRequest)50}51// Note, no need to check for negative because the "-" would cause the52// Split check to fail.53return start, end, nil54}
55
56// blobExists returns true if cas has a cached blob for d.
57func blobExists(cas *store.CAStore, d core.Digest) (bool, error) {58if _, err := cas.GetCacheFileStat(d.Hex()); err != nil {59if os.IsNotExist(err) {60return false, nil61}62return false, handler.Errorf("cache file stat: %s", err)63}64return true, nil65}
66
67func setUploadLocation(w http.ResponseWriter, uid string) {68w.Header().Set("Location", uid)69}
70
71func setContentLength(w http.ResponseWriter, n int) {72w.Header().Set("Content-Length", strconv.Itoa(n))73}
74
75func setOctetStreamContentType(w http.ResponseWriter) {76w.Header().Set("Content-Type", "application/octet-stream-v1")77}
78