Permalink
Browse files

Don't crash on captive networks that intercept SSL

Some captive WiFi networks intercept SSL traffic using an untrusted
certificate. This causes HttpWebRequest.EndGetRequestStream to throw a
TrustFailure WebException with the following message:

    The underlying connection was closed: Could not establish trust
    relationship for the SSL/TLS secure channel.

Http.RequestStreamCallback was ignoring this exception, which caused us
to call HttpWebRequest.BeginGetResponse even though we hadn't written
the request body to the request stream. This caused a
ProtocolViolationException to be thrown and was the cause of the crash.

We now handle all kinds of exceptions (and still special-case
RequestCanceled exceptions). I made a similar change in
Http.ResponseCallback even though I didn't personally run into a bug
there.
  • Loading branch information...
1 parent db44f42 commit 9b98ba329a6a34fb84ae6f0116a0d4ebe842b278 @aroben committed Jun 19, 2012
Showing with 20 additions and 23 deletions.
  1. +20 −23 RestSharp/Http.Async.cs
View
@@ -192,23 +192,22 @@ private void RequestStreamCallback(IAsyncResult result, Action<HttpResponse> cal
}
}
}
- catch (WebException ex)
+ catch (Exception ex)
{
- if (ex.Status == WebExceptionStatus.RequestCanceled)
+ HttpResponse response;
+ if (ex is WebException && ((WebException)ex).Status == WebExceptionStatus.RequestCanceled)
{
- var response = new HttpResponse {ResponseStatus = ResponseStatus.TimedOut};
- ExecuteCallback(response, callback);
- return;
+ response = new HttpResponse {ResponseStatus = ResponseStatus.TimedOut};
}
- }
- catch (Exception ex)
- {
- var response = new HttpResponse
+ else
{
- ErrorMessage = ex.Message,
- ErrorException = ex,
- ResponseStatus = ResponseStatus.Error
- };
+ response = new HttpResponse
+ {
+ ErrorMessage = ex.Message,
+ ErrorException = ex,
+ ResponseStatus = ResponseStatus.Error
+ };
+ }
ExecuteCallback(response, callback);
return;
}
@@ -297,20 +296,18 @@ private void ResponseCallback(IAsyncResult result, Action<HttpResponse> callback
ExecuteCallback(response, callback);
});
}
- catch(WebException ex)
+ catch(Exception ex)
{
- if(ex.Status == WebExceptionStatus.RequestCanceled)
+ if(ex is WebException && ((WebException)ex).Status == WebExceptionStatus.RequestCanceled)
{
response.ResponseStatus = ResponseStatus.Aborted;
- ExecuteCallback(response, callback);
- return;
}
- }
- catch(Exception ex)
- {
- response.ErrorMessage = ex.Message;
- response.ErrorException = ex;
- response.ResponseStatus = ResponseStatus.Error;
+ else
+ {
+ response.ErrorMessage = ex.Message;
+ response.ErrorException = ex;
+ response.ResponseStatus = ResponseStatus.Error;
+ }
ExecuteCallback(response, callback);
}
}

0 comments on commit 9b98ba3

Please sign in to comment.