Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.67 KB | None | 0 0
  1. function TIdHTTPProtocol.ProcessResponse: TIdHTTPWhatsNext;
  2.   procedure RaiseException;
  3.   var
  4.     LRespStream: TStringStream;
  5.     LTempStream: TStream;
  6.     LTemp: Integer;
  7.   begin
  8.     LTemp := FHTTP.ReadTimeout;
  9.     FHTTP.ReadTimeout := 2000; // Lets wait 2 seconds for any kind of content
  10.     LRespStream := TStringStream.Create('');
  11.     LTempStream := Response.ContentStream;
  12.     Response.ContentStream := LRespStream;
  13.     try
  14.       FHTTP.ReadResult(Response);
  15.       raise EIdHTTPProtocolException.CreateError(Response.ResponseCode, FHTTP.ResponseText, LRespStream.DataString);
  16.     finally
  17.       Response.ContentStream := LTempStream;
  18.       LRespStream.Free;
  19.       FHTTP.ReadTimeout := LTemp;
  20.     end;
  21.   end;
  22.  
  23.   procedure ReadContent;
  24.   Var
  25.     LTempResponse: TStringStream;
  26.     LTempStream: TStream;
  27.   begin
  28.     LTempResponse := TStringStream.Create('');
  29.     LTempStream := Response.ContentStream;
  30.     Response.ContentStream := LTempResponse;
  31.     try
  32.       FHTTP.ReadResult(Response);
  33.     finally
  34.       LTempResponse.Free;
  35.       Response.ContentStream := LTempStream;
  36.     end;
  37.   end;
  38.  
  39. var
  40.   LTemp: Integer;
  41.   LLocation: string;
  42.   LMethod: TIdHTTPMethod;
  43.   LResponseDigit: Integer;
  44.   LNeedAutorization: Boolean;
  45. begin
  46.   result := wnDontKnow;
  47.   LNeedAutorization := False;
  48.   LResponseDigit := Response.ResponseCode div 100;
  49.   // Handle Redirects
  50.   if ((LResponseDigit = 3) and (Response.ResponseCode <> 304)) or (Length(Response.Location) > 0) then
  51.   begin
  52.     // LLocation := TIdURI.URLDecode(Response.Location);
  53.     LLocation := Response.Location;
  54.  
  55.     if (FHTTP.FHandleRedirects) and (FHTTP.FRedirectCount < FHTTP.FRedirectMax) then
  56.     begin
  57.       LMethod := Request.Method;
  58.       if FHTTP.DoOnRedirect(LLocation, LMethod, FHTTP.FRedirectCount) then
  59.       begin
  60.         result := wnGoToURL;
  61.         Request.URL := LLocation;
  62.         Request.Method := LMethod;
  63.       end
  64.       else
  65.         RaiseException;
  66.     end
  67.     else // Just fire the event
  68.     begin
  69.       LMethod := Request.Method;
  70.       result := wnJustExit;
  71.       if not FHTTP.DoOnRedirect(LLocation, LMethod, FHTTP.FRedirectCount) then // If not Handled
  72.         RaiseException
  73.       else
  74.         Response.Location := LLocation;
  75.     end;
  76.  
  77.     if FHTTP.Connected then
  78.     begin
  79.       // This is a workaround for buggy HTTP 1.1 servers which
  80.       // does not return any body with 302 response code
  81.       LTemp := FHTTP.ReadTimeout;
  82.       FHTTP.ReadTimeout := 4000; // Lets wait 4 seconds for any kind of content
  83.       try
  84.         ReadContent;
  85.       except end;
  86.       FHTTP.ReadTimeout := LTemp;
  87.     end;
  88.   end
  89.   else
  90.   begin
  91.     // GREGOR Workaround
  92.     // if we get an error we disconnect if we use SSLIOHandler
  93.     if Assigned(FHTTP.IOHandler) then
  94.     begin
  95.       Response.KeepAlive := not (FHTTP.Connected and (FHTTP.IOHandler is TIdSSLIOHandlerSocket) and Response.KeepAlive);
  96.     end;
  97.  
  98.     if LResponseDigit <> 2 then
  99.     begin
  100.       result := wnGoToURL;
  101.       case Response.ResponseCode of
  102.         401:
  103.           begin // HTTP Server authorization requered
  104.             if (FHTTP.FAuthRetries >= FHTTP.AuthRetries) or not FHTTP.DoOnAuthorization(Request, Response) then
  105.             begin
  106.               if Assigned(Request.Authentication) then
  107.                 Request.Authentication.Reset;
  108.               RaiseException;
  109.             end else begin
  110.               if hoInProcessAuth in FHTTP.HTTPOptions then
  111.                 LNeedAutorization := True;
  112.             end;
  113.           end;
  114.         407:
  115.           begin // Proxy Server authorization requered
  116.             if (FHTTP.FAuthProxyRetries >= FHTTP.AuthRetries) or not FHTTP.DoOnProxyAuthorization(Request, Response) then
  117.             begin
  118.               if Assigned(FHTTP.ProxyParams.Authentication) then
  119.                 FHTTP.ProxyParams.Authentication.Reset;
  120.               RaiseException;
  121.             end else begin
  122.               if hoInProcessAuth in FHTTP.HTTPOptions then
  123.                 LNeedAutorization := True;
  124.             end;
  125.           end;
  126.         else begin
  127.           RaiseException;
  128.         end;
  129.       end;
  130.     end;
  131.  
  132.     if FHTTP.Connected then begin
  133.       if LNeedAutorization then begin
  134.         // Read the content of Error message in temporary stream
  135.         LTemp := FHTTP.ReadTimeout;
  136.         FHTTP.ReadTimeout := 4000; // Lets wait 4 seconds for any kind of content
  137.         try
  138.           ReadContent;
  139.         except end;
  140.         FHTTP.ReadTimeout := LTemp;
  141.         result := wnAuthRequest
  142.       end
  143.       else if (Response.ResponseCode <> 204) then
  144.       begin
  145.         FHTTP.ReadResult(Response);
  146.         result := wnJustExit;
  147.       end
  148.       else
  149.         result := wnJustExit;
  150.     end;
  151.   end;
  152. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement