SolidSnakeRU
Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору Было: Код: procedure TDSHTTPRequestWebBroker.UpdateAuthStrings; function Trim(const S: AnsiString): AnsiString; var I, L: Integer; begin L := Length(S); I := 1; while (I <= L) and (S[I] <= ' ') do Inc(I); if I > L then Result := '' else begin while S[L] <= ' ' do Dec(L); Result := Copy(S, I, L - I + 1); end; end; var LAuthorization: AnsiString; LDecodedAuthorization: AnsiString; LPos: NativeInt; P: PAnsiChar; begin if not FHaveAuth then begin FHaveAuth := True; LAuthorization := (FRequestInfo.Authorization); if System.AnsiStrings.AnsiStrComp(PAnsiChar(Copy(LAuthorization, 1, 5)), 'Basic') = 0 then begin LDecodedAuthorization := Base64DecodeToString(Trim(Copy(LAuthorization, 6, MaxInt))); P := System.AnsiStrings.AnsiStrScan(PAnsiChar(LDecodedAuthorization), ':'); if P <> nil then begin LPos := P - PAnsiChar(LDecodedAuthorization); FAuthUserName := Copy(LDecodedAuthorization, 1, LPos); FAuthPassword := Copy(LDecodedAuthorization, LPos+2, MaxInt); end; end; end; end; | С поддержкой UTF8 логина и пароля: Код: procedure TDSHTTPRequestWebBroker.UpdateAuthStrings; var LAuthorization: String; LDecodedAuthorization: String; P: integer; begin if not FHaveAuth then begin FHaveAuth := True; LAuthorization := string(FRequestInfo.Authorization); // Строка в BASE64, гасим ворнинг без последствий if SameText(Copy(LAuthorization, 1, 5), 'Basic') then begin LDecodedAuthorization := TIdDecoderMIME.DecodeString( Trim( Copy(LAuthorization, 6, MaxInt) ), IndyTextEncoding_UTF8 ); P := Pos(':', LDecodedAuthorization); if P <> 0 then begin FAuthUserName := Copy(LDecodedAuthorization, 1, P-1); FAuthPassword := IfThen(Length(LDecodedAuthorization) > p, Copy(LDecodedAuthorization, P+1, length(LDecodedAuthorization) - P), ''); end; end; end; end; | Можно и короче написать, если TStrings использовать с делимитером ":". | Всего записей: 248 | Зарегистр. 27-08-2008 | Отправлено: 19:40 12-12-2014 | Исправлено: SolidSnakeRU, 23:55 12-12-2014 |
|