Skip to content

Commit

Permalink
#177 Managing HttpListenerException exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Offen committed May 12, 2017
1 parent 12e25a5 commit 8dbff92
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Grapevine/Interfaces/Server/HttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,16 @@ public void SendResponse(byte[] contents)
Response.Headers["Content-Encoding"] = "gzip";
}

Response.ContentLength64 = contents.Length;
Response.OutputStream.Write(contents, 0, contents.Length);
Response.OutputStream.Close();
Advanced.Close();
try
{
Response.ContentLength64 = contents.Length;
Response.OutputStream.Write(contents, 0, contents.Length);
}
finally
{
Response.OutputStream.Close();
Advanced.Close();
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/Grapevine/Server/Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using Grapevine.Exceptions.Server;
using Grapevine.Interfaces.Server;
using Grapevine.Interfaces.Shared;
using Grapevine.Server.Attributes;
using Grapevine.Shared;
using Grapevine.Shared.Loggers;
using HttpStatusCode = Grapevine.Shared.HttpStatusCode;

namespace Grapevine.Server
{
Expand Down Expand Up @@ -585,6 +587,13 @@ public void Route(IHttpContext context, IList<IRoute> routing)
if (context.WasRespondedTo) break;
}
}
catch (HttpListenerException e)
{
var msg = e.NativeErrorCode == 64
? "Connection aborted by client"
: "An error occured while attempting to respond to the request";
Logger.Error(msg, e);
}
finally
{
OnAfterRouting(context);
Expand Down

0 comments on commit 8dbff92

Please sign in to comment.