server/frontend/chname.go

40 lines
1.0 KiB
Go
Raw Normal View History

package main
import (
"log"
"net/http"
"strings"
)
type ChNameHandler struct {}
func (n ChNameHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Handling %s name change request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
w.Header().Set("Content-Type", "application/json")
// Check request type and size
if r.Method != "POST" {
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
return
} else if r.ContentLength < 0 || r.ContentLength > 1023 {
http.Error(w, "{\"errmsg\":\"Requête trop longue ou de taille inconnue\"}", http.StatusRequestEntityTooLarge)
return
}
// Extract URL arguments
var sURL = strings.Split(r.URL.Path, "/")
if len(sURL) != 1 {
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
return
}
team := sURL[0]
// Enqueue file for backend treatment
if saveTeamFile(SubmissionDir, team, "name", w, r) {
http.Error(w, "{\"errmsg\":\"Demande de changement de nom acceptée\"}", http.StatusAccepted)
}
}