From 53b08f0090960d68312d9789f7d2d473d5fbc120 Mon Sep 17 00:00:00 2001 From: carrot <68383195+AnnikaV9@users.noreply.github.com> Date: Sat, 2 Nov 2024 12:16:30 +0800 Subject: [PATCH] fix: respond with correct mimetype --- main.go | 4 ++-- routes/ping.go | 2 +- routes/run.go | 15 ++++++++------- server/middleware.go | 5 ++++- server/send.go | 3 ++- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 091fa5b..8ae8f33 100644 --- a/main.go +++ b/main.go @@ -96,12 +96,12 @@ func main() { } http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { - server.Send(w, http.StatusNotFound, []byte(`{"detail": "not found"}`)) + server.Send(w, http.StatusNotFound, []byte(`{"detail": "not found"}`), "application/json") }) http.HandleFunc("POST /run", server.ScopedMiddleWare(routes.Run, scopedParams)) http.HandleFunc("/run", func(w http.ResponseWriter, _ *http.Request) { - server.Send(w, http.StatusMethodNotAllowed, []byte(`{"detail": "method not allowed"}`)) + server.Send(w, http.StatusMethodNotAllowed, []byte(`{"detail": "method not allowed"}`), "application/json") }) if enablePing { diff --git a/routes/ping.go b/routes/ping.go index 2498096..300b9b7 100644 --- a/routes/ping.go +++ b/routes/ping.go @@ -23,5 +23,5 @@ import ( ) func Ping(w http.ResponseWriter, _ *http.Request) { - server.Send(w, http.StatusOK, []byte("pong")) + server.Send(w, http.StatusOK, []byte("pong"), "text/plain") } diff --git a/routes/run.go b/routes/run.go index fc757a7..56ab32b 100644 --- a/routes/run.go +++ b/routes/run.go @@ -72,39 +72,40 @@ func getLanguageConfig() map[string]map[string]string { func Run(w http.ResponseWriter, r *http.Request) { masterKey := r.Header.Get("X-Master-Key") + if masterKey == "" { - server.Send(w, http.StatusUnauthorized, []byte(`{"detail": "unauthorized"}`)) + server.Send(w, http.StatusUnauthorized, []byte(`{"detail": "unauthorized"}`), "application/json") return } ks, _ := r.Context().Value(server.KeyStoreContextKey).(*control.KeyStore) if !ks.CheckKey(masterKey, r.Context().Value(server.MasterKeyContextKey).([]string)) { - server.Send(w, http.StatusUnauthorized, []byte(`{"detail": "unauthorized"}`)) + server.Send(w, http.StatusUnauthorized, []byte(`{"detail": "unauthorized"}`), "application/json") return } mimeType := r.Header.Get("Content-Type") if strings.Split(mimeType, ";")[0] != "application/json" { - server.Send(w, http.StatusUnsupportedMediaType, []byte(`{"detail": "unsupported media type"}`)) + server.Send(w, http.StatusUnsupportedMediaType, []byte(`{"detail": "unsupported media type"}`), "application/json") return } var user User if err := json.NewDecoder(r.Body).Decode(&user); err != nil { - server.Send(w, http.StatusBadRequest, []byte(`{"detail": "invalid request format"}`)) + server.Send(w, http.StatusBadRequest, []byte(`{"detail": "invalid request format"}`), "application/json") return } langConfig, exists := getLanguageConfig()[user.LanguageID.value] if !exists { - server.Send(w, http.StatusBadRequest, []byte(`{"detail": "invalid value for parameter language_id, refer to the documentation"}`)) + server.Send(w, http.StatusBadRequest, []byte(`{"detail": "invalid value for parameter language_id, refer to the documentation"}`), "application/json") return } codeBytes, err := base64.StdEncoding.DecodeString(user.Code) if err != nil || user.Code == "" { - server.Send(w, http.StatusBadRequest, []byte(`{"detail": "invalid value for parameter code, must be a base64 encoded string"}`)) + server.Send(w, http.StatusBadRequest, []byte(`{"detail": "invalid value for parameter code, must be a base64 encoded string"}`), "application/json") return } @@ -116,5 +117,5 @@ func Run(w http.ResponseWriter, r *http.Request) { status, result := ex.RunCode(string(codeBytes), entry, ext, img, r.Context().Value(server.EnableCacheContextKey).(bool)) resultBytes, _ := json.Marshal(result) - server.Send(w, status, resultBytes) + server.Send(w, status, resultBytes, "application/json") } diff --git a/server/middleware.go b/server/middleware.go index abe167c..f56806c 100644 --- a/server/middleware.go +++ b/server/middleware.go @@ -49,14 +49,17 @@ func MiddleWare(handler http.Handler, params MiddleWareParams) http.Handler { host, _, _ := net.SplitHostPort(r.RemoteAddr) if params.Standalone && !params.RateLimiter.CheckClient(host, params.RlBurst, params.RlRefill) { - log.Printf("%s %s %s [ratelimited]", host, r.Method, r.URL) + log.Printf("%s %s %s [blocked: rate]", host, r.Method, r.URL) + w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusTooManyRequests) + w.Write([]byte(`{"detail": "you are sending too many requests"}`)) return } log.Printf("%s %s %s", host, r.Method, r.URL) if params.Proxy != "" && host != params.Proxy { + log.Printf("%s %s %s [blocked: proxy]", host, r.Method, r.URL) w.WriteHeader(http.StatusForbidden) return } diff --git a/server/send.go b/server/send.go index acaaf33..4d61748 100644 --- a/server/send.go +++ b/server/send.go @@ -21,7 +21,8 @@ import ( "net/http" ) -func Send(w http.ResponseWriter, status int, message []byte) { +func Send(w http.ResponseWriter, status int, message []byte, contentType string) { + w.Header().Set("Content-Type", contentType) w.WriteHeader(status) if _, err := w.Write(message); err != nil { http.Error(w, "internal server error", http.StatusInternalServerError)