def _validate_headers(self): if 'CONTENT_LENGTH' in self.environ: try: if self.content_length < 0: raise InvalidArgument('Content-Length', self.content_length) except (ValueError, TypeError): raise InvalidArgument('Content-Length', self.environ['CONTENT_LENGTH']) if 'Date' in self.headers: now = datetime.datetime.utcnow() date = email.utils.parsedate(self.headers['Date']) if 'Expires' in self.params: try: d = email.utils.formatdate(float(self.params['Expires'])) except ValueError: raise AccessDenied() # check expiration expdate = email.utils.parsedate(d) ex = datetime.datetime(*expdate[0:6]) if now > ex: raise AccessDenied('Request has expired') elif date is not None: epoch = datetime.datetime(1970, 1, 1, 0, 0, 0, 0) d1 = datetime.datetime(*date[0:6]) if d1 < epoch: raise AccessDenied() # If the standard date is too far ahead or behind, it is an # error delta = datetime.timedelta(seconds=60 * 5) if abs(d1 - now) > delta: raise RequestTimeTooSkewed() else: raise AccessDenied() if 'Content-MD5' in self.headers: value = self.headers['Content-MD5'] if not re.match('^[A-Za-z0-9+/]+={0,2}$', value): # Non-base64-alphabet characters in value. raise InvalidDigest(content_md5=value) try: self.headers['ETag'] = value.decode('base64').encode('hex') except Exception: raise InvalidDigest(content_md5=value) if 'X-Amz-Copy-Source' in self.headers: try: check_path_header(self, 'X-Amz-Copy-Source', 2, '') except swob.HTTPException: msg = 'Copy Source must mention the source bucket and key: ' \ 'sourcebucket/sourcekey' raise InvalidArgument('x-amz-copy-source', self.headers['X-Amz-Copy-Source'], msg) if 'x-amz-metadata-directive' in self.headers: value = self.headers['x-amz-metadata-directive'] if value not in ('COPY', 'REPLACE'): err_msg = 'Unknown metadata directive.' raise InvalidArgument('x-amz-metadata-directive', value, err_msg) if 'x-amz-storage-class' in self.headers: # Only STANDARD is supported now. if self.headers['x-amz-storage-class'] != 'STANDARD': raise InvalidStorageClass() if 'x-amz-mfa' in self.headers: raise S3NotImplemented('MFA Delete is not supported.') if 'x-amz-server-side-encryption' in self.headers: raise S3NotImplemented('Server-side encryption is not supported.') if 'x-amz-website-redirect-location' in self.headers: raise S3NotImplemented('Website redirection is not supported.')
def _validate_headers(self): if 'CONTENT_LENGTH' in self.environ: try: if self.content_length < 0: raise InvalidArgument('Content-Length', self.content_length) except (ValueError, TypeError): raise InvalidArgument('Content-Length', self.environ['CONTENT_LENGTH']) self._validate_dates() value = _header_strip(self.headers.get('Content-MD5')) if value is not None: if not re.match('^[A-Za-z0-9+/]+={0,2}$', value): # Non-base64-alphabet characters in value. raise InvalidDigest(content_md5=value) try: self.headers['ETag'] = value.decode('base64').encode('hex') except Exception: raise InvalidDigest(content_md5=value) if len(self.headers['ETag']) != 32: raise InvalidDigest(content_md5=value) if self.method == 'PUT' and any(h in self.headers for h in ( 'If-Match', 'If-None-Match', 'If-Modified-Since', 'If-Unmodified-Since')): raise S3NotImplemented( 'Conditional object PUTs are not supported.') if 'X-Amz-Copy-Source' in self.headers: try: check_path_header(self, 'X-Amz-Copy-Source', 2, '') except swob.HTTPException: msg = 'Copy Source must mention the source bucket and key: ' \ 'sourcebucket/sourcekey' raise InvalidArgument('x-amz-copy-source', self.headers['X-Amz-Copy-Source'], msg) if 'x-amz-metadata-directive' in self.headers: value = self.headers['x-amz-metadata-directive'] if value not in ('COPY', 'REPLACE'): err_msg = 'Unknown metadata directive.' raise InvalidArgument('x-amz-metadata-directive', value, err_msg) if 'x-amz-storage-class' in self.headers: # Only STANDARD is supported now. if self.headers['x-amz-storage-class'] != 'STANDARD': raise InvalidStorageClass() if 'x-amz-mfa' in self.headers: raise S3NotImplemented('MFA Delete is not supported.') if 'x-amz-server-side-encryption' in self.headers: raise S3NotImplemented('Server-side encryption is not supported.') if 'x-amz-website-redirect-location' in self.headers: raise S3NotImplemented('Website redirection is not supported.')
def _validate_headers(self): if "CONTENT_LENGTH" in self.environ: try: if self.content_length < 0: raise InvalidArgument("Content-Length", self.content_length) except (ValueError, TypeError): raise InvalidArgument("Content-Length", self.environ["CONTENT_LENGTH"]) if "Date" in self.headers: now = datetime.datetime.utcnow() date = email.utils.parsedate(self.headers["Date"]) if "Expires" in self.params: try: d = email.utils.formatdate(float(self.params["Expires"])) except ValueError: raise AccessDenied() # check expiration expdate = email.utils.parsedate(d) ex = datetime.datetime(*expdate[0:6]) if now > ex: raise AccessDenied("Request has expired") elif date is not None: epoch = datetime.datetime(1970, 1, 1, 0, 0, 0, 0) d1 = datetime.datetime(*date[0:6]) if d1 < epoch: raise AccessDenied() # If the standard date is too far ahead or behind, it is an # error delta = datetime.timedelta(seconds=60 * 5) if abs(d1 - now) > delta: raise RequestTimeTooSkewed() else: raise AccessDenied() if "Content-MD5" in self.headers: value = self.headers["Content-MD5"] if not re.match("^[A-Za-z0-9+/]+={0,2}$", value): # Non-base64-alphabet characters in value. raise InvalidDigest(content_md5=value) try: self.headers["ETag"] = value.decode("base64").encode("hex") except Exception: raise InvalidDigest(content_md5=value) if len(self.headers["ETag"]) != 32: raise InvalidDigest(content_md5=value) if self.method == "PUT" and any( h in self.headers for h in ("If-Match", "If-None-Match", "If-Modified-Since", "If-Unmodified-Since") ): raise S3NotImplemented("Conditional object PUTs are not supported.") if "X-Amz-Copy-Source" in self.headers: try: check_path_header(self, "X-Amz-Copy-Source", 2, "") except swob.HTTPException: msg = "Copy Source must mention the source bucket and key: " "sourcebucket/sourcekey" raise InvalidArgument("x-amz-copy-source", self.headers["X-Amz-Copy-Source"], msg) if "x-amz-metadata-directive" in self.headers: value = self.headers["x-amz-metadata-directive"] if value not in ("COPY", "REPLACE"): err_msg = "Unknown metadata directive." raise InvalidArgument("x-amz-metadata-directive", value, err_msg) if "x-amz-storage-class" in self.headers: # Only STANDARD is supported now. if self.headers["x-amz-storage-class"] != "STANDARD": raise InvalidStorageClass() if "x-amz-mfa" in self.headers: raise S3NotImplemented("MFA Delete is not supported.") if "x-amz-server-side-encryption" in self.headers: raise S3NotImplemented("Server-side encryption is not supported.") if "x-amz-website-redirect-location" in self.headers: raise S3NotImplemented("Website redirection is not supported.")