api-get-options.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2015-2020 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/v7/pkg/encrypt"
  23. )
  24. //AdvancedGetOptions for internal use by MinIO server - not intended for client use.
  25. type AdvancedGetOptions struct {
  26. ReplicationDeleteMarker bool
  27. ReplicationProxyRequest string
  28. }
  29. // GetObjectOptions are used to specify additional headers or options
  30. // during GET requests.
  31. type GetObjectOptions struct {
  32. headers map[string]string
  33. ServerSideEncryption encrypt.ServerSide
  34. VersionID string
  35. // To be not used by external applications
  36. Internal AdvancedGetOptions
  37. }
  38. // StatObjectOptions are used to specify additional headers or options
  39. // during GET info/stat requests.
  40. type StatObjectOptions = GetObjectOptions
  41. // Header returns the http.Header representation of the GET options.
  42. func (o GetObjectOptions) Header() http.Header {
  43. headers := make(http.Header, len(o.headers))
  44. for k, v := range o.headers {
  45. headers.Set(k, v)
  46. }
  47. if o.ServerSideEncryption != nil && o.ServerSideEncryption.Type() == encrypt.SSEC {
  48. o.ServerSideEncryption.Marshal(headers)
  49. }
  50. // this header is set for active-active replication scenario where GET/HEAD
  51. // to site A is proxy'd to site B if object/version missing on site A.
  52. if o.Internal.ReplicationProxyRequest != "" {
  53. headers.Set(minIOBucketReplicationProxyRequest, o.Internal.ReplicationProxyRequest)
  54. }
  55. return headers
  56. }
  57. // Set adds a key value pair to the options. The
  58. // key-value pair will be part of the HTTP GET request
  59. // headers.
  60. func (o *GetObjectOptions) Set(key, value string) {
  61. if o.headers == nil {
  62. o.headers = make(map[string]string)
  63. }
  64. o.headers[http.CanonicalHeaderKey(key)] = value
  65. }
  66. // SetMatchETag - set match etag.
  67. func (o *GetObjectOptions) SetMatchETag(etag string) error {
  68. if etag == "" {
  69. return errInvalidArgument("ETag cannot be empty.")
  70. }
  71. o.Set("If-Match", "\""+etag+"\"")
  72. return nil
  73. }
  74. // SetMatchETagExcept - set match etag except.
  75. func (o *GetObjectOptions) SetMatchETagExcept(etag string) error {
  76. if etag == "" {
  77. return errInvalidArgument("ETag cannot be empty.")
  78. }
  79. o.Set("If-None-Match", "\""+etag+"\"")
  80. return nil
  81. }
  82. // SetUnmodified - set unmodified time since.
  83. func (o *GetObjectOptions) SetUnmodified(modTime time.Time) error {
  84. if modTime.IsZero() {
  85. return errInvalidArgument("Modified since cannot be empty.")
  86. }
  87. o.Set("If-Unmodified-Since", modTime.Format(http.TimeFormat))
  88. return nil
  89. }
  90. // SetModified - set modified time since.
  91. func (o *GetObjectOptions) SetModified(modTime time.Time) error {
  92. if modTime.IsZero() {
  93. return errInvalidArgument("Modified since cannot be empty.")
  94. }
  95. o.Set("If-Modified-Since", modTime.Format(http.TimeFormat))
  96. return nil
  97. }
  98. // SetRange - set the start and end offset of the object to be read.
  99. // See https://tools.ietf.org/html/rfc7233#section-3.1 for reference.
  100. func (o *GetObjectOptions) SetRange(start, end int64) error {
  101. switch {
  102. case start == 0 && end < 0:
  103. // Read last '-end' bytes. `bytes=-N`.
  104. o.Set("Range", fmt.Sprintf("bytes=%d", end))
  105. case 0 < start && end == 0:
  106. // Read everything starting from offset
  107. // 'start'. `bytes=N-`.
  108. o.Set("Range", fmt.Sprintf("bytes=%d-", start))
  109. case 0 <= start && start <= end:
  110. // Read everything starting at 'start' till the
  111. // 'end'. `bytes=N-M`
  112. o.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end))
  113. default:
  114. // All other cases such as
  115. // bytes=-3-
  116. // bytes=5-3
  117. // bytes=-2-4
  118. // bytes=-3-0
  119. // bytes=-3--2
  120. // are invalid.
  121. return errInvalidArgument(
  122. fmt.Sprintf(
  123. "Invalid range specified: start=%d end=%d",
  124. start, end))
  125. }
  126. return nil
  127. }