api-get-options.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Minio Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2015-2017 Minio, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package minio
  18. import (
  19. "fmt"
  20. "net/http"
  21. "time"
  22. "github.com/minio/minio-go/pkg/encrypt"
  23. )
  24. // GetObjectOptions are used to specify additional headers or options
  25. // during GET requests.
  26. type GetObjectOptions struct {
  27. headers map[string]string
  28. Materials encrypt.Materials
  29. }
  30. // StatObjectOptions are used to specify additional headers or options
  31. // during GET info/stat requests.
  32. type StatObjectOptions struct {
  33. GetObjectOptions
  34. }
  35. // Header returns the http.Header representation of the GET options.
  36. func (o GetObjectOptions) Header() http.Header {
  37. headers := make(http.Header, len(o.headers))
  38. for k, v := range o.headers {
  39. headers.Set(k, v)
  40. }
  41. return headers
  42. }
  43. // Set adds a key value pair to the options. The
  44. // key-value pair will be part of the HTTP GET request
  45. // headers.
  46. func (o *GetObjectOptions) Set(key, value string) {
  47. if o.headers == nil {
  48. o.headers = make(map[string]string)
  49. }
  50. o.headers[http.CanonicalHeaderKey(key)] = value
  51. }
  52. // SetMatchETag - set match etag.
  53. func (o *GetObjectOptions) SetMatchETag(etag string) error {
  54. if etag == "" {
  55. return ErrInvalidArgument("ETag cannot be empty.")
  56. }
  57. o.Set("If-Match", "\""+etag+"\"")
  58. return nil
  59. }
  60. // SetMatchETagExcept - set match etag except.
  61. func (o *GetObjectOptions) SetMatchETagExcept(etag string) error {
  62. if etag == "" {
  63. return ErrInvalidArgument("ETag cannot be empty.")
  64. }
  65. o.Set("If-None-Match", "\""+etag+"\"")
  66. return nil
  67. }
  68. // SetUnmodified - set unmodified time since.
  69. func (o *GetObjectOptions) SetUnmodified(modTime time.Time) error {
  70. if modTime.IsZero() {
  71. return ErrInvalidArgument("Modified since cannot be empty.")
  72. }
  73. o.Set("If-Unmodified-Since", modTime.Format(http.TimeFormat))
  74. return nil
  75. }
  76. // SetModified - set modified time since.
  77. func (o *GetObjectOptions) SetModified(modTime time.Time) error {
  78. if modTime.IsZero() {
  79. return ErrInvalidArgument("Modified since cannot be empty.")
  80. }
  81. o.Set("If-Modified-Since", modTime.Format(http.TimeFormat))
  82. return nil
  83. }
  84. // SetRange - set the start and end offset of the object to be read.
  85. // See https://tools.ietf.org/html/rfc7233#section-3.1 for reference.
  86. func (o *GetObjectOptions) SetRange(start, end int64) error {
  87. switch {
  88. case start == 0 && end < 0:
  89. // Read last '-end' bytes. `bytes=-N`.
  90. o.Set("Range", fmt.Sprintf("bytes=%d", end))
  91. case 0 < start && end == 0:
  92. // Read everything starting from offset
  93. // 'start'. `bytes=N-`.
  94. o.Set("Range", fmt.Sprintf("bytes=%d-", start))
  95. case 0 <= start && start <= end:
  96. // Read everything starting at 'start' till the
  97. // 'end'. `bytes=N-M`
  98. o.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end))
  99. default:
  100. // All other cases such as
  101. // bytes=-3-
  102. // bytes=5-3
  103. // bytes=-2-4
  104. // bytes=-3-0
  105. // bytes=-3--2
  106. // are invalid.
  107. return ErrInvalidArgument(
  108. fmt.Sprintf(
  109. "Invalid range specified: start=%d end=%d",
  110. start, end))
  111. }
  112. return nil
  113. }