Skip to content

Commit

Permalink
fix 修正Packet设置元素的错误,该错误导致websocket数据解密出错
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jun 29, 2022
1 parent 0ef79fc commit f754a9b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
8 changes: 5 additions & 3 deletions NewLife.Core/Data/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,17 @@ public Byte this[Int32 index]

// 设置 对应索引 的数据 应该也是针对整个链表的有效数据区
var p = Offset + index;
if (p >= Offset + Count)
if (index >= Count)
{
if (Next == null) throw new IndexOutOfRangeException($"索引[{index}]越界[>{Total - 1}]");

Next[p - Data.Length] = value;
}
if(p < Count)
else
{
Data[p] = value;

}

// 基础类需要严谨给出明确功用,不能模棱两可,因此不能越界
}
}
Expand Down
5 changes: 5 additions & 0 deletions NewLife.Core/Http/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class WebSocket
/// <summary>Http上下文</summary>
public IHttpContext Context { get; set; }

/// <summary>版本</summary>
public String Version { get; set; }

/// <summary>活跃时间</summary>
public DateTime ActiveTime { get; set; }
#endregion
Expand Down Expand Up @@ -55,6 +58,8 @@ public static WebSocket Handshake(IHttpContext context)
};
if (context is DefaultHttpContext dhc) dhc.WebSocket = manager;

if (request.Headers.TryGetValue("Sec-WebSocket-Version", out var ver)) manager.Version = ver;

return manager;
}

Expand Down
4 changes: 3 additions & 1 deletion XUnitTest.Core/Http/HttpServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ public async void MapWebSocket()
{
_server.Map("/ws", new WebSocketHandler());

var content = "Hello NewLife".GetBytes();

var client = new ClientWebSocket();
await client.ConnectAsync(new Uri("ws://127.0.0.1:8080/ws"), default);
await client.SendAsync("Hello NewLife".GetBytes(), System.Net.WebSockets.WebSocketMessageType.Text, true, default);
await client.SendAsync(content, System.Net.WebSockets.WebSocketMessageType.Text, true, default);

var buf = new Byte[1024];
var rs = await client.ReceiveAsync(buf, default);
Expand Down

0 comments on commit f754a9b

Please sign in to comment.