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.StatusCode = status;
res.ContentType = "text/html"; res.ContentType = "text/html";
var desc = status.GetStatusDescription ();
var msg = message != null && message.Length > 0 var msg = message != null && message.Length > 0
? String.Format ("<h1>{0} ({1})</h1>", desc, message) ? String.Format ("{0} ({1})", res.StatusDescription, message)
: String.Format ("<h1>{0}</h1>", desc); : res.StatusDescription;
var entity = res.ContentEncoding.GetBytes (msg); var enc = res.ContentEncoding;
res.Close (entity, false); 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 { catch {
// Response was already closed. // Response was already closed.

View File

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