IdHeaderList.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 10185: IdHeaderList.pas
  11. {
  12. { Rev 1.1 2/25/2003 12:47:34 PM JPMugaas
  13. { Updated with Hadi's fix. If complete boolean evaluation was on, the code
  14. { could sometimes with an index out of range.
  15. }
  16. {
  17. { Rev 1.0 2002.11.12 10:40:38 PM czhower
  18. }
  19. unit IdHeaderList;
  20. {
  21. 2002-Jan-27 Don Siders
  22. - Modified FoldLine to include Comma in break character set.
  23. 2000-May-31 J. Peter Mugaas
  24. - started this class to facilitate some work on Indy so we don't have to
  25. convert '=' to ":" and vice-versa just to use the Values property.
  26. }
  27. {
  28. NOTE: This is a modification of Borland's TStrings definition in a
  29. TStringList descendant. I had to conceal the original Values to do
  30. this since most of low level property setting routines aren't virtual
  31. and are private.
  32. }
  33. interface
  34. uses
  35. Classes;
  36. type
  37. TIdHeaderList = class(TStringList)
  38. protected
  39. FNameValueSeparator : String;
  40. FCaseSensitive : Boolean;
  41. FUnfoldLines : Boolean;
  42. FFoldLines : Boolean;
  43. FFoldLinesLength : Integer;
  44. //
  45. {This deletes lines which were folded}
  46. Procedure DeleteFoldedLines(Index : Integer);
  47. {This folds one line into several lines}
  48. function FoldLine(AString : string) : TStringList;
  49. {Folds lines and inserts them into a position, Index}
  50. procedure FoldAndInsert(AString : String; Index : Integer);
  51. {Name property get method}
  52. function GetName(Index: Integer): string;
  53. {Value property get method}
  54. function GetValue(const AName: string): string;
  55. {Value property set method}
  56. procedure SetValue(const Name, Value: string);
  57. {Gets a value from a string}
  58. function GetValueFromLine(ALine : Integer) : String;
  59. Function GetNameFromLine(ALine : Integer) : String;
  60. public
  61. procedure AddStdValues(ASrc: TStrings);
  62. procedure ConvertToStdValues(ADest: TStrings);
  63. constructor Create;
  64. { This method given a name specified by AName extracts all of the values for that name - and puts them in a new string
  65. list (just the values) one per line in the ADest TStrings.}
  66. procedure Extract(const AName: string; ADest: TStrings);
  67. { This property works almost exactly as Borland's IndexOfName except it uses
  68. our deliniator defined in NameValueSeparator }
  69. function IndexOfName(const AName: string): Integer; reintroduce;
  70. { This property works almost exactly as Borland's Values except it uses
  71. our deliniator defined in NameValueSeparator }
  72. property Names[Index: Integer]: string read GetName;
  73. { This property works almost exactly as Borland's Values except it uses
  74. our deliniator defined in NameValueSeparator }
  75. property Values[const Name: string]: string read GetValue write SetValue;
  76. { This is the separator we need to separate the name from the value }
  77. property NameValueSeparator : String read FNameValueSeparator
  78. write FNameValueSeparator;
  79. { Should the names be tested in a case-senstive manner. }
  80. property CaseSensitive : Boolean read FCaseSensitive write FCaseSensitive;
  81. { Should we unfold lines so that continuation header data is returned as
  82. well}
  83. property UnfoldLines : Boolean read FUnfoldLines write FUnfoldLines;
  84. { Should we fold lines we the Values(x) property is set with an
  85. assignment }
  86. property FoldLines : Boolean read FFoldLines write FFoldLines;
  87. { The Wrap position for our folded lines }
  88. property FoldLength : Integer read FFoldLinesLength write FFoldLinesLength;
  89. end;
  90. implementation
  91. uses
  92. IdGlobal,
  93. SysUtils;
  94. {This is taken from Borland's SysUtils and modified for our folding} {Do not Localize}
  95. function FoldWrapText(const Line, BreakStr: string; BreakChars: TSysCharSet;
  96. MaxCol: Integer): string;
  97. const
  98. QuoteChars = ['"']; {Do not Localize}
  99. var
  100. Col, Pos: Integer;
  101. LinePos, LineLen: Integer;
  102. BreakLen, BreakPos: Integer;
  103. QuoteChar, CurChar: Char;
  104. ExistingBreak: Boolean;
  105. begin
  106. Col := 1;
  107. Pos := 1;
  108. LinePos := 1;
  109. BreakPos := 0;
  110. QuoteChar := ' '; {Do not Localize}
  111. ExistingBreak := False;
  112. LineLen := Length(Line);
  113. BreakLen := Length(BreakStr);
  114. Result := ''; {Do not Localize}
  115. while Pos <= LineLen do
  116. begin
  117. CurChar := Line[Pos];
  118. if CurChar in LeadBytes then
  119. begin
  120. Inc(Pos);
  121. Inc(Col);
  122. end //if CurChar in LeadBytes then
  123. else
  124. if CurChar = BreakStr[1] then
  125. begin
  126. if QuoteChar = ' ' then {Do not Localize}
  127. begin
  128. ExistingBreak := AnsiSameText(BreakStr, Copy(Line, Pos, BreakLen));
  129. if ExistingBreak then
  130. begin
  131. Inc(Pos, BreakLen-1);
  132. BreakPos := Pos;
  133. end; //if ExistingBreak then
  134. end // if QuoteChar = ' ' then {Do not Localize}
  135. end // if CurChar = BreakStr[1] then
  136. else
  137. if CurChar in BreakChars then
  138. begin
  139. if QuoteChar = ' ' then {Do not Localize}
  140. BreakPos := Pos
  141. end // if CurChar in BreakChars then
  142. else
  143. if CurChar in QuoteChars then
  144. if CurChar = QuoteChar then
  145. QuoteChar := ' ' {Do not Localize}
  146. else
  147. if QuoteChar = ' ' then {Do not Localize}
  148. QuoteChar := CurChar;
  149. Inc(Pos);
  150. Inc(Col);
  151. if not (QuoteChar in QuoteChars) and (ExistingBreak or
  152. ((Col > MaxCol) and (BreakPos > LinePos))) then
  153. begin
  154. Col := Pos - BreakPos;
  155. Result := Result + Copy(Line, LinePos, BreakPos - LinePos + 1);
  156. if not (CurChar in QuoteChars) then
  157. while (Pos <= LineLen) and (Line[Pos] in BreakChars + [#13, #10]) do Inc(Pos);
  158. if not ExistingBreak and (Pos < LineLen) then
  159. Result := Result + BreakStr;
  160. Inc(BreakPos);
  161. LinePos := BreakPos;
  162. ExistingBreak := False;
  163. end; //if not
  164. end; //while Pos <= LineLen do
  165. Result := Result + Copy(Line, LinePos, MaxInt);
  166. end;
  167. { TIdHeaderList }
  168. procedure TIdHeaderList.AddStdValues(ASrc: TStrings);
  169. var
  170. i: integer;
  171. begin
  172. for i := 0 to ASrc.Count - 1 do begin
  173. Add(StringReplace(ASrc[i], '=', NameValueSeparator, [])); {Do not Localize}
  174. end;
  175. end;
  176. procedure TIdHeaderList.ConvertToStdValues(ADest: TStrings);
  177. var
  178. i: integer;
  179. begin
  180. for i := 0 to Count - 1 do begin
  181. ADest.Add(StringReplace(Strings[i], NameValueSeparator, '=', [])); {Do not Localize}
  182. end;
  183. end;
  184. constructor TIdHeaderList.Create;
  185. begin
  186. inherited Create;
  187. FNameValueSeparator := ': '; {Do not Localize}
  188. FCaseSensitive := False;
  189. FUnfoldLines := True;
  190. FFoldLines := True;
  191. { 78 was specified by a message draft available at
  192. http://www.imc.org/draft-ietf-drums-msg-fmt }
  193. FFoldLinesLength := 78;
  194. end;
  195. procedure TIdHeaderList.DeleteFoldedLines(Index: Integer);
  196. begin
  197. Inc(Index); {skip the current line}
  198. if Index < Count then begin
  199. while ( Index < Count ) and ( ( Length( Get( Index ) ) > 0) and
  200. ( Get( Index ) [ 1 ] = ' ' ) or ( Get( Index ) [ 1 ] = #9 ) ) do {Do not Localize}
  201. begin
  202. Delete( Index );
  203. end; //while
  204. end;
  205. end;
  206. procedure TIdHeaderList.Extract(const AName: string; ADest: TStrings);
  207. var idx : Integer;
  208. begin
  209. if not Assigned(ADest) then
  210. Exit;
  211. for idx := 0 to Count - 1 do
  212. begin
  213. if AnsiSameText(AName, GetNameFromLine(idx)) then
  214. begin
  215. ADest.Add(GetValueFromLine(idx));
  216. end;
  217. end;
  218. end;
  219. procedure TIdHeaderList.FoldAndInsert(AString : String; Index: Integer);
  220. var strs : TStringList;
  221. idx : Integer;
  222. begin
  223. strs := FoldLine( AString );
  224. try
  225. idx := strs.Count - 1;
  226. Put(Index, strs [ idx ] );
  227. {We decrement by one because we put the last string into the HeaderList}
  228. Dec( idx );
  229. while ( idx > -1 ) do
  230. begin
  231. Insert(Index, strs [ idx ] );
  232. Dec( idx );
  233. end;
  234. finally
  235. FreeAndNil( strs );
  236. end; //finally
  237. end;
  238. function TIdHeaderList.FoldLine(AString : string): TStringList;
  239. var s : String;
  240. begin
  241. Result := TStringList.Create;
  242. try
  243. {we specify a space so that starts a folded line}
  244. s := FoldWrapText(AString, EOL+' ', LWS+[','], FFoldLinesLength); {Do not Localize}
  245. while s <> '' do {Do not Localize}
  246. begin
  247. Result.Add( TrimRight( Fetch( s, EOL ) ) );
  248. end; // while s <> '' do {Do not Localize}
  249. finally
  250. end; //try..finally
  251. end;
  252. function TIdHeaderList.GetName(Index: Integer): string;
  253. var
  254. P: Integer;
  255. begin
  256. Result := Get( Index );
  257. P := IndyPos( FNameValueSeparator , Result );
  258. if P <> 0 then
  259. begin
  260. SetLength( Result, P - 1 );
  261. end // if P <> 0 then
  262. else
  263. begin
  264. SetLength( Result, 0 );
  265. end; // else if P <> 0 then
  266. Result := Result;
  267. end;
  268. function TIdHeaderList.GetNameFromLine(ALine: Integer): String;
  269. var p : Integer;
  270. begin
  271. Result := Get( ALine );
  272. if not FCaseSensitive then
  273. begin
  274. Result := UpperCase( Result );
  275. end; // if not FCaseSensitive then
  276. {We trim right to remove space to accomodate header errors such as
  277. Message-ID:<asdf@fdfs
  278. }
  279. P := IndyPos( TrimRight( FNameValueSeparator ), Result );
  280. Result := Copy( Result, 1, P - 1 );
  281. end;
  282. function TIdHeaderList.GetValue(const AName: string): string;
  283. begin
  284. Result := GetValueFromLine(IndexOfName(AName));
  285. end;
  286. function TIdHeaderList.GetValueFromLine(ALine: Integer): String;
  287. var
  288. LFoldedLine: string;
  289. LName: string;
  290. begin
  291. if (ALine >= 0) and (ALine < Count) then begin
  292. LName := GetNameFromLine(ALine);
  293. Result := Copy(Get(ALine), Length(LName) + 2, MaxInt);
  294. if FUnfoldLines then begin
  295. while True do begin
  296. Inc(ALine);
  297. if ALine = Count then begin
  298. Break;
  299. end;
  300. LFoldedLine := Get(ALine);
  301. // s[1] is safe since header lines cannot be empty as that causes then end of the header block
  302. if not (LFoldedLine[1] in LWS) then begin
  303. Break;
  304. end;
  305. Result := Trim(Result) + ' ' + Trim(LFoldedLine); {Do not Localize}
  306. end;
  307. end;
  308. end else begin
  309. Result := ''; {Do not Localize}
  310. end;
  311. // User may be fetching an folded line diretly.
  312. Result := Trim(Result);
  313. end;
  314. function TIdHeaderList.IndexOfName(const AName: string): Integer;
  315. var
  316. i: Integer;
  317. begin
  318. Result := -1;
  319. for i := 0 to Count - 1 do begin
  320. if AnsiSameText(GetNameFromLine(i), AName) then begin
  321. Result := i;
  322. Break;
  323. end;
  324. end;
  325. end;
  326. procedure TIdHeaderList.SetValue(const Name, Value: string);
  327. var
  328. I: Integer;
  329. begin
  330. I := IndexOfName(Name);
  331. if Value <> '' then {Do not Localize}
  332. begin
  333. if I < 0 then
  334. begin
  335. I := Add( '' ); {Do not Localize}
  336. end; //if I < 0 then
  337. if FFoldLines then
  338. begin
  339. DeleteFoldedLines( I );
  340. FoldAndInsert( Name + FNameValueSeparator + Value, I );
  341. end
  342. else
  343. begin
  344. Put( I, Name + FNameValueSeparator + Value );
  345. end; //else..FFoldLines
  346. end //if Value <> '' then {Do not Localize}
  347. else
  348. begin
  349. if I >= 0 then
  350. begin
  351. if FFoldLines then
  352. begin
  353. DeleteFoldedLines( I );
  354. end;
  355. Delete( I );
  356. end; //if I >= 0 then
  357. end; //else .. if Value <> '' then {Do not Localize}
  358. end;
  359. end.