public void ShouldHandleEmptyContent() { Encoding encoding = Encoding.UTF8; byte[] binaryContent = encoding.GetBytes(String.Empty); MemoryStream bodyStream = new MemoryStream(binaryContent); StreamingMultiPartParser parser = new StreamingMultiPartParser(bodyStream, encoding, "AaB03x"); parser.PreambleFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.SectionFound += (o, e) => Assert.Fail("No sections should be found if the contents are empty."); parser.EpilogueFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.Parse().Wait(); }
public void ShouldHandlePreambleOnly() { Encoding encoding = Encoding.UTF8; const string preamble = "This is an invalid message containing only a preamble."; byte[] binaryContent = encoding.GetBytes(preamble); MemoryStream bodyStream = new MemoryStream(binaryContent); StreamingMultiPartParser parser = new StreamingMultiPartParser(bodyStream, encoding, "AaB03x"); parser.PreambleFound += (o, e) => Assert.AreEqual(preamble, toString(encoding, e)); parser.SectionFound += (o, e) => Assert.Fail("No sections should be found if the contents are empty."); parser.EpilogueFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.Parse().Wait(); }
public void ShouldHandleEndBoundaryOnly() { Encoding encoding = Encoding.UTF8; const string boundary = "ABC123abc456"; const string contents = "--" + boundary + "--"; byte[] binaryContent = encoding.GetBytes(contents); MemoryStream bodyStream = new MemoryStream(binaryContent); StreamingMultiPartParser parser = new StreamingMultiPartParser(bodyStream, encoding, boundary); parser.PreambleFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.SectionFound += (o, e) => Assert.Fail("No sections should be returned if the content is just the end boundary."); parser.EpilogueFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.Parse().Wait(); }
public void ShouldHandlePartialSection() { Encoding encoding = Encoding.UTF8; const string contents = "--" + boundary + @" " + formDataHeaderName1 + ": " + formDataHeaderValue1 + @" "; byte[] binaryContent = encoding.GetBytes(contents); MemoryStream bodyStream = new MemoryStream(binaryContent); StreamingMultiPartParser parser = new StreamingMultiPartParser(bodyStream, encoding, boundary); parser.PreambleFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.SectionFound += (o, e) => Assert.Fail("No sections should be returned if the content is just the end boundary."); parser.EpilogueFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.Parse().Wait(); }
public void ShouldHandlePreambleWithStartBoundary() { Encoding encoding = Encoding.UTF8; const string boundary = "ABC123abc456"; const string thisPreamble = "This is an invalid message containing only a preamble."; const string contents = thisPreamble + @" --" + boundary; byte[] binaryContent = encoding.GetBytes(contents); MemoryStream bodyStream = new MemoryStream(binaryContent); StreamingMultiPartParser parser = new StreamingMultiPartParser(bodyStream, encoding, boundary); parser.PreambleFound += (o, e) => Assert.AreEqual(thisPreamble, toString(encoding, e)); parser.SectionFound += (o, e) => Assert.Fail("No sections should be returned because the message ends prematurely."); parser.EpilogueFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.Parse().Wait(); }
public void ShouldHandleMultiPartMixedContent() { Encoding encoding = Encoding.UTF8; byte[] binaryContent = encoding.GetBytes(getMultiPartMixedContents()); MemoryStream bodyStream = new MemoryStream(binaryContent); List <MultiPartSection> sections = new List <MultiPartSection>(); StreamingMultiPartParser parser = new StreamingMultiPartParser(bodyStream, encoding, "AaB03x"); parser.PreambleFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.SectionFound += (o, e) => sections.Add(e); parser.EpilogueFound += (o, e) => Assert.AreEqual(String.Empty, toString(encoding, e)); parser.Parse().Wait(); Assert.AreEqual(3, sections.Count, "The wrong number of sections were found."); Assert.AreEqual("Larry", toString(encoding, sections[0].Content), "The first section content was wrong."); Assert.AreEqual("... contents of file1.txt ...", toString(encoding, sections[1].Content), "The second section content was wrong."); Assert.AreEqual("...contents of file2.gif...", toString(encoding, sections[2].Content), "The third section content was wrong."); }
public void ShouldParseMultiPartFile() { Encoding encoding = Encoding.UTF8; byte[] binaryContent = encoding.GetBytes(getMultiPartContents()); MemoryStream bodyStream = new MemoryStream(binaryContent); List <MultiPartSection> sections = new List <MultiPartSection>(); StreamingMultiPartParser parser = new StreamingMultiPartParser(bodyStream, encoding, boundary); string parsedPreamble = null; string parsedEpilogue = null; parser.PreambleFound += (o, e) => parsedPreamble = toString(encoding, e); parser.SectionFound += (o, e) => sections.Add(e); parser.EpilogueFound += (o, e) => parsedEpilogue = toString(encoding, e); parser.Parse().Wait(); Assert.AreEqual(preamble, parsedPreamble, "The preample was not read correctly."); Assert.AreEqual(2, sections.Count, "Two sections should have been parsed from the body."); var section1 = sections[0]; Assert.AreEqual(formDataHeaderValue1, section1.Headers[formDataHeaderName1], "The first header was not parsed."); string parsedFormData = toString(encoding, section1.Content); Assert.AreEqual(formData, parsedFormData, "The form data was not read correctly."); var section2 = sections[1]; Assert.AreEqual(formDataHeaderValue2, section2.Headers[formDataHeaderName2], "The second header was not parsed."); Assert.AreEqual(formDataHeaderValue3, section2.Headers[formDataHeaderName3], "The third header was not parsed."); string parsedFileData = toString(encoding, section2.Content); Assert.AreEqual(fileData, parsedFileData, "The file data was not read correctly."); Assert.AreEqual(epilogue, parsedEpilogue, "The epilogue was not parsed."); }
public void Extract(HttpListenerRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (!request.HasEntityBody) { return; } // Make sure it is a multi-part request string[] parts = request.ContentType.Split(';').Select(s => s.Trim()).ToArray(); if (!parts[0].Equals("multipart/form-data", StringComparison.InvariantCultureIgnoreCase)) { return; } // Parse the content type parameters var contentTypeParameters = parts .Skip(1) .Select(p => p.Split(new char[] { '=' }, 2)) .Where(p => p.Length == 2) .ToLookup(p => p[0], p => p[1], StringComparer.InvariantCultureIgnoreCase); // Check the boundary is specified, and only once if (contentTypeParameters["boundary"].Count() != 1) { return; } string boundary = contentTypeParameters["boundary"].First(); using (Stream responseStream = request.InputStream) { Encoding encoding = request.ContentEncoding; StreamingMultiPartParser parser = new StreamingMultiPartParser(responseStream, encoding, boundary); parser.SectionFound += (o, e) => { var data = getSectionData(e); if (data == null) { return; } if (String.IsNullOrWhiteSpace(data.FileName)) { string value = encoding.GetString(data.Contents); this.parameters.Add(data.Name, value); } else { var file = new MultiPartFile() { Name = data.Name, FileName = data.FileName, ContentType = data.ContentType, Contents = data.Contents }; this.files.Add(file.Name, file); } }; parser.Parse().Wait(); } }
public void Extract(HttpListenerRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (!request.HasEntityBody) { return; } // Make sure it is a multi-part request string[] parts = request.ContentType.Split(';').Select(s => s.Trim()).ToArray(); if (!parts[0].Equals("multipart/form-data", StringComparison.InvariantCultureIgnoreCase)) { return; } // Parse the content type parameters var contentTypeParameters = parts .Skip(1) .Select(p => p.Split(new char[] { '=' }, 2)) .Where(p => p.Length == 2) .ToLookup(p => p[0], p => p[1], StringComparer.InvariantCultureIgnoreCase); // Check the boundary is specified, and only once if (contentTypeParameters["boundary"].Count() != 1) { return; } string boundary = contentTypeParameters["boundary"].First(); using (Stream responseStream = request.InputStream) { Encoding encoding = request.ContentEncoding; StreamingMultiPartParser parser = new StreamingMultiPartParser(responseStream, encoding, boundary); parser.SectionFound += (o, e) => { var data = getSectionData(e); if (data == null) { return; } if (String.IsNullOrWhiteSpace(data.FileName)) { string value = encoding.GetString(data.Contents); this.parameters.Add(data.Name, value); } else { var file = new MultiPartFile() { Name = data.Name, FileName = data.FileName, ContentType = data.ContentType, Contents = data.Contents }; this.files.Add(file.Name, file); } }; parser.Parse().Wait(); } }