feat: 优化URL处理逻辑,清理多余空格和重复协议前缀
- 在 handleGet 和 handlePost 函数中添加URL清理逻辑,去除多余的空格和重复的 http:// 或 https:// 前缀。 - 确保在URL没有协议前缀时自动添加 http://。 - 更新响应结构,新增 statuscode 字段以更好地反映请求状态。
This commit is contained in:
@@ -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"] = "*"
|
||||
|
||||
Reference in New Issue
Block a user