private List <Section> getSections(Stream stream) { var sections = new List <Section>(); var data = Misc.ToByteArray(stream); string content = Encoding.GetString(data); // The first line should contain the delimiter int delimiterEndIndex = content.IndexOf("\r\n"); if (delimiterEndIndex == -1) { return(sections); } delimiter = content.Substring(0, delimiterEndIndex); foreach (var sectionData in Misc.Split(data, Encoding.GetBytes(delimiter))) { var section = new Section() { AsBytes = sectionData, AsString = Encoding.GetString(sectionData) }; sections.Add(section); } return(sections); }
private void Parse(Stream stream, Encoding encoding) { this.Success = false; // Read the stream into a byte array byte[] data = Misc.ToByteArray(stream); // Copy to a string for header parsing string content = encoding.GetString(data); string name = string.Empty; string value = string.Empty; bool lookForValue = false; int charCount = 0; foreach (var c in content) { if (c == '=') { lookForValue = true; } else if (c == '&') { lookForValue = false; AddParameter(name, value); name = string.Empty; value = string.Empty; } else if (!lookForValue) { name += c; } else { value += c; } if (++charCount == content.Length) { AddParameter(name, value); break; } } // Get the start & end indexes of the file contents //int startIndex = nameMatch.Index + nameMatch.Length + "\r\n\r\n".Length; //Parameters.Add(name, s.Substring(startIndex).TrimEnd(new char[] { '\r', '\n' }).Trim()); // If some data has been successfully received, set success to true if (Parameters.Count != 0) { this.Success = true; } }
private void Parse(Stream stream, Encoding encoding) { this.Success = false; // Read the stream into a byte array byte[] data = Misc.ToByteArray(stream); // Copy to a string for header parsing string content = encoding.GetString(data); // The first line should contain the delimiter int delimiterEndIndex = content.IndexOf("\r\n"); if (delimiterEndIndex > -1) { string delimiter = content.Substring(0, content.IndexOf("\r\n")); string[] sections = content.Split(new string[] { delimiter }, StringSplitOptions.RemoveEmptyEntries); foreach (string s in sections) { if (s.Contains("Content-Disposition")) { // If we find "Content-Disposition", this is a valid multi-part section // Now, look for the "name" parameter Match nameMatch = new Regex(@"(?<=name\=\"")(.*?)(?=\"")").Match(s); string name = nameMatch.Value.Trim().ToLower(); if (name == FilePartName) { // Look for Content-Type Regex re = new Regex(@"(?<=Content\-Type:)(.*?)(?=\r\n\r\n)"); Match contentTypeMatch = re.Match(content); // Look for filename re = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")"); Match filenameMatch = re.Match(content); // Did we find the required values? if (contentTypeMatch.Success && filenameMatch.Success) { // Set properties this.ContentType = contentTypeMatch.Value.Trim(); this.Filename = filenameMatch.Value.Trim(); // Get the start & end indexes of the file contents int startIndex = contentTypeMatch.Index + contentTypeMatch.Length + "\r\n\r\n".Length; byte[] delimiterBytes = encoding.GetBytes("\r\n" + delimiter); int endIndex = Misc.IndexOf(data, delimiterBytes, startIndex); int contentLength = endIndex - startIndex; // Extract the file contents from the byte array byte[] fileData = new byte[contentLength]; Buffer.BlockCopy(data, startIndex, fileData, 0, contentLength); this.FileContents = fileData; } } #if !FOR_DOTNET4 else if (!IsNullOrWhiteSpace(name)) #else else if (!string.IsNullOrWhiteSpace(name)) #endif { // Get the start & end indexes of the file contents int startIndex = nameMatch.Index + nameMatch.Length + "\r\n\r\n".Length; Parameters.Add(name, s.Substring(startIndex).TrimEnd(new char[] { '\r', '\n' }).Trim()); } } } // If some data has been successfully received, set success to true if (FileContents != null || Parameters.Count != 0) { this.Success = true; } } }