Skip to content

Commit fe28277

Browse files
committed
Refactored HttpConnection.cs
1 parent 138628b commit fe28277

File tree

3 files changed

+55
-63
lines changed

3 files changed

+55
-63
lines changed

websocket-sharp/Net/EndPointListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ private static void onAccept (object sender, EventArgs e)
218218

219219
HttpConnection conn = null;
220220
try {
221-
conn = new HttpConnection (accepted, listener, listener._secure, listener._cert);
221+
conn = new HttpConnection (accepted, listener);
222222
lock (((ICollection) listener._unregistered).SyncRoot)
223223
listener._unregistered [conn] = conn;
224224

websocket-sharp/Net/HttpConnection.cs

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* The MIT License
99
*
1010
* Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
11-
* Copyright (c) 2012-2013 sta.blockhead
11+
* Copyright (c) 2012-2014 sta.blockhead
1212
*
1313
* Permission is hereby granted, free of charge, to any person obtaining a copy
1414
* of this software and associated documentation files (the "Software"), to deal
@@ -33,7 +33,7 @@
3333
#region Authors
3434
/*
3535
* Authors:
36-
* Gonzalo Paniagua Javier <[email protected]>
36+
* - Gonzalo Paniagua Javier <[email protected]>
3737
*/
3838
#endregion
3939

@@ -100,27 +100,25 @@ enum LineState {
100100

101101
#region Public Constructors
102102

103-
public HttpConnection (
104-
Socket socket,
105-
EndPointListener listener,
106-
bool secure,
107-
X509Certificate2 cert)
103+
public HttpConnection (Socket socket, EndPointListener listener)
108104
{
109105
_socket = socket;
110106
_epListener = listener;
111-
_secure = secure;
107+
_secure = listener.IsSecure;
112108

113109
var netStream = new NetworkStream (socket, false);
114-
if (!secure)
110+
if (!_secure) {
115111
_stream = netStream;
112+
}
116113
else {
117114
var sslStream = new SslStream (netStream, false);
118-
sslStream.AuthenticateAsServer (cert);
115+
sslStream.AuthenticateAsServer (listener.Certificate);
119116
_stream = sslStream;
120117
}
121118

122119
_timeout = 90000; // 90k ms for first request, 15k ms from then on.
123-
_timer = new Timer (onTimeout, null, Timeout.Infinite, Timeout.Infinite);
120+
_timer = new Timer (onTimeout, this, Timeout.Infinite, Timeout.Infinite);
121+
124122
init ();
125123
}
126124

@@ -211,80 +209,77 @@ private void init ()
211209
private static void onRead (IAsyncResult asyncResult)
212210
{
213211
var conn = (HttpConnection) asyncResult.AsyncState;
214-
conn.onReadInternal (asyncResult);
215-
}
216-
217-
private void onReadInternal (IAsyncResult asyncResult)
218-
{
219-
_timer.Change (Timeout.Infinite, Timeout.Infinite);
212+
conn._timer.Change (Timeout.Infinite, Timeout.Infinite);
220213

221214
var read = -1;
222215
try {
223-
read = _stream.EndRead (asyncResult);
224-
_requestBuffer.Write (_buffer, 0, read);
225-
if (_requestBuffer.Length > 32768) {
226-
SendError ();
227-
Close (true);
216+
read = conn._stream.EndRead (asyncResult);
217+
conn._requestBuffer.Write (conn._buffer, 0, read);
218+
if (conn._requestBuffer.Length > 32768) {
219+
conn.SendError ();
220+
conn.Close (true);
228221

229222
return;
230223
}
231224
}
232225
catch {
233-
if (_requestBuffer != null && _requestBuffer.Length > 0)
234-
SendError ();
226+
if (conn._requestBuffer != null && conn._requestBuffer.Length > 0)
227+
conn.SendError ();
235228

236-
if (_socket != null) {
237-
closeSocket ();
238-
unbind ();
229+
if (conn._socket != null) {
230+
conn.closeSocket ();
231+
conn.unbind ();
239232
}
240233

241234
return;
242235
}
243236

244237
if (read <= 0) {
245-
closeSocket ();
246-
unbind ();
238+
conn.closeSocket ();
239+
conn.unbind ();
247240

248241
return;
249242
}
250243

251-
if (processInput (_requestBuffer.GetBuffer ())) {
252-
if (!_context.HaveError)
253-
_context.Request.FinishInitialization ();
244+
if (conn.processInput (conn._requestBuffer.GetBuffer ())) {
245+
if (!conn._context.HaveError) {
246+
conn._context.Request.FinishInitialization ();
247+
}
254248
else {
255-
SendError ();
256-
Close (true);
249+
conn.SendError ();
250+
conn.Close (true);
257251

258252
return;
259253
}
260254

261-
if (!_epListener.BindContext (_context)) {
262-
SendError ("Invalid host", 400);
263-
Close (true);
255+
if (!conn._epListener.BindContext (conn._context)) {
256+
conn.SendError ("Invalid host.", 400);
257+
conn.Close (true);
264258

265259
return;
266260
}
267261

268-
var listener = _context.Listener;
269-
if (_lastListener != listener) {
270-
removeConnection ();
271-
listener.AddConnection (this);
272-
_lastListener = listener;
262+
var listener = conn._context.Listener;
263+
if (conn._lastListener != listener) {
264+
conn.removeConnection ();
265+
listener.AddConnection (conn);
266+
conn._lastListener = listener;
273267
}
274268

275-
_contextWasBound = true;
276-
listener.RegisterContext (_context);
269+
conn._contextWasBound = true;
270+
listener.RegisterContext (conn._context);
277271

278272
return;
279273
}
280274

281-
_stream.BeginRead (_buffer, 0, _bufferSize, onRead, this);
275+
conn._stream.BeginRead (conn._buffer, 0, _bufferSize, onRead, conn);
282276
}
283277

284-
private void onTimeout (object unused)
278+
private static void onTimeout (object state)
285279
{
286-
closeSocket ();
287-
unbind ();
280+
var conn = (HttpConnection) state;
281+
conn.closeSocket ();
282+
conn.unbind ();
288283
}
289284

290285
// true -> Done processing.
@@ -295,8 +290,7 @@ private bool processInput (byte [] data)
295290
var used = 0;
296291
string line;
297292
try {
298-
while ((line = readLine (
299-
data, _position, length - _position, ref used)) != null) {
293+
while ((line = readLine (data, _position, length - _position, ref used)) != null) {
300294
_position += used;
301295
if (line.Length == 0) {
302296
if (_inputState == InputState.RequestLine)
@@ -332,8 +326,7 @@ private bool processInput (byte [] data)
332326
return false;
333327
}
334328

335-
private string readLine (
336-
byte [] buffer, int offset, int length, ref int used)
329+
private string readLine (byte [] buffer, int offset, int length, ref int used)
337330
{
338331
if (_currentLine == null)
339332
_currentLine = new StringBuilder ();
@@ -343,15 +336,12 @@ private string readLine (
343336
for (int i = offset; i < last && _lineState != LineState.LF; i++) {
344337
used++;
345338
var b = buffer [i];
346-
if (b == 13) {
339+
if (b == 13)
347340
_lineState = LineState.CR;
348-
}
349-
else if (b == 10) {
341+
else if (b == 10)
350342
_lineState = LineState.LF;
351-
}
352-
else {
343+
else
353344
_currentLine.Append ((char) b);
354-
}
355345
}
356346

357347
string result = null;
@@ -505,8 +495,8 @@ public void SendError (string message, int status)
505495

506496
var description = status.GetStatusDescription ();
507497
var error = message != null && message.Length > 0
508-
? String.Format ("<h1>{0} ({1})</h1>", description, message)
509-
: String.Format ("<h1>{0}</h1>", description);
498+
? String.Format ("<h1>{0} ({1})</h1>", description, message)
499+
: String.Format ("<h1>{0}</h1>", description);
510500

511501
var entity = res.ContentEncoding.GetBytes (error);
512502
res.Close (entity, false);

websocket-sharp/Net/HttpListener.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,8 @@ private void sendServiceUnavailable ()
452452

453453
internal void AddConnection (HttpConnection connection)
454454
{
455-
_connections [connection] = connection;
455+
lock (((ICollection) _connections).SyncRoot)
456+
_connections [connection] = connection;
456457
}
457458

458459
internal ListenerAsyncResult BeginGetContext (ListenerAsyncResult asyncResult)
@@ -509,7 +510,8 @@ internal void RegisterContext (HttpListenerContext context)
509510

510511
internal void RemoveConnection (HttpConnection connection)
511512
{
512-
_connections.Remove (connection);
513+
lock (((ICollection) _connections).SyncRoot)
514+
_connections.Remove (connection);
513515
}
514516

515517
internal AuthenticationSchemes SelectAuthenticationScheme (HttpListenerContext context)

0 commit comments

Comments
 (0)