Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function TIdHTTPProtocol.ProcessResponse: TIdHTTPWhatsNext;
- procedure RaiseException;
- var
- LRespStream: TStringStream;
- LTempStream: TStream;
- LTemp: Integer;
- begin
- LTemp := FHTTP.ReadTimeout;
- FHTTP.ReadTimeout := 2000; // Lets wait 2 seconds for any kind of content
- LRespStream := TStringStream.Create('');
- LTempStream := Response.ContentStream;
- Response.ContentStream := LRespStream;
- try
- FHTTP.ReadResult(Response);
- raise EIdHTTPProtocolException.CreateError(Response.ResponseCode, FHTTP.ResponseText, LRespStream.DataString);
- finally
- Response.ContentStream := LTempStream;
- LRespStream.Free;
- FHTTP.ReadTimeout := LTemp;
- end;
- end;
- procedure ReadContent;
- Var
- LTempResponse: TStringStream;
- LTempStream: TStream;
- begin
- LTempResponse := TStringStream.Create('');
- LTempStream := Response.ContentStream;
- Response.ContentStream := LTempResponse;
- try
- FHTTP.ReadResult(Response);
- finally
- LTempResponse.Free;
- Response.ContentStream := LTempStream;
- end;
- end;
- var
- LTemp: Integer;
- LLocation: string;
- LMethod: TIdHTTPMethod;
- LResponseDigit: Integer;
- LNeedAutorization: Boolean;
- begin
- result := wnDontKnow;
- LNeedAutorization := False;
- LResponseDigit := Response.ResponseCode div 100;
- // Handle Redirects
- if ((LResponseDigit = 3) and (Response.ResponseCode <> 304)) or (Length(Response.Location) > 0) then
- begin
- // LLocation := TIdURI.URLDecode(Response.Location);
- LLocation := Response.Location;
- if (FHTTP.FHandleRedirects) and (FHTTP.FRedirectCount < FHTTP.FRedirectMax) then
- begin
- LMethod := Request.Method;
- if FHTTP.DoOnRedirect(LLocation, LMethod, FHTTP.FRedirectCount) then
- begin
- result := wnGoToURL;
- Request.URL := LLocation;
- Request.Method := LMethod;
- end
- else
- RaiseException;
- end
- else // Just fire the event
- begin
- LMethod := Request.Method;
- result := wnJustExit;
- if not FHTTP.DoOnRedirect(LLocation, LMethod, FHTTP.FRedirectCount) then // If not Handled
- RaiseException
- else
- Response.Location := LLocation;
- end;
- if FHTTP.Connected then
- begin
- // This is a workaround for buggy HTTP 1.1 servers which
- // does not return any body with 302 response code
- LTemp := FHTTP.ReadTimeout;
- FHTTP.ReadTimeout := 4000; // Lets wait 4 seconds for any kind of content
- try
- ReadContent;
- except end;
- FHTTP.ReadTimeout := LTemp;
- end;
- end
- else
- begin
- // GREGOR Workaround
- // if we get an error we disconnect if we use SSLIOHandler
- if Assigned(FHTTP.IOHandler) then
- begin
- Response.KeepAlive := not (FHTTP.Connected and (FHTTP.IOHandler is TIdSSLIOHandlerSocket) and Response.KeepAlive);
- end;
- if LResponseDigit <> 2 then
- begin
- result := wnGoToURL;
- case Response.ResponseCode of
- 401:
- begin // HTTP Server authorization requered
- if (FHTTP.FAuthRetries >= FHTTP.AuthRetries) or not FHTTP.DoOnAuthorization(Request, Response) then
- begin
- if Assigned(Request.Authentication) then
- Request.Authentication.Reset;
- RaiseException;
- end else begin
- if hoInProcessAuth in FHTTP.HTTPOptions then
- LNeedAutorization := True;
- end;
- end;
- 407:
- begin // Proxy Server authorization requered
- if (FHTTP.FAuthProxyRetries >= FHTTP.AuthRetries) or not FHTTP.DoOnProxyAuthorization(Request, Response) then
- begin
- if Assigned(FHTTP.ProxyParams.Authentication) then
- FHTTP.ProxyParams.Authentication.Reset;
- RaiseException;
- end else begin
- if hoInProcessAuth in FHTTP.HTTPOptions then
- LNeedAutorization := True;
- end;
- end;
- else begin
- RaiseException;
- end;
- end;
- end;
- if FHTTP.Connected then begin
- if LNeedAutorization then begin
- // Read the content of Error message in temporary stream
- LTemp := FHTTP.ReadTimeout;
- FHTTP.ReadTimeout := 4000; // Lets wait 4 seconds for any kind of content
- try
- ReadContent;
- except end;
- FHTTP.ReadTimeout := LTemp;
- result := wnAuthRequest
- end
- else if (Response.ResponseCode <> 204) then
- begin
- FHTTP.ReadResult(Response);
- result := wnJustExit;
- end
- else
- result := wnJustExit;
- end;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement