Skip to content

Commit

Permalink
fix: respond with correct mimetype
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnikaV9 committed Nov 2, 2024
1 parent f8492cd commit 53b08f0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion routes/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
15 changes: 8 additions & 7 deletions routes/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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")
}
5 changes: 4 additions & 1 deletion server/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion server/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 53b08f0

Please sign in to comment.