Browse Source

Minor fix and push before sleep

Toby Chui 1 month ago
parent
commit
5b5bec7a29
1 changed files with 15 additions and 10 deletions
  1. 15 10
      mod/dynamicproxy/Server.go

+ 15 - 10
mod/dynamicproxy/Server.go

@@ -209,25 +209,18 @@ func (h *ProxyHandler) handleRootRouting(w http.ResponseWriter, r *http.Request)
 		http.Redirect(w, r, redirectTarget, http.StatusTemporaryRedirect)
 	case DefaultSite_NotFoundPage:
 		//Serve the not found page, use template if exists
-		w.Header().Set("Content-Type", "text/html; charset=utf-8")
-		w.WriteHeader(http.StatusNotFound)
-		template, err := os.ReadFile(filepath.Join(h.Parent.Option.WebDirectory, "templates/notfound.html"))
-		if err != nil {
-			w.Write(page_hosterror)
-		} else {
-			w.Write(template)
-		}
+		h.serve404PageWithTemplate(w, r)
 	case DefaultSite_NoResponse:
 		//No response. Just close the connection
 		h.Parent.logRequest(r, false, 444, "root-no_resp", domainOnly)
 		hijacker, ok := w.(http.Hijacker)
 		if !ok {
-			w.Header().Set("Connection", "close")
+			h.serve404PageWithTemplate(w, r)
 			return
 		}
 		conn, _, err := hijacker.Hijack()
 		if err != nil {
-			w.Header().Set("Connection", "close")
+			h.serve404PageWithTemplate(w, r)
 			return
 		}
 		conn.Close()
@@ -241,3 +234,15 @@ func (h *ProxyHandler) handleRootRouting(w http.ResponseWriter, r *http.Request)
 		http.Error(w, "544 - No Route Defined", 544)
 	}
 }
+
+// Serve 404 page with template if exists
+func (h *ProxyHandler) serve404PageWithTemplate(w http.ResponseWriter, r *http.Request) {
+	w.Header().Set("Content-Type", "text/html; charset=utf-8")
+	w.WriteHeader(http.StatusNotFound)
+	template, err := os.ReadFile(filepath.Join(h.Parent.Option.WebDirectory, "templates/notfound.html"))
+	if err != nil {
+		w.Write(page_hosterror)
+	} else {
+		w.Write(template)
+	}
+}