Changeset 46 for trunk/UAcronym.pas
- Timestamp:
- Jul 15, 2016, 2:24:18 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UAcronym.pas
r45 r46 170 170 TImportSource = class 171 171 private 172 ResponseStream: TMemoryStream; 172 173 procedure DoPassword(Sender: TObject; var RepeatRequest : Boolean); 173 174 public … … 180 181 ItemCount: Integer; 181 182 Categories: TAcronymCategories; 183 UserName: string; 184 Password: string; 182 185 function DownloadHTTP(URL: string; Stream: TStream): Boolean; 183 186 procedure Process; … … 235 238 resourcestring 236 239 SWrongFileFormat = 'Wrong file format'; 240 SUnsupportedImportFormat = 'Unsupported import format'; 237 241 SDescription = 'Description'; 238 242 SMeaning = 'Meaning'; … … 366 370 367 371 procedure TImportSource.DoPassword(Sender: TObject; var RepeatRequest: Boolean); 368 begin 369 if TFPHttpClient(Sender).Password = '' then begin 370 TFPHttpClient(Sender).UserName := 'test'; 371 TFPHttpClient(Sender).Password := 'test'; 372 RepeatRequest := True; 373 end else RepeatRequest := False; 374 end; 372 var 373 H: string; 374 P: Integer; 375 begin 376 with TFPHttpClient(Sender) do begin 377 H := GetHeader(ResponseHeaders, 'WWW-Authenticate'); 378 if Pos(' ', H) > 0 then H := Copy(H, 1, Pos(' ', H) - 1); 379 380 if H <> 'Basic' then 381 raise Exception.Create('Unsupported HTTP authorization method'); 382 383 if (Self.UserName <> '') and (UserName = '') then begin 384 UserName := Self.UserName; 385 Password := Self.Password; 386 ResponseStream.Clear; 387 RepeatRequest := True; 388 end else RepeatRequest := False; 389 end; 390 end; 391 375 392 376 393 function TImportSource.DownloadHTTP(URL: string; Stream: TStream): Boolean; … … 379 396 FormData: TStringList; 380 397 begin 398 Result := False; 381 399 HTTPClient := TFPHttpClient.Create(nil); 400 HTTPClient.AllowRedirect := True; 382 401 HTTPClient.OnPassword := DoPassword; 383 402 FormData := TStringList.Create; 384 (* 385 FormData.action=clientlogin 386 &loginreturnurl=http://example.com/& 387 logintoken=29590a3037d325be70b93fb8258ed29257448cfb%2B%5C& 388 username=Bob& 389 password=secret& 390 rememberMe=1 391 HTTPClient.FormPost(URL, FormData, Stream); 392 *) 393 HTTPClient.Get(URL, Stream); 394 FormData.Free; 395 HTTPClient.Free; 396 Result := True; 403 try 404 HTTPClient.Get(URL, Stream); 405 Result := True; 406 finally 407 FormData.Free; 408 HTTPClient.Free; 409 end; 397 410 end; 398 411 … … 402 415 ifkParse: ProcessTextParse; 403 416 ifkMSAccess: ProcessMSAccess; 404 else raise Exception.Create( 'Unsupported import format');417 else raise Exception.Create(SUnsupportedImportFormat); 405 418 end; 406 419 end; … … 603 616 procedure TImportSource.ProcessTextParse; 604 617 var 605 Stream: TMemoryStream;606 618 S: string; 607 619 SS: string; … … 616 628 begin 617 629 ItemCount := 0; 618 Stream := TMemoryStream.Create;619 630 NewAcronym := TAcronymEntry.Create; 620 631 try 621 if DownloadHTTP(URL, Stream) then begin622 Stream.Position := 0;623 SetLength(S, Stream.Size);624 Stream.Read(S[1], Length(S));632 if DownloadHTTP(URL, ResponseStream) then begin 633 ResponseStream.Position := 0; 634 SetLength(S, ResponseStream.Size); 635 ResponseStream.Read(S[1], Length(S)); 625 636 626 637 // Find main block … … 705 716 finally 706 717 NewAcronym.Free; 707 Stream.Free;708 718 end; 709 719 end; … … 718 728 ItemCount := Source.ItemCount; 719 729 Categories.Assign(Source.Categories); 730 UserName := Source.UserName; 731 Password := Source.Password; 720 732 end; 721 733 … … 730 742 WriteBoolean(Node, 'Enabled', Enabled); 731 743 WriteInteger(Node, 'ItemCount', ItemCount); 744 WriteString(Node, 'UserName', UserName); 732 745 733 746 NewNode := Node.OwnerDocument.CreateElement('Categories'); … … 745 758 Enabled := ReadBoolean(Node, 'Enabled', True); 746 759 ItemCount := ReadInteger(Node, 'ItemCount', 0); 760 UserName := ReadString(Node, 'UserName', ''); 747 761 748 762 Categories.Db := Sources.AcronymDb; … … 758 772 Categories := TAcronymCategories.Create; 759 773 Categories.OwnsObjects := False; 774 ResponseStream := TMemoryStream.Create; 760 775 end; 761 776 762 777 destructor TImportSource.Destroy; 763 778 begin 779 FreeAndNil(ResponseStream); 764 780 FreeAndNil(Categories); 765 781 inherited Destroy;
Note:
See TracChangeset
for help on using the changeset viewer.