From 999bb7d65ecf61f59f8d54a60362307537e49030 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Tue, 25 Mar 2025 12:49:28 +0800 Subject: [PATCH] Add 403 and 501 pages --- http_error_page.go | 11 +++++++++++ http_handle_group_index.go | 4 ++-- http_handle_repo_contrib_one.go | 2 +- http_handle_users.go | 4 ++-- http_server.go | 6 +++--- templates/403.tmpl | 25 +++++++++++++++++++++++++ templates/501.tmpl | 24 ++++++++++++++++++++++++ diff --git a/http_error_page.go b/http_error_page.go index 0ddf055821ddc281ae3f8e2d13eaa80519d15bf5..355b5047795d6f3591c640e5c68735a7c775e3fe 100644 --- a/http_error_page.go +++ b/http_error_page.go @@ -18,6 +18,12 @@ params["complete_error_msg"] = msg _ = templates.ExecuteTemplate(w, "400", params) } +func errorPage403(w http.ResponseWriter, params map[string]any, msg string) { + w.WriteHeader(http.StatusForbidden) + params["complete_error_msg"] = msg + _ = templates.ExecuteTemplate(w, "403", params) +} + func errorPage451(w http.ResponseWriter, params map[string]any, msg string) { w.WriteHeader(http.StatusUnavailableForLegalReasons) params["complete_error_msg"] = msg @@ -29,3 +35,8 @@ w.WriteHeader(http.StatusInternalServerError) params["complete_error_msg"] = msg _ = templates.ExecuteTemplate(w, "500", params) } + +func errorPage501(w http.ResponseWriter, params map[string]any) { + w.WriteHeader(http.StatusNotImplemented) + _ = templates.ExecuteTemplate(w, "501", params) +} diff --git a/http_handle_group_index.go b/http_handle_group_index.go index d8e496f285737b595ec76f223b7ca3e008fb35f0..eef66821eb98a7fb0abcfbd2fd454e51dd0e606e 100644 --- a/http_handle_group_index.go +++ b/http_handle_group_index.go @@ -79,7 +79,7 @@ directAccess := (count > 0) if request.Method == http.MethodPost { if !directAccess { - http.Error(writer, "You do not have direct access to this group", http.StatusForbidden) + errorPage403(writer, params, "You do not have direct access to this group") return } @@ -87,7 +87,7 @@ repoName := request.FormValue("repo_name") repoDesc := request.FormValue("repo_desc") contribReq := request.FormValue("repo_contrib") if repoName == "" { - http.Error(writer, "Repo name is required", http.StatusBadRequest) + errorPage400(writer, params, "Repo name is required") return } diff --git a/http_handle_repo_contrib_one.go b/http_handle_repo_contrib_one.go index 4a5f6b8ae4e9ee852de7b99ad2446e8d58426f93..134f50b3d75631594ba6f1f0afc6fb9d635c6f41 100644 --- a/http_handle_repo_contrib_one.go +++ b/http_handle_repo_contrib_one.go @@ -26,7 +26,7 @@ mrIDStr = params["mr_id"].(string) mrIDInt64, err := strconv.ParseInt(mrIDStr, 10, strconv.IntSize) if err != nil { - http.Error(writer, "Merge request ID not an integer: "+err.Error(), http.StatusBadRequest) + errorPage400(writer, params, "Merge request ID not an integer") return } mrIDInt = int(mrIDInt64) diff --git a/http_handle_users.go b/http_handle_users.go index 2570de7323b684646ae6e09abd6f17105aeb3485..ba6755d002f3e80341aac25147de5f022fb74770 100644 --- a/http_handle_users.go +++ b/http_handle_users.go @@ -7,6 +7,6 @@ import ( "net/http" ) -func httpHandleUsers(writer http.ResponseWriter, _ *http.Request, _ map[string]any) { - http.Error(writer, "Not implemented", http.StatusNotImplemented) +func httpHandleUsers(writer http.ResponseWriter, _ *http.Request, params map[string]any) { + errorPage501(writer, params) } diff --git a/http_server.go b/http_server.go index 2bf1587f6ee5ba85937ec9781f5022f22379bdd2..c8528feac9d2a2c3883901c388bad0d09d0e3cfe 100644 --- a/http_server.go +++ b/http_server.go @@ -35,7 +35,7 @@ var sepIndex int params := make(map[string]any) if segments, _, err = parseReqURI(request.RequestURI); err != nil { - http.Error(writer, err.Error(), http.StatusBadRequest) + errorPage400(writer, params, "Error parsing request URI: "+err.Error()) return } dirMode := false @@ -212,7 +212,7 @@ } httpHandleRepoRaw(writer, request, params) case "log": if len(segments) > sepIndex+4 { - http.Error(writer, "Too many parameters", http.StatusBadRequest) + errorPage400(writer, params, "Too many parameters") return } if redirectDir(writer, request) { @@ -236,7 +236,7 @@ case sepIndex + 5: params["mr_id"] = segments[sepIndex+4] httpHandleRepoContribOne(writer, request, params) default: - http.Error(writer, "Too many parameters", http.StatusBadRequest) + errorPage400(writer, params, "Too many parameters") } default: errorPage404(writer, params) diff --git a/templates/403.tmpl b/templates/403.tmpl new file mode 100644 index 0000000000000000000000000000000000000000..62a265410388c0123e14923a670272fb62f3eb9c --- /dev/null +++ b/templates/403.tmpl @@ -0,0 +1,25 @@ +{{/* + SPDX-License-Identifier: AGPL-3.0-only + SPDX-FileContributor: Runxi Yu +*/}} +{{- define "403" -}} + + + + {{- template "head_common" . -}} + 403 Forbidden – {{ .global.forge_title }} + + + {{- template "header" . -}} +
+

403 Forbidden

+

{{- .complete_error_msg -}}

+
+
Lindenii Forge
+
+
+ {{- template "footer" . -}} +
+ + +{{- end -}} diff --git a/templates/501.tmpl b/templates/501.tmpl new file mode 100644 index 0000000000000000000000000000000000000000..b49bc36b27fb0541fe145754197162304e4b4617 --- /dev/null +++ b/templates/501.tmpl @@ -0,0 +1,24 @@ +{{/* + SPDX-License-Identifier: AGPL-3.0-only + SPDX-FileContributor: Runxi Yu +*/}} +{{- define "501" -}} + + + + {{- template "head_common" . -}} + 501 Not Implemented – {{ .global.forge_title }} + + + {{- template "header" . -}} +
+

501 Not Implemented

+
+
Lindenii Forge
+
+ + + +{{- end -}} -- 2.48.1