IdReplyFTP.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.15 2/8/05 6:09:56 PM RLebeau
  18. Updated GetFormattedReply() to call Sys.IntToStr() only once.
  19. Rev 1.14 10/26/2004 10:39:54 PM JPMugaas
  20. Updated refs.
  21. Rev 1.13 8/8/04 12:28:04 AM RLebeau
  22. Bug fix for SetFormattedReply() to better conform to RFC 959
  23. Rev 1.12 6/20/2004 8:30:28 PM JPMugaas
  24. TIdReply was ignoring Formatted Output in some strings used in output.
  25. Rev 1.11 5/18/04 2:42:30 PM RLebeau
  26. Changed TIdRepliesFTP to derive from TIdRepliesRFC, and changed constructor
  27. back to using 'override'
  28. Rev 1.10 5/17/04 9:52:36 AM RLebeau
  29. Changed TIdRepliesFTP constructor to use 'reintroduce' instead
  30. Rev 1.9 5/16/04 5:27:56 PM RLebeau
  31. Added TIdRepliesFTP class
  32. Rev 1.8 2004.02.03 5:45:46 PM czhower
  33. Name changes
  34. Rev 1.7 2004.01.29 12:07:52 AM czhower
  35. .Net constructor problem fix.
  36. Rev 1.6 1/20/2004 10:03:26 AM JPMugaas
  37. Fixed a problem with a server where there was a line with only one " ". It
  38. was throwing things off. Fixed by checking to see if a line <4 chars is
  39. actually a number.
  40. Rev 1.5 1/3/2004 8:05:46 PM JPMugaas
  41. Bug fix: Sometimes, replies will appear twice due to the way functionality
  42. was enherited.
  43. Rev 1.4 10/26/2003 04:25:46 PM JPMugaas
  44. Fixed a bug where a line such as:
  45. " Version wu-2.6.2-11.73.1" would be considered the end of a command
  46. response.
  47. Rev 1.3 2003.10.18 9:42:12 PM czhower
  48. Boatload of bug fixes to command handlers.
  49. Rev 1.2 2003.09.20 10:38:38 AM czhower
  50. Bug fix to allow clearing code field (Return to default value)
  51. Rev 1.1 5/30/2003 9:23:44 PM BGooijen
  52. Changed TextCode to Code
  53. Rev 1.0 5/26/2003 12:21:10 PM JPMugaas
  54. }
  55. unit IdReplyFTP;
  56. interface
  57. {$i IdCompilerDefines.inc}
  58. uses
  59. Classes,
  60. IdReply,
  61. IdReplyRFC;
  62. type
  63. TIdReplyRFCFormat = (rfNormal, rfIndentMidLines);
  64. const
  65. DEF_ReplyFormat = rfNormal;
  66. type
  67. TIdReplyFTP = class(TIdReplyRFC)
  68. protected
  69. FReplyFormat : TIdReplyRFCFormat;
  70. function GetFormattedReply: TStrings; override;
  71. procedure SetFormattedReply(const AValue: TStrings); override;
  72. procedure AssignTo(ADest: TPersistent); override;
  73. public
  74. constructor CreateWithReplyTexts(ACollection: TCollection = nil; AReplyTexts: TIdReplies = nil); override;
  75. procedure Clear; override;
  76. procedure RaiseReplyError; override;
  77. class function IsEndMarker(const ALine: string): Boolean; override;
  78. class function IsEndReply(const AReplyCode, ALine: string): Boolean;
  79. published
  80. property ReplyFormat : TIdReplyRFCFormat read FReplyFormat write FReplyFormat default DEF_ReplyFormat;
  81. end;
  82. TIdRepliesFTP = class(TIdRepliesRFC)
  83. public
  84. constructor Create(AOwner: TPersistent); override;
  85. end;
  86. EIdFTPServiceNotAvailable = class(EIdReplyRFCError);
  87. implementation
  88. uses
  89. IdGlobal, SysUtils;
  90. { TIdReplyFTP }
  91. procedure TIdReplyFTP.AssignTo(ADest: TPersistent);
  92. var
  93. LR: TIdReplyFTP;
  94. begin
  95. if ADest is TIdReplyFTP then begin
  96. LR := TIdReplyFTP(ADest);
  97. //set code first as it possibly clears the reply
  98. LR.NumericCode := NumericCode;
  99. LR.ReplyFormat := ReplyFormat;
  100. LR.Text.Assign(Text);
  101. end else begin
  102. inherited AssignTo(ADest);
  103. end;
  104. end;
  105. constructor TIdReplyFTP.CreateWithReplyTexts(ACollection: TCollection = nil; AReplyTexts: TIdReplies = nil);
  106. begin
  107. inherited CreateWithReplyTexts(ACollection, AReplyTexts);
  108. FReplyFormat := DEF_ReplyFormat;
  109. end;
  110. procedure TIdReplyFTP.Clear;
  111. begin
  112. inherited Clear;
  113. // FReplyFormat := DEF_ReplyFormat;
  114. end;
  115. function TIdReplyFTP.GetFormattedReply: TStrings;
  116. var
  117. i : Integer;
  118. LCode: String;
  119. begin
  120. Result := GetFormattedReplyStrings;
  121. if NumericCode > 0 then begin
  122. LCode := IntToStr(NumericCode);
  123. if Text.Count > 0 then begin
  124. for i := 0 to Text.Count - 1 do begin
  125. if i < Text.Count - 1 then begin
  126. if FReplyFormat = rfIndentMidLines then begin
  127. if i = 0 then begin
  128. Result.Add(LCode + '-' + Text[i]);
  129. end else begin
  130. Result.Add(' ' + Text[i]);
  131. end;
  132. end else begin
  133. Result.Add(LCode + '-' + Text[i]);
  134. end;
  135. end else begin
  136. Result.Add(LCode + ' ' + Text[i]);
  137. end;
  138. end;
  139. end else begin
  140. Result.Add(LCode + ' ');
  141. end;
  142. end else if Text.Count > 0 then begin
  143. Result.AddStrings(Text);
  144. end;
  145. end;
  146. class function TIdReplyFTP.IsEndMarker(const ALine: string): Boolean;
  147. begin
  148. // Use copy not ALine[4] as it might not be long enough for that reference
  149. // to be valid
  150. // RLebeau 03/09/2009: noticed a Microsoft FTP server send multi-line
  151. // text that had a "+44" at the beginning of a line. That threw off
  152. // IdGlobal.IsNumeric(String) because the compiler's Val() did not
  153. // report an error for it. We will use the overloaded version of
  154. // IdGlobal.IsNumeric() now so that each character is validated
  155. // individually to prevent that from happening again.
  156. {
  157. Result := (Length(ALine) < 4) and IsNumeric(ALine);
  158. if Result then begin
  159. //" Version wu-2.6.2-11.73.1" is not a end of reply
  160. //"211 End of status" is the end of a reply
  161. Result := IsNumeric(ALine, 3) and CharEquals(ALine, 4, ' ');
  162. end;
  163. }
  164. Result := (Length(ALine) >= 3) and IsNumeric(ALine, 3);
  165. if Result then begin
  166. Result := (Length(ALine) = 3) or CharEquals(ALine, 4, ' ');
  167. end;
  168. end;
  169. class function TIdReplyFTP.IsEndReply(const AReplyCode, ALine: string): Boolean;
  170. begin
  171. Result := IsEndMarker(ALine) and TextIsSame(Copy(ALine, 1, 3), AReplyCode);
  172. end;
  173. procedure TIdReplyFTP.SetFormattedReply(const AValue: TStrings);
  174. var
  175. i: Integer;
  176. LCode, LTemp: string;
  177. begin
  178. Clear;
  179. if AValue.Count > 0 then begin
  180. // Get 4 chars - for POP3
  181. LCode := Trim(Copy(AValue[0], 1, 4));
  182. if CharEquals(LCode, 4, '-') then begin {do not localize}
  183. SetLength(LCode, 3);
  184. end;
  185. Code := LCode;
  186. Text.Add(Copy(AValue[0], Length(LCode)+2, MaxInt));
  187. FReplyFormat := rfNormal;
  188. if AValue.Count > 1 then begin
  189. for i := 1 to AValue.Count - 1 do begin
  190. // RLebeau - RFC 959 does not require the response code
  191. // to be prepended to every line like with other protocols.
  192. // Most FTP servers do this, but not all of them do, so
  193. // check here for that possibility ...
  194. if TextStartsWith(AValue[i], LCode) then begin
  195. LTemp := Copy(AValue[i], Length(LCode)+2, MaxInt);
  196. end else begin
  197. if TextStartsWith(AValue[i], ' ') then begin
  198. FReplyFormat := rfIndentMidLines;
  199. end;
  200. LTemp := TrimLeft(AValue[i]);
  201. end;
  202. Text.Add(LTemp);
  203. end;
  204. end;
  205. end;
  206. end;
  207. procedure TIdReplyFTP.RaiseReplyError;
  208. begin
  209. // any FTP command can return a 421 reply if the server is going to
  210. // shut down the command connection...
  211. if NumericCode = 421 then begin
  212. raise EIdFTPServiceNotAvailable.CreateError(NumericCode, Text.Text);
  213. end else begin
  214. inherited;
  215. end;
  216. end;
  217. { TIdRepliesFTP }
  218. constructor TIdRepliesFTP.Create(AOwner: TPersistent);
  219. begin
  220. inherited Create(AOwner, TIdReplyFTP);
  221. end;
  222. end.