Refactored the HttpListenerResponse.Close (byte[], bool) and HttpConnection.SendError (string, int) methods

This commit is contained in:
sta 2015-06-09 17:46:56 +09:00
parent 659ab51b59
commit f3cd96966e
2 changed files with 12 additions and 10 deletions

View File

@ -533,13 +533,19 @@ namespace WebSocketSharp.Net
res.StatusCode = status;
res.ContentType = "text/html";
var desc = status.GetStatusDescription ();
var msg = message != null && message.Length > 0
? String.Format ("<h1>{0} ({1})</h1>", desc, message)
: String.Format ("<h1>{0}</h1>", desc);
? String.Format ("{0} ({1})", res.StatusDescription, message)
: res.StatusDescription;
var entity = res.ContentEncoding.GetBytes (msg);
res.Close (entity, false);
var enc = res.ContentEncoding;
if (enc == null) {
enc = Encoding.UTF8;
res.ContentEncoding = enc;
}
var entity = enc.GetBytes (String.Format ("<html><body><h1>{0}</h1></body></html>", msg));
res.ContentLength64 = entity.LongLength;
res.Close (entity, true);
}
catch {
// Response was already closed.

View File

@ -743,20 +743,16 @@ namespace WebSocketSharp.Net
/// <exception cref="ArgumentNullException">
/// <paramref name="responseEntity"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="InvalidOperationException">
/// The response has already been sent.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// This object is closed.
/// </exception>
public void Close (byte[] responseEntity, bool willBlock)
{
checkDisposed ();
if (responseEntity == null)
throw new ArgumentNullException ("responseEntity");
var len = responseEntity.Length;
ContentLength64 = len;
var output = OutputStream;
if (willBlock) {
output.Write (responseEntity, 0, len);