#1 2016-05-17 08:47:51

Sabbiolina
Member
Registered: 2014-05-20
Posts: 120

Allow configuring Proxy Username/Password on TWinHTTP

Hi,
I noticed that the part of the user / pass authentication for HTTP proxy is not present for TwinHttp.

I had made these changes a few months ago:


unit SynCrtSock;


add  proxy_username and proxy_password

  THttpRequestExtendedOptions = record
    /// let HTTPS be less paranoid about SSL certificates
    // - IgnoreSSLCertificateErrors is handled by TWinHttp and TCurlHTTP
    IgnoreSSLCertificateErrors: Boolean;

    proxy_username,proxy_password:ansistring;  // add this
    /// allow HTTP authentication to take place at connection
    // - Auth.Scheme and UserName/Password properties are handled
    // by the TWinHttp class only by now
    Auth: record
      UserName: SynUnicode;
      Password: SynUnicode;
      Scheme: THttpRequestAuthentication;
    end;
  end;

THttpRequest

add these properties:

    property Proxy_username: ansistring // ansistring is
      read fExtendedOptions.Proxy_username
      write fExtendedOptions.Proxy_username;

    property Proxy_password: ansistring
      read fExtendedOptions.Proxy_password
      write fExtendedOptions.Proxy_password;

go to :
procedure TWinHTTP.InternalSendRequest(const aData: SockString);

add consts

const
  WINHTTP_OPTION_PROXY_USERNAME = $1002;
  WINHTTP_OPTION_PROXY_PASSWORD = $1003;

Add calls here:

procedure TWinHTTP.InternalSendRequest(const aData: SockString);
var L: integer;
    winAuth: DWORD;
begin
  with fExtendedOptions do
  if AuthScheme<>wraNone then begin
    case AuthScheme of
    wraBasic: winAuth := WINHTTP_AUTH_SCHEME_BASIC;
    wraDigest: winAuth := WINHTTP_AUTH_SCHEME_DIGEST;
    wraNegotiate: winAuth := WINHTTP_AUTH_SCHEME_NEGOTIATE;
    else raise EWinHTTP.CreateFmt('Unsupported AuthScheme=%d',[ord(AuthScheme)]);
    end;
    if not WinHttpSetCredentials(fRequest,WINHTTP_AUTH_TARGET_SERVER,
       winAuth,pointer(AuthUserName),pointer(AuthPassword),nil) then
      RaiseLastModuleError(winhttpdll,EWinHTTP);
	  
// add this:	  
   if PROXY_USERNAME<>'' then
   begin
    if not WinHttpSetOption( fRequest, WINHTTP_OPTION_PROXY_USERNAME, PWidechar(WideString(PROXY_USERNAME)), length(PROXY_USERNAME)) then
     RaiseLastModuleError(winhttpdll,EWinHTTP);
    end;

   if PROXY_PASSWORD<>'' then
   begin
    if not WinHttpSetOption( fRequest, WINHTTP_OPTION_PROXY_PASSWORD, PWidechar(WideString(PROXY_PASSWORD)), length(PROXY_PASSWORD)) then
    RaiseLastModuleError(winhttpdll,EWinHTTP);
   end;	 
  end;
 // end add
  
  if fHTTPS and IgnoreSSLCertificateErrors then
    if not WinHttpSetOption(fRequest, WINHTTP_OPTION_SECURITY_FLAGS,
       @SECURITY_FLAT_IGNORE_CERTIFICATES, SizeOf(SECURITY_FLAT_IGNORE_CERTIFICATES)) then
      RaiseLastModuleError(winhttpdll,EWinHTTP);
  L := length(aData);
  if not WinHttpSendRequest(fRequest, nil, 0, pointer(aData), L, L, 0) or
     not WinHttpReceiveResponse(fRequest,nil) then
    RaiseLastModuleError(winhttpdll,EWinHTTP);
end;

Offline

Board footer

Powered by FluxBB