Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.42 KB | None | 0 0
  1. procedure TIdCustomHTTP.ConnectToHost(ARequest: TIdHTTPRequest; AResponse: TIdHTTPResponse);
  2. var
  3.   LLocalHTTP: TIdHTTPProtocol;
  4. begin
  5.   ARequest.FUseProxy := SetHostAndPort;
  6.  
  7.   if ARequest.UseProxy = ctProxy then
  8.   begin
  9.     ARequest.URL := FURI.URI;
  10.   end;
  11.  
  12.   case ARequest.UseProxy of
  13.     ctNormal:
  14.       if (ProtocolVersion = pv1_0) and (Length(ARequest.Connection) = 0) then
  15.         ARequest.Connection := 'keep-alive';
  16.     ctSSL, ctSSLProxy: ARequest.Connection := '';
  17.     ctProxy:
  18.       if (ProtocolVersion = pv1_0) and (Length(ARequest.Connection) = 0) then
  19.         ARequest.ProxyConnection := 'keep-alive';
  20.   end;
  21.  
  22.   if ARequest.UseProxy = ctSSLProxy then begin
  23.     LLocalHTTP := TIdHTTPProtocol.Create(Self);
  24.  
  25.     with LLocalHTTP do begin
  26.       Request.UserAgent := ARequest.UserAgent;
  27.       Request.Host := ARequest.Host;
  28.       Request.ContentLength := ARequest.ContentLength;
  29.       Request.Pragma := 'no-cache';
  30.       Request.URL := URL.Host + ':' + URL.Port;
  31.       Request.Method := hmConnect;
  32.       Request.ProxyConnection := 'keep-alive';
  33.       Response.ContentStream := TMemoryStream.Create;
  34.       try
  35.         try
  36.           repeat
  37.             CheckAndConnect(Response);
  38.             BuildAndSendRequest(nil);
  39.  
  40.             Response.ResponseText := ReadLn;
  41.             if Length(Response.ResponseText) = 0 then begin
  42.               Response.ResponseText := 'HTTP/1.0 200 OK'; // Support for HTTP responses whithout Status line and headers
  43.               Response.Connection := 'close';
  44.             end
  45.             else begin
  46.               RetrieveHeaders;
  47.               ProcessCookies(LLocalHTTP.Request, LLocalHTTP.Response);
  48.             end;
  49.  
  50.             if Response.ResponseCode = 200 then
  51.             begin
  52.               // Connection established
  53.               (IOHandler as TIdSSLIOHandlerSocket).PassThrough := false;
  54.               break;
  55.             end
  56.             else begin
  57.               ProcessResponse;
  58.             end;
  59.           until false;
  60.         except
  61.           raise;
  62.           // TODO: Add property that will contain the error messages.
  63.         end;
  64.       finally
  65.         LLocalHTTP.Response.ContentStream.Free;
  66.         LLocalHTTP.Free;
  67.       end;
  68.     end;
  69.   end
  70.   else begin
  71.     CheckAndConnect(AResponse);
  72.   end;
  73.  
  74.   FHTTPProto.BuildAndSendRequest(URL);
  75.  
  76.   if (ARequest.Method in [hmPost, hmPut]) then
  77.   begin
  78.     WriteStream(ARequest.Source, True, false);
  79.   end;
  80. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement