|
|
@@ -21,13 +21,43 @@ func ParseHttp(payload []byte) (string, string) {
|
|
|
return string(method), string(uri)
|
|
|
}
|
|
|
|
|
|
-func ParseHttpHost(payload []byte, isTls bool) (string, string, string, uint16) {
|
|
|
+// parseHttpHeader 解析 HTTP 头部字段的值
|
|
|
+func parseHttpHeader(payload []byte, headerName string) string {
|
|
|
+ headerBytes := []byte(headerName)
|
|
|
+ headerIdx := bytes.Index(payload, headerBytes)
|
|
|
+ if headerIdx == -1 {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+
|
|
|
+ headerStart := headerIdx + len(headerBytes)
|
|
|
+ if headerStart >= len(payload) {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查找头部值的结束位置(\r\n)
|
|
|
+ headerEnd := bytes.Index(payload[headerStart:], []byte("\r\n"))
|
|
|
+ if headerEnd == -1 {
|
|
|
+ // 如果没有找到 \r\n,使用剩余数据的长度
|
|
|
+ headerEnd = len(payload) - headerStart
|
|
|
+ }
|
|
|
+
|
|
|
+ if headerEnd <= 0 {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+
|
|
|
+ headerValue := payload[headerStart : headerStart+headerEnd]
|
|
|
+ return string(bytes.TrimSpace(headerValue))
|
|
|
+}
|
|
|
+
|
|
|
+// parseHttpHostCommon 解析 HTTP 请求的公共逻辑,返回 method, requestURI, host, port, rest
|
|
|
+// rest 是解析完 method 和 uri 后剩余的 payload 部分,用于后续解析其他头部
|
|
|
+func parseHttpHostCommon(payload []byte, isTls bool) (string, string, string, uint16, []byte) {
|
|
|
method, rest, ok := bytes.Cut(payload, space)
|
|
|
if !ok {
|
|
|
- return "", "", "", 0
|
|
|
+ return "", "", "", 0, nil
|
|
|
}
|
|
|
if !isHttpMethod(string(method)) {
|
|
|
- return "", "", "", 0
|
|
|
+ return "", "", "", 0, nil
|
|
|
}
|
|
|
|
|
|
uri, rest, ok := bytes.Cut(rest, space)
|
|
|
@@ -35,21 +65,12 @@ func ParseHttpHost(payload []byte, isTls bool) (string, string, string, uint16)
|
|
|
uri = append(uri, []byte("...")...)
|
|
|
}
|
|
|
|
|
|
- //hostStart := bytes.Index(rest, []byte("Host:")) + len("Host:")
|
|
|
- hostHeader := "Host:"
|
|
|
- hostIdx := bytes.Index(rest, []byte(hostHeader))
|
|
|
- if hostIdx == -1 {
|
|
|
- return string(method), string(uri), "", 0
|
|
|
- }
|
|
|
- hostStart := hostIdx + len(hostHeader)
|
|
|
- hostEnd := bytes.Index(rest[hostStart:], []byte("\r\n"))
|
|
|
- var hostPortBts []byte
|
|
|
- if hostEnd == -1 {
|
|
|
- hostPortBts = rest[hostStart:]
|
|
|
- } else {
|
|
|
- hostPortBts = rest[hostStart : hostStart+hostEnd]
|
|
|
+ // 解析 Host 头部
|
|
|
+ hostPort := parseHttpHeader(rest, "Host:")
|
|
|
+ if hostPort == "" {
|
|
|
+ return string(method), string(uri), "", 0, rest
|
|
|
}
|
|
|
- hostPort := string(bytes.TrimSpace(hostPortBts))
|
|
|
+
|
|
|
hostParts := strings.Split(hostPort, ":")
|
|
|
host := hostParts[0]
|
|
|
|
|
|
@@ -69,5 +90,21 @@ func ParseHttpHost(payload []byte, isTls bool) (string, string, string, uint16)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return string(method), string(uri), host, port
|
|
|
+ return string(method), string(uri), host, port, rest
|
|
|
+}
|
|
|
+
|
|
|
+// ParseHttpHost 解析 HTTP 请求,返回 method, requestURI, host, port
|
|
|
+// 不解析 User-Agent,性能更优
|
|
|
+func ParseHttpHost(payload []byte, isTls bool) (string, string, string, uint16) {
|
|
|
+ method, uri, host, port, _ := parseHttpHostCommon(payload, isTls)
|
|
|
+ return method, uri, host, port
|
|
|
+}
|
|
|
+
|
|
|
+// ParseHttpHostWithUserAgent 解析 HTTP 请求,返回 method, requestURI, host, port, userAgent
|
|
|
+// 包含 User-Agent 解析,适用于需要 User-Agent 信息的场景
|
|
|
+func ParseHttpHostWithUserAgent(payload []byte, isTls bool) (string, string, string, uint16, string) {
|
|
|
+ method, uri, host, port, rest := parseHttpHostCommon(payload, isTls)
|
|
|
+ // 解析 User-Agent 头部
|
|
|
+ userAgent := parseHttpHeader(rest, "User-Agent:")
|
|
|
+ return method, uri, host, port, userAgent
|
|
|
}
|