Refactored HttpConnection.cs

This commit is contained in:
sta 2014-04-27 14:42:03 +09:00
parent fe28277f14
commit 16f5c03b95
4 changed files with 107 additions and 21 deletions

View File

@ -52,21 +52,6 @@ namespace WebSocketSharp.Net
{ {
internal sealed class HttpConnection internal sealed class HttpConnection
{ {
#region Internal Enums
enum InputState {
RequestLine,
Headers
}
enum LineState {
None,
CR,
LF
}
#endregion
#region Private Const Fields #region Private Const Fields
private const int _bufferSize = 8192; private const int _bufferSize = 8192;
@ -107,14 +92,14 @@ namespace WebSocketSharp.Net
_secure = listener.IsSecure; _secure = listener.IsSecure;
var netStream = new NetworkStream (socket, false); var netStream = new NetworkStream (socket, false);
if (!_secure) { if (_secure) {
_stream = netStream;
}
else {
var sslStream = new SslStream (netStream, false); var sslStream = new SslStream (netStream, false);
sslStream.AuthenticateAsServer (listener.Certificate); sslStream.AuthenticateAsServer (listener.Certificate);
_stream = sslStream; _stream = sslStream;
} }
else {
_stream = netStream;
}
_timeout = 90000; // 90k ms for first request, 15k ms from then on. _timeout = 90000; // 90k ms for first request, 15k ms from then on.
_timer = new Timer (onTimeout, this, Timeout.Infinite, Timeout.Infinite); _timer = new Timer (onTimeout, this, Timeout.Infinite, Timeout.Infinite);
@ -216,7 +201,7 @@ namespace WebSocketSharp.Net
read = conn._stream.EndRead (asyncResult); read = conn._stream.EndRead (asyncResult);
conn._requestBuffer.Write (conn._buffer, 0, read); conn._requestBuffer.Write (conn._buffer, 0, read);
if (conn._requestBuffer.Length > 32768) { if (conn._requestBuffer.Length > 32768) {
conn.SendError (); conn.SendError ("Bad request", 400);
conn.Close (true); conn.Close (true);
return; return;
@ -253,7 +238,7 @@ namespace WebSocketSharp.Net
} }
if (!conn._epListener.BindContext (conn._context)) { if (!conn._epListener.BindContext (conn._context)) {
conn.SendError ("Invalid host.", 400); conn.SendError ("Invalid host", 400);
conn.Close (true); conn.Close (true);
return; return;

View File

@ -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 <gonzalo@novell.com>
*/
#endregion
using System;
namespace WebSocketSharp.Net
{
internal enum InputState
{
RequestLine,
Headers
}
}

View File

@ -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 <gonzalo@novell.com>
*/
#endregion
using System;
namespace WebSocketSharp.Net
{
internal enum LineState
{
None,
CR,
LF
}
}

View File

@ -129,6 +129,8 @@
<Compile Include="Net\HttpDigestIdentity.cs" /> <Compile Include="Net\HttpDigestIdentity.cs" />
<Compile Include="Net\NetworkCredential.cs" /> <Compile Include="Net\NetworkCredential.cs" />
<Compile Include="Server\WebSocketServiceManager.cs" /> <Compile Include="Server\WebSocketServiceManager.cs" />
<Compile Include="Net\InputState.cs" />
<Compile Include="Net\LineState.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup> <ItemGroup>