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"] = "*"