IdHTTPProxyServer.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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.24 10/14/2004 1:45:32 PM BGooijen
  18. Beauty fixes ;)
  19. Rev 1.23 10/14/2004 1:05:48 PM BGooijen
  20. set PerformReply to false, else "200 OK" was added behind the document body
  21. Rev 1.22 09.08.2004 09:30:00 OMonien
  22. changed disconnect handling. Previous implementation failed when exceptions
  23. ocured in command handler.
  24. Rev 1.21 08.08.2004 10:35:56 OMonien
  25. Greeting removed
  26. Rev 1.20 6/11/2004 9:36:28 AM DSiders
  27. Added "Do not Localize" comments.
  28. Rev 1.19 2004.05.20 1:39:24 PM czhower
  29. Last of the IdStream updates
  30. Rev 1.18 2004.05.20 11:37:20 AM czhower
  31. IdStreamVCL
  32. Rev 1.17 4/19/2004 7:07:38 PM BGooijen
  33. the remote headers are now passed to the OnHTTPDocument event
  34. Rev 1.16 4/18/2004 11:31:26 PM BGooijen
  35. Fixed POST
  36. Build CONNECT
  37. fixed some bugs where chars were replaced when that was not needed ( thus
  38. causing corrupt data )
  39. Rev 1.15 2004.04.13 10:24:24 PM czhower
  40. Bug fix for when user changes stream.
  41. Rev 1.14 2004.02.03 5:45:12 PM czhower
  42. Name changes
  43. Rev 1.13 1/21/2004 2:42:52 PM JPMugaas
  44. InitComponent
  45. Rev 1.12 10/25/2003 06:52:12 AM JPMugaas
  46. Updated for new API changes and tried to restore some functionality.
  47. Rev 1.11 2003.10.24 10:43:10 AM czhower
  48. TIdSTream to dos
  49. Rev 1.10 10/17/2003 12:10:08 AM DSiders
  50. Added localization comments.
  51. Rev 1.9 2003.10.12 3:50:44 PM czhower
  52. Compile todos
  53. Rev 1.8 7/13/2003 7:57:38 PM SPerry
  54. fixed problem with commandhandlers
  55. Rev 1.6 5/25/2003 03:54:42 AM JPMugaas
  56. Rev 1.5 2/24/2003 08:56:50 PM JPMugaas
  57. Rev 1.4 1/20/2003 1:15:44 PM BGooijen
  58. Changed to TIdTCPServer / TIdCmdTCPServer classes
  59. Rev 1.3 1-14-2003 19:19:22 BGooijen
  60. The first line of the header was sent to the server twice, fixed that.
  61. Rev 1.2 1-1-2003 21:52:06 BGooijen
  62. Changed for TIdContext
  63. Rev 1.1 12-29-2002 13:00:02 BGooijen
  64. - Works on Indy 10 now
  65. - Cleaned up some code
  66. Rev 1.0 2002.11.22 8:37:50 PM czhower
  67. Rev 1.0 2002.11.22 8:37:16 PM czhower
  68. 10-May-2002: Created Unit.
  69. }
  70. unit IdHTTPProxyServer;
  71. interface
  72. {$i IdCompilerDefines.inc}
  73. {
  74. Indy HTTP proxy Server
  75. Original Programmer: Bas Gooijen ([email protected])
  76. Current Maintainer: Bas Gooijen
  77. Code is given to the Indy Pit Crew.
  78. Modifications by Chad Z. Hower (Kudzu)
  79. }
  80. uses
  81. Classes,
  82. IdAssignedNumbers,
  83. IdGlobal,
  84. IdHeaderList,
  85. IdTCPConnection,
  86. IdCustomTCPServer, //for TIdServerContext
  87. IdCmdTCPServer,
  88. IdCommandHandlers,
  89. IdContext,
  90. IdYarn;
  91. const
  92. IdPORT_HTTPProxy = 8080;
  93. type
  94. TIdHTTPProxyTransferMode = ( tmFullDocument, tmStreaming );
  95. TIdHTTPProxyTransferSource = ( tsClient, tsServer );
  96. TIdHTTPProxyServerContext = class(TIdServerContext)
  97. protected
  98. FHeaders: TIdHeaderList;
  99. FCommand: String;
  100. FDocument: String;
  101. FOutboundClient: TIdTCPConnection;
  102. FTarget: String;
  103. FTransferMode: TIdHTTPProxyTransferMode;
  104. FTransferSource: TIdHTTPProxyTransferSource;
  105. public
  106. constructor Create(AConnection: TIdTCPConnection; AYarn: TIdYarn; AList: TIdContextThreadList = nil); override;
  107. destructor Destroy; override;
  108. property Headers: TIdHeaderList read FHeaders;
  109. property Command: String read FCommand;
  110. property Document: String read FDocument;
  111. property OutboundClient: TIdTCPConnection read FOutboundClient;
  112. property Target: String read FTarget;
  113. property TransferMode: TIdHTTPProxyTransferMode read FTransferMode write FTransferMode;
  114. property TransferSource: TIdHTTPProxyTransferSource read FTransferSource;
  115. end;
  116. TIdHTTPProxyServer = class;
  117. TOnHTTPContextEvent = procedure(AContext: TIdHTTPProxyServerContext) of object;
  118. TOnHTTPDocument = procedure(AContext: TIdHTTPProxyServerContext; var VStream: TStream) of object;
  119. TIdHTTPProxyServer = class(TIdCmdTCPServer)
  120. protected
  121. FDefTransferMode: TIdHTTPProxyTransferMode;
  122. FOnHTTPBeforeCommand: TOnHTTPContextEvent;
  123. FOnHTTPResponse: TOnHTTPContextEvent;
  124. FOnHTTPDocument: TOnHTTPDocument;
  125. // CommandHandlers
  126. procedure CommandPassThrough(ASender: TIdCommand);
  127. procedure CommandCONNECT(ASender: TIdCommand); // for ssl
  128. procedure DoHTTPBeforeCommand(AContext: TIdHTTPProxyServerContext);
  129. procedure DoHTTPDocument(AContext: TIdHTTPProxyServerContext; var VStream: TStream);
  130. procedure DoHTTPResponse(AContext: TIdHTTPProxyServerContext);
  131. procedure InitializeCommandHandlers; override;
  132. procedure TransferData(AContext: TIdHTTPProxyServerContext; ASrc, ADest: TIdTCPConnection);
  133. public
  134. constructor Create(AOwner: TComponent); override;
  135. published
  136. property DefaultPort default IdPORT_HTTPProxy;
  137. property DefaultTransferMode: TIdHTTPProxyTransferMode read FDefTransferMode write FDefTransferMode default tmFullDocument;
  138. property OnHTTPBeforeCommand: TOnHTTPContextEvent read FOnHTTPBeforeCommand write FOnHTTPBeforeCommand;
  139. property OnHTTPResponse: TOnHTTPContextEvent read FOnHTTPResponse write FOnHTTPResponse;
  140. property OnHTTPDocument: TOnHTTPDocument read FOnHTTPDocument write FOnHTTPDocument;
  141. end;
  142. implementation
  143. uses
  144. IdResourceStringsProtocols, IdIOHandler, IdTCPClient,
  145. IdURI, IdGlobalProtocols, IdStack, IdStackConsts, IdTCPStream, IdException, SysUtils;
  146. constructor TIdHTTPProxyServerContext.Create(AConnection: TIdTCPConnection;
  147. AYarn: TIdYarn; AList: TIdContextThreadList = nil);
  148. begin
  149. inherited Create(AConnection, AYarn, AList);
  150. FHeaders := TIdHeaderList.Create(QuoteHTTP);
  151. end;
  152. destructor TIdHTTPProxyServerContext.Destroy;
  153. begin
  154. FHeaders.Free;
  155. inherited Destroy;
  156. end;
  157. { TIdHTTPProxyServer }
  158. constructor TIdHTTPProxyServer.Create(AOwner: TComponent);
  159. begin
  160. inherited Create(AOwner);
  161. ContextClass := TIdHTTPProxyServerContext;
  162. DefaultPort := IdPORT_HTTPProxy;
  163. FDefTransferMode := tmFullDocument;
  164. Greeting.Text.Text := ''; // RS
  165. ReplyUnknownCommand.Text.Text := ''; // RS
  166. end;
  167. procedure TIdHTTPProxyServer.InitializeCommandHandlers;
  168. var
  169. LCommandHandler: TIdCommandHandler;
  170. begin
  171. inherited;
  172. LCommandHandler := CommandHandlers.Add;
  173. LCommandHandler.Command := 'GET'; {do not localize}
  174. LCommandHandler.OnCommand := CommandPassThrough;
  175. LCommandHandler.ParseParams := True;
  176. LCommandHandler.Disconnect := True;
  177. LCommandHandler := CommandHandlers.Add;
  178. LCommandHandler.Command := 'POST'; {do not localize}
  179. LCommandHandler.OnCommand := CommandPassThrough;
  180. LCommandHandler.ParseParams := True;
  181. LCommandHandler.Disconnect := True;
  182. LCommandHandler := CommandHandlers.Add;
  183. LCommandHandler.Command := 'HEAD'; {do not localize}
  184. LCommandHandler.OnCommand := CommandPassThrough;
  185. LCommandHandler.ParseParams := True;
  186. LCommandHandler.Disconnect := True;
  187. LCommandHandler := CommandHandlers.Add;
  188. LCommandHandler.Command := 'CONNECT'; {do not localize}
  189. LCommandHandler.OnCommand := CommandCONNECT;
  190. LCommandHandler.ParseParams := True;
  191. LCommandHandler.Disconnect := True;
  192. //HTTP Servers/Proxies do not send a greeting
  193. Greeting.Clear;
  194. end;
  195. procedure TIdHTTPProxyServer.TransferData(AContext: TIdHTTPProxyServerContext;
  196. ASrc, ADest: TIdTCPConnection);
  197. var
  198. LStream: TStream;
  199. LSize: Int64;
  200. S: String;
  201. begin
  202. // RLebeau: TODO - support chunked, gzip, and deflate transfers.
  203. // RLebeau: determine how many bytes to read
  204. S := AContext.Headers.Values['Content-Length']; {Do not Localize}
  205. if S <> '' then
  206. begin
  207. LSize := IndyStrToStreamSize(S, -1) ; {Do not Localize}
  208. if LSize < 0 then begin
  209. // Write HTTP error status response
  210. if AContext.TransferSource = tsClient then begin
  211. ASrc.IOHandler.WriteLn('HTTP/1.0 400 Bad Request'); {Do not Localize}
  212. end else begin
  213. ASrc.IOHandler.WriteLn('HTTP/1.0 502 Bad Gateway'); {Do not Localize}
  214. end;
  215. ASrc.IOHandler.WriteLn;
  216. Exit;
  217. end;
  218. end else begin
  219. LSize := -1;
  220. end;
  221. if AContext.TransferSource = tsClient then begin
  222. ADest.IOHandler.WriteLn(AContext.Command + ' ' + AContext.Document + ' HTTP/1.0'); {Do not Localize}
  223. end;
  224. if (AContext.TransferSource = tsServer) or (LSize > 0) then
  225. begin
  226. LStream := nil;
  227. try
  228. if AContext.TransferMode = tmFullDocument then
  229. begin
  230. //TODO: Have an event to let the user perform stream creation
  231. LStream := TMemoryStream.Create;
  232. // RLebeau: do not write the source headers until the OnHTTPDocument
  233. // event has had a chance to update them if it alters the document data...
  234. ASrc.IOHandler.ReadStream(LStream, LSize, LSize < 0);
  235. LStream.Position := 0;
  236. DoHTTPDocument(AContext, LStream);
  237. ADest.IOHandler.Write(AContext.Headers);
  238. ADest.IOHandler.WriteLn;
  239. ADest.IOHandler.Write(LStream);
  240. end else
  241. begin
  242. // RLebeau: direct pass-through, send everything as-is...
  243. LStream := TIdTCPStream.Create(ADest);
  244. ADest.IOHandler.Write(AContext.Headers);
  245. ADest.IOHandler.WriteLn;
  246. ASrc.IOHandler.ReadStream(LStream, LSize, LSize < 0);
  247. end;
  248. finally
  249. LStream.Free;
  250. end;
  251. end else
  252. begin
  253. // RLebeau: the client sent a document with no data in it, so just pass
  254. // along the headers by themselves ...
  255. ADest.IOHandler.Write(AContext.Headers);
  256. ADest.IOHandler.WriteLn;
  257. end;
  258. end;
  259. procedure TIdHTTPProxyServer.CommandPassThrough(ASender: TIdCommand);
  260. var
  261. LURI: TIdURI;
  262. LContext: TIdHTTPProxyServerContext;
  263. LConnection: string;
  264. function IsVersionAtLeast11(const AVersionStr: string): Boolean;
  265. var
  266. s: string;
  267. LMajor, LMinor: Integer;
  268. begin
  269. s := AVersionStr;
  270. Fetch(s, '/'); {Do not localize}
  271. LMajor := IndyStrToInt(Fetch(s, '.'), -1); {Do not Localize}
  272. LMinor := IndyStrToInt(S, -1);
  273. Result := (LMajor > 1) or ((LMajor = 1) and (LMinor >= 1));
  274. end;
  275. begin
  276. ASender.PerformReply := False;
  277. LContext := TIdHTTPProxyServerContext(ASender.Context);
  278. LContext.FCommand := ASender.CommandHandler.Command;
  279. LContext.FTarget := ASender.Params.Strings[0];
  280. LContext.FOutboundClient := TIdTCPClient.Create(nil);
  281. try
  282. LURI := TIdURI.Create(LContext.Target);
  283. try
  284. TIdTCPClient(LContext.FOutboundClient).Host := LURI.Host;
  285. if LURI.Port <> '' then begin
  286. TIdTCPClient(LContext.FOutboundClient).Port := IndyStrToInt(LURI.Port, 80);
  287. end
  288. else if TextIsSame(LURI.Protocol, 'http') then begin {do not localize}
  289. TIdTCPClient(LContext.FOutboundClient).Port := IdPORT_HTTP;
  290. end
  291. else if TextIsSame(LURI.Protocol, 'https') then begin {do not localize}
  292. TIdTCPClient(LContext.FOutboundClient).Port := IdPORT_https;
  293. end else begin
  294. raise EIdException.Create(RSHTTPUnknownProtocol); // TODO: create a new Exception class for this
  295. end;
  296. //We have to remove the host and port from the request
  297. LContext.FDocument := LURI.GetPathAndParams;
  298. finally
  299. LURI.Free;
  300. end;
  301. LContext.Headers.Clear;
  302. LContext.Connection.IOHandler.Capture(LContext.Headers, '', False);
  303. LContext.FTransferMode := FDefTransferMode;
  304. LContext.FTransferSource := tsClient;
  305. DoHTTPBeforeCommand(LContext);
  306. LConnection := LContext.Headers.Values['Proxy-Connection']; {do not localize}
  307. if LConnection <> '' then begin
  308. ASender.Disconnect := TextIsSame(LConnection, 'close'); {do not localize}
  309. end else begin
  310. LConnection := LContext.Headers.Values['Connection']; {do not localize}
  311. if IsVersionAtLeast11(ASender.Params.Strings[1]) then begin
  312. ASender.Disconnect := TextIsSame(LConnection, 'close'); {do not localize}
  313. end else begin
  314. ASender.Disconnect := not TextIsSame(LConnection, 'keep-alive'); {do not localize}
  315. end;
  316. end;
  317. // TODO: If the client requests a keep-alive with the target server, don't disconnect the
  318. // TIdTCPClient below, so it can be reused for subsequent requests. Disconnect it only
  319. // when the requesting client disconnects, the keep-alive times out, or a different
  320. // host/port is requested...
  321. TIdTCPClient(LContext.FOutboundClient).Connect;
  322. try
  323. // TODO: if FDefTransferMode is tmStreaming, send the request and receive the response
  324. // in parallel, similar to how CommandCONNECT() does. This would also facilitate the
  325. // server being able to send back an error reponse while the client is still sending
  326. // its request...
  327. LContext.Headers.Values['Connection'] := 'close'; {do not localize}
  328. TransferData(LContext, LContext.Connection, LContext.FOutboundClient);
  329. LContext.Headers.Clear;
  330. LContext.FOutboundClient.IOHandler.Capture(LContext.Headers, '', False);
  331. LContext.FTransferMode := FDefTransferMode;
  332. LContext.FTransferSource := tsServer;
  333. DoHTTPResponse(LContext);
  334. LContext.Headers.Values['Proxy-Connection'] := iif(ASender.Disconnect, 'close', 'keep-alive'); {do not localize}
  335. TransferData(LContext, LContext.FOutboundClient, LContext.Connection);
  336. finally
  337. LContext.FOutboundClient.Disconnect;
  338. end;
  339. finally
  340. FreeAndNil(LContext.FOutboundClient);
  341. end;
  342. end;
  343. procedure TIdHTTPProxyServer.CommandCONNECT(ASender: TIdCommand);
  344. var
  345. LRemoteHost: string;
  346. LContext: TIdHTTPProxyServerContext;
  347. LReadList, LDataAvailList: TIdSocketList;
  348. LClientToServerStream, LServerToClientStream: TStream;
  349. LConnectionHandle, LOutBoundHandle: TIdStackSocketHandle;
  350. LConnectionIO, LOutboundIO: TIdIOHandler;
  351. procedure CheckForData(DoRead: Boolean);
  352. begin
  353. if DoRead and LConnectionIO.InputBufferIsEmpty and LOutboundIO.InputBufferIsEmpty then
  354. begin
  355. if LReadList.SelectReadList(LDataAvailList, IdTimeoutInfinite) then
  356. begin
  357. if LDataAvailList.ContainsSocket(LConnectionHandle) then
  358. begin
  359. LConnectionIO.CheckForDataOnSource(0);
  360. end;
  361. if LDataAvailList.ContainsSocket(LOutBoundHandle) then
  362. begin
  363. LOutboundIO.CheckForDataOnSource(0);
  364. end;
  365. end;
  366. end;
  367. if not LConnectionIO.InputBufferIsEmpty then
  368. begin
  369. LConnectionIO.InputBuffer.ExtractToStream(LClientToServerStream);
  370. end;
  371. if not LOutboundIO.InputBufferIsEmpty then
  372. begin
  373. LOutboundIO.InputBuffer.ExtractToStream(LServerToClientStream);
  374. end;
  375. LConnectionIO.CheckForDisconnect;
  376. LOutboundIO.CheckForDisconnect;
  377. end;
  378. begin
  379. // RLebeau 7/31/09: we can't make any assumptions about the contents of
  380. // the data being exchanged after the connection has been established.
  381. // It may not (and likely will not) be HTTP data at all. We must pass
  382. // it along as-is in both directions, in as near-realtime as we can...
  383. ASender.PerformReply := False;
  384. LContext := TIdHTTPProxyServerContext(ASender.Context);
  385. LContext.FCommand := ASender.CommandHandler.Command;
  386. LContext.FTarget := ASender.Params.Strings[0];
  387. LContext.FOutboundClient := TIdTCPClient.Create(nil);
  388. try
  389. LClientToServerStream := TIdTCPStream.Create(LContext.FOutboundClient);
  390. try
  391. LServerToClientStream := TIdTCPStream.Create(LContext.Connection);
  392. try
  393. LRemoteHost := LContext.Target;
  394. TIdTCPClient(LContext.FOutboundClient).Host := Fetch(LRemoteHost, ':', True);
  395. TIdTCPClient(LContext.FOutboundClient).Port := IndyStrToInt(LRemoteHost, 443);
  396. LConnectionIO := LContext.Connection.IOHandler;
  397. LContext.Headers.Clear;
  398. LConnectionIO.Capture(LContext.Headers, '', False);
  399. LContext.FTransferMode := FDefTransferMode; // TODO: should this be forced to tmStreaming instead?
  400. LContext.FTransferSource := tsClient;
  401. DoHTTPBeforeCommand(LContext);
  402. TIdTCPClient(LContext.FOutboundClient).Connect;
  403. try
  404. LOutboundIO := LContext.FOutboundClient.IOHandler;
  405. LConnectionHandle := LContext.Binding.Handle;
  406. LOutBoundHandle := LContext.FOutboundClient.Socket.Binding.Handle;
  407. LReadList := TIdSocketList.CreateSocketList;
  408. try
  409. LReadList.Add(LConnectionHandle);
  410. LReadList.Add(LOutBoundHandle);
  411. LDataAvailList := TIdSocketList.CreateSocketList;
  412. try
  413. LConnectionIO.WriteLn('HTTP/1.0 200 Connection established'); {do not localize}
  414. LConnectionIO.WriteLn('Proxy-agent: Indy-Proxy/1.1'); {do not localize}
  415. LConnectionIO.WriteLn;
  416. CheckForData(False);
  417. while LContext.Connection.Connected and LContext.FOutboundClient.Connected do
  418. begin
  419. CheckForData(True);
  420. end;
  421. if LContext.FOutboundClient.Connected and (not LConnectionIO.InputBufferIsEmpty) then
  422. begin
  423. LConnectionIO.InputBuffer.ExtractToStream(LClientToServerStream);
  424. end;
  425. if LContext.Connection.Connected and (not LOutboundIO.InputBufferIsEmpty) then
  426. begin
  427. LOutboundIO.InputBuffer.ExtractToStream(LServerToClientStream);
  428. end;
  429. finally
  430. LDataAvailList.Free;
  431. end;
  432. finally
  433. LReadList.Free;
  434. end;
  435. finally
  436. LContext.FOutboundClient.Disconnect;
  437. LOutboundIO := nil;
  438. end;
  439. finally
  440. LServerToClientStream.Free;
  441. LConnectionIO := nil;
  442. end;
  443. finally
  444. LClientToServerStream.Free;
  445. end;
  446. finally
  447. FreeAndNil(LContext.FOutboundClient);
  448. end;
  449. end;
  450. procedure TIdHTTPProxyServer.DoHTTPBeforeCommand(AContext: TIdHTTPProxyServerContext);
  451. begin
  452. if Assigned(OnHTTPBeforeCommand) then begin
  453. OnHTTPBeforeCommand(AContext);
  454. end;
  455. end;
  456. procedure TIdHTTPProxyServer.DoHTTPDocument(AContext: TIdHTTPProxyServerContext;
  457. var VStream: TStream);
  458. begin
  459. if Assigned(OnHTTPDocument) then begin
  460. OnHTTPDocument(AContext, VStream);
  461. end;
  462. end;
  463. procedure TIdHTTPProxyServer.DoHTTPResponse(AContext: TIdHTTPProxyServerContext);
  464. begin
  465. if Assigned(OnHTTPResponse) then begin
  466. OnHTTPResponse(AContext);
  467. end;
  468. end;
  469. end.