From c85d08d7a41320753150ff2c07462ef58218ee5f Mon Sep 17 00:00:00 2001 From: wood chen Date: Sat, 22 Mar 2025 16:03:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=A8=A1=E5=9D=97=E5=B9=B6=E6=9B=B4=E6=96=B0=E7=9B=AE?= =?UTF-8?q?=E6=A0=87URL=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E8=BF=94=E5=9B=9E=E5=80=BC=E4=BB=A5=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=87=E7=94=A8=E7=9B=AE=E6=A0=87=E6=A0=87=E8=AE=B0?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/errors/errors.go | 22 ---------------------- internal/handler/proxy.go | 13 ++++++++++++- internal/utils/utils.go | 9 +++++---- 3 files changed, 17 insertions(+), 27 deletions(-) delete mode 100644 internal/errors/errors.go diff --git a/internal/errors/errors.go b/internal/errors/errors.go deleted file mode 100644 index 15adeab..0000000 --- a/internal/errors/errors.go +++ /dev/null @@ -1,22 +0,0 @@ -package errors - -type ErrorCode int - -const ( - ErrInvalidConfig ErrorCode = iota + 1 - ErrRateLimit - ErrMetricsCollection -) - -type MetricsError struct { - Code ErrorCode - Message string - Err error -} - -func (e *MetricsError) Error() string { - if e.Err != nil { - return e.Message + ": " + e.Err.Error() - } - return e.Message -} diff --git a/internal/handler/proxy.go b/internal/handler/proxy.go index 78382d7..f93e334 100644 --- a/internal/handler/proxy.go +++ b/internal/handler/proxy.go @@ -195,7 +195,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } // 使用统一的路由选择逻辑 - targetBase := utils.GetTargetURL(h.client, r, pathConfig, decodedPath) + targetBase, usedAltTarget := utils.GetTargetURL(h.client, r, pathConfig, decodedPath) // 重新编码路径,保留 '/' parts := strings.Split(decodedPath, "/") @@ -277,6 +277,12 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Encoding", item.ContentEncoding) } w.Header().Set("Proxy-Go-Cache", "HIT") + + // 如果使用了扩展名映射的备用目标,添加标记响应头 + if usedAltTarget { + w.Header().Set("Proxy-Go-AltTarget", "true") + } + if notModified { w.WriteHeader(http.StatusNotModified) return @@ -305,6 +311,11 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { copyHeader(w.Header(), resp.Header) w.Header().Set("Proxy-Go-Cache", "MISS") + // 如果使用了扩展名映射的备用目标,添加标记响应头 + if usedAltTarget { + w.Header().Set("Proxy-Go-AltTarget", "true") + } + // 设置响应状态码 w.WriteHeader(resp.StatusCode) diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 5725c7a..c4569cb 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -183,9 +183,10 @@ func GetFileSize(client *http.Client, url string) (int64, error) { } // GetTargetURL 根据路径和配置决定目标URL -func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathConfig, path string) string { +func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathConfig, path string) (string, bool) { // 默认使用默认目标 targetBase := pathConfig.DefaultTarget + usedAltTarget := false // 如果配置了扩展名映射 if pathConfig.ExtensionMap != nil { @@ -198,7 +199,7 @@ func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathCo contentLength, err := GetFileSize(client, targetBase+path) if err != nil { log.Printf("[Route] %s -> %s (error getting size: %v)", path, targetBase, err) - return targetBase + return targetBase, false } // 如果没有设置最小阈值,使用默认值 500KB @@ -244,7 +245,7 @@ func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathCo log.Printf("[Route] %s -> %s (size: %s > %s and <= %s)", path, altTarget, FormatBytes(contentLength), FormatBytes(minThreshold), FormatBytes(maxThreshold)) - return altTarget + return altTarget, true } log.Printf("[Route] %s -> %s (fallback: alternative target not accessible)", path, targetBase) @@ -269,7 +270,7 @@ func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathCo log.Printf("[Route] %s -> %s (no extension map)", path, targetBase) } - return targetBase + return targetBase, usedAltTarget } // isTargetAccessible 检查目标URL是否可访问