Пример #1
0
        public Stream?MarshalRequest(PutBucketTaggingRequest request, IConfig config)
        {
            request.SetQueryParameter(AmzParameters.Tagging, string.Empty);

            if (!request.Tags.HasData())
            {
                return(null);
            }

            FastXmlWriter writer = new FastXmlWriter(200);

            writer.WriteStartElement("Tagging", "http://s3.amazonaws.com/doc/2006-03-01/");
            writer.WriteStartElement("TagSet");

            foreach (KeyValuePair <string, string> tag in request.Tags)
            {
                writer.WriteStartElement("Tag");
                writer.WriteElement("Key", tag.Key);
                writer.WriteElement("Value", tag.Value);
                writer.WriteEndElement("Tag");
            }

            writer.WriteEndElement("TagSet");
            writer.WriteEndElement("Tagging");
            return(new MemoryStream(writer.GetBytes()));
        }
Пример #2
0
        public Stream?MarshalRequest(PutBucketLockConfigurationRequest request, IConfig config)
        {
            request.SetQueryParameter(AmzParameters.ObjectLock, string.Empty);

            FastXmlWriter writer = new FastXmlWriter(128);

            writer.WriteStartElement("ObjectLockConfiguration", "http://s3.amazonaws.com/doc/2006-03-01/");
            writer.WriteElement("ObjectLockEnabled", "Enabled");

            if (request.LockMode != LockMode.Unknown)
            {
                writer.WriteStartElement("Rule");
                writer.WriteStartElement("DefaultRetention");
                writer.WriteElement("Mode", ValueHelper.EnumToString(request.LockMode));

                if (request.LockRetainUntil.HasValue)
                {
                    writer.WriteElement("Days", (request.LockRetainUntil.Value - DateTimeOffset.UtcNow).Days);
                }

                writer.WriteEndElement("DefaultRetention");
                writer.WriteEndElement("Rule");
            }

            writer.WriteEndElement("ObjectLockConfiguration");

            return(new MemoryStream(writer.GetBytes()));
        }
Пример #3
0
        public Stream?MarshalRequest(DeleteObjectsRequest request, IConfig config)
        {
            request.SetQueryParameter(AmzParameters.Delete, string.Empty);

            FastXmlWriter xml = new FastXmlWriter(512);

            xml.WriteStartElement("Delete");

            if (request.Quiet)
            {
                xml.WriteElement("Quiet", true);
            }

            foreach (S3DeleteInfo info in request.Objects)
            {
                xml.WriteStartElement("Object");
                xml.WriteElement("Key", info.Name);

                if (info.VersionId != null)
                {
                    xml.WriteElement("VersionId", info.VersionId);
                }

                xml.WriteEndElement("Object");
            }

            xml.WriteEndElement("Delete");
            return(new MemoryStream(xml.GetBytes()));
        }
Пример #4
0
        internal void EncodeNormalValue(XmlStandard standard)
        {
            FastXmlWriter writer = new FastXmlWriter(50, standard);

            writer.WriteElement("Test", "Value");

            Assert.Equal("<Test>Value</Test>", writer.ToString());
        }
Пример #5
0
        internal void EncodeSyntaxCharacters(XmlStandard standard)
        {
            FastXmlWriter writer = new FastXmlWriter(50, standard);

            writer.WriteElement("Test", "<&>");

            Assert.Equal("<Test>&lt;&amp;&gt;</Test>", writer.ToString());
        }
Пример #6
0
    internal void EncodeNormalValue(XmlStandard standard)
    {
        FastXmlWriter writer = new FastXmlWriter(50, standard);

        writer.WriteElement("Test", "Value");

        Assert.Equal("<Test>Value</Test>", Encoding.UTF8.GetString(writer.GetBytes()));
    }
