From 23c88b5f481e20b9e0dae040283cd2311b3ffc98 Mon Sep 17 00:00:00 2001 From: yoyo Date: Sat, 27 Dec 2025 21:58:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96URL=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E6=B8=85=E7=90=86=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E7=A9=BA=E6=A0=BC=E5=92=8C=E9=87=8D=E5=A4=8D=E5=8D=8F=E8=AE=AE?= =?UTF-8?q?=E5=89=8D=E7=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 handleGet 和 handlePost 函数中添加URL清理逻辑,去除多余的空格和重复的 http:// 或 https:// 前缀。 - 确保在URL没有协议前缀时自动添加 http://。 - 更新响应结构,新增 statuscode 字段以更好地反映请求状态。 --- internal/handler/get.go | 54 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/internal/handler/get.go b/internal/handler/get.go index c233ea8..f031120 100644 --- a/internal/handler/get.go +++ b/internal/handler/get.go @@ -114,7 +114,30 @@ func handleGet(c *gin.Context, urlStr string, params map[string]interface{}) { seq = seqVal } - // 解析URL + // 清理URL:去除多余的空格和重复的协议前缀 + urlStr = strings.TrimSpace(urlStr) + // 如果URL中包含多个 http:// 或 https://,只保留第一个 + if strings.Contains(urlStr, "http://") { + // 找到第一个 http:// 的位置 + firstHttp := strings.Index(urlStr, "http://") + if firstHttp > 0 { + // 如果 http:// 不在开头,说明前面有内容,需要清理 + urlStr = urlStr[firstHttp:] + } + // 移除后续重复的 http:// + urlStr = strings.Replace(urlStr, "http://http://", "http://", -1) + urlStr = strings.Replace(urlStr, "http://https://", "https://", -1) + } + if strings.Contains(urlStr, "https://") { + firstHttps := strings.Index(urlStr, "https://") + if firstHttps > 0 { + urlStr = urlStr[firstHttps:] + } + urlStr = strings.Replace(urlStr, "https://https://", "https://", -1) + urlStr = strings.Replace(urlStr, "https://http://", "http://", -1) + } + + // 如果URL没有协议前缀,添加 http:// if !strings.HasPrefix(urlStr, "http://") && !strings.HasPrefix(urlStr, "https://") { urlStr = "http://" + urlStr } @@ -194,6 +217,7 @@ func handleGet(c *gin.Context, urlStr string, params map[string]interface{}) { result["ip"] = "访问失败" } result["error"] = errMsg + result["statuscode"] = 0 result["totaltime"] = "*" result["downtime"] = "*" result["downsize"] = "*" @@ -212,6 +236,7 @@ func handleGet(c *gin.Context, urlStr string, params map[string]interface{}) { // 没有响应也没有错误,不应该发生 result["error"] = "未知错误" result["ip"] = "访问失败" + result["statuscode"] = 0 result["totaltime"] = "*" result["downtime"] = "*" result["downsize"] = "*" @@ -313,7 +338,30 @@ func handlePost(c *gin.Context, urlStr string, params map[string]interface{}) { seq = seqVal } - // 解析URL + // 清理URL:去除多余的空格和重复的协议前缀 + urlStr = strings.TrimSpace(urlStr) + // 如果URL中包含多个 http:// 或 https://,只保留第一个 + if strings.Contains(urlStr, "http://") { + // 找到第一个 http:// 的位置 + firstHttp := strings.Index(urlStr, "http://") + if firstHttp > 0 { + // 如果 http:// 不在开头,说明前面有内容,需要清理 + urlStr = urlStr[firstHttp:] + } + // 移除后续重复的 http:// + urlStr = strings.Replace(urlStr, "http://http://", "http://", -1) + urlStr = strings.Replace(urlStr, "http://https://", "https://", -1) + } + if strings.Contains(urlStr, "https://") { + firstHttps := strings.Index(urlStr, "https://") + if firstHttps > 0 { + urlStr = urlStr[firstHttps:] + } + urlStr = strings.Replace(urlStr, "https://https://", "https://", -1) + urlStr = strings.Replace(urlStr, "https://http://", "http://", -1) + } + + // 如果URL没有协议前缀,添加 http:// if !strings.HasPrefix(urlStr, "http://") && !strings.HasPrefix(urlStr, "https://") { urlStr = "http://" + urlStr } @@ -394,6 +442,7 @@ func handlePost(c *gin.Context, urlStr string, params map[string]interface{}) { result["ip"] = "访问失败" } result["error"] = errMsg + result["statuscode"] = 0 result["totaltime"] = "*" result["downtime"] = "*" result["downsize"] = "*" @@ -412,6 +461,7 @@ func handlePost(c *gin.Context, urlStr string, params map[string]interface{}) { // 没有响应也没有错误,不应该发生 result["error"] = "未知错误" result["ip"] = "访问失败" + result["statuscode"] = 0 result["totaltime"] = "*" result["downtime"] = "*" result["downsize"] = "*"