public object Decode(IChannel channel, PacketBuffer buffer) { var index = buffer.FindBytes(new byte[] {13, 10}); if (index == -1) return null; string data = _encoding.GetString(buffer.ReadBytes((int) index)); buffer.ReadBytes(2); return data; }
private void LoadPostData(string contentType, PacketBuffer buffer, string name = null) { if (contentType == null) { string data = buffer.ReadString((int) buffer.AvailableBytes()); if(name != null) _postData.Add(name, data); } else if (contentType == "application/x-www-form-urlencoded") { string stringData = buffer.ReadString((int) buffer.AvailableBytes()); foreach (string q in stringData.Split('&')) { int valueStartPoint = q.IndexOf("=", System.StringComparison.Ordinal); if (valueStartPoint == -1 && q.Length > valueStartPoint + 1) continue; _postData.Add(q.Substring(0, valueStartPoint), q.Substring(valueStartPoint + 1)); } } else if (contentType.StartsWith("multipart/form-data;")) { byte[] p = System.Text.Encoding.UTF8.GetBytes(string.Format("\r\n--{0}", contentType.Substring(contentType.IndexOf("boundary=") + 9))); long len = 0; while ((len = buffer.FindBytes(p)) > -1) { var buf = new PacketBuffer(); buf.WriteBytes(buffer.ReadBytes((int)len)); buffer.ReadBytes(p.Length); buf.BeginBufferIndex(); buf.ReadLine(); var hDictionary = new Dictionary<string, string>(); string h = ""; while((h = buf.ReadLine()) != "") { if (h == null) break; int valueStartIndex = h.IndexOf(": ", System.StringComparison.Ordinal); if (valueStartIndex == -1) continue; string key = h.Substring(0, valueStartIndex).ToLower(); string value = h.Substring(valueStartIndex + 2, h.Length - valueStartIndex - 2); hDictionary.Add(key, value); } if (hDictionary.Count != 0) { string pname = hDictionary["content-disposition"].Substring( hDictionary["content-disposition"].IndexOf("name") + 6); pname = pname.Substring(0, pname.Length - 1); string type = null; try { type = hDictionary["content-type"]; } catch (Exception) { } LoadPostData(type, buf, pname); } buf.Dispose(); } } else if (name != null) { string tempName = Path.GetTempFileName(); var stream = new FileStream(tempName, FileMode.OpenOrCreate, FileAccess.Write); byte[] data = new byte[1024]; int len; while((len = buffer.Read(data, 0, 1024)) > 0) stream.Write(data, 0, len); stream.Close(); ((Dictionary<string, string>)_postData["FILES"]).Add(name, tempName); } }
public object Decode(IChannel channel, PacketBuffer buffer) { var index = buffer.FindBytes(new byte[] {10, 13}); return index == -1 ? null : _encoding.GetString(buffer.ReadBytes((int)index)); }