aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Servers/HttpServer/WebsocketServerHandler.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/Servers/HttpServer/WebsocketServerHandler.cs')
-rw-r--r--OpenSim/Framework/Servers/HttpServer/WebsocketServerHandler.cs82
1 files changed, 41 insertions, 41 deletions
diff --git a/OpenSim/Framework/Servers/HttpServer/WebsocketServerHandler.cs b/OpenSim/Framework/Servers/HttpServer/WebsocketServerHandler.cs
index c2925e3..c8af90f 100644
--- a/OpenSim/Framework/Servers/HttpServer/WebsocketServerHandler.cs
+++ b/OpenSim/Framework/Servers/HttpServer/WebsocketServerHandler.cs
@@ -75,7 +75,7 @@ namespace OpenSim.Framework.Servers.HttpServer
75 public event PongDelegate OnPong; 75 public event PongDelegate OnPong;
76 76
77 /// <summary> 77 /// <summary>
78 /// This is a regular HTTP Request... This may be removed in the future. 78 /// This is a regular HTTP Request... This may be removed in the future.
79 /// </summary> 79 /// </summary>
80// public event RegularHttpRequestDelegate OnRegularHttpRequest; 80// public event RegularHttpRequestDelegate OnRegularHttpRequest;
81 81
@@ -93,9 +93,9 @@ namespace OpenSim.Framework.Servers.HttpServer
93 /// When the websocket is closed, this will be fired. 93 /// When the websocket is closed, this will be fired.
94 /// </summary> 94 /// </summary>
95 public event CloseDelegate OnClose; 95 public event CloseDelegate OnClose;
96 96
97 /// <summary> 97 /// <summary>
98 /// Set this delegate to allow your module to validate the origin of the 98 /// Set this delegate to allow your module to validate the origin of the
99 /// Websocket request. Primary line of defense against cross site scripting 99 /// Websocket request. Primary line of defense against cross site scripting
100 /// </summary> 100 /// </summary>
101 public ValidateHandshake HandshakeValidateMethodOverride = null; 101 public ValidateHandshake HandshakeValidateMethodOverride = null;
@@ -181,7 +181,7 @@ namespace OpenSim.Framework.Servers.HttpServer
181 { 181 {
182 throw new InvalidOperationException("The socket has been shutdown"); 182 throw new InvalidOperationException("The socket has been shutdown");
183 } 183 }
184 } 184 }
185 set 185 set
186 { 186 {
187 if (_networkContext != null && _networkContext.Socket != null) 187 if (_networkContext != null && _networkContext.Socket != null)
@@ -194,8 +194,8 @@ namespace OpenSim.Framework.Servers.HttpServer
194 } 194 }
195 195
196 /// <summary> 196 /// <summary>
197 /// This triggers the websocket to start the upgrade process... 197 /// This triggers the websocket to start the upgrade process...
198 /// This is a Generalized Networking 'common sense' helper method. Some people expect to call Start() instead 198 /// This is a Generalized Networking 'common sense' helper method. Some people expect to call Start() instead
199 /// of the more context appropriate HandshakeAndUpgrade() 199 /// of the more context appropriate HandshakeAndUpgrade()
200 /// </summary> 200 /// </summary>
201 public void Start() 201 public void Start()
@@ -261,7 +261,7 @@ namespace OpenSim.Framework.Servers.HttpServer
261 acceptKey = GenerateAcceptKey(websocketKey); 261 acceptKey = GenerateAcceptKey(websocketKey);
262 string rawaccept = string.Format(HandshakeAcceptText, acceptKey); 262 string rawaccept = string.Format(HandshakeAcceptText, acceptKey);
263 SendUpgradeSuccess(rawaccept); 263 SendUpgradeSuccess(rawaccept);
264 264
265 265
266 } 266 }
267 else 267 else
@@ -282,7 +282,7 @@ namespace OpenSim.Framework.Servers.HttpServer
282 } 282 }
283 283
284 /// <summary> 284 /// <summary>
285 /// Generates a handshake response key string based on the client's 285 /// Generates a handshake response key string based on the client's
286 /// provided key to prove to the client that we're allowing the Websocket 286 /// provided key to prove to the client that we're allowing the Websocket
287 /// upgrade of our own free will and we were not coerced into doing it. 287 /// upgrade of our own free will and we were not coerced into doing it.
288 /// </summary> 288 /// </summary>
@@ -298,7 +298,7 @@ namespace OpenSim.Framework.Servers.HttpServer
298 SHA1 hashobj = SHA1.Create(); 298 SHA1 hashobj = SHA1.Create();
299 string ret = Convert.ToBase64String(hashobj.ComputeHash(Encoding.UTF8.GetBytes(acceptkey))); 299 string ret = Convert.ToBase64String(hashobj.ComputeHash(Encoding.UTF8.GetBytes(acceptkey)));
300 hashobj.Clear(); 300 hashobj.Clear();
301 301
302 return ret; 302 return ret;
303 } 303 }
304 304
@@ -310,11 +310,11 @@ namespace OpenSim.Framework.Servers.HttpServer
310 { 310 {
311 // Create a new websocket state so we can keep track of data in between network reads. 311 // Create a new websocket state so we can keep track of data in between network reads.
312 WebSocketState socketState = new WebSocketState() { ReceivedBytes = new List<byte>(), Header = WebsocketFrameHeader.HeaderDefault(), FrameComplete = true}; 312 WebSocketState socketState = new WebSocketState() { ReceivedBytes = new List<byte>(), Header = WebsocketFrameHeader.HeaderDefault(), FrameComplete = true};
313 313
314 byte[] bhandshakeResponse = Encoding.UTF8.GetBytes(pHandshakeResponse); 314 byte[] bhandshakeResponse = Encoding.UTF8.GetBytes(pHandshakeResponse);
315 315
316 316
317 317
318 318
319 try 319 try
320 { 320 {
@@ -324,7 +324,7 @@ namespace OpenSim.Framework.Servers.HttpServer
324 } 324 }
325 // Begin reading the TCP stream before writing the Upgrade success message to the other side of the stream. 325 // Begin reading the TCP stream before writing the Upgrade success message to the other side of the stream.
326 _networkContext.Stream.BeginRead(_buffer, 0, _bufferLength, OnReceive, socketState); 326 _networkContext.Stream.BeginRead(_buffer, 0, _bufferLength, OnReceive, socketState);
327 327
328 // Write the upgrade handshake success message 328 // Write the upgrade handshake success message
329 _networkContext.Stream.Write(bhandshakeResponse, 0, bhandshakeResponse.Length); 329 _networkContext.Stream.Write(bhandshakeResponse, 0, bhandshakeResponse.Length);
330 _networkContext.Stream.Flush(); 330 _networkContext.Stream.Flush();
@@ -345,7 +345,7 @@ namespace OpenSim.Framework.Servers.HttpServer
345 catch (ObjectDisposedException) 345 catch (ObjectDisposedException)
346 { 346 {
347 Close(string.Empty); 347 Close(string.Empty);
348 } 348 }
349 } 349 }
350 350
351 /// <summary> 351 /// <summary>
@@ -369,7 +369,7 @@ namespace OpenSim.Framework.Servers.HttpServer
369 369
370 /// <summary> 370 /// <summary>
371 /// This is our ugly Async OnReceive event handler. 371 /// This is our ugly Async OnReceive event handler.
372 /// This chunks the input stream based on the length of the provided buffer and processes out 372 /// This chunks the input stream based on the length of the provided buffer and processes out
373 /// as many frames as it can. It then moves the unprocessed data to the beginning of the buffer. 373 /// as many frames as it can. It then moves the unprocessed data to the beginning of the buffer.
374 /// </summary> 374 /// </summary>
375 /// <param name="ar">Our Async State from beginread</param> 375 /// <param name="ar">Our Async State from beginread</param>
@@ -390,7 +390,7 @@ namespace OpenSim.Framework.Servers.HttpServer
390 390
391 if (_bufferPosition > _bufferLength) 391 if (_bufferPosition > _bufferLength)
392 { 392 {
393 // Message too big for chunksize.. not sure how this happened... 393 // Message too big for chunksize.. not sure how this happened...
394 //Close(string.Empty); 394 //Close(string.Empty);
395 } 395 }
396 396
@@ -413,7 +413,7 @@ namespace OpenSim.Framework.Servers.HttpServer
413 if (pheader.PayloadLen > (ulong) _maxPayloadBytes) 413 if (pheader.PayloadLen > (ulong) _maxPayloadBytes)
414 { 414 {
415 Close("Invalid Payload size"); 415 Close("Invalid Payload size");
416 416
417 return; 417 return;
418 } 418 }
419 if (pheader.PayloadLen > 0) 419 if (pheader.PayloadLen > 0)
@@ -487,7 +487,7 @@ namespace OpenSim.Framework.Servers.HttpServer
487 _socketState.ReceivedBytes.Clear(); 487 _socketState.ReceivedBytes.Clear();
488 _socketState.ExpectedBytes = 0; 488 _socketState.ExpectedBytes = 0;
489 // do some processing 489 // do some processing
490 } 490 }
491 } 491 }
492 } 492 }
493 if (offset > 0) 493 if (offset > 0)
@@ -504,7 +504,7 @@ namespace OpenSim.Framework.Servers.HttpServer
504 } 504 }
505 else 505 else
506 { 506 {
507 // We can't read the stream anymore... 507 // We can't read the stream anymore...
508 } 508 }
509 } 509 }
510 catch (IOException) 510 catch (IOException)
@@ -533,7 +533,7 @@ namespace OpenSim.Framework.Servers.HttpServer
533 textMessageFrame.Header.Opcode = WebSocketReader.OpCode.Text; 533 textMessageFrame.Header.Opcode = WebSocketReader.OpCode.Text;
534 textMessageFrame.Header.IsEnd = true; 534 textMessageFrame.Header.IsEnd = true;
535 SendSocket(textMessageFrame.ToBytes()); 535 SendSocket(textMessageFrame.ToBytes());
536 536
537 } 537 }
538 538
539 public void SendData(byte[] data) 539 public void SendData(byte[] data)
@@ -657,7 +657,7 @@ namespace OpenSim.Framework.Servers.HttpServer
657 SendSocket(pongFrame.ToBytes()); 657 SendSocket(pongFrame.ToBytes());
658 break; 658 break;
659 case WebSocketReader.OpCode.Pong: 659 case WebSocketReader.OpCode.Pong:
660 660
661 PongDelegate pongD = OnPong; 661 PongDelegate pongD = OnPong;
662 if (pongD != null) 662 if (pongD != null)
663 { 663 {
@@ -701,7 +701,7 @@ namespace OpenSim.Framework.Servers.HttpServer
701 { 701 {
702 textD(this, new WebsocketTextEventArgs() { Data = Encoding.UTF8.GetString(psocketState.ReceivedBytes.ToArray()) }); 702 textD(this, new WebsocketTextEventArgs() { Data = Encoding.UTF8.GetString(psocketState.ReceivedBytes.ToArray()) });
703 } 703 }
704 704
705 // Send Done Event! 705 // Send Done Event!
706 } 706 }
707 break; 707 break;
@@ -719,7 +719,7 @@ namespace OpenSim.Framework.Servers.HttpServer
719 { 719 {
720 if (psocketState.ContinuationFrame.Header.Opcode == WebSocketReader.OpCode.Text) 720 if (psocketState.ContinuationFrame.Header.Opcode == WebSocketReader.OpCode.Text)
721 { 721 {
722 // Send Done event 722 // Send Done event
723 TextDelegate textD = OnText; 723 TextDelegate textD = OnText;
724 if (textD != null) 724 if (textD != null)
725 { 725 {
@@ -744,9 +744,9 @@ namespace OpenSim.Framework.Servers.HttpServer
744 break; 744 break;
745 case WebSocketReader.OpCode.Close: 745 case WebSocketReader.OpCode.Close:
746 Close(string.Empty); 746 Close(string.Empty);
747 747
748 break; 748 break;
749 749
750 } 750 }
751 psocketState.Header.SetDefault(); 751 psocketState.Header.SetDefault();
752 psocketState.ReceivedBytes.Clear(); 752 psocketState.ReceivedBytes.Clear();
@@ -837,12 +837,12 @@ namespace OpenSim.Framework.Servers.HttpServer
837 } 837 }
838 838
839 /// <summary> 839 /// <summary>
840 /// Attempts to read a header off the provided buffer. Returns true, exports a WebSocketFrameheader, 840 /// Attempts to read a header off the provided buffer. Returns true, exports a WebSocketFrameheader,
841 /// and an int to move the buffer forward when it reads a header. False when it can't read a header 841 /// and an int to move the buffer forward when it reads a header. False when it can't read a header
842 /// </summary> 842 /// </summary>
843 /// <param name="pBuffer">Bytes read from the stream</param> 843 /// <param name="pBuffer">Bytes read from the stream</param>
844 /// <param name="pOffset">Starting place in the stream to begin trying to read from</param> 844 /// <param name="pOffset">Starting place in the stream to begin trying to read from</param>
845 /// <param name="length">Lenth in the stream to try and read from. Provided for cases where the 845 /// <param name="length">Lenth in the stream to try and read from. Provided for cases where the
846 /// buffer's length is larger then the data in it</param> 846 /// buffer's length is larger then the data in it</param>
847 /// <param name="oHeader">Outputs the read WebSocket frame header</param> 847 /// <param name="oHeader">Outputs the read WebSocket frame header</param>
848 /// <param name="moveBuffer">Informs the calling stream to move the buffer forward</param> 848 /// <param name="moveBuffer">Informs the calling stream to move the buffer forward</param>
@@ -897,7 +897,7 @@ namespace OpenSim.Framework.Servers.HttpServer
897 oHeader.PayloadLen = BitConverter.ToUInt16(pBuffer, pOffset + index); 897 oHeader.PayloadLen = BitConverter.ToUInt16(pBuffer, pOffset + index);
898 index += 2; 898 index += 2;
899 break; 899 break;
900 case 127: // we got more this is a bigger frame 900 case 127: // we got more this is a bigger frame
901 // 8 bytes - uint64 - most significant bit 0 network byte order 901 // 8 bytes - uint64 - most significant bit 0 network byte order
902 minumheadersize += 8; 902 minumheadersize += 8;
903 if (length < minumheadersize) 903 if (length < minumheadersize)
@@ -909,7 +909,7 @@ namespace OpenSim.Framework.Servers.HttpServer
909 oHeader.PayloadLen = BitConverter.ToUInt64(pBuffer, pOffset + index); 909 oHeader.PayloadLen = BitConverter.ToUInt64(pBuffer, pOffset + index);
910 index += 8; 910 index += 8;
911 break; 911 break;
912 912
913 } 913 }
914 //oHeader.PayloadLeft = oHeader.PayloadLen; // Start the count in case it's chunked over the network. This is different then frame fragmentation 914 //oHeader.PayloadLeft = oHeader.PayloadLen; // Start the count in case it's chunked over the network. This is different then frame fragmentation
915 if (oHeader.IsMasked) 915 if (oHeader.IsMasked)
@@ -937,9 +937,9 @@ namespace OpenSim.Framework.Servers.HttpServer
937 /* 937 /*
938 * RFC6455 938 * RFC6455
939nib 0 1 2 3 4 5 6 7 939nib 0 1 2 3 4 5 6 7
940byt 0 1 2 3 940byt 0 1 2 3
941dec 0 1 2 3 941dec 0 1 2 3
942 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 942 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
943 +-+-+-+-+-------+-+-------------+-------------------------------+ 943 +-+-+-+-+-------+-+-------------+-------------------------------+
944 |F|R|R|R| opcode|M| Payload len | Extended payload length | 944 |F|R|R|R| opcode|M| Payload len | Extended payload length |
945 |I|S|S|S| (4) |A| (7) | (16/64) + 945 |I|S|S|S| (4) |A| (7) | (16/64) +
@@ -963,7 +963,7 @@ dec 0 1 2 3
963 public static readonly WebSocketFrame DefaultFrame = new WebSocketFrame(){Header = new WebsocketFrameHeader(),WebSocketPayload = new byte[0]}; 963 public static readonly WebSocketFrame DefaultFrame = new WebSocketFrame(){Header = new WebsocketFrameHeader(),WebSocketPayload = new byte[0]};
964 public WebsocketFrameHeader Header; 964 public WebsocketFrameHeader Header;
965 public byte[] WebSocketPayload; 965 public byte[] WebSocketPayload;
966 966
967 public byte[] ToBytes() 967 public byte[] ToBytes()
968 { 968 {
969 Header.PayloadLen = (ulong)WebSocketPayload.Length; 969 Header.PayloadLen = (ulong)WebSocketPayload.Length;
@@ -991,7 +991,7 @@ dec 0 1 2 3
991 public int Mask; 991 public int Mask;
992 /* 992 /*
993byt 0 1 2 3 993byt 0 1 2 3
994 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 994 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
995 +---------------+---------------+---------------+---------------+ 995 +---------------+---------------+---------------+---------------+
996 | Octal 1 | Octal 2 | Octal 3 | Octal 4 | 996 | Octal 1 | Octal 2 | Octal 3 | Octal 4 |
997 +---------------+---------------+---------------+---------------+ 997 +---------------+---------------+---------------+---------------+
@@ -1002,11 +1002,11 @@ byt 0 1 2 3
1002 1002
1003 public UInt64 PayloadLen; 1003 public UInt64 PayloadLen;
1004 //public UInt64 PayloadLeft; 1004 //public UInt64 PayloadLeft;
1005 // Payload is X + Y 1005 // Payload is X + Y
1006 //public UInt64 ExtensionDataLength; 1006 //public UInt64 ExtensionDataLength;
1007 //public UInt64 ApplicationDataLength; 1007 //public UInt64 ApplicationDataLength;
1008 public static readonly WebsocketFrameHeader ZeroHeader = WebsocketFrameHeader.HeaderDefault(); 1008 public static readonly WebsocketFrameHeader ZeroHeader = WebsocketFrameHeader.HeaderDefault();
1009 1009
1010 public void SetDefault() 1010 public void SetDefault()
1011 { 1011 {
1012 1012
@@ -1025,16 +1025,16 @@ byt 0 1 2 3
1025 /// <summary> 1025 /// <summary>
1026 /// Returns a byte array representing the Frame header 1026 /// Returns a byte array representing the Frame header
1027 /// </summary> 1027 /// </summary>
1028 /// <param name="payload">This is the frame data payload. The header describes the size of the payload. 1028 /// <param name="payload">This is the frame data payload. The header describes the size of the payload.
1029 /// If payload is null, a Zero sized payload is assumed</param> 1029 /// If payload is null, a Zero sized payload is assumed</param>
1030 /// <returns>Returns a byte array representing the frame header</returns> 1030 /// <returns>Returns a byte array representing the frame header</returns>
1031 public byte[] ToBytes(byte[] payload) 1031 public byte[] ToBytes(byte[] payload)
1032 { 1032 {
1033 List<byte> result = new List<byte>(); 1033 List<byte> result = new List<byte>();
1034 1034
1035 // Squeeze in our opcode and our ending bit. 1035 // Squeeze in our opcode and our ending bit.
1036 result.Add((byte)((byte)Opcode | (IsEnd?0x80:0x00) )); 1036 result.Add((byte)((byte)Opcode | (IsEnd?0x80:0x00) ));
1037 1037
1038 // Again with the three different byte interpretations of size.. 1038 // Again with the three different byte interpretations of size..
1039 1039
1040 //bytesize 1040 //bytesize
@@ -1056,7 +1056,7 @@ byt 0 1 2 3
1056 Array.Reverse(payloadLengthByte); 1056 Array.Reverse(payloadLengthByte);
1057 result.AddRange(payloadLengthByte); 1057 result.AddRange(payloadLengthByte);
1058 } 1058 }
1059 1059
1060 // Only add a payload if it's not null 1060 // Only add a payload if it's not null
1061 if (payload != null) 1061 if (payload != null)
1062 { 1062 {
@@ -1155,5 +1155,5 @@ byt 0 1 2 3
1155 1155
1156 } 1156 }
1157 1157
1158 1158
1159} 1159}