server/frontend/submit.go

55 lines
1.4 KiB
Go

package main
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
fronttime "srs.epita.fr/fic-server/frontend/time"
)
type SubmissionHandler struct {}
func (s SubmissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Handling %s submission 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) != 2 {
http.Error(w, "{\"errmsg\":\"Arguments manquants.\"}", http.StatusBadRequest)
return
}
team := sURL[0]
if time.Now().Sub(fronttime.ChallengeEnd) > 0 {
http.Error(w, "{\"errmsg\":\"Vous ne pouvez plus soumettre, le challenge est terminé.\"}", http.StatusForbidden)
return
}
if pex, err := strconv.Atoi(sURL[1]); err != nil {
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
return
} else {
exercice := fmt.Sprintf("%d", pex)
if saveTeamFile(TeamsDir, team, exercice, w, r) {
http.Error(w, "{\"errmsg\":\"Son traitement est en cours...\"}", http.StatusAccepted)
}
}
}