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
|
|
|
)
|
|
|
|
|
2016-01-23 12:19:28 +00:00
|
|
|
type SubmissionHandler struct {
|
2016-04-28 14:53:52 +00:00
|
|
|
ChallengeEnd time.Time
|
2016-01-23 11:26:20 +00:00
|
|
|
}
|
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) {
|
2016-01-06 18:30:57 +00:00
|
|
|
log.Printf("Handling %s 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" {
|
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, "/")
|
|
|
|
|
2016-12-09 08:36:18 +00:00
|
|
|
if len(sURL) != 2 {
|
|
|
|
http.Error(w, "{\"errmsg\":\"Arguments manquants.\"}", http.StatusBadRequest)
|
2016-01-23 11:26:20 +00:00
|
|
|
return
|
|
|
|
}
|
2016-12-09 08:36:18 +00:00
|
|
|
team := sURL[0]
|
2016-01-23 11:26:20 +00:00
|
|
|
|
|
|
|
if time.Now().Sub(s.ChallengeEnd) > 0 {
|
|
|
|
http.Error(w, "{\"errmsg\":\"Vous ne pouvez plus soumettre, le challenge est terminé.\"}", http.StatusForbidden)
|
2016-01-06 18:30:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-09 08:36:18 +00:00
|
|
|
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 {
|
2016-12-09 08:36:18 +00:00
|
|
|
exercice := fmt.Sprintf("%d", pex)
|
2016-01-06 18:30:57 +00:00
|
|
|
|
2016-12-09 08:36:18 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|