mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-19 04:10:30 +08:00
优化代码
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
package nodes
|
package nodes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/rpc"
|
"github.com/TeaOSLab/EdgeNode/internal/rpc"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -108,12 +108,37 @@ Loop:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *HTTPAccessLogQueue) toValidUTF8(accessLog *pb.HTTPAccessLog) {
|
func (this *HTTPAccessLogQueue) toValidUTF8(accessLog *pb.HTTPAccessLog) {
|
||||||
var v = reflect.Indirect(reflect.ValueOf(accessLog))
|
accessLog.RemoteUser = this.toValidUTF8string(accessLog.RemoteUser)
|
||||||
var countFields = v.NumField()
|
accessLog.RequestURI = this.toValidUTF8string(accessLog.RequestURI)
|
||||||
for i := 0; i < countFields; i++ {
|
accessLog.RequestPath = this.toValidUTF8string(accessLog.RequestPath)
|
||||||
var field = v.Field(i)
|
accessLog.RequestFilename = this.toValidUTF8string(accessLog.RequestFilename)
|
||||||
if field.Kind() == reflect.String {
|
accessLog.RequestBody = bytes.ToValidUTF8(accessLog.RequestBody, []byte{})
|
||||||
field.SetString(strings.ToValidUTF8(field.String(), ""))
|
|
||||||
|
for _, v := range accessLog.SentHeader {
|
||||||
|
for index, s := range v.Values {
|
||||||
|
v.Values[index] = this.toValidUTF8string(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
accessLog.Referer = this.toValidUTF8string(accessLog.Referer)
|
||||||
|
accessLog.UserAgent = this.toValidUTF8string(accessLog.UserAgent)
|
||||||
|
accessLog.Request = this.toValidUTF8string(accessLog.Request)
|
||||||
|
accessLog.ContentType = this.toValidUTF8string(accessLog.ContentType)
|
||||||
|
|
||||||
|
for k, c := range accessLog.Cookie {
|
||||||
|
accessLog.Cookie[k] = this.toValidUTF8string(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
accessLog.Args = this.toValidUTF8string(accessLog.Args)
|
||||||
|
accessLog.QueryString = this.toValidUTF8string(accessLog.QueryString)
|
||||||
|
|
||||||
|
for _, v := range accessLog.Header {
|
||||||
|
for index, s := range v.Values {
|
||||||
|
v.Values[index] = this.toValidUTF8string(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *HTTPAccessLogQueue) toValidUTF8string(v string) string {
|
||||||
|
return strings.ToValidUTF8(v, "")
|
||||||
|
}
|
||||||
|
|||||||
@@ -32,17 +32,28 @@ func TestHTTPAccessLogQueue_Push(t *testing.T) {
|
|||||||
|
|
||||||
//bytes = []byte("真不错")
|
//bytes = []byte("真不错")
|
||||||
|
|
||||||
|
var accessLog = &pb.HTTPAccessLog{
|
||||||
|
ServerId: 23,
|
||||||
|
RequestId: strconv.FormatInt(time.Now().Unix(), 10) + strconv.Itoa(requestId) + strconv.FormatInt(1, 10),
|
||||||
|
NodeId: 48,
|
||||||
|
Host: "www.hello.com",
|
||||||
|
RequestURI: string(utf8Bytes),
|
||||||
|
RequestPath: string(utf8Bytes),
|
||||||
|
Timestamp: time.Now().Unix(),
|
||||||
|
Cookie: map[string]string{"test": string(utf8Bytes)},
|
||||||
|
|
||||||
|
Header: map[string]*pb.Strings{
|
||||||
|
"test": {Values: []string{string(utf8Bytes)}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
new(HTTPAccessLogQueue).toValidUTF8(accessLog)
|
||||||
|
|
||||||
|
// logs.PrintAsJSON(accessLog)
|
||||||
|
|
||||||
//t.Log(strings.ToValidUTF8(string(utf8Bytes), ""))
|
//t.Log(strings.ToValidUTF8(string(utf8Bytes), ""))
|
||||||
_, err = client.HTTPAccessLogRPC().CreateHTTPAccessLogs(client.Context(), &pb.CreateHTTPAccessLogsRequest{HttpAccessLogs: []*pb.HTTPAccessLog{
|
_, err = client.HTTPAccessLogRPC().CreateHTTPAccessLogs(client.Context(), &pb.CreateHTTPAccessLogsRequest{HttpAccessLogs: []*pb.HTTPAccessLog{
|
||||||
{
|
accessLog,
|
||||||
ServerId: 23,
|
|
||||||
RequestId: strconv.FormatInt(time.Now().Unix(), 10) + strconv.Itoa(requestId) + strconv.FormatInt(1, 10),
|
|
||||||
NodeId: 48,
|
|
||||||
Host: "www.hello.com",
|
|
||||||
RequestURI: string(utf8Bytes),
|
|
||||||
RequestPath: string(utf8Bytes),
|
|
||||||
Timestamp: time.Now().Unix(),
|
|
||||||
},
|
|
||||||
}})
|
}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// 这里只是为了重现错误
|
// 这里只是为了重现错误
|
||||||
|
|||||||
Reference in New Issue
Block a user