storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/copy-part-range.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2017 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"context"
    21  	"net/http"
    22  	"net/url"
    23  )
    24  
    25  // Writes S3 compatible copy part range error.
    26  func writeCopyPartErr(ctx context.Context, w http.ResponseWriter, err error, url *url.URL, browser bool) {
    27  	switch err {
    28  	case errInvalidRange:
    29  		WriteErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrInvalidCopyPartRange), url, browser)
    30  		return
    31  	case errInvalidRangeSource:
    32  		WriteErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrInvalidCopyPartRangeSource), url, browser)
    33  		return
    34  	default:
    35  		apiErr := errorCodes.ToAPIErr(ErrInvalidCopyPartRangeSource)
    36  		apiErr.Description = err.Error()
    37  		WriteErrorResponse(ctx, w, apiErr, url, browser)
    38  		return
    39  	}
    40  }
    41  
    42  // Parses x-amz-copy-source-range for CopyObjectPart API. Its behavior
    43  // is different from regular HTTP range header. It only supports the
    44  // form `bytes=first-last` where first and last are zero-based byte
    45  // offsets. See
    46  // http://docs.aws.amazon.com/AmazonS3/latest/API/mpUploadUploadPartCopy.html
    47  // for full details. This function treats an empty rangeString as
    48  // referring to the whole resource.
    49  func parseCopyPartRangeSpec(rangeString string) (hrange *HTTPRangeSpec, err error) {
    50  	hrange, err = parseRequestRangeSpec(rangeString)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	if hrange.IsSuffixLength || hrange.Start < 0 || hrange.End < 0 {
    55  		return nil, errInvalidRange
    56  	}
    57  	return hrange, nil
    58  }
    59  
    60  // checkCopyPartRangeWithSize adds more check to the range string in case of
    61  // copy object part. This API requires having specific start and end  range values
    62  // e.g. 'bytes=3-10'. Other use cases will be rejected.
    63  func checkCopyPartRangeWithSize(rs *HTTPRangeSpec, resourceSize int64) (err error) {
    64  	if rs == nil {
    65  		return nil
    66  	}
    67  	if rs.IsSuffixLength || rs.Start >= resourceSize || rs.End >= resourceSize {
    68  		return errInvalidRangeSource
    69  	}
    70  	return nil
    71  }