private void Parse(Stream stream, Encoding encoding) { Success = false; // Read the stream into a byte array byte[] data = HttpMisc.ToByteArray(stream); // Copy to a string for header parsing var content = encoding.GetString(data); var name = string.Empty; var value = string.Empty; var lookForValue = false; var 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) { Success = true; } }
private void Parse(Stream stream, Encoding encoding) { Success = false; // Read the stream into a byte array var data = HttpMisc.ToByteArray(stream); // Copy to a string for header parsing var content = encoding.GetString(data); // The first line should contain the delimiter var delimiterEndIndex = content.IndexOf("\r\n", StringComparison.InvariantCultureIgnoreCase); if (delimiterEndIndex > -1) { var delimiter = content.Substring(0, content.IndexOf("\r\n", StringComparison.InvariantCultureIgnoreCase)); var sections = content.Split(new[] { 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 var re = new Regex(@"(?<=Content\-Type:)(.*?)(?=\r\n\r\n)"); var contentTypeMatch = re.Match(content); // Look for filename re = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")"); var filenameMatch = re.Match(content); // Did we find the required values? if (contentTypeMatch.Success && filenameMatch.Success) { // Set properties ContentType = contentTypeMatch.Value.Trim(); Filename = filenameMatch.Value.Trim(); // Get the start & end indexes of the file contents var startIndex = contentTypeMatch.Index + contentTypeMatch.Length + "\r\n\r\n".Length; var delimiterBytes = encoding.GetBytes("\r\n" + delimiter); var endIndex = HttpMisc.IndexOf(data, delimiterBytes, startIndex); var contentLength = endIndex - startIndex; // Extract the file contents from the byte array var fileData = new byte[contentLength]; Buffer.BlockCopy(data, startIndex, fileData, 0, contentLength); FileContents = fileData; } } else if (!string.IsNullOrWhiteSpace(name)) { // 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('\r', '\n').Trim()); } } } // If some data has been successfully received, set success to true if (FileContents != null || Parameters.Count != 0) { Success = true; } } }