'Handle HTTP PUT request.'

@public
@timing_stats()
def PUT(self, req):

    (drive, part, account, container, obj) = split_and_validate_path(req, 4, 5, True)
    req_timestamp = valid_timestamp(req)
    if ('x-container-sync-to' in req.headers):
        (err, sync_to, realm, realm_key) = validate_sync_to(req.headers['x-container-sync-to'], self.allowed_sync_hosts, self.realms_conf)
        if err:
            return HTTPBadRequest(err)
    if (self.mount_check and (not check_mount(self.root, drive))):
        return HTTPInsufficientStorage(drive=drive, request=req)
    requested_policy_index = self.get_and_validate_policy_index(req)
    broker = self._get_container_broker(drive, part, account, container)
    if obj:
        obj_policy_index = (requested_policy_index or 0)
        if (account.startswith(self.auto_create_account_prefix) and (not os.path.exists(broker.db_file))):
            try:
                broker.initialize(req_timestamp.internal, obj_policy_index)
            except DatabaseAlreadyExists:
                pass
        if (not os.path.exists(broker.db_file)):
            return HTTPNotFound()
        broker.put_object(obj, req_timestamp.internal, int(req.headers['x-size']), req.headers['x-content-type'], req.headers['x-etag'], 0, obj_policy_index, req.headers.get('x-content-type-timestamp'), req.headers.get('x-meta-timestamp'))
        return HTTPCreated(request=req)
    else:
        if (requested_policy_index is None):
            new_container_policy = req.headers.get('X-Backend-Storage-Policy-Default', int(POLICIES.default))
        else:
            new_container_policy = requested_policy_index
        created = self._update_or_create(req, broker, req_timestamp.internal, new_container_policy, requested_policy_index)
        metadata = {}
        metadata.update(((key, (value, req_timestamp.internal)) for (key, value) in req.headers.items() if ((key.lower() in self.save_headers) or is_sys_or_user_meta('container', key))))
        if ('X-Container-Sync-To' in metadata):
            if (('X-Container-Sync-To' not in broker.metadata) or (metadata['X-Container-Sync-To'][0] != broker.metadata['X-Container-Sync-To'][0])):
                broker.set_x_container_sync_points((-1), (-1))
        broker.update_metadata(metadata, validate_metadata=True)
        if metadata:
            self._update_sync_store(broker, 'PUT')
        resp = self.account_update(req, account, container, broker)
        if resp:
            return resp
        if created:
            return HTTPCreated(request=req, headers={'x-backend-storage-policy-index': broker.storage_policy_index})
        else:
            return HTTPAccepted(request=req, headers={'x-backend-storage-policy-index': broker.storage_policy_index})
