public ClientBody ReadBody() { ClientBody CB = new ClientBody(); if (this.ContentLength > 0) { byte[] bodybytes = Reader.ReadBytes(this.ContentLength); byte[] boundarybytes = Encoding.UTF8.GetBytes("--" + this.boundaryMultipart); List <int> indexes = new List <int> (); int ct = 0; for (int i = 0; i < bodybytes.Length; ++i) { if (bodybytes[i] == boundarybytes[ct]) { ++ct; if (ct == boundarybytes.Length) { indexes.Add(i); ct = 0; } } else { ct = 0; } } List <byte[]> parts = new List <byte[]> (indexes.Count - 1); for (int i = 0; i < indexes.Count - 1; ++i) { int range = (indexes [i + 1] - indexes [i]) - (boundarybytes.Length + 4); byte[] part = new byte[range]; Buffer.BlockCopy(bodybytes, indexes[i] + 3, part, 0, range); parts.Add(part); } foreach (byte[] part in parts) { List <string> headerLines = new List <string> (); int index = 0; string line = String.Empty; char cb; do { cb = Convert.ToChar(part[index]); if (cb == '\r') { continue; } if (cb == '\n') { if (line == String.Empty) { break; } headerLines.Add(String.Copy(line)); line = String.Empty; } else { line += cb; } } while (index++ < part.Length); byte[] partbody = new byte[part.Length - index - 1]; Buffer.BlockCopy(part, index + 1, partbody, 0, partbody.Length); Dictionary <string, string> infodict = new Dictionary <string, string> (); MIME CT = null; foreach (string headerLine in headerLines) { string[] sline = headerLine.Split(new string[] { ": " }, 2, StringSplitOptions.None); switch (sline[0]) { case "Content-Disposition": foreach (KeyValuePair <string, string> KVP in sline[1].Split(new string[] { "; " }, StringSplitOptions.None).Select((s) => s.Split('=')).Where((sa) => sa.Length > 1).Select((ss) => new KeyValuePair <string, string>(ss.ElementAt(0), ss.ElementAt(1)))) { infodict.Add(KVP.Key.Trim('\"'), KVP.Value.Trim('\"')); } break; case "Content-Type": CT = MIME.FromText(sline[1]); break; default: break; } } if (CT == null) { string name; if (infodict.Count > 0 && infodict.TryGetValue("name", out name)) { CB.FormData.Add(name, Encoding.UTF8.GetString(partbody)); } } else { string name; string filename; if (infodict.Count > 1 && infodict.TryGetValue("name", out name) && infodict.TryGetValue("filename", out filename)) { CB.FormDataBodies.Add(name, new Tuple <string, IStreamableContent> (filename, new GeneratedResource(partbody, CT))); } } } return(CB); } else { return(null); } }
public abstract ClientReturn Post(ClientRequest CR, ClientBody CB);