private static string ParseStouReply(FTPReply reply) { string fileName; int i = reply.Message.LastIndexOf(' '); if (i < 0) throw new FTPProtocolException(reply); fileName = reply.Message.Substring(i + 1, reply.Message.Length - i - 2); return fileName; }
private FTPReply GetReply() { try { FTPReply reply = new FTPReply(); bool replyDone = false; do { string replyLine = ctrlSr.ReadLine(); Match m = Regex.Match(replyLine, @"^([0-9]{3})([\s\-])(.*)$"); if (m.Success) { int code = int.Parse(m.Groups[1].Value); string messageLine = m.Groups[3].Value; replyDone = (m.Groups[2].Value == " "); if (reply.Code == 0) { reply.Code = code; reply.Message = messageLine; } else // Multiline message { if (reply.Code != code) throw new FTPReplyParseException(replyLine); reply.Message += "\r\n" + messageLine; } } else // Multiline message { if (reply.Code == 0) throw new FTPReplyParseException(replyLine); reply.Message += "\r\n" + replyLine.TrimStart(); } } while (!replyDone); waitingCompletionReply = (reply.Code < 200); if (reply.Code >= 400) throw new FTPCommandException(reply); return reply; } catch (Exception) { waitingCompletionReply = false; throw; } }
private static IPEndPoint ParsePasvReply(FTPReply reply) { IPEndPoint dataEndPoint; int i = reply.Message.IndexOf('('); if (i < 0) throw new FTPProtocolException(reply); int j = reply.Message.IndexOf(')', i + 1); if (j < 0) throw new FTPProtocolException(reply); string[] parts = reply.Message.Substring(i + 1, j - i - 1).Split(','); if (parts.Length != 6) throw new FTPProtocolException(reply); byte[] addr = new byte[4]; for (i = 0; i < addr.Length; i++) addr[i] = byte.Parse(parts[i]); int port = byte.Parse(parts[4]) * 256 + byte.Parse(parts[5]); dataEndPoint = new IPEndPoint(new IPAddress(addr), port); return dataEndPoint; }
private static string ParsePasswordReply(FTPReply reply) { int i = reply.Message.IndexOf('\"'); if (i < 0) throw new FTPProtocolException(reply); int j = reply.Message.IndexOf('\"', i + 1); if (j < 0) throw new FTPProtocolException(reply); string dirName = reply.Message.Substring(i + 1, j - i - 1); return dirName; }
public FTPProtocolException(FTPReply reply) : base("Invalid FTP protocol reply: " + reply.ToString()) { this.reply = reply; }
public FTPCommandException(FTPReply reply) : base(reply.Message) { this.errorCode = reply.Code; }