server/frontend/submit.go

55 lines
1.4 KiB
Go
Raw Normal View History

2016-01-06 18:30:57 +00:00
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
2016-01-23 11:26:20 +00:00
"time"
2016-01-06 18:30:57 +00:00
fronttime "srs.epita.fr/fic-server/frontend/time"
)
type SubmissionHandler struct {}
2016-01-06 18:30:57 +00:00
2016-01-23 11:26:20 +00:00
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())
2016-01-06 18:30:57 +00:00
w.Header().Set("Content-Type", "application/json")
// Check request type and size
if r.Method != "POST" {
2016-01-23 11:26:20 +00:00
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
2016-01-06 18:30:57 +00:00
return
2016-01-23 11:26:20 +00:00
} else if r.ContentLength < 0 || r.ContentLength > 1023 {
http.Error(w, "{\"errmsg\":\"Requête trop longue ou de taille inconnue\"}", http.StatusRequestEntityTooLarge)
2016-01-06 18:30:57 +00:00
return
}
// Extract URL arguments
var sURL = strings.Split(r.URL.Path, "/")
if len(sURL) != 2 {
http.Error(w, "{\"errmsg\":\"Arguments manquants.\"}", http.StatusBadRequest)
2016-01-23 11:26:20 +00:00
return
}
team := sURL[0]
2016-01-23 11:26:20 +00:00
if time.Now().Sub(fronttime.ChallengeEnd) > 0 {
2016-01-23 11:26:20 +00:00
http.Error(w, "{\"errmsg\":\"Vous ne pouvez plus soumettre, le challenge est terminé.\"}", http.StatusForbidden)
2016-01-06 18:30:57 +00:00
return
}
if pex, err := strconv.Atoi(sURL[1]); err != nil {
2016-01-23 11:26:20 +00:00
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)
2016-01-06 18:30:57 +00:00
return
} else {
exercice := fmt.Sprintf("%d", pex)
2016-01-06 18:30:57 +00:00
if saveTeamFile(TeamsDir, team, exercice, w, r) {
http.Error(w, "{\"errmsg\":\"Son traitement est en cours...\"}", http.StatusAccepted)
2016-01-06 18:30:57 +00:00
}
}
}