Changeset 46 for trunk/UAcronym.pas


Ignore:
Timestamp:
Jul 15, 2016, 2:24:18 PM (8 years ago)
Author:
chronos
Message:
  • Added: Now user name and password can be specified for import sources which require user authentication. Password is not stored in project file for safety reasons. Only Basic authentication is supported.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UAcronym.pas

    r45 r46  
    170170  TImportSource = class
    171171  private
     172    ResponseStream: TMemoryStream;
    172173    procedure DoPassword(Sender: TObject; var RepeatRequest : Boolean);
    173174  public
     
    180181    ItemCount: Integer;
    181182    Categories: TAcronymCategories;
     183    UserName: string;
     184    Password: string;
    182185    function DownloadHTTP(URL: string; Stream: TStream): Boolean;
    183186    procedure Process;
     
    235238resourcestring
    236239  SWrongFileFormat = 'Wrong file format';
     240  SUnsupportedImportFormat = 'Unsupported import format';
    237241  SDescription = 'Description';
    238242  SMeaning = 'Meaning';
     
    366370
    367371procedure 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;
     372var
     373  H: string;
     374  P: Integer;
     375begin
     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;
     390end;
     391
    375392
    376393function TImportSource.DownloadHTTP(URL: string; Stream: TStream): Boolean;
     
    379396  FormData: TStringList;
    380397begin
     398  Result := False;
    381399  HTTPClient := TFPHttpClient.Create(nil);
     400  HTTPClient.AllowRedirect := True;
    382401  HTTPClient.OnPassword := DoPassword;
    383402  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;
    397410end;
    398411
     
    402415    ifkParse: ProcessTextParse;
    403416    ifkMSAccess: ProcessMSAccess;
    404     else raise Exception.Create('Unsupported import format');
     417    else raise Exception.Create(SUnsupportedImportFormat);
    405418  end;
    406419end;
     
    603616procedure TImportSource.ProcessTextParse;
    604617var
    605   Stream: TMemoryStream;
    606618  S: string;
    607619  SS: string;
     
    616628begin
    617629  ItemCount := 0;
    618   Stream := TMemoryStream.Create;
    619630  NewAcronym := TAcronymEntry.Create;
    620631  try
    621     if DownloadHTTP(URL, Stream) then begin
    622       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));
    625636
    626637      // Find main block
     
    705716  finally
    706717    NewAcronym.Free;
    707     Stream.Free;
    708718  end;
    709719end;
     
    718728  ItemCount := Source.ItemCount;
    719729  Categories.Assign(Source.Categories);
     730  UserName := Source.UserName;
     731  Password := Source.Password;
    720732end;
    721733
     
    730742  WriteBoolean(Node, 'Enabled', Enabled);
    731743  WriteInteger(Node, 'ItemCount', ItemCount);
     744  WriteString(Node, 'UserName', UserName);
    732745
    733746  NewNode := Node.OwnerDocument.CreateElement('Categories');
     
    745758  Enabled := ReadBoolean(Node, 'Enabled', True);
    746759  ItemCount := ReadInteger(Node, 'ItemCount', 0);
     760  UserName := ReadString(Node, 'UserName', '');
    747761
    748762  Categories.Db := Sources.AcronymDb;
     
    758772  Categories := TAcronymCategories.Create;
    759773  Categories.OwnsObjects := False;
     774  ResponseStream := TMemoryStream.Create;
    760775end;
    761776
    762777destructor TImportSource.Destroy;
    763778begin
     779  FreeAndNil(ResponseStream);
    764780  FreeAndNil(Categories);
    765781  inherited Destroy;
Note: See TracChangeset for help on using the changeset viewer.