Пример #7
0
        internal void EncodeSurrogatePair(XmlStandard standard)
        {
            FastXmlWriter writer = new FastXmlWriter(50, standard);

            writer.WriteElement("Test", "💩");

            string actual = writer.ToString();

            Assert.Equal("<Test>💩</Test>", actual);
        }
        public Stream MarshalRequest(PutBucketAccelerateConfigurationRequest request, IConfig config)
        {
            request.SetQueryParameter(AmzParameters.Accelerate, string.Empty);

            FastXmlWriter writer = new FastXmlWriter(150);

            writer.WriteStartElement("AccelerateConfiguration", "http://s3.amazonaws.com/doc/2006-03-01/");
            writer.WriteElement("Status", request.AccelerationEnabled ? "Enabled" : "Suspended");
            writer.WriteEndElement("AccelerateConfiguration");

            return(new MemoryStream(writer.GetBytes()));
        }
        public Stream MarshalRequest(PutObjectLegalHoldRequest request, IConfig config)
        {
            request.SetQueryParameter(AmzParameters.LegalHold, string.Empty);

            FastXmlWriter xml = new FastXmlWriter(512);

            xml.WriteStartElement("LegalHold", "http://s3.amazonaws.com/doc/2006-03-01/");
            xml.WriteElement("Status", request.LockLegalHold !.Value ? "ON" : "OFF");
            xml.WriteEndElement("LegalHold");

            return(new MemoryStream(xml.GetBytes()));
        }
Пример #10
0
        internal void XDocumentCompatibility(XmlStandard standard)
        {
            FastXmlWriter writer = new FastXmlWriter(ushort.MaxValue + 200, standard, XmlCharMode.Omit, XmlCharMode.Omit);
            StringBuilder sb     = new StringBuilder(ushort.MaxValue);

            for (int i = 0; i < ushort.MaxValue; i++)
            {
                sb.Append((char)i);
            }

            writer.WriteElement("test", sb.ToString());

            XDocument _ = XDocument.Parse(writer.ToString());
        }
Пример #11
0
    internal void XmlDocumentCompatibility(XmlStandard standard)
    {
        FastXmlWriter writer = new FastXmlWriter(ushort.MaxValue + 200, standard, XmlCharMode.Omit, XmlCharMode.Omit);
        StringBuilder sb     = new StringBuilder(ushort.MaxValue);

        for (int i = 0; i < ushort.MaxValue; i++)
        {
            sb.Append((char)i);
        }

        writer.WriteElement("test", sb.ToString());

        XmlDocument xmlDoc = new XmlDocument();

        using MemoryStream ms = new MemoryStream(writer.GetBytes());
        xmlDoc.Load(ms);
    }
        public Stream MarshalRequest(CreateBucketRequest request, IConfig config)
        {
            //We only enable object locking on creation. We can't disable it, so there is no "false" option
            if (request.EnableObjectLocking.HasValue && request.EnableObjectLocking.Value)
            {
                request.SetHeader(AmzHeaders.XAmzBucketObjectLockEnabled, request.EnableObjectLocking.Value ? "TRUE" : string.Empty);
            }

            //Hard-code the LocationConstraint to the region from the config
            FastXmlWriter writer = new FastXmlWriter(128);

            writer.WriteStartElement("CreateBucketConfiguration");
            writer.WriteElement("LocationConstraint", ValueHelper.EnumToString(config.Region));
            writer.WriteEndElement("CreateBucketConfiguration");

            return(new MemoryStream(writer.GetBytes()));
        }
Пример #13
0
        public Stream MarshalRequest(CompleteMultipartUploadRequest request, IConfig config)
        {
            //build the XML required to describe each part
            FastXmlWriter xml = new FastXmlWriter(512);

            xml.WriteStartElement("CompleteMultipartUpload");

            foreach (S3PartInfo partInfo in request.UploadParts)
            {
                xml.WriteStartElement("Part");
                xml.WriteElement("ETag", partInfo.ETag.Trim('"'));
                xml.WriteElement("PartNumber", partInfo.PartNumber.ToString(NumberFormatInfo.InvariantInfo));
                xml.WriteEndElement("Part");
            }

            xml.WriteEndElement("CompleteMultipartUpload");

            return(new MemoryStream(xml.GetBytes()));
        }
    public Stream?MarshalRequest(PutBucketVersioningRequest request, SimpleS3Config config)
    {
        request.SetQueryParameter(AmzParameters.Versioning, string.Empty);

        FastXmlWriter writer = new FastXmlWriter(128);

        writer.WriteStartElement("VersioningConfiguration", "http://s3.amazonaws.com/doc/2006-03-01/");
        writer.WriteElement("Status", request.Status ? "Enabled" : "Suspended");

        string?mfa = request.Mfa.Build();

        if (mfa != null)
        {
            writer.WriteElement("MfaDelete", mfa);
        }

        writer.WriteEndElement("VersioningConfiguration");

        return(new MemoryStream(writer.GetBytes()));
    }
Пример #15
0
    public string FastXmlWriterTest()
    {
        FastXmlWriter writer = new FastXmlWriter(100);

        writer.WriteStartElement("rootnode");
        writer.WriteStartElement("person");

        writer.WriteElement("name", "santa claus");
        writer.WriteElement("age", "800");
        writer.WriteElement("status", "missing");

        writer.WriteElement("name", "Donald 💩💩💩💩💩\0\0\0\0");
        writer.WriteElement("age", "7");
        writer.WriteElement("status", "present");

        writer.WriteEndElement("person");
        writer.WriteEndElement("rootnode");

        return(writer.GetXmlString());
    }
Пример #16
0
    public Stream?MarshalRequest(CreateBucketRequest request, SimpleS3Config config)
    {
        //We only enable object locking on creation. We can't disable it, so there is no "false" option
        if (request.EnableObjectLocking.HasValue && request.EnableObjectLocking.Value)
        {
            request.SetHeader(AmzHeaders.XAmzBucketObjectLockEnabled, "TRUE");
        }

        //Hard-code the LocationConstraint to the region from the config
        if (config.ProviderName != "BackBlazeB2")
        {
            FastXmlWriter writer = new FastXmlWriter(128);
            writer.WriteStartElement("CreateBucketConfiguration");
            writer.WriteElement("LocationConstraint", config.RegionCode);
            writer.WriteEndElement("CreateBucketConfiguration");

            return(new MemoryStream(writer.GetBytes()));
        }

        return(null);
    }
Пример #17
0
        public Stream MarshalRequest(PutBucketLifecycleConfigurationRequest request, IConfig config)
        {
            request.SetQueryParameter(AmzParameters.Lifecycle, string.Empty);

            FastXmlWriter writer = new FastXmlWriter(600);

            writer.WriteStartElement("LifecycleConfiguration", "http://s3.amazonaws.com/doc/2006-03-01/");

            foreach (S3Rule rule in request.Rules)
            {
                writer.WriteStartElement("Rule");

                if (rule.AbortIncompleteMultipartUploadDays.HasValue)
                {
                    writer.WriteStartElement("AbortIncompleteMultipartUpload");
                    writer.WriteElement("DaysAfterInitiation", rule.AbortIncompleteMultipartUploadDays.Value);
                    writer.WriteEndElement("AbortIncompleteMultipartUpload");
                }

                if (rule.Expiration != null)
                {
                    writer.WriteStartElement("Expiration");

                    if (rule.Expiration.ExpireOnDate.HasValue)
                    {
                        writer.WriteElement("Date", ValueHelper.DateToString(rule.Expiration.ExpireOnDate.Value, DateTimeFormat.Iso8601Date));
                    }

                    if (rule.Expiration.ExpireAfterDays.HasValue)
                    {
                        writer.WriteElement("Days", rule.Expiration.ExpireAfterDays.Value);
                    }

                    if (rule.Expiration.ExpireObjectDeleteMarker.HasValue)
                    {
                        writer.WriteElement("ExpiredObjectDeleteMarker", rule.Expiration.ExpireObjectDeleteMarker.Value);
                    }

                    writer.WriteEndElement("Expiration");
                }

                if (rule.Filter != null)
                {
                    if (rule.Filter.Prefix != null)
                    {
                        writer.WriteElement("Prefix", rule.Filter.Prefix);
                    }

                    if (rule.Filter.Tag != null)
                    {
                        writer.WriteStartElement("Tag");
                        writer.WriteElement("Key", rule.Filter.Tag.Value.Key);
                        writer.WriteElement("Value", rule.Filter.Tag.Value.Value);
                        writer.WriteEndElement("Tag");
                    }

                    foreach (S3AndCondition condition in rule.Filter.Conditions)
                    {
                        writer.WriteStartElement("And");

                        if (condition.Prefix != null)
                        {
                            writer.WriteElement("Prefix", condition.Prefix);
                        }

                        if (condition.Tag != null)
                        {
                            writer.WriteStartElement("Tag");
                            writer.WriteElement("Key", condition.Tag.Value.Key);
                            writer.WriteElement("Value", condition.Tag.Value.Value);
                            writer.WriteEndElement("Tag");
                        }

                        writer.WriteEndElement("And");
                    }
                }

                if (rule.Id != null)
                {
                    writer.WriteElement("ID", rule.Id);
                }

                if (rule.NonCurrentVersionExpirationDays != null)
                {
                    writer.WriteStartElement("NoncurrentVersionExpiration");
                    writer.WriteElement("NoncurrentDays", rule.NonCurrentVersionExpirationDays.Value);
                    writer.WriteEndElement("NoncurrentVersionExpiration");
                }

                foreach (S3NonCurrentVersionTransition transition in rule.NonCurrentVersionTransitions)
                {
                    writer.WriteStartElement("NoncurrentVersionTransition");
                    writer.WriteElement("NoncurrentDays", transition.NonCurrentDays.Value);
                    writer.WriteElement("StorageClass", ValueHelper.EnumToString(transition.StorageClass));
                    writer.WriteEndElement("NoncurrentVersionTransition");
                }

                writer.WriteElement("Status", rule.Enabled ? "Enabled" : "Disabled");

                foreach (S3Transition transition in rule.Transitions)
                {
                    writer.WriteStartElement("Transition");

                    if (transition.TransitionOnDate.HasValue)
                    {
                        writer.WriteElement("Date", ValueHelper.DateToString(transition.TransitionOnDate.Value, DateTimeFormat.Iso8601Date));
                    }

                    if (transition.TransitionAfterDays.HasValue)
                    {
                        writer.WriteElement("Days", transition.TransitionAfterDays.Value);
                    }

                    if (transition.StorageClass != StorageClass.Unknown)
                    {
                        writer.WriteElement("StorageClass", ValueHelper.EnumToString(transition.StorageClass));
                    }

                    writer.WriteEndElement("Transition");
                }

                writer.WriteEndElement("Rule");
            }

            writer.WriteEndElement("LifecycleConfiguration");

            return(new MemoryStream(writer.GetBytes()));
        }
Пример #18
0
        internal void EncodeInvalidSurrogateValue(XmlStandard standard)
        {
            FastXmlWriter writer = new FastXmlWriter(50, standard);

            Assert.Throws <XmlException>(() => writer.WriteElement("Test", "Value" + '\uD800')); //standalone high surrogate
        }
Пример #19
0
    public Stream?MarshalRequest(RestoreObjectRequest request, SimpleS3Config config)
    {
        request.SetQueryParameter(AmzParameters.Restore, string.Empty);

        FastXmlWriter xml = new FastXmlWriter(512);

        xml.WriteStartElement("RestoreRequest", "http://s3.amazonaws.com/doc/2006-03-01/");

        if (request.Days > 0)
        {
            xml.WriteElement("Days", request.Days);
        }

        if (request.RequestType != RestoreRequestType.Unknown)
        {
            xml.WriteElement("Type", ValueHelper.EnumToString(request.RequestType));
        }

        if (request.RequestTier != RetrievalTier.Unknown)
        {
            xml.WriteElement("Tier", ValueHelper.EnumToString(request.RequestTier));
        }

        if (request.GlacierTier != RetrievalTier.Unknown)
        {
            xml.WriteStartElement("GlacierJobParameters");
            xml.WriteElement("Tier", ValueHelper.EnumToString(request.GlacierTier));
            xml.WriteEndElement("GlacierJobParameters");
        }

        if (request.Description != null)
        {
            xml.WriteElement("Description", request.Description);
        }

        if (request.SelectParameters != null)
        {
            xml.WriteStartElement("SelectParameters");

            if (request.SelectParameters.InputFormat != null)
            {
                xml.WriteStartElement("InputSerialization");

                if (request.SelectParameters.InputFormat.CompressionType != CompressionType.Unknown)
                {
                    xml.WriteElement("CompressionType", ValueHelper.EnumToString(request.SelectParameters.InputFormat.CompressionType));
                }

                switch (request.SelectParameters.InputFormat)
                {
                case S3CsvInputFormat csvInput:
                {
                    xml.WriteStartElement("CSV");

                    if (csvInput.HeaderUsage != HeaderUsage.Unknown)
                    {
                        xml.WriteElement("FileHeaderInfo", ValueHelper.EnumToString(csvInput.HeaderUsage));
                    }

                    if (csvInput.CommentCharacter != null)
                    {
                        xml.WriteElement("Comments", ConvertChar(csvInput.CommentCharacter));
                    }

                    if (csvInput.QuoteEscapeCharacter != null)
                    {
                        xml.WriteElement("QuoteEscapeCharacter", ConvertChar(csvInput.QuoteEscapeCharacter));
                    }

                    if (csvInput.RecordDelimiter != null)
                    {
                        xml.WriteElement("RecordDelimiter", ConvertChar(csvInput.RecordDelimiter));
                    }

                    if (csvInput.FieldDelimiter != null)
                    {
                        xml.WriteElement("FieldDelimiter", ConvertChar(csvInput.FieldDelimiter));
                    }

                    if (csvInput.QuoteCharacter != null)
                    {
                        xml.WriteElement("QuoteCharacter", ConvertChar(csvInput.QuoteCharacter));
                    }

                    if (csvInput.AllowQuotedRecordDelimiter != null)
                    {
                        xml.WriteElement("AllowQuotedRecordDelimiter", csvInput.AllowQuotedRecordDelimiter);
                    }

                    xml.WriteEndElement("CSV");

                    break;
                }

                case S3JsonInputFormat jsonInput:
                {
                    xml.WriteStartElement("JSON");

                    if (jsonInput.JsonType != JsonType.Unknown)
                    {
                        xml.WriteElement("Type", ValueHelper.EnumToString(jsonInput.JsonType));
                    }

                    xml.WriteEndElement("JSON");

                    break;
                }

                case S3ParquetInputFormat _:
                    xml.WriteElement("Parquet", string.Empty);
                    break;
                }

                xml.WriteEndElement("InputSerialization");
            }

            if (request.SelectParameters.ExpressionType != ExpressionType.Unknown)
            {
                xml.WriteElement("ExpressionType", ValueHelper.EnumToString(request.SelectParameters.ExpressionType));
            }

            xml.WriteElement("Expression", request.SelectParameters.Expression);

            xml.WriteStartElement("OutputSerialization");

            switch (request.SelectParameters.OutputFormat)
            {
            case S3CsvOutputFormat csvOutput:
                xml.WriteStartElement("CSV");

                if (csvOutput.FieldDelimiter != null)
                {
                    xml.WriteElement("FieldDelimiter", ConvertChar(csvOutput.FieldDelimiter));
                }

                if (csvOutput.QuoteCharacter != null)
                {
                    xml.WriteElement("QuoteCharacter", ConvertChar(csvOutput.QuoteCharacter));
                }

                if (csvOutput.QuoteEscapeCharacter != null)
                {
                    xml.WriteElement("QuoteEscapeCharacter", ConvertChar(csvOutput.QuoteEscapeCharacter));
                }

                if (csvOutput.QuoteFields != QuoteField.Unknown)
                {
                    xml.WriteElement("QuoteFields", ValueHelper.EnumToString(csvOutput.QuoteFields));
                }

                if (csvOutput.RecordDelimiter != null)
                {
                    xml.WriteElement("RecordDelimiter", ConvertChar(csvOutput.RecordDelimiter));
                }

                xml.WriteEndElement("CSV");

                break;

            case S3JsonOutputFormat jsonOutput:
                xml.WriteElement("RecordDelimiter", jsonOutput.RecordDelimiter);

                break;
            }

            xml.WriteEndElement("OutputSerialization");

            xml.WriteEndElement("SelectParameters");
        }

        if (request.OutputLocation != null)
        {
            xml.WriteStartElement("OutputLocation");
            xml.WriteStartElement("S3");

            //These two are required, so we don't check for null
            xml.WriteElement("BucketName", request.OutputLocation.BucketName);

            if (request.OutputLocation.Prefix != null)
            {
                xml.WriteElement("Prefix", request.OutputLocation.Prefix);
            }

            if (request.OutputLocation.StorageClass != StorageClass.Unknown)
            {
                xml.WriteElement("StorageClass", ValueHelper.EnumToString(request.OutputLocation.StorageClass));
            }

            if (request.OutputLocation.Acl != ObjectCannedAcl.Unknown)
            {
                xml.WriteElement("CannedACL", ValueHelper.EnumToString(request.OutputLocation.Acl));
            }

            //TODO: AccessControlList support

            if (request.OutputLocation.SseAlgorithm != SseAlgorithm.Unknown)
            {
                xml.WriteStartElement("Encryption");
                xml.WriteElement("EncryptionType", ValueHelper.EnumToString(request.OutputLocation.SseAlgorithm));

                string?context = request.OutputLocation.SseContext.Build();
                if (context != null)
                {
                    xml.WriteElement("KMSContext", context);
                }

                if (request.OutputLocation.SseKmsKeyId != null)
                {
                    xml.WriteElement("KMSKeyId", request.OutputLocation.SseKmsKeyId);
                }

                xml.WriteEndElement("Encryption");
            }

            List <KeyValuePair <string, string> > tags = request.OutputLocation.Tags.ToList();

            if (tags.Count > 0)
            {
                xml.WriteStartElement("Tagging");
                xml.WriteStartElement("TagSet");

                foreach (KeyValuePair <string, string> tag in tags)
                {
                    xml.WriteStartElement("Tag");
                    xml.WriteElement("Key", tag.Key);
                    xml.WriteElement("Value", tag.Value);
                    xml.WriteEndElement("Tag");
                }

                xml.WriteEndElement("TagSet");
                xml.WriteEndElement("Tagging");
            }

            List <KeyValuePair <string, string> > metadata = request.OutputLocation.Metadata.ToList();

            if (metadata.Count > 0)
            {
                xml.WriteStartElement("UserMetadata");

                foreach (KeyValuePair <string, string> meta in metadata)
                {
                    xml.WriteStartElement("MetadataEntry");
                    xml.WriteElement("Name", meta.Key);
                    xml.WriteElement("Value", meta.Value);
                    xml.WriteEndElement("MetadataEntry");
                }

                xml.WriteEndElement("UserMetadata");
            }

            xml.WriteEndElement("S3");

            xml.WriteEndElement("OutputLocation");
        }

        xml.WriteEndElement("RestoreRequest");

        return(new MemoryStream(xml.GetBytes()));
    }
Пример #20
0
        internal void EncodeNullValue(XmlStandard standard)
        {
            FastXmlWriter writer = new FastXmlWriter(50, standard);

            Assert.Throws <XmlException>(() => writer.WriteElement("Test", "Value\0Value"));
        }
Пример #21
0
        internal void EncodeDiscouragedValue(XmlStandard standard)
        {
            FastXmlWriter writer = new FastXmlWriter(50, standard);

            Assert.Throws <XmlException>(() => writer.WriteElement("Test", "Value" + '\u007F')); // \u007F is discouraged in both standards
        }