From 16f5c03b9555ea93d09cdbd7e169c5f8bdb5563f Mon Sep 17 00:00:00 2001 From: sta Date: Sun, 27 Apr 2014 14:42:03 +0900 Subject: [PATCH] Refactored HttpConnection.cs --- websocket-sharp/Net/HttpConnection.cs | 27 ++++---------- websocket-sharp/Net/InputState.cs | 49 +++++++++++++++++++++++++ websocket-sharp/Net/LineState.cs | 50 ++++++++++++++++++++++++++ websocket-sharp/websocket-sharp.csproj | 2 ++ 4 files changed, 107 insertions(+), 21 deletions(-) create mode 100644 websocket-sharp/Net/InputState.cs create mode 100644 websocket-sharp/Net/LineState.cs diff --git a/websocket-sharp/Net/HttpConnection.cs b/websocket-sharp/Net/HttpConnection.cs index 469a7a81..218d34eb 100644 --- a/websocket-sharp/Net/HttpConnection.cs +++ b/websocket-sharp/Net/HttpConnection.cs @@ -52,21 +52,6 @@ namespace WebSocketSharp.Net { internal sealed class HttpConnection { - #region Internal Enums - - enum InputState { - RequestLine, - Headers - } - - enum LineState { - None, - CR, - LF - } - - #endregion - #region Private Const Fields private const int _bufferSize = 8192; @@ -107,14 +92,14 @@ namespace WebSocketSharp.Net _secure = listener.IsSecure; var netStream = new NetworkStream (socket, false); - if (!_secure) { - _stream = netStream; - } - else { + if (_secure) { var sslStream = new SslStream (netStream, false); sslStream.AuthenticateAsServer (listener.Certificate); _stream = sslStream; } + else { + _stream = netStream; + } _timeout = 90000; // 90k ms for first request, 15k ms from then on. _timer = new Timer (onTimeout, this, Timeout.Infinite, Timeout.Infinite); @@ -216,7 +201,7 @@ namespace WebSocketSharp.Net read = conn._stream.EndRead (asyncResult); conn._requestBuffer.Write (conn._buffer, 0, read); if (conn._requestBuffer.Length > 32768) { - conn.SendError (); + conn.SendError ("Bad request", 400); conn.Close (true); return; @@ -253,7 +238,7 @@ namespace WebSocketSharp.Net } if (!conn._epListener.BindContext (conn._context)) { - conn.SendError ("Invalid host.", 400); + conn.SendError ("Invalid host", 400); conn.Close (true); return; diff --git a/websocket-sharp/Net/InputState.cs b/websocket-sharp/Net/InputState.cs new file mode 100644 index 00000000..72361dfd --- /dev/null +++ b/websocket-sharp/Net/InputState.cs @@ -0,0 +1,49 @@ +#region License +/* + * InputState.cs + * + * This code is derived from System.Net.HttpConnection.cs of Mono + * (http://www.mono-project.com). + * + * The MIT License + * + * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) + * Copyright (c) 2014 sta.blockhead + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#endregion + +#region Authors +/* + * Authors: + * - Gonzalo Paniagua Javier + */ +#endregion + +using System; + +namespace WebSocketSharp.Net +{ + internal enum InputState + { + RequestLine, + Headers + } +} diff --git a/websocket-sharp/Net/LineState.cs b/websocket-sharp/Net/LineState.cs new file mode 100644 index 00000000..7266c7ed --- /dev/null +++ b/websocket-sharp/Net/LineState.cs @@ -0,0 +1,50 @@ +#region License +/* + * LineState.cs + * + * This code is derived from System.Net.HttpConnection.cs of Mono + * (http://www.mono-project.com). + * + * The MIT License + * + * Copyright (c) 2005 Novell, Inc. (http://www.novell.com) + * Copyright (c) 2014 sta.blockhead + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#endregion + +#region Authors +/* + * Authors: + * - Gonzalo Paniagua Javier + */ +#endregion + +using System; + +namespace WebSocketSharp.Net +{ + internal enum LineState + { + None, + CR, + LF + } +} diff --git a/websocket-sharp/websocket-sharp.csproj b/websocket-sharp/websocket-sharp.csproj index 8fe65c31..03a55d63 100644 --- a/websocket-sharp/websocket-sharp.csproj +++ b/websocket-sharp/websocket-sharp.csproj @@ -129,6 +129,8 @@ + +