Skip to content

Commit

Permalink
为二进制数据生成请求体内容。对超长内容(>1024)进行gzip压缩。主要用于星尘监控数据上传等接口
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jun 9, 2022
1 parent 9db447d commit c92c995
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
21 changes: 21 additions & 0 deletions NewLife.Core/IO/IOHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,27 @@ public static Stream DecompressGZip(this Stream inStream, Stream outStream = nul

return ms;
}

/// <summary>压缩字节数组</summary>
/// <param name="data">字节数组</param>
/// <returns></returns>
public static Byte[] CompressGZip(this Byte[] data)
{
var ms = new MemoryStream();
CompressGZip(new MemoryStream(data), ms);
return ms.ToArray();
}

/// <summary>解压缩字节数组</summary>
/// <returns>Deflate算法,如果是ZLIB格式,则前面多两个字节,解压缩之前去掉,RocketMQ中有用到</returns>
/// <param name="data">字节数组</param>
/// <returns></returns>
public static Byte[] DecompressGZip(this Byte[] data)
{
var ms = new MemoryStream();
DecompressGZip(new MemoryStream(data), ms);
return ms.ToArray();
}
#endregion

#region 复制数据流
Expand Down
10 changes: 9 additions & 1 deletion NewLife.Core/Log/ITracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ public static ISpan NewSpan(this ITracer tracer, HttpRequestMessage request)
return span;
}

/// <summary>支持作为标签数据的内容类型</summary>
static String[] _TagTypes = new[] {
"text/plain", "text/xml", "application/json", "application/xml", "application/x-www-form-urlencoded"
};
static String[] _ExcludeHeaders = new[] { "traceparent", "Cookie" };
private static ISpan CreateSpan(ITracer tracer, String method, Uri uri, HttpRequestMessage request)
{
Expand All @@ -331,7 +335,11 @@ private static ISpan CreateSpan(ITracer tracer, String method, Uri uri, HttpRequ

if (span is DefaultSpan ds && ds.TraceFlag > 0 && request != null)
{
if (request.Content != null)
if (request.Content is ByteArrayContent content &&
content.Headers.ContentLength != null &&
content.Headers.ContentLength < 1024 * 8 &&
content.Headers.ContentType != null &&
content.Headers.ContentType.MediaType.StartsWithIgnoreCase(_TagTypes))
{
// 既然都读出来了,不管多长,都要前面1024字符
var str = request.Content.ReadAsStringAsync().Result;
Expand Down
4 changes: 4 additions & 0 deletions NewLife.Core/Net/Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class Setting : Config<Setting>
/// <summary>启用Http压缩。内部新建的HttpClient将自动添加接受压缩的头部,并在响应中对压缩进行解码,默认true</summary>
[Description("启用Http压缩。内部新建的HttpClient将自动添加接受压缩的头部,并在响应中对压缩进行解码,默认true")]
public Boolean EnableHttpCompression { get; set; } = true;

/// <summary>自动启用GZip压缩的请求体大小。默认1024,用0表示不压缩</summary>
[Description("自动启用GZip压缩的请求体大小。默认1024,用0表示不压缩")]
public Int32 AutoGZip { get; set; } = 1024;
#endregion

#region 方法
Expand Down
35 changes: 26 additions & 9 deletions NewLife.Core/Remoting/ApiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,11 @@ public static HttpRequestMessage BuildRequest(HttpMethod method, String action,
{
if (args is Packet pk)
{
var content =
pk.Next == null ?
new ByteArrayContent(pk.Data, pk.Offset, pk.Count) :
new ByteArrayContent(pk.ToArray());
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
request.Content = content;
request.Content = BuildContent(pk);
}
else if (args is Byte[] buf)
{
var content = new ByteArrayContent(buf);
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
request.Content = content;
request.Content = BuildContent(buf);
}
else if (args != null)
{
Expand All @@ -172,6 +165,30 @@ public static HttpRequestMessage BuildRequest(HttpMethod method, String action,
return request;
}

/// <summary>为二进制数据生成请求体内容。对超长内容进行压缩</summary>
/// <param name="pk"></param>
/// <returns></returns>
public static HttpContent BuildContent(Packet pk)
{
var gzip = NewLife.Net.Setting.Current.AutoGZip;
if (gzip > 0 && pk.Total >= gzip)
{
var buf = pk.ReadBytes();
buf = buf.CompressGZip();
var content = new ByteArrayContent(buf);
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-gzip");
return content;
}
else
{
var content = pk.Next == null ?
new ByteArrayContent(pk.Data, pk.Offset, pk.Count) :
new ByteArrayContent(pk.ToArray());
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return content;
}
}

/// <summary>结果代码名称。默认 code/errcode</summary>
public static IList<String> CodeNames { get; } = new List<String> { "code", "errcode" };

Expand Down

0 comments on commit c92c995

Please sign in to comment.