github.com/uber/kraken@v0.1.4/origin/blobserver/utils.go (about)

     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.
    14  package blobserver
    15  
    16  import (
    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.
    28  func parseContentRange(h http.Header) (start, end int64, err error) {
    29  	contentRange := h.Get("Content-Range")
    30  	if len(contentRange) == 0 {
    31  		return 0, 0, handler.Errorf("no Content-Range header").Status(http.StatusBadRequest)
    32  	}
    33  	parts := strings.Split(contentRange, "-")
    34  	if len(parts) != 2 {
    35  		return 0, 0, handler.Errorf(
    36  			"cannot parse Content-Range header %q: expected format \"start-end\"", contentRange).
    37  			Status(http.StatusBadRequest)
    38  	}
    39  	start, err = strconv.ParseInt(parts[0], 10, 64)
    40  	if err != nil {
    41  		return 0, 0, handler.Errorf(
    42  			"cannot parse start of range in Content-Range header %q: %s", contentRange, err).
    43  			Status(http.StatusBadRequest)
    44  	}
    45  	end, err = strconv.ParseInt(parts[1], 10, 64)
    46  	if err != nil {
    47  		return 0, 0, handler.Errorf(
    48  			"cannot parse end of range in Content-Range header %q: %s", contentRange, err).
    49  			Status(http.StatusBadRequest)
    50  	}
    51  	// Note, no need to check for negative because the "-" would cause the
    52  	// Split check to fail.
    53  	return start, end, nil
    54  }
    55  
    56  // blobExists returns true if cas has a cached blob for d.
    57  func blobExists(cas *store.CAStore, d core.Digest) (bool, error) {
    58  	if _, err := cas.GetCacheFileStat(d.Hex()); err != nil {
    59  		if os.IsNotExist(err) {
    60  			return false, nil
    61  		}
    62  		return false, handler.Errorf("cache file stat: %s", err)
    63  	}
    64  	return true, nil
    65  }
    66  
    67  func setUploadLocation(w http.ResponseWriter, uid string) {
    68  	w.Header().Set("Location", uid)
    69  }
    70  
    71  func setContentLength(w http.ResponseWriter, n int) {
    72  	w.Header().Set("Content-Length", strconv.Itoa(n))
    73  }
    74  
    75  func setOctetStreamContentType(w http.ResponseWriter) {
    76  	w.Header().Set("Content-Type", "application/octet-stream-v1")
    77  }