IdConnectThroughHttpProxy.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.6 11/12/2004 11:31:06 AM JPMugaas
  18. IPv6 expansions.
  19. Rev 1.5 2004.02.03 5:45:00 PM czhower
  20. Name changes
  21. Rev 1.4 10/19/2003 11:48:12 AM DSiders
  22. Added localization comments.
  23. Rev 1.3 4/5/2003 7:27:48 PM BGooijen
  24. Checks for errors, added authorisation
  25. Rev 1.2 4/1/2003 4:14:22 PM BGooijen
  26. Fixed + cleaned up
  27. Rev 1.1 2/24/2003 08:20:46 PM JPMugaas
  28. Now should compile with new code.
  29. Rev 1.0 11/14/2002 02:16:10 PM JPMugaas
  30. }
  31. unit IdConnectThroughHttpProxy;
  32. {
  33. implements:
  34. http://www.web-cache.com/Writings/Internet-Drafts/draft-luotonen-web-proxy-tunneling-01.txt
  35. }
  36. interface
  37. {$i IdCompilerDefines.inc}
  38. uses
  39. Classes, IdCustomTransparentProxy, IdGlobal, IdIOHandler;
  40. type
  41. TIdConnectThroughHttpProxy = class(TIdCustomTransparentProxy)
  42. private
  43. FAuthorizationRequired: Boolean;
  44. protected
  45. FEnabled: Boolean;
  46. function GetEnabled: Boolean; override;
  47. procedure SetEnabled(AValue: Boolean); override;
  48. procedure MakeConnection(AIOHandler: TIdIOHandler; const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); override;
  49. procedure DoMakeConnection(AIOHandler: TIdIOHandler; const AHost: string;
  50. const APort: TIdPort; const ALogin:boolean; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);virtual;
  51. public
  52. procedure Assign(ASource: TPersistent); override;
  53. published
  54. property Enabled;
  55. property Host;
  56. property Port;
  57. property ChainedProxy;
  58. property Username;
  59. property Password;
  60. end;
  61. implementation
  62. uses
  63. IdCoderMIME, IdExceptionCore, IdHeaderList, IdGlobalProtocols, SysUtils;
  64. { TIdConnectThroughHttpProxy }
  65. procedure TIdConnectThroughHttpProxy.Assign(ASource: TPersistent);
  66. begin
  67. if ASource is TIdConnectThroughHttpProxy then begin
  68. FEnabled := TIdConnectThroughHttpProxy(ASource).Enabled;
  69. end;
  70. // always allow TIdCustomTransparentProxy to assign its properties as well
  71. inherited Assign(ASource);
  72. end;
  73. function TIdConnectThroughHttpProxy.GetEnabled: Boolean;
  74. begin
  75. Result := FEnabled;
  76. end;
  77. procedure TIdConnectThroughHttpProxy.DoMakeConnection(AIOHandler: TIdIOHandler;
  78. const AHost: string; const APort: TIdPort; const ALogin: Boolean; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);
  79. var
  80. LStatus: string;
  81. LResponseCode: Integer;
  82. LHeaders: TIdHeaderList;
  83. LContentLength: Int64;
  84. LEncoder: TIdEncoderMIME;
  85. begin
  86. LHeaders := TIdHeaderList.Create(QuoteHTTP);
  87. try
  88. AIOHandler.WriteLn(IndyFormat('CONNECT %s:%d HTTP/1.0', [AHost,APort])); {do not localize}
  89. if ALogin then begin
  90. LEncoder := TIdEncoderMIME.Create;
  91. try
  92. AIOHandler.WriteLn('Proxy-Authorization: Basic ' + LEncoder.Encode(Username + ':' + Password)); {do not localize}
  93. finally
  94. LEncoder.Free;
  95. end;
  96. end;
  97. AIOHandler.WriteLn;
  98. LStatus := AIOHandler.ReadLn;
  99. if LStatus <> '' then begin // if empty response then we assume it succeeded
  100. AIOHandler.Capture(LHeaders, '', False);
  101. // TODO: support chunked replies...
  102. LContentLength := IndyStrToInt64(LHeaders.Values['Content-Length'], -1); {do not localize}
  103. if LContentLength > 0 then begin
  104. AIOHandler.Discard(LContentLength);
  105. end;
  106. Fetch(LStatus);// to remove the http/1.0 or http/1.1
  107. LResponseCode := IndyStrToInt(Fetch(LStatus, ' ', False), 200); // if invalid response then we assume it succeeded
  108. if (LResponseCode = 407) and (not ALogin) and ((Length(Username) > 0) or (Length(Password) > 0)) then begin // authorization required
  109. if TextIsSame(LHeaders.Values['Proxy-Connection'], 'close') or {do not localize}
  110. TextIsSame(LHeaders.Values['Connection'], 'close') then begin {do not localize}
  111. // need to reconnect before trying again with login
  112. AIOHandler.Close;
  113. FAuthorizationRequired := True;
  114. try
  115. AIOHandler.Open;
  116. finally
  117. FAuthorizationRequired := False;
  118. end;
  119. end else begin
  120. // still connected so try again with login
  121. DoMakeConnection(AIOHandler, AHost, APort, True);
  122. end;
  123. end
  124. else if not (LResponseCode in [200]) then begin // maybe more responsecodes to add
  125. raise EIdHttpProxyError.Create(LStatus);//BGO: TODO: maybe split into more exceptions?
  126. end;
  127. end;
  128. finally
  129. LHeaders.Free;
  130. end;
  131. end;
  132. procedure TIdConnectThroughHttpProxy.MakeConnection(AIOHandler: TIdIOHandler;
  133. const AHost: string; const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION);
  134. begin
  135. DoMakeConnection(AIOHandler, AHost, APort, FAuthorizationRequired);
  136. end;
  137. procedure TIdConnectThroughHttpProxy.SetEnabled(AValue: Boolean);
  138. begin
  139. FEnabled := AValue;
  140. end;
  141. end.