Skip to content

Commit 9b98ba3

Browse files
committed
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.
1 parent db44f42 commit 9b98ba3

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

RestSharp/Http.Async.cs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -192,23 +192,22 @@ private void RequestStreamCallback(IAsyncResult result, Action<HttpResponse> cal
192192
}
193193
}
194194
}
195-
catch (WebException ex)
195+
catch (Exception ex)
196196
{
197-
if (ex.Status == WebExceptionStatus.RequestCanceled)
197+
HttpResponse response;
198+
if (ex is WebException && ((WebException)ex).Status == WebExceptionStatus.RequestCanceled)
198199
{
199-
var response = new HttpResponse {ResponseStatus = ResponseStatus.TimedOut};
200-
ExecuteCallback(response, callback);
201-
return;
200+
response = new HttpResponse {ResponseStatus = ResponseStatus.TimedOut};
202201
}
203-
}
204-
catch (Exception ex)
205-
{
206-
var response = new HttpResponse
202+
else
207203
{
208-
ErrorMessage = ex.Message,
209-
ErrorException = ex,
210-
ResponseStatus = ResponseStatus.Error
211-
};
204+
response = new HttpResponse
205+
{
206+
ErrorMessage = ex.Message,
207+
ErrorException = ex,
208+
ResponseStatus = ResponseStatus.Error
209+
};
210+
}
212211
ExecuteCallback(response, callback);
213212
return;
214213
}
@@ -297,20 +296,18 @@ private void ResponseCallback(IAsyncResult result, Action<HttpResponse> callback
297296
ExecuteCallback(response, callback);
298297
});
299298
}
300-
catch(WebException ex)
299+
catch(Exception ex)
301300
{
302-
if(ex.Status == WebExceptionStatus.RequestCanceled)
301+
if(ex is WebException && ((WebException)ex).Status == WebExceptionStatus.RequestCanceled)
303302
{
304303
response.ResponseStatus = ResponseStatus.Aborted;
305-
ExecuteCallback(response, callback);
306-
return;
307304
}
308-
}
309-
catch(Exception ex)
310-
{
311-
response.ErrorMessage = ex.Message;
312-
response.ErrorException = ex;
313-
response.ResponseStatus = ResponseStatus.Error;
305+
else
306+
{
307+
response.ErrorMessage = ex.Message;
308+
response.ErrorException = ex;
309+
response.ResponseStatus = ResponseStatus.Error;
310+
}
314311
ExecuteCallback(response, callback);
315312
}
316313
}

0 commit comments

Comments
 (0)