diff --git a/pkg/rest/api.go b/pkg/rest/api.go index cbcfc378..36548b3c 100644 --- a/pkg/rest/api.go +++ b/pkg/rest/api.go @@ -147,12 +147,12 @@ func PutConfig(w http.ResponseWriter, r *http.Request) { func DoProxy(w http.ResponseWriter, r *http.Request) { target := r.Header.Get("X-Target-Uri") if target == "" { - WriteJson(w, model.NewErrorResult("param invalid: X-Target-Uri", model.CodeInvalidParam)) + writeError(w, "param invalid: X-Target-Uri") return } targetUrl, err := url.Parse(target) if err != nil { - WriteJson(w, model.NewErrorResult(err.Error())) + writeError(w, err.Error()) return } r.RequestURI = "" @@ -160,7 +160,7 @@ func DoProxy(w http.ResponseWriter, r *http.Request) { r.Host = targetUrl.Host resp, err := http.DefaultClient.Do(r) if err != nil { - WriteJson(w, model.NewErrorResult(err.Error())) + writeError(w, err.Error()) return } defer resp.Body.Close() @@ -179,11 +179,16 @@ func DoProxy(w http.ResponseWriter, r *http.Request) { reader = resp.Body } if _, err := io.Copy(w, reader); err != nil { - WriteJson(w, model.NewErrorResult(err.Error())) + writeError(w, err.Error()) return } } +func writeError(w http.ResponseWriter, msg string) { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(msg)) +} + func getServerConfig() *download.DownloaderStoreConfig { _, cfg, _ := Downloader.GetConfig() return cfg diff --git a/pkg/rest/server_test.go b/pkg/rest/server_test.go index 748c7e7e..cb3d4da2 100644 --- a/pkg/rest/server_test.go +++ b/pkg/rest/server_test.go @@ -229,6 +229,15 @@ func TestDoProxy(t *testing.T) { t.Errorf("DoProxy() got = %v, want %v", got, want) } }) + + doTest(func() { + code, _ := doHttpRequest0(http.MethodGet, "/api/v1/proxy", map[string]string{ + "X-Target-Uri": "https://github.com/GopeedLab/gopeed/raw/695da7ea87d2b455552b709d3cb4d7879484d4d1/NOT_FOUND", + }, nil) + if code != http.StatusNotFound { + t.Errorf("DoProxy() got = %v, want %v", code, http.StatusNotFound) + } + }) } func TestAuthorization(t *testing.T) { @@ -307,14 +316,15 @@ func doHttpRequest0(method string, path string, headers map[string]string, body if err != nil { panic(err) } - if response.StatusCode != http.StatusOK { - panic(fmt.Sprintf("http request failed, status code: %d", response.StatusCode)) - } + return response.StatusCode, respBody } func doHttpRequest[T any](method string, path string, headers map[string]string, body any) (int, *model.Result[T]) { - _, respBody := doHttpRequest0(method, path, headers, body) + statusCode, respBody := doHttpRequest0(method, path, headers, body) + if statusCode != http.StatusOK { + panic(fmt.Sprintf("http request failed, status code: %d", statusCode)) + } var r model.Result[T] if err := json.Unmarshal(respBody, &r); err != nil { diff --git a/ui/flutter/lib/app/modules/app/controllers/app_controller.dart b/ui/flutter/lib/app/modules/app/controllers/app_controller.dart index e75f6864..ec2b03e7 100644 --- a/ui/flutter/lib/app/modules/app/controllers/app_controller.dart +++ b/ui/flutter/lib/app/modules/app/controllers/app_controller.dart @@ -120,7 +120,7 @@ class AppController extends GetxController { var allFail = true; for (var cdn in allTrackerSubscribeUrlCdns[u]!) { try { - result.addAll(await _fetchTrackers(u)); + result.addAll(await _fetchTrackers(cdn)); allFail = false; break; } catch (e) { @@ -168,10 +168,14 @@ class AppController extends GetxController { Future> _fetchTrackers(String subscribeUrl) async { final resp = await proxyRequest(subscribeUrl); if (resp.statusCode != 200) { - throw Exception('Failed to get trackers'); + throw Exception( + 'Failed to get trackers, status code: ${resp.statusCode}'); + } + if (resp.data == null || resp.data!.isEmpty) { + throw Exception('Failed to get trackers, data is null'); } const ls = LineSplitter(); - return ls.convert(resp.data).where((e) => e.isNotEmpty).toList(); + return ls.convert(resp.data!).where((e) => e.isNotEmpty).toList(); } _initDownloaderConfig() async {