| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package gs | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 "net/http" | |
| 10 ) | |
| 11 | |
| 12 // Options are the set of extra options to apply to the Google Storage request. | |
| 13 type Options struct { | |
| 14 // From is the range request starting index. If >0, the beginning of the | |
| 15 // range request will be set. | |
| 16 From int64 | |
| 17 // To is the range request ending index. If >0, the end of the | |
| 18 // range request will be set. | |
| 19 // | |
| 20 // If no From index is set, this will result in a request indexed from t
he end | |
| 21 // of the object. | |
| 22 To int64 | |
| 23 } | |
| 24 | |
| 25 // gsRoundTripper is an http.RoundTripper implementation that allows us to | |
| 26 // inject some HTTP headers. It is necessary because the stock Google Storage | |
| 27 // API doesn't allow access to specific header manipulation. | |
| 28 // | |
| 29 // https://cloud.google.com/storage/docs/reference-headers#range | |
| 30 type gsRoundTripper struct { | |
| 31 http.RoundTripper | |
| 32 *Options | |
| 33 } | |
| 34 | |
| 35 func (rt *gsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | |
| 36 if req.Header == nil { | |
| 37 req.Header = make(http.Header) | |
| 38 } | |
| 39 | |
| 40 // Add Range header, if configured. | |
| 41 rh := "" | |
| 42 switch { | |
| 43 case rt.From > 0 && rt.To > 0: | |
| 44 rh = fmt.Sprintf("bytes=%d-%d", rt.From, rt.To) | |
| 45 | |
| 46 case rt.From > 0: | |
| 47 rh = fmt.Sprintf("bytes=%d-", rt.From) | |
| 48 | |
| 49 case rt.To > 0: | |
| 50 rh = fmt.Sprintf("bytes=-%d", rt.To) | |
| 51 } | |
| 52 if rh != "" { | |
| 53 req.Header.Add("Range", rh) | |
| 54 } | |
| 55 | |
| 56 return rt.RoundTripper.RoundTrip(req) | |
| 57 } | |
| OLD | NEW